From 9aab498f0e9ec7d9747d7eb3685bc3458c7fa3bf Mon Sep 17 00:00:00 2001 From: William Hua Date: Tue, 1 Aug 2023 14:43:58 -0400 Subject: [PATCH] provider: return hex chain id for eth_chainId --- .../browser/wallet-provider/dapp.test.ts | 6 ++- packages/provider/src/provider.ts | 2 +- packages/provider/tests/provider.spec.ts | 46 ++++++++++--------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/packages/0xsequence/tests/browser/wallet-provider/dapp.test.ts b/packages/0xsequence/tests/browser/wallet-provider/dapp.test.ts index e66c376c7..98cc1ede7 100644 --- a/packages/0xsequence/tests/browser/wallet-provider/dapp.test.ts +++ b/packages/0xsequence/tests/browser/wallet-provider/dapp.test.ts @@ -123,6 +123,8 @@ export const tests = async () => { }) await test('multiple networks', async () => { + const toHexString = n => `0x${n.toString(16)}` + // chainId 31337 { assert.equal(provider.getChainId(), 31337, 'provider chainId is 31337') @@ -134,7 +136,7 @@ export const tests = async () => { assert.equal(netVersion, '31337', 'net_version check') const chainId = await provider.send('eth_chainId', []) - assert.equal(chainId, 31337, 'eth_chainId check') + assert.equal(chainId, toHexString(31337), 'eth_chainId check') const chainId2 = await signer.getChainId() assert.equal(chainId2, 31337, 'chainId check') @@ -152,7 +154,7 @@ export const tests = async () => { assert.equal(netVersion, '31338', '2nd chain, net_version check - 4') const chainId = await provider2.send('eth_chainId', []) - assert.equal(chainId, 31338, '2nd chain, eth_chainId check - 5') + assert.equal(chainId, toHexString(31338), '2nd chain, eth_chainId check - 5') const chainId2 = await provider2.getSigner().getChainId() assert.equal(chainId2, 31338, '2nd chain, chainId check - 6') diff --git a/packages/provider/src/provider.ts b/packages/provider/src/provider.ts index 91ef7248c..88be9c51f 100644 --- a/packages/provider/src/provider.ts +++ b/packages/provider/src/provider.ts @@ -247,7 +247,7 @@ export class SequenceProvider extends ethers.providers.BaseProvider implements I async perform(method: string, params: any): Promise { // First we check if the method should be handled by the client if (method === 'eth_chainId') { - return this.useChainId() + return `0x${(await this.useChainId()).toString(16)}` } if (method === 'eth_accounts') { diff --git a/packages/provider/tests/provider.spec.ts b/packages/provider/tests/provider.spec.ts index 332fb132c..4c39f1146 100644 --- a/packages/provider/tests/provider.spec.ts +++ b/packages/provider/tests/provider.spec.ts @@ -1188,27 +1188,29 @@ describe('SequenceProvider', () => { }) describe('perform implementation', () => { + const toHexString = n => `0x${n.toString(16)}` + describe('perform eth_chainId', async () => { it('should return initial default chainId', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.perform('eth_chainId', [])).to.equal(31337) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31337)) }) it('should return new default chainId', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) provider.setDefaultChainId(31338) - expect(await provider.perform('eth_chainId', [])).to.equal(31338) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31338)) }) it('should return static chainId', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.getProvider(31337).perform('eth_chainId', [])).to.equal(31337) - expect(await provider.getProvider(31338).perform('eth_chainId', [])).to.equal(31338) + expect(await provider.getProvider(31337).perform('eth_chainId', [])).to.equal(toHexString(31337)) + expect(await provider.getProvider(31338).perform('eth_chainId', [])).to.equal(toHexString(31338)) }) it('should return chainId using request', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.request({ method: 'eth_chainId' })).to.equal(31337) + expect(await provider.request({ method: 'eth_chainId' })).to.equal(toHexString(31337)) }) }) @@ -1241,7 +1243,7 @@ describe('SequenceProvider', () => { describe('perform wallet_switchEthereumChain', async () => { it('should switch default chainId using request', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.request({ method: 'eth_chainId' })).to.equal(31337) + expect(await provider.request({ method: 'eth_chainId' })).to.equal(toHexString(31337)) await provider.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: '0x7a6a' }] }) expect(defaultChainId).to.equal(31338) @@ -1249,38 +1251,38 @@ describe('SequenceProvider', () => { it('should switch default chainId using object', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.perform('eth_chainId', [])).to.equal(31337) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31337)) await provider.perform('wallet_switchEthereumChain', [{ chainId: '0x7a6a' }]) expect(defaultChainId).to.equal(31338) - expect(await provider.perform('eth_chainId', [])).to.equal(31338) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31338)) }) it('should switch default chainId using hex string', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.perform('eth_chainId', [])).to.equal(31337) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31337)) await provider.perform('wallet_switchEthereumChain', ['0x7a6a']) expect(defaultChainId).to.equal(31338) - expect(await provider.perform('eth_chainId', [])).to.equal(31338) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31338)) }) it('should switch default chainId using number', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.perform('eth_chainId', [])).to.equal(31337) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31337)) await provider.perform('wallet_switchEthereumChain', [31338]) expect(defaultChainId).to.equal(31338) - expect(await provider.perform('eth_chainId', [])).to.equal(31338) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31338)) }) it('should switch default chainId using string', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.perform('eth_chainId', [])).to.equal(31337) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31337)) await provider.perform('wallet_switchEthereumChain', ['31338']) expect(defaultChainId).to.equal(31338) - expect(await provider.perform('eth_chainId', [])).to.equal(31338) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31338)) }) it('should fail to switch default chainId on static network provider', async () => { @@ -1292,38 +1294,38 @@ describe('SequenceProvider', () => { describe('using the setDefaultChainId method', async () => { it('should switch default chainId using name', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.perform('eth_chainId', [])).to.equal(31337) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31337)) provider.setDefaultChainId('hardhat2') expect(defaultChainId).to.equal(31338) - expect(await provider.perform('eth_chainId', [])).to.equal(31338) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31338)) }) it('should switch default chainId using number', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.perform('eth_chainId', [])).to.equal(31337) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31337)) provider.setDefaultChainId(31338) expect(defaultChainId).to.equal(31338) - expect(await provider.perform('eth_chainId', [])).to.equal(31338) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31338)) }) it('should switch default chainId using string', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.perform('eth_chainId', [])).to.equal(31337) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31337)) provider.setDefaultChainId('31338') expect(defaultChainId).to.equal(31338) - expect(await provider.perform('eth_chainId', [])).to.equal(31338) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31338)) }) it('should switch default chainId using hex string', async () => { const provider = new SequenceProvider(basicMockClient, providerFor) - expect(await provider.perform('eth_chainId', [])).to.equal(31337) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31337)) provider.setDefaultChainId('0x7a6a') expect(defaultChainId).to.equal(31338) - expect(await provider.perform('eth_chainId', [])).to.equal(31338) + expect(await provider.perform('eth_chainId', [])).to.equal(toHexString(31338)) }) it('should fail to switch default chainId on static network provider', async () => {