Skip to content

Commit bf08241

Browse files
authored
common-v2.1.7-alpha.2, core-v2.6.0-alpha.3, react-v2.2.5-alpha.2, vue-v2.1.5-alpha.2]: Fix: Chain id type fix (#1158)
* chainid fix * chainid fix * chainid fix * chainid fix * decimaltoHex * decimaltoHex * decimaltoHex * decimaltoHex * pr-fix * merge develop
1 parent db4672d commit bf08241

File tree

11 files changed

+38
-25
lines changed

11 files changed

+38
-25
lines changed

packages/common/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/common",
3-
"version": "2.1.7-alpha.2",
3+
"version": "2.1.7-alpha.3",
44
"description": "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, 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/common/src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,9 @@ export type GetInterfaceHelpers = {
236236
EventEmitter: typeof EventEmitter
237237
}
238238

239-
export type ChainId = string | number
239+
export type ChainId = string
240+
241+
export type DecimalChainId = number
240242

241243
export type RpcUrl = string
242244

@@ -434,6 +436,8 @@ export interface Chain {
434436
blockExplorerUrl?: string
435437
}
436438

439+
export type ChainWithDecimalId = Omit<Chain, 'id'> & { id: DecimalChainId }
440+
437441
export type TokenSymbol = string // eg ETH
438442

439443
export interface CustomNetwork {

packages/core/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/core",
3-
"version": "2.6.0-alpha.5",
3+
"version": "2.6.0-alpha.6",
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",
@@ -83,7 +83,7 @@
8383
"typescript": "^4.5.5"
8484
},
8585
"dependencies": {
86-
"@web3-onboard/common": "^2.1.7-alpha.2",
86+
"@web3-onboard/common": "^2.1.7-alpha.3",
8787
"bignumber.js": "^9.0.0",
8888
"bnc-sdk": "^4.4.1",
8989
"bowser": "^2.11.0",

packages/core/src/chain.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { state } from './store'
66
import { switchChainModal$ } from './streams'
77
import { validateSetChainOptions } from './validation'
88
import type { WalletState } from './types'
9+
import { toHexString } from './utils'
910

1011
async function setChain(options: {
1112
chainId: string | number
@@ -20,10 +21,11 @@ async function setChain(options: {
2021

2122
const { wallets, chains } = state.get()
2223
const { chainId, chainNamespace = 'evm', wallet: walletToSet } = options
24+
const chainIdHex = toHexString(chainId)
2325

2426
// validate that chainId has been added to chains
2527
const chain = chains.find(
26-
({ namespace, id }) => namespace === chainNamespace && id === chainId
28+
({ namespace, id }) => namespace === chainNamespace && id === chainIdHex
2729
)
2830

2931
if (!chain) {
@@ -50,13 +52,13 @@ async function setChain(options: {
5052
// check if wallet is already connected to chainId
5153
if (
5254
walletConnectedChain.namespace === chainNamespace &&
53-
walletConnectedChain.id === chainId
55+
walletConnectedChain.id === chainIdHex
5456
) {
5557
return true
5658
}
5759

5860
try {
59-
await switchChain(wallet.provider, chainId)
61+
await switchChain(wallet.provider, chainIdHex)
6062
return true
6163
} catch (error) {
6264
const { code } = error as { code: number }
@@ -69,7 +71,7 @@ async function setChain(options: {
6971
// chain has not been added to wallet
7072
try {
7173
await addNewChain(wallet.provider, chain)
72-
await switchChain(wallet.provider, chainId)
74+
await switchChain(wallet.provider, chainIdHex)
7375
return true
7476
} catch (error) {
7577
// display notification to user to switch chain

packages/core/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,7 @@ function init(options: InitOptions): OnboardAPI {
9191
} = options
9292

9393
initI18N(i18n)
94-
addChains(chains.map(chainIdToHex))
95-
94+
addChains(chainIdToHex(chains))
9695
const { device, svelteInstance } = configuration
9796

9897
// update accountCenter

packages/core/src/store/actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function addChains(chains: Chain[]): void {
6363
payload: chains.map(({ namespace = 'evm', id, ...rest }) => ({
6464
...rest,
6565
namespace,
66-
id: id
66+
id: id.toLowerCase()
6767
}))
6868
}
6969

packages/core/src/types.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import type {
77
EIP1193Provider,
88
WalletModule,
99
Chain,
10-
TokenSymbol
10+
TokenSymbol,
11+
ChainWithDecimalId
1112
} from '@web3-onboard/common'
1213

1314
import type gas from '@web3-onboard/gas'
@@ -23,7 +24,7 @@ export interface InitOptions {
2324
/**
2425
* The chains that your app works with
2526
*/
26-
chains: Chain[]
27+
chains: Chain[] | ChainWithDecimalId[]
2728
/**
2829
* Additional metadata about your app to be displayed in the Onboard UI
2930
*/

packages/core/src/utils.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import type {
88
ChainId,
99
Chain,
1010
WalletInit,
11-
WalletModule
11+
WalletModule,
12+
ChainWithDecimalId
1213
} from '@web3-onboard/common'
1314

1415
import ethereumIcon from './icons/ethereum'
@@ -92,10 +93,15 @@ export async function copyWalletAddress(text: string): Promise<void> {
9293
}
9394
}
9495

95-
export const chainIdToHex = (chain: Chain): Chain =>
96-
typeof chain.id === 'number'
97-
? { ...chain, id: `0x${chain.id.toString(16)}` }
98-
: chain
96+
export const toHexString = (val: number | string): string => typeof val === 'number' ? `0x${val.toString(16)}` : val
97+
98+
export function chainIdToHex(chains : Chain[] | ChainWithDecimalId[] ):
99+
Chain[] {
100+
return chains.map(({ id, ...rest }) => {
101+
const hexId = toHexString(id)
102+
return { id: hexId, ...rest }
103+
})
104+
}
99105

100106
export const chainIdToLabel: Record<string, string> = {
101107
'0x1': 'Ethereum',

packages/core/src/validation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import Joi, { ObjectSchema, Schema } from 'joi'
22
import type {
33
Chain,
44
ChainId,
5+
DecimalChainId,
56
WalletInit,
67
WalletModule
78
} from '@web3-onboard/common'
@@ -304,7 +305,7 @@ export function validateString(str: string, label?: string): ValidateReturn {
304305
}
305306

306307
export function validateSetChainOptions(data: {
307-
chainId: ChainId
308+
chainId: ChainId | DecimalChainId
308309
chainNamespace?: string
309310
wallet?: WalletState['label']
310311
}): ValidateReturn {

packages/react/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/react",
3-
"version": "2.2.5-alpha.4",
3+
"version": "2.2.5-alpha.5",
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,8 +62,8 @@
6262
"typescript": "^4.5.5"
6363
},
6464
"dependencies": {
65-
"@web3-onboard/core": "^2.6.0-alpha.4",
66-
"@web3-onboard/common": "^2.1.7-alpha.2",
65+
"@web3-onboard/core": "^2.6.0-alpha.6",
66+
"@web3-onboard/common": "^2.1.7-alpha.3",
6767
"use-sync-external-store": "1.0.0"
6868
},
6969
"peerDependencies": {

0 commit comments

Comments
 (0)