Skip to content

Commit ec5dbbb

Browse files
[core-v2.8.5-alpha.1, react-v2.3.5-alpha.1, vue-v2.2.5-alpha.1] : Fix - Handle add new chain canceled by user (#1279)
* Fix bug when user cancels add new chain to wallet, bump versions * Update packages/core/src/chain.ts Co-authored-by: Aaron <abarnard@protonmail.com>
1 parent 8eba62b commit ec5dbbb

File tree

5 files changed

+33
-18
lines changed

5 files changed

+33
-18
lines changed

packages/core/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/core",
3-
"version": "2.8.4",
3+
"version": "2.8.5-alpha.1",
44
"description": "Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardized spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",

packages/core/src/chain.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { firstValueFrom } from 'rxjs'
1+
import { BehaviorSubject, firstValueFrom } from 'rxjs'
22
import { filter, map } from 'rxjs/operators'
3-
import { ProviderRpcErrorCode } from '@web3-onboard/common'
3+
import { Chain, ProviderRpcErrorCode } from '@web3-onboard/common'
44
import { addNewChain, switchChain } from './provider.js'
55
import { state } from './store/index.js'
66
import { switchChainModal$ } from './streams.js'
@@ -66,21 +66,12 @@ async function setChain(options: {
6666
filter(x => x === null),
6767
map(() => false)
6868
)
69-
7069
if (
7170
code === ProviderRpcErrorCode.CHAIN_NOT_ADDED ||
7271
code === ProviderRpcErrorCode.UNRECOGNIZED_CHAIN_ID
7372
) {
7473
// chain has not been added to wallet
75-
try {
76-
await addNewChain(wallet.provider, chain)
77-
await switchChain(wallet.provider, chainIdHex)
78-
return true
79-
} catch (error) {
80-
// display notification to user to switch chain
81-
switchChainModal$.next({ chain })
82-
return firstValueFrom(switchChainModalClosed$)
83-
}
74+
return chainNotInWallet(wallet, chain, switchChainModal$, chainIdHex)
8475
}
8576

8677
if (code === ProviderRpcErrorCode.UNSUPPORTED_METHOD) {
@@ -93,4 +84,28 @@ async function setChain(options: {
9384
return false
9485
}
9586

87+
const chainNotInWallet = async (
88+
wallet: WalletState,
89+
chain: Chain,
90+
switchChainModalClosed$: BehaviorSubject<{
91+
chain: Chain
92+
}>,
93+
chainIdHex: string
94+
) => {
95+
try {
96+
await addNewChain(wallet.provider, chain)
97+
await switchChain(wallet.provider, chainIdHex)
98+
return true
99+
} catch (error) {
100+
const { code } = error as { code: number }
101+
if (code === ProviderRpcErrorCode.ACCOUNT_ACCESS_REJECTED) {
102+
// add new chain rejected by user
103+
return false
104+
}
105+
// display notification to user to switch chain
106+
switchChainModal$.next({ chain })
107+
return firstValueFrom(switchChainModalClosed$)
108+
}
109+
}
110+
96111
export default setChain

packages/demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
},
2424
"dependencies": {
2525
"@web3-onboard/coinbase": "^2.1.2",
26-
"@web3-onboard/core": "^2.8.4",
26+
"@web3-onboard/core": "^2.8.5-alpha.1",
2727
"@web3-onboard/dcent": "^2.2.0",
2828
"@web3-onboard/fortmatic": "^2.0.13",
2929
"@web3-onboard/gas": "^2.1.2",

packages/react/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/react",
3-
"version": "2.3.4",
3+
"version": "2.3.5-alpha.1",
44
"description": "A collection of React hooks for integrating Web3-Onboard in to React and Next.js projects. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",
@@ -62,7 +62,7 @@
6262
"typescript": "^4.5.5"
6363
},
6464
"dependencies": {
65-
"@web3-onboard/core": "^2.8.4",
65+
"@web3-onboard/core": "^2.8.5-alpha.1",
6666
"@web3-onboard/common": "^2.2.2",
6767
"use-sync-external-store": "1.0.0"
6868
},

packages/vue/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/vue",
3-
"version": "2.2.4",
3+
"version": "2.2.5-alpha.1",
44
"description": "A collection of Vue Composables for integrating Web3-Onboard in to a Vue or Nuxt project. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",
@@ -63,7 +63,7 @@
6363
"@vueuse/core": "^8.4.2",
6464
"@vueuse/rxjs": "^8.2.0",
6565
"@web3-onboard/common": "^2.2.2",
66-
"@web3-onboard/core": "^2.8.4",
66+
"@web3-onboard/core": "^2.8.5-alpha.1",
6767
"vue-demi": "^0.12.4"
6868
},
6969
"peerDependencies": {

0 commit comments

Comments
 (0)