Skip to content

Commit

Permalink
Merge pull request #1983 from Web3Auth/feat/injected-provider-chain
Browse files Browse the repository at this point in the history
Switch chain when connecting to injected provider
  • Loading branch information
chaitanyapotti authored Oct 14, 2024
2 parents 8a7ee73 + 8a38634 commit 3d3fef8
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 64 deletions.
42 changes: 17 additions & 25 deletions demo/vue-app-new/package-lock.json

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

2 changes: 1 addition & 1 deletion demo/vue-app-new/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"@solana/web3.js": "^1.95.3",
"@toruslabs/constants": "^14.0.0",
"@toruslabs/ethereum-controllers": "^6.2.1",
"@toruslabs/ethereum-controllers": "^6.2.2",
"@toruslabs/vue-components": "^7.8.4",
"@toruslabs/vue-icons": "^7.6.2",
"@web3auth/account-abstraction-provider": "file:../../packages/providers/account-abstraction-provider",
Expand Down
40 changes: 19 additions & 21 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion packages/adapters/base-evm-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"pre-commit": "lint-staged --cwd ."
},
"dependencies": {
"@toruslabs/base-controllers": "^6.2.1",
"@toruslabs/base-controllers": "^6.2.2",
"@web3auth/base": "^9.2.2"
},
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/base-solana-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"pre-commit": "lint-staged --cwd ."
},
"dependencies": {
"@toruslabs/base-controllers": "^6.2.1",
"@toruslabs/base-controllers": "^6.2.2",
"@web3auth/base": "^9.2.2",
"bs58": "^5.0.0"
},
Expand Down
26 changes: 21 additions & 5 deletions packages/adapters/default-evm-adapter/src/injectedEvmAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,33 @@ class InjectedEvmAdapter extends BaseEvmAdapter<void> {

async connect(): Promise<IProvider | null> {
super.checkConnectionRequirements();
if (!this.injectedProvider) throw WalletLoginError.connectionError("Injected provider is not available");
this.status = ADAPTER_STATUS.CONNECTING;
this.emit(ADAPTER_EVENTS.CONNECTING, { adapter: this.name });
try {
await this.injectedProvider?.request({ method: "eth_requestAccounts" });
await this.injectedProvider.request({ method: "eth_requestAccounts" });
// switch chain if not connected to the right chain
if (this.injectedProvider.chainId !== this.chainConfig.chainId) {
try {
await this.switchChain(this.chainConfig, true);
} catch (error) {
await this.addChain(this.chainConfig, true);
await this.switchChain(this.chainConfig, true);
}
}
this.status = ADAPTER_STATUS.CONNECTED;
const disconnectHandler = () => {
// ready to be connected again
const chainDisconnectHandler = () => {
this.disconnect();
this.injectedProvider?.removeListener("disconnect", disconnectHandler);
if (this.injectedProvider.removeListener) this.injectedProvider.removeListener("disconnect", chainDisconnectHandler);
};
this.injectedProvider.on("disconnect", chainDisconnectHandler);
const accountDisconnectHandler = (accounts: string[]) => {
if (accounts.length === 0) {
this.disconnect();
if (this.injectedProvider.removeListener) this.injectedProvider.removeListener("accountsChanged", accountDisconnectHandler);
}
};
this.injectedProvider.on("disconnect", disconnectHandler);
this.injectedProvider.on("accountsChanged", accountDisconnectHandler);
this.emit(ADAPTER_EVENTS.CONNECTED, {
adapter: this.name,
reconnected: this.rehydrated,
Expand Down
2 changes: 1 addition & 1 deletion packages/adapters/wallet-connect-v2-adapter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@solana/web3.js": "^1.95.3",
"@toruslabs/base-controllers": "^6.2.1",
"@toruslabs/base-controllers": "^6.2.2",
"@walletconnect/sign-client": "^2.17.0",
"@walletconnect/types": "^2.17.0",
"@walletconnect/utils": "^2.17.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"pre-commit": "lint-staged --cwd ."
},
"dependencies": {
"@toruslabs/base-controllers": "^6.2.1",
"@toruslabs/base-controllers": "^6.2.2",
"@toruslabs/constants": "^14.0.0",
"@toruslabs/http-helpers": "^7.0.0",
"@web3auth/auth": "^9.4.1",
Expand Down
Loading

0 comments on commit 3d3fef8

Please sign in to comment.