Skip to content

Commit d146b79

Browse files
author
Nguyen Anh Tu
committed
WIP switch chain
1 parent 53ac099 commit d146b79

File tree

8 files changed

+34
-7
lines changed

8 files changed

+34
-7
lines changed

demo/vue-app-new/src/MainView.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const options = computed((): Web3AuthOptions => {
140140
// sessionTime?: number;
141141
// useCoreKitKey?: boolean;
142142
// chainConfig,
143-
chains: [chainConfig],
143+
chains: [chainConfig, chainConfigs.eip155.find((x) => x.chainId === "0xaa36a7")!],
144144
enableLogging: true,
145145
connectors: externalConnectors.value,
146146
multiInjectedProviderDiscovery: formData.multiInjectedProviderDiscovery,

demo/vue-app-new/src/components/AppDashboard.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ const onGetBalance = async () => {
188188
const onSwitchChain = async () => {
189189
log.info("switching chain");
190190
try {
191-
await switchChain({ chainId: "0x89" });
191+
await switchChain({ chainId: "0xaa36a7" });
192192
printToConsole("switchedChain");
193193
} catch (error) {
194194
printToConsole("switchedChain error", error);
@@ -291,6 +291,7 @@ const onSignPersonalMsg = async () => {
291291
<Button block size="xs" pill class="mb-2" @click="onGetBalance">
292292
{{ t("app.buttons.btnGetBalance") }}
293293
</Button>
294+
<Button block size="xs" pill class="mb-2" @click="onSwitchChain">{{ t("app.buttons.btnSwitchChain") }}</Button>
294295
<Button block size="xs" pill class="mb-2" @click="onSendEth">{{ t("app.buttons.btnSendEth") }}</Button>
295296
<Button block size="xs" pill class="mb-2" @click="onSignEthTransaction">
296297
{{ t("app.buttons.btnSignTransaction") }}

demo/vue-app-new2/.env.sample

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
VITE_APP_PIMLICO_API_KEY=""
2-
VITE_SOLANA_MAINNET_RPC=""
2+
VITE_APP_SOLANA_MAINNET_RPC=""
3+
VITE_APP_SOLANA_TESTNET_RPC=""
4+
VITE_APP_SOLANA_DEVNET_RPC=""

demo/vue-app-new2/src/config.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { LANGUAGE_TYPE, LANGUAGES, LOGIN_PROVIDER, LOGIN_PROVIDER_TYPE, WhiteLabelData } from "@web3auth/auth";
2-
import { CHAIN_NAMESPACES, ChainNamespaceType, CustomChainConfig, WEB3AUTH_NETWORK, WEB3AUTH_NETWORK_TYPE, SignTypedDataMessageV4, CONFIRMATION_STRATEGY, type CONFIRMATION_STRATEGY_TYPE } from "@web3auth/modal";
2+
import {
3+
CHAIN_NAMESPACES,
4+
ChainNamespaceType,
5+
CustomChainConfig,
6+
WEB3AUTH_NETWORK,
7+
WEB3AUTH_NETWORK_TYPE,
8+
SignTypedDataMessageV4,
9+
CONFIRMATION_STRATEGY,
10+
type CONFIRMATION_STRATEGY_TYPE,
11+
} from "@web3auth/modal";
312

413
import { FormConfigSettings } from "./interfaces";
514

@@ -80,7 +89,7 @@ export const chainConfigs: Record<ChainNamespaceType, CustomChainConfig[]> = {
8089
},
8190
{
8291
chainNamespace: CHAIN_NAMESPACES.SOLANA,
83-
rpcTarget: import.meta.env.VITE_SOLANA_MAINNET_RPC,
92+
rpcTarget: import.meta.env.VITE_APP_SOLANA_MAINNET_RPC,
8493
blockExplorerUrl: "https://explorer.solana.com",
8594
logo: "https://cryptologos.cc/logos/solana-sol-logo.png",
8695
chainId: "0x1",

packages/no-modal/src/base/core/IWeb3Auth.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ export interface IWeb3AuthCoreOptions {
4242
clientId: string;
4343

4444
/**
45-
* multiple chain configurations
46-
* only chains provided will be used
45+
* multiple chain configurations,
46+
* only provided chains will be used
4747
*/
4848
chains?: CustomChainConfig[];
4949
/**

packages/no-modal/src/connectors/auth-connector/authConnector.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,7 @@ class AuthConnector extends BaseConnector<AuthLoginParams> {
323323
reconnected: this.rehydrated,
324324
provider: this.provider,
325325
} as CONNECTED_EVENT_DATA);
326+
// handle disconnect from ws embed
326327
this.wsEmbedInstance?.provider.on("accountsChanged", (accounts: unknown[] = []) => {
327328
if ((accounts as string[]).length === 0 && this.status === CONNECTOR_STATUS.CONNECTED) this.disconnect({ cleanup: false });
328329
});

packages/no-modal/src/noModal.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,18 @@ export class Web3AuthNoModal extends SafeEventEmitter<Web3AuthNoModalEvents> imp
162162
}
163163

164164
public async switchChain(params: { chainId: string }): Promise<void> {
165+
if (params.chainId === this.currentChain.chainId) return;
166+
const newChainConfig = this.coreOptions.chains.find((x) => x.chainId === params.chainId);
167+
if (!newChainConfig) throw WalletInitializationError.invalidParams("Invalid chainId");
168+
165169
if (this.status === CONNECTOR_STATUS.CONNECTED && this.connectedConnector) {
166170
await this.connectedConnector.switchChain(params);
171+
// only update chain state in commonJRPCProvider instead of switchChain in provider inside commonJRPCProvider
172+
this.commonJRPCProvider?.updateChain(newChainConfig);
167173
this.setCurrentChain(params.chainId);
168174
return;
169175
}
176+
170177
if (this.commonJRPCProvider) {
171178
await this.commonJRPCProvider.switchChain(params);
172179
this.setCurrentChain(params.chainId);

packages/no-modal/src/providers/base-provider/CommonJRPCProvider.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ export class CommonJRPCProvider extends BaseProvider<CommonJRPCProviderConfig, C
4545
await this.setupProvider();
4646
}
4747

48+
public updateChain(chainConfig: CustomChainConfig) {
49+
this.update({
50+
chainId: chainConfig.chainId,
51+
});
52+
this.configure({ chainConfig });
53+
}
54+
4855
public updateProviderEngineProxy(provider: SafeEventEmitterProvider) {
4956
if (this._providerEngineProxy) {
5057
// eslint-disable-next-line @typescript-eslint/no-explicit-any

0 commit comments

Comments
 (0)