Add Signer.estimateGas()

This commit is contained in:
Mark Spanbroek 2022-01-24 14:40:47 +01:00
parent 30bea4759c
commit 7e32f5ee51
8 changed files with 51 additions and 3 deletions

View File

@ -25,3 +25,7 @@ method getTransactionCount*(provider: Provider,
blockTag = BlockTag.latest):
Future[UInt256] {.base.} =
doAssert false, "not implemented"
method estimateGas*(provider: Provider,
transaction: Transaction): Future[UInt256] {.base.} =
doAssert false, "not implemented"

View File

@ -75,6 +75,11 @@ method getTransactionCount*(provider: JsonRpcProvider,
let client = await provider.client
return await client.eth_getTransactionCount(address, blockTag)
method estimateGas*(provider: JsonRpcProvider,
transaction: Transaction): Future[UInt256] {.async.} =
let client = await provider.client
return await client.eth_estimateGas(transaction)
# Signer
method provider*(signer: JsonRpcSigner): Provider =

View File

@ -34,7 +34,9 @@ func fromJson*(json: JsonNode, name: string, result: var UInt256) =
# Transaction
func `%`*(tx: Transaction): JsonNode =
%{ "to": %tx.to, "data": %tx.data }
result = %{ "to": %tx.to, "data": %tx.data }
if sender =? tx.sender:
result["from"] = %sender
# BlockTag

View File

@ -1,5 +1,6 @@
proc eth_accounts: seq[Address]
proc eth_blockNumber: UInt256
proc eth_call(tx: Transaction): seq[byte]
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

View File

@ -19,3 +19,9 @@ method getTransactionCount*(signer: Signer,
Future[UInt256] {.base, async.} =
let address = await signer.getAddress()
return await signer.provider.getTransactionCount(address, blockTag)
method estimateGas*(signer: Signer,
transaction: Transaction): Future[UInt256] {.base, async.} =
var transaction = transaction
transaction.sender = some(await signer.getAddress)
return await signer.provider.estimateGas(transaction)

View File

@ -2,8 +2,14 @@ import pkg/stew/byteutils
import ./basics
type Transaction* = object
sender*: ?Address
to*: Address
data*: seq[byte]
func `$`*(transaction: Transaction): string =
"(to: " & $transaction.to & ", data: 0x" & $transaction.data.toHex & ")"
result = "("
if sender =? transaction.sender:
result &= "from: " & $sender & ", "
result &= "to: " & $transaction.to & ", "
result &= "data: 0x" & $transaction.data.toHex
result &= ")"

18
testmodule/examples.nim Normal file
View File

@ -0,0 +1,18 @@
import std/random
import std/sequtils
import pkg/ethers
randomize()
proc example*(_: type Address): Address =
var address: array[20, byte]
for b in address.mitems:
b = rand(byte)
Address.init(address)
proc example*(_: type seq[byte]): seq[byte] =
let length = rand(0..<20)
newSeqWith(length, rand(byte))
proc example*(_: type Transaction): Transaction =
Transaction(to: Address.example, data: seq[byte].example)

View File

@ -1,5 +1,6 @@
import pkg/asynctest
import pkg/ethers
import ./examples
suite "JsonRpcSigner":
@ -27,3 +28,8 @@ suite "JsonRpcSigner":
let signer = provider.getSigner(accounts[9])
let count = await signer.getTransactionCount(BlockTag.pending)
check count == 0.u256
test "can estimate gas cost of a transaction":
let signer = provider.getSigner()
let estimate = await signer.estimateGas(Transaction.example)
check estimate > 0.u256