Skip to content

Commit

Permalink
fix chainId format inconsistencies
Browse files Browse the repository at this point in the history
  • Loading branch information
clbrge committed Jun 26, 2022
1 parent ee16fca commit 85e72c9
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/stores.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const getWindowEthereum = () => {
}
}

// always get chainId as number
const alwaysNumber = n => Web3.utils.isHex(n) ? Web3.utils.hexToNumber(n) : n

export const createStore = () => {
const { emit, get, subscribe, assign, deleteAll } = proxied()

Expand All @@ -49,7 +52,7 @@ export const createStore = () => {
}) => {
// console.log('switch1193Provider', { accounts, chainId }, get('web3'), get('eipProvider'))
if (!chainId) {
chainId = await get('web3').eth.getChainId()
chainId = alwaysNumber(await get('web3').eth.getChainId())
}
if (!accounts) {
accounts = await get('web3').eth.getAccounts()
Expand All @@ -71,7 +74,7 @@ export const createStore = () => {
}

const accountsChangedHandler = accounts => switch1193Provider({ accounts })
const chainChangedHandler = chainId => switch1193Provider({ chainId })
const chainChangedHandler = chainId => switch1193Provider({ chainId: alwaysNumber(chainId) })
// TODO better error support ?
const disconnectHandler = error => switch1193Provider({ error })

Expand Down Expand Up @@ -133,7 +136,7 @@ export const createStore = () => {
return set1193Provider(provider, addressOrIndex)
init()
const web3 = new Web3(provider)
const chainId = await web3.eth.getChainId()
const chainId = alwaysNumber(await get('web3').eth.getChainId())
let accounts = []
try {
// not all provider support accounts
Expand Down

0 comments on commit 85e72c9

Please sign in to comment.