mirror of
https://github.com/status-im/nim-ethers.git
synced 2025-01-26 07:18:57 +00:00
Add Signer.estimateGas()
This commit is contained in:
parent
30bea4759c
commit
7e32f5ee51
@ -25,3 +25,7 @@ method getTransactionCount*(provider: Provider,
|
|||||||
blockTag = BlockTag.latest):
|
blockTag = BlockTag.latest):
|
||||||
Future[UInt256] {.base.} =
|
Future[UInt256] {.base.} =
|
||||||
doAssert false, "not implemented"
|
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
|
let client = await provider.client
|
||||||
return await client.eth_getTransactionCount(address, blockTag)
|
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
|
# Signer
|
||||||
|
|
||||||
method provider*(signer: JsonRpcSigner): Provider =
|
method provider*(signer: JsonRpcSigner): Provider =
|
||||||
|
@ -34,7 +34,9 @@ func fromJson*(json: JsonNode, name: string, result: var UInt256) =
|
|||||||
# Transaction
|
# Transaction
|
||||||
|
|
||||||
func `%`*(tx: Transaction): JsonNode =
|
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
|
# BlockTag
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
proc eth_accounts: seq[Address]
|
proc eth_accounts: seq[Address]
|
||||||
proc eth_blockNumber: UInt256
|
proc eth_blockNumber: UInt256
|
||||||
proc eth_call(tx: 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
|
||||||
|
@ -19,3 +19,9 @@ method getTransactionCount*(signer: Signer,
|
|||||||
Future[UInt256] {.base, async.} =
|
Future[UInt256] {.base, async.} =
|
||||||
let address = await signer.getAddress()
|
let address = await signer.getAddress()
|
||||||
return await signer.provider.getTransactionCount(address, blockTag)
|
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
|
import ./basics
|
||||||
|
|
||||||
type Transaction* = object
|
type Transaction* = object
|
||||||
|
sender*: ?Address
|
||||||
to*: Address
|
to*: Address
|
||||||
data*: seq[byte]
|
data*: seq[byte]
|
||||||
|
|
||||||
func `$`*(transaction: Transaction): string =
|
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
18
testmodule/examples.nim
Normal 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)
|
@ -1,5 +1,6 @@
|
|||||||
import pkg/asynctest
|
import pkg/asynctest
|
||||||
import pkg/ethers
|
import pkg/ethers
|
||||||
|
import ./examples
|
||||||
|
|
||||||
suite "JsonRpcSigner":
|
suite "JsonRpcSigner":
|
||||||
|
|
||||||
@ -27,3 +28,8 @@ suite "JsonRpcSigner":
|
|||||||
let signer = provider.getSigner(accounts[9])
|
let signer = provider.getSigner(accounts[9])
|
||||||
let count = await signer.getTransactionCount(BlockTag.pending)
|
let count = await signer.getTransactionCount(BlockTag.pending)
|
||||||
check count == 0.u256
|
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…
x
Reference in New Issue
Block a user