Skip to content

Commit

Permalink
Merge pull request #50 from zzGHzz/main
Browse files Browse the repository at this point in the history
return block id in hex string for eth_chainId
  • Loading branch information
zzGHzz authored Sep 19, 2023
2 parents 8963b62 + b6741fe commit 66de205
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
3 changes: 1 addition & 2 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,7 @@ export class Provider extends EventEmitter implements IProvider {
}

private _getChainId = async (_: any[]) => {
const bigIntValue = BigInt(this.chainId);
return bigIntValue;
return this.chainId;
}

private _getBlockByNumber = async (params: any[]) => {
Expand Down
10 changes: 5 additions & 5 deletions test/ethers/getChainId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { TestObject } from '../testSetup';
import { modifyProvider } from '../../src/ethers';
import { BrowserProvider } from 'ethers';

describe('Testing function getTransaction', function () {
describe('Testing getting chain id', function () {
before(function () {
const { eip1193Providers, connexs } = this.testObject as TestObject;
this.provider = modifyProvider(new BrowserProvider(eip1193Providers.main));
Expand All @@ -15,14 +15,14 @@ describe('Testing function getTransaction', function () {
})


it('get mainnet genesis Id', async function () {
const expected: string = "0x851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a";
it('should return the correct mainnet genesis Id', async function () {
const expected: string = "0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a";

try {
let actual = await this.provider.getNetwork()
let actual = await this.provider.getNetwork();
let chainId = actual.chainId;

expect("0x" + chainId.toString(16)).to.eq(expected)
expect(chainId).to.eq(BigInt(expected));

} catch (err: any) {
assert.fail(`Unexpected error: ${err}`);
Expand Down
18 changes: 18 additions & 0 deletions test/provider/eth_chainId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import "mocha";
import { expect, assert } from "chai";
import { TestObject } from "../testSetup";

describe('Test function eth_chainId', function () {
it('should return the correct mainnet genesis id', async function () {
const { eip1193Providers, connexs } = this.testObject as TestObject;
const provider = eip1193Providers.main;
const connex = connexs.main;

try {
const id = await provider.request({ method: 'eth_chainId' });
expect(id).to.eql(connex.thor.genesis.id);
} catch (err: any) {
assert.fail(err.message || err);
}
})
});
16 changes: 6 additions & 10 deletions test/web3/getChainId.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@ import { Web3 } from 'web3';
import { ProviderWeb3 } from '../../src/index';

describe('Testing function getChainId', function () {
before(async function () {
it('should return the correct mainnet genesis Id', async function() {
const id: string = "0x00000000851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a";
const { eip1193Providers } = this.testObject as TestObject;
this.web3 = new Web3(new ProviderWeb3(eip1193Providers.main));
})

it('get mainnet genesis Id', async function() {
const tag: string = "0x851caf3cfdb6e899cf5958bfb1ac3413d346d43539627e6be7ec1b4a";
const web3 = new Web3(new ProviderWeb3(eip1193Providers.main));

try {
this.web3.extend({
web3.extend({
property: 'eth',
methods: [{
name: 'getChainId',
call: 'eth_chainId',
params: 0,
}]
});

let res = await this.web3.eth.getChainId();
expect("0x" + res.toString(16)).to.eql(tag);
let res = await web3.eth.getChainId();
expect(res.toString(16)).to.eql(id.toLowerCase());
} catch (err: any) {
assert.fail(`Unexpected error: ${err}`);
}
Expand Down

0 comments on commit 66de205

Please sign in to comment.