Add Signer.getChainId()

This commit is contained in:
Mark Spanbroek 2022-01-24 17:29:25 +01:00
parent 7e32f5ee51
commit 4acc6ef45c
5 changed files with 20 additions and 0 deletions

View File

@ -29,3 +29,6 @@ method getTransactionCount*(provider: Provider,
method estimateGas*(provider: Provider, method estimateGas*(provider: Provider,
transaction: Transaction): Future[UInt256] {.base.} = transaction: Transaction): Future[UInt256] {.base.} =
doAssert false, "not implemented" doAssert false, "not implemented"
method getChainId*(provider: Provider): Future[UInt256] {.base.} =
doAssert false, "not implemented"

View File

@ -80,6 +80,13 @@ method estimateGas*(provider: JsonRpcProvider,
let client = await provider.client let client = await provider.client
return await client.eth_estimateGas(transaction) 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 # Signer
method provider*(signer: JsonRpcSigner): Provider = method provider*(signer: JsonRpcSigner): Provider =

View File

@ -1,6 +1,8 @@
proc net_version(): string
proc eth_accounts: seq[Address] proc eth_accounts: seq[Address]
proc eth_blockNumber: UInt256 proc eth_blockNumber: UInt256
proc eth_call(transaction: Transaction): seq[byte] proc eth_call(transaction: Transaction): seq[byte]
proc eth_gasPrice(): UInt256 proc eth_gasPrice(): UInt256
proc eth_getTransactionCount(address: Address, blockTag: BlockTag): UInt256 proc eth_getTransactionCount(address: Address, blockTag: BlockTag): UInt256
proc eth_estimateGas(transaction: Transaction): UInt256 proc eth_estimateGas(transaction: Transaction): UInt256
proc eth_chainId(): UInt256

View File

@ -25,3 +25,6 @@ method estimateGas*(signer: Signer,
var transaction = transaction var transaction = transaction
transaction.sender = some(await signer.getAddress) transaction.sender = some(await signer.getAddress)
return await signer.provider.estimateGas(transaction) return await signer.provider.estimateGas(transaction)
method getChainId*(signer: Signer): Future[UInt256] {.base.} =
signer.provider.getChainId()

View File

@ -33,3 +33,8 @@ suite "JsonRpcSigner":
let signer = provider.getSigner() let signer = provider.getSigner()
let estimate = await signer.estimateGas(Transaction.example) let estimate = await signer.estimateGas(Transaction.example)
check estimate > 0.u256 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