Skip to content

Commit

Permalink
provider: return hex chain id for eth_chainId
Browse files Browse the repository at this point in the history
  • Loading branch information
attente committed Aug 1, 2023
1 parent 7fa0984 commit 9aab498
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
Expand All @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion packages/provider/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export class SequenceProvider extends ethers.providers.BaseProvider implements I
async perform(method: string, params: any): Promise<any> {
// 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') {
Expand Down
46 changes: 24 additions & 22 deletions packages/provider/tests/provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
})

Expand Down Expand Up @@ -1241,46 +1243,46 @@ 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)
})

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 () => {
Expand All @@ -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 () => {
Expand Down

0 comments on commit 9aab498

Please sign in to comment.