Add Signer.estimateGas()
This commit is contained in:
parent
30bea4759c
commit
7e32f5ee51
|
@ -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"
|
||||
|
|
|
@ -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 =
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 &= ")"
|
||||
|
|
|
@ -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)
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue