forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wallet-ext: move network switch handling to background service (Myste…
…nLabs#7429) * all UI instances now use the same network (changing network in one instance will change all others as well) * in case of any error while switching network show an error toast * set growth book attributes in background service as well https://user-images.githubusercontent.com/10210143/212878899-dc883200-58c7-4d19-ab2e-c94a2bb9ad33.mov https://user-images.githubusercontent.com/10210143/212879044-1cc91183-97ee-4ce8-b1e8-e3aac489ccfa.mov The only visible change (except the toast) is that when user clicks on custom rpc url now the current selected network still is selected (shows the checkmark green next to it) until saving the new custom rpc url. Before: https://user-images.githubusercontent.com/10210143/212880937-1950deb7-901b-428b-972b-f90a20dabbff.mov After: https://user-images.githubusercontent.com/10210143/212879832-2c5a3920-91a8-4fde-8971-a5c1adca28ef.mov part of: APPS-308
- Loading branch information
1 parent
62839fd
commit 252217e
Showing
18 changed files
with
363 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import mitt from 'mitt'; | ||
import Browser from 'webextension-polyfill'; | ||
|
||
import FeatureGating from './FeatureGating'; | ||
import { API_ENV, API_ENV_TO_INFO, DEFAULT_API_ENV } from '_app/ApiProvider'; | ||
import { FEATURES } from '_src/shared/experimentation/features'; | ||
import { isValidUrl } from '_src/shared/utils'; | ||
|
||
export type NetworkEnvType = | ||
| { env: Exclude<API_ENV, API_ENV.customRPC>; customRpcUrl: null } | ||
| { env: API_ENV.customRPC; customRpcUrl: string }; | ||
|
||
class NetworkEnv { | ||
#events = mitt<{ changed: NetworkEnvType }>(); | ||
|
||
async getActiveNetwork(): Promise<NetworkEnvType> { | ||
const { sui_Env, sui_Env_RPC } = await Browser.storage.local.get({ | ||
sui_Env: DEFAULT_API_ENV, | ||
sui_Env_RPC: null, | ||
}); | ||
const adjEnv = (await this.#isNetworkAvailable(sui_Env)) | ||
? sui_Env | ||
: DEFAULT_API_ENV; | ||
const adjCustomUrl = adjEnv === API_ENV.customRPC ? sui_Env_RPC : null; | ||
return { env: adjEnv, customRpcUrl: adjCustomUrl }; | ||
} | ||
|
||
async setActiveNetwork(network: NetworkEnvType) { | ||
const { env, customRpcUrl } = network; | ||
if (!(await this.#isNetworkAvailable(env))) { | ||
throw new Error( | ||
`Error changing network, ${API_ENV_TO_INFO[env].name} is not available.` | ||
); | ||
} | ||
if (env === API_ENV.customRPC && !isValidUrl(customRpcUrl)) { | ||
throw new Error(`Invalid custom RPC url ${customRpcUrl}`); | ||
} | ||
await Browser.storage.local.set({ | ||
sui_Env: env, | ||
sui_Env_RPC: customRpcUrl, | ||
}); | ||
this.#events.emit('changed', network); | ||
} | ||
|
||
on = this.#events.on; | ||
|
||
off = this.#events.off; | ||
|
||
async #isNetworkAvailable(apiEnv: API_ENV) { | ||
const growthBook = await FeatureGating.getGrowthBook(); | ||
return ( | ||
(apiEnv === API_ENV.testNet && | ||
growthBook.isOn(FEATURES.USE_TEST_NET_ENDPOINT)) || | ||
apiEnv !== API_ENV.testNet | ||
); | ||
} | ||
} | ||
|
||
export default new NetworkEnv(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
apps/wallet/src/shared/messaging/messages/payloads/network/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { isBasePayload } from '_payloads'; | ||
|
||
import type { BasePayload, Payload } from '_payloads'; | ||
import type { NetworkEnvType } from '_src/background/NetworkEnv'; | ||
|
||
export interface SetNetworkPayload extends BasePayload { | ||
type: 'set-network'; | ||
network: NetworkEnvType; | ||
} | ||
|
||
export function isSetNetworkPayload( | ||
payload: Payload | ||
): payload is SetNetworkPayload { | ||
return isBasePayload(payload) && payload.type === 'set-network'; | ||
} |
Oops, something went wrong.