diff --git a/main/provider/chains/index.ts b/main/provider/chains/index.ts index 530ca0f2d..5df252278 100644 --- a/main/provider/chains/index.ts +++ b/main/provider/chains/index.ts @@ -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 @@ -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, diff --git a/main/provider/index.ts b/main/provider/index.ts index dded8f3a7..0c0c1008f 100644 --- a/main/provider/index.ts +++ b/main/provider/index.ts @@ -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 }) } @@ -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') @@ -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( diff --git a/test/main/provider/chains/index.test.js b/test/main/provider/chains/index.test.js index ae98f828c..37786dc86 100644 --- a/test/main/provider/chains/index.test.js +++ b/test/main/provider/chains/index.test.js @@ -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, @@ -55,13 +45,6 @@ const chainMeta = { nativeCurrency: ether, primaryColor: 'accent1' }, - 4: { - nativeCurrency: { - ...ether, - name: 'Rinkeby Ether' - }, - primaryColor: 'accent2' - }, 5: { nativeCurrency: { ...ether, @@ -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', () => { @@ -103,7 +86,8 @@ describe('#getActiveChains', () => { wallet: { colors: [{ r: 0, g: 210, b: 190, hex: '#00d2be' }] } - } + }, + connected: true }) }) }) @@ -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, @@ -196,7 +182,8 @@ describe('#createChainsObserver', () => { wallet: { colors: [{ r: 246, g: 36, b: 35, hex: '#f62423' }] } - } + }, + connected: true } ]) }) @@ -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() @@ -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() @@ -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 }) diff --git a/test/main/provider/index.test.js b/test/main/provider/index.test.js index 1eed11285..533e07c2d 100644 --- a/test/main/provider/index.test.js +++ b/test/main/provider/index.test.js @@ -65,7 +65,7 @@ beforeEach(() => { connection.connections = { ethereum: { 1: { chainConfig: chainConfig(1, 'london'), primary: { connected: true } }, - 4: { chainConfig: chainConfig(4, 'london'), primary: { connected: true } } + 5: { chainConfig: chainConfig(5, 'london'), primary: { connected: true } } } } @@ -79,7 +79,9 @@ beforeEach(() => { describe('#send', () => { beforeEach(() => { - store.set('main.origins', '8073729a-5e59-53b7-9e69-5d9bcff94087', { chain: { id: 1, type: 'ethereum' } }) + store.set('main.origins', '8073729a-5e59-53b7-9e69-5d9bcff94087', { + chain: { id: 1, type: 'ethereum', on: true } + }) }) const send = (request, cb = jest.fn()) => @@ -103,7 +105,11 @@ describe('#send', () => { send(request) - expect(connection.send).toHaveBeenCalledWith(request, expect.any(Function), { type: 'ethereum', id: 1 }) + expect(connection.send).toHaveBeenCalledWith(request, expect.any(Function), { + type: 'ethereum', + id: 1, + on: true + }) }) it('returns an error when an unknown chain is given', (done) => { @@ -130,7 +136,7 @@ describe('#send', () => { describe('#eth_chainId', () => { it('returns the current chain id from the store', () => { - store.set('main.networks.ethereum', 1, { id: 1 }) + store.set('main.networks.ethereum', 1, { id: 1, on: true }) send({ method: 'eth_chainId', chainId: '0x1' }, (response) => { expect(response.result).toBe('0x1') @@ -138,20 +144,17 @@ describe('#send', () => { }) it('returns a chain id from the target chain', () => { - store.set('main.networks.ethereum', 4, { id: 4 }) + store.set('main.networks.ethereum', 5, { id: 5, on: true }) - send({ method: 'eth_chainId', chainId: '0x4' }, (response) => { - expect(response.result).toBe('0x4') + send({ method: 'eth_chainId', chainId: '0x5' }, (response) => { + expect(response.result).toBe('0x5') }) }) - it('returns an error for a disconnected chain', () => { - connection.connections.ethereum[11] = { - chainConfig: chainConfig(11, 'london'), - primary: { connected: false } - } + it('returns an error for a disabled chain', () => { + store.set('main.networks.ethereum', 5, { id: 5, on: false }) - send({ method: 'eth_chainId', chainId: '0xb' }, (response) => { + send({ method: 'eth_chainId', chainId: '0x5' }, (response) => { expect(response.error.message).toBe('not connected') expect(response.result).toBeUndefined() }) @@ -188,7 +191,7 @@ describe('#send', () => { done() } - sendRequest({ chainId: '0x4', nativeCurrency: { symbol: 'rETH' } }, cb) + sendRequest({ chainId: '0x5', nativeCurrency: { symbol: 'gETH' } }, cb) }) it('rejects a request with no native currency', (done) => { @@ -198,7 +201,7 @@ describe('#send', () => { done() } - sendRequest({ chainId: '0x4', chainName: 'Rinkeby' }, cb) + sendRequest({ chainId: '0x5', chainName: 'Goerli' }, cb) }) it('should create a request to add the chain', (done) => { @@ -398,6 +401,7 @@ describe('#send', () => { let request beforeEach(() => { + store.set('main.networks.ethereum.1', { id: 1, on: true }) store.set('main.tokens.custom', []) request = { @@ -448,6 +452,26 @@ describe('#send', () => { }) }) + it('rejects a request when the chain does not exist', () => { + store.set('main.networks.ethereum.1', undefined) + + send(request, ({ error }) => { + expect(error.code).toBe(-1) + expect(error.message).toMatch('not connected') + expect(accountRequests).toHaveLength(0) + }) + }) + + it('rejects a request when the chain is disabled', () => { + store.set('main.networks.ethereum.1', { id: 1, on: false }) + + send(request, ({ error }) => { + expect(error.code).toBe(-1) + expect(error.message).toMatch('not connected') + expect(accountRequests).toHaveLength(0) + }) + }) + it('rejects a request with no type', () => { delete request.params.type @@ -503,7 +527,7 @@ describe('#send', () => { }) }) - it('returns a list of active chains', () => { + it('returns a list of enabled chains', () => { store.set('main.networks.ethereum', { 137: { name: 'polygon', @@ -541,7 +565,8 @@ describe('#send', () => { name: 'Ether', symbol: 'ETH', decimals: 18 - } + }, + connected: true }, { name: 'polygon', @@ -558,20 +583,21 @@ describe('#send', () => { name: 'Matic', symbol: 'MATIC', decimals: 18 - } + }, + connected: true } ]) }) }) - it('does not return disconnected chains', () => { + it('does not return disabled chains', () => { store.set('main.networks.ethereum', { 137: { name: 'polygon', id: 137, explorer: 'https://polygonscan.com', connection: { primary: { connected: false }, secondary: { connected: false } }, - on: true + on: false }, 1: { name: 'mainnet', @@ -597,7 +623,8 @@ describe('#send', () => { name: 'Ether', symbol: 'ETH', decimals: 18 - } + }, + connected: true } ]) }) @@ -692,7 +719,7 @@ describe('#send', () => { }) describe('#eth_getTransactionByHash', () => { - const chain = 4 + const chain = 5 const txHash = '0x06c1c968d4bd20c0ebfed34f6f34d8a5d189d9d2ce801f2ee8dd45dac32628d5' const request = { method: 'eth_getTransactionByHash', @@ -824,7 +851,7 @@ describe('#send', () => { } catch (e) { done(e) } - }, '0x4') + }, '0x5') }) it('populates the transaction with the request chain id if not provided in the transaction', (done) => { @@ -1751,7 +1778,8 @@ describe('state change events', () => { name: 'Ether', symbol: 'ETH', decimals: 18 - } + }, + connected: true }, { name: 'Polygon', @@ -1768,7 +1796,8 @@ describe('state change events', () => { name: 'Matic', symbol: 'MATIC', decimals: 18 - } + }, + connected: true } ])