Skip to content

Commit

Permalink
Fixes provider switch chain
Browse files Browse the repository at this point in the history
  • Loading branch information
arch1995 committed Feb 22, 2024
1 parent 342b183 commit 87c2339
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 33 deletions.
46 changes: 23 additions & 23 deletions demo/react-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 8 additions & 8 deletions demo/react-app/src/services/web3auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,14 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
return;
}
const newChain: CustomChainConfig = {
rpcTarget: "https://rpc.ankr.com/polygon",
blockExplorerUrl: "https://polygonscan.com/",
chainId: "0x89",
displayName: "Polygon Mainnet",
ticker: "matic",
tickerName: "Matic",
logo: "https://images.toruswallet.io/matic.svg",
chainNamespace: CHAIN_NAMESPACES.EIP155,
chainId: "0xaa36a7",
rpcTarget: "https://rpc.ankr.com/eth_sepolia",
displayName: "Sepolia Testnet",
blockExplorerUrl: "https://sepolia.etherscan.io",
ticker: "ETH",
tickerName: "Ethereum",
logo: "https://images.toruswallet.io/eth.svg",
};
await web3Auth?.addChain(newChain);
uiConsole("New Chain Added");
Expand All @@ -293,7 +293,7 @@ export const Web3AuthProvider: FunctionComponent<IWeb3AuthState> = ({ children,
uiConsole("provider not initialized yet");
return;
}
await web3Auth?.switchChain({ chainId: "0xaa36a7" });
await web3Auth?.switchChain({ chainId: "0x89" });
uiConsole("Chain Switched");
};

Expand Down
4 changes: 4 additions & 0 deletions packages/no-modal/src/noModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,17 @@ export class Web3AuthNoModal extends SafeEventEmitter implements IWeb3Auth {
}

public async addChain(chainConfig: CustomChainConfig): Promise<void> {
if (this.status === ADAPTER_STATUS.CONNECTED && this.connectedAdapterName)
return this.walletAdapters[this.connectedAdapterName].addChain(chainConfig);
if (this.commonJRPCProvider) {
return this.commonJRPCProvider.addChain(chainConfig);
}
throw WalletInitializationError.notReady(`No wallet is ready`);
}

public async switchChain(params: { chainId: string }): Promise<void> {
if (this.status === ADAPTER_STATUS.CONNECTED && this.connectedAdapterName)
return this.walletAdapters[this.connectedAdapterName].switchChain(params);
if (this.commonJRPCProvider) {
return this.commonJRPCProvider.switchChain(params);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugins/wallet-services-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export class WalletServicesPlugin implements IPlugin {
log.warn(`${web3Auth.connectedAdapterName} is not compatible with wallet services connector plugin`);
return;
}
this.provider = web3Auth.provider;
this.provider = web3Auth.walletAdapters[WALLET_ADAPTERS.OPENLOGIN].provider;
if (!this.provider) throw WalletServicesPluginError.web3AuthNotConnected();
this.subscribeToProviderEvents(this.provider);
});
Expand All @@ -190,7 +190,7 @@ export class WalletServicesPlugin implements IPlugin {
const [accounts, chainId, chainConfig] = await Promise.all([
this.provider.request<never, string[]>({ method: "eth_accounts" }),
this.provider.request<never, string>({ method: "eth_chainId" }),
this.provider.request<never, CustomChainConfig>({ method: "provider_config" }),
this.provider.request<never, CustomChainConfig>({ method: "eth_provider_config" }),
]);
return {
chainId: parseInt(chainId as string, 16),
Expand Down

0 comments on commit 87c2339

Please sign in to comment.