Skip to content

Commit

Permalink
Provider updates for better chain handling (#1502)
Browse files Browse the repository at this point in the history
  • Loading branch information
goosewobbler authored Apr 6, 2023
1 parent ab8ca0c commit 436c76f
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 109 deletions.
5 changes: 2 additions & 3 deletions main/provider/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ function getActiveChains(): RPC.GetEthereumChains.Chain[] {
const colorway = storeApi.getColorway()

return Object.values(chains)
.filter(
(chain) => chain.on && (chain.connection.primary.connected || chain.connection.secondary.connected)
)
.filter((chain) => chain.on)
.sort((a, b) => a.id - b.id)
.map((chain) => {
const { id, explorer, name } = chain
Expand All @@ -86,6 +84,7 @@ function getActiveChains(): RPC.GetEthereumChains.Chain[] {
chainId: id,
networkId: id,
name,
connected: chain.connection.primary.connected || chain.connection.secondary.connected,
nativeCurrency: {
name: currencyName,
symbol,
Expand Down
31 changes: 12 additions & 19 deletions main/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,23 +185,19 @@ export class Provider extends EventEmitter {
}

getNetVersion(payload: RPCRequestPayload, res: RPCRequestCallback, targetChain: Chain) {
const connection = this.connection.connections[targetChain.type][targetChain.id]
const chainConnected = connection && (connection.primary?.connected || connection.secondary?.connected)

const response = chainConnected
? { result: connection.chainId }
: { error: { message: 'not connected', code: 1 } }
const chain = store('main.networks.ethereum', targetChain.id)
const response = chain?.on
? { result: targetChain.id }
: { error: { message: 'not connected', code: -1 } }

res({ id: payload.id, jsonrpc: payload.jsonrpc, ...response })
}

getChainId(payload: RPCRequestPayload, res: RPCSuccessCallback, targetChain: Chain) {
const connection = this.connection.connections[targetChain.type][targetChain.id]
const chainConnected = connection.primary?.connected || connection.secondary?.connected

const response = chainConnected
const chain = store('main.networks.ethereum', targetChain.id)
const response = chain?.on
? { result: intToHex(targetChain.id) }
: { error: { message: 'not connected', code: 1 } }
: { error: { message: 'not connected', code: -1 } }

res({ id: payload.id, jsonrpc: payload.jsonrpc, ...response })
}
Expand Down Expand Up @@ -856,9 +852,12 @@ export class Provider extends EventEmitter {

this.getChainId(
payload,
(resp) => {
const chainId = parseInt(resp.result)
(resp: RPCResponsePayload) => {
if (resp.error) {
return resError(resp.error, payload, cb)
}

const chainId = parseInt(resp.result)
const address = (tokenData.address || '').toLowerCase()
const symbol = (tokenData.symbol || '').toUpperCase()
const decimals = parseInt(tokenData.decimals || '1')
Expand Down Expand Up @@ -888,12 +887,6 @@ export class Provider extends EventEmitter {
logoURI: tokenData.image || tokenData.logoURI || ''
}

// const result = {
// suggestedAssetMeta: {
// asset: { token }
// }
// }

const handlerId = this.addRequestHandler(res)

accounts.addRequest(
Expand Down
83 changes: 21 additions & 62 deletions test/main/provider/chains/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,6 @@ const chains = {
connection: { primary: { connected: true }, secondary: { connected: false } },
on: true
},
4: {
name: 'Ethereum Testnet Rinkeby',
id: 4,
explorer: 'https://rinkeby.etherscan.io',
connection: {
primary: { status: 'connected', connected: true, on: true },
secondary: { status: 'standby', connected: false, on: true }
},
on: true
},
5: {
name: 'Ethereum Testnet Görli',
id: 5,
Expand All @@ -55,13 +45,6 @@ const chainMeta = {
nativeCurrency: ether,
primaryColor: 'accent1'
},
4: {
nativeCurrency: {
...ether,
name: 'Rinkeby Ether'
},
primaryColor: 'accent2'
},
5: {
nativeCurrency: {
...ether,
Expand All @@ -78,7 +61,7 @@ beforeEach(() => {

describe('#getActiveChains', () => {
it('returns all chains that are active', () => {
expect(getActiveChains().map((chain) => chain.chainId)).toEqual([1, 4])
expect(getActiveChains().map((chain) => chain.chainId)).toEqual([1, 5])
})

it('returns an EVM chain object', () => {
Expand All @@ -103,7 +86,8 @@ describe('#getActiveChains', () => {
wallet: {
colors: [{ r: 0, g: 210, b: 190, hex: '#00d2be' }]
}
}
},
connected: true
})
})
})
Expand Down Expand Up @@ -154,28 +138,30 @@ describe('#createChainsObserver', () => {
wallet: {
colors: [{ r: 0, g: 210, b: 190, hex: '#00d2be' }]
}
}
},
connected: true
},
{
chainId: 4,
networkId: 4,
name: 'Ethereum Testnet Rinkeby',
chainId: 5,
networkId: 5,
name: 'Ethereum Testnet Görli',
icon: [{ url: 'https://assets.coingecko.com/coins/images/ethereum.png' }],
nativeCurrency: {
name: 'Rinkeby Ether',
name: 'Görli Ether',
symbol: 'ETH',
decimals: 18
},
explorers: [
{
url: 'https://rinkeby.etherscan.io'
url: 'https://goerli.etherscan.io'
}
],
external: {
wallet: {
colors: [{ r: 255, g: 153, b: 51, hex: '#ff9933' }]
}
}
},
connected: false
},
{
chainId: 10,
Expand All @@ -196,7 +182,8 @@ describe('#createChainsObserver', () => {
wallet: {
colors: [{ r: 246, g: 36, b: 35, hex: '#f62423' }]
}
}
},
connected: true
}
])
})
Expand All @@ -215,11 +202,11 @@ describe('#createChainsObserver', () => {
observer()

const changedChains = handler.chainsChanged.mock.calls[0][0]
expect(changedChains.map((c) => c.chainId)).toEqual([1, 4, 10])
expect(changedChains.map((c) => c.chainId)).toEqual([1, 5, 10])
})

it('invokes the handler when a chain is removed', () => {
const { 4: rinkeby, ...remaining } = chains
const { 5: goerli, ...remaining } = chains
setChains(remaining)

observer()
Expand All @@ -239,16 +226,16 @@ describe('#createChainsObserver', () => {
observer()

const changedChains = handler.chainsChanged.mock.calls[0][0]
expect(changedChains.map((c) => c.chainId)).toEqual([1, 4, 137])
expect(changedChains.map((c) => c.chainId)).toEqual([1, 5, 137])
})

it('invokes the handler when a chain is deactivated', () => {
const {
4: { ...rinkeby }
5: { ...goerli }
} = chains
rinkeby.on = false
goerli.on = false

setChains({ ...chains, 4: rinkeby })
setChains({ ...chains, 5: goerli })

observer()

Expand All @@ -257,38 +244,10 @@ describe('#createChainsObserver', () => {
})

it('invokes the handler when a chain name changes', () => {
const {
4: { ...rinkeby }
} = chains
rinkeby.name = 'Rink-a-Bee'

setChains({ ...chains, 4: rinkeby })

observer()

const changedChains = handler.chainsChanged.mock.calls[0][0]
expect(changedChains.map((c) => c.chainId)).toEqual([1, 4])
})

it('invokes the handler when a connected chain is disconnected', () => {
const {
4: { ...rinkeby }
} = chains
rinkeby.connection.primary.connected = false

setChains({ ...chains, 4: rinkeby })

observer()

const changedChains = handler.chainsChanged.mock.calls[0][0]
expect(changedChains.map((c) => c.chainId)).toEqual([1])
})

it('invokes the handler when a disconnected chain is connected', () => {
const {
5: { ...goerli }
} = chains
goerli.connection.primary.connected = true
goerli.name = 'Girly'

setChains({ ...chains, 5: goerli })

Expand Down
Loading

0 comments on commit 436c76f

Please sign in to comment.