diff --git a/ethers/provider.nim b/ethers/provider.nim index 0972868..9487372 100644 --- a/ethers/provider.nim +++ b/ethers/provider.nim @@ -29,3 +29,6 @@ method getTransactionCount*(provider: Provider, method estimateGas*(provider: Provider, transaction: Transaction): Future[UInt256] {.base.} = doAssert false, "not implemented" + +method getChainId*(provider: Provider): Future[UInt256] {.base.} = + doAssert false, "not implemented" diff --git a/ethers/providers/jsonrpc.nim b/ethers/providers/jsonrpc.nim index 9f70576..0562cc7 100644 --- a/ethers/providers/jsonrpc.nim +++ b/ethers/providers/jsonrpc.nim @@ -80,6 +80,13 @@ method estimateGas*(provider: JsonRpcProvider, let client = await provider.client return await client.eth_estimateGas(transaction) +method getChainId*(provider: JsonRpcProvider): Future[UInt256] {.async.} = + let client = await provider.client + try: + return await client.eth_chainId() + except CatchableError: + return parse(await client.net_version(), UInt256) + # Signer method provider*(signer: JsonRpcSigner): Provider = diff --git a/ethers/providers/rpccalls/signatures.nim b/ethers/providers/rpccalls/signatures.nim index b308ef0..68ade27 100644 --- a/ethers/providers/rpccalls/signatures.nim +++ b/ethers/providers/rpccalls/signatures.nim @@ -1,6 +1,8 @@ +proc net_version(): string proc eth_accounts: seq[Address] proc eth_blockNumber: UInt256 proc eth_call(transaction: Transaction): seq[byte] proc eth_gasPrice(): UInt256 proc eth_getTransactionCount(address: Address, blockTag: BlockTag): UInt256 proc eth_estimateGas(transaction: Transaction): UInt256 +proc eth_chainId(): UInt256 diff --git a/ethers/signer.nim b/ethers/signer.nim index 17b8840..b1c4bf7 100644 --- a/ethers/signer.nim +++ b/ethers/signer.nim @@ -25,3 +25,6 @@ method estimateGas*(signer: Signer, var transaction = transaction transaction.sender = some(await signer.getAddress) return await signer.provider.estimateGas(transaction) + +method getChainId*(signer: Signer): Future[UInt256] {.base.} = + signer.provider.getChainId() diff --git a/testmodule/testJsonRpcSigner.nim b/testmodule/testJsonRpcSigner.nim index 8963b25..1b8a1dd 100644 --- a/testmodule/testJsonRpcSigner.nim +++ b/testmodule/testJsonRpcSigner.nim @@ -33,3 +33,8 @@ suite "JsonRpcSigner": let signer = provider.getSigner() let estimate = await signer.estimateGas(Transaction.example) check estimate > 0.u256 + + test "can retrieve chain id": + let signer = provider.getSigner() + let chainId = await signer.getChainId() + check chainId == 31337.u256 # hardhat chain id