Adds Signer.getTransactionCount()

This commit is contained in:
Mark Spanbroek 2022-01-24 12:14:31 +01:00
parent 54c66db827
commit 30bea4759c
8 changed files with 76 additions and 4 deletions

37
ethers/blocktag.nim Normal file
View File

@ -0,0 +1,37 @@
import pkg/stint
import pkg/upraises
push: {.upraises: [].}
type
BlockTagKind = enum
stringBlockTag
numberBlockTag
BlockTag* = object
case kind: BlockTagKind
of stringBlockTag:
stringValue: string
of numberBlockTag:
numberValue: UInt256
func init(_: type BlockTag, value: string): BlockTag =
BlockTag(kind: stringBlockTag, stringValue: value)
func init*(_: type BlockTag, value: UInt256): BlockTag =
BlockTag(kind: numberBlockTag, numberValue: value)
func earliest*(_: type BlockTag): BlockTag =
BlockTag.init("earliest")
func latest*(_: type BlockTag): BlockTag =
BlockTag.init("latest")
func pending*(_: type BlockTag): BlockTag =
BlockTag.init("pending")
func `$`*(blockTag: BlockTag): string =
case blockTag.kind
of stringBlockTag:
blockTag.stringValue
of numberBlockTag:
"0x" & blockTag.numberValue.toHex

View File

@ -1,8 +1,10 @@
import ./basics
import ./transaction
import ./blocktag
export basics
export transaction
export blocktag
push: {.upraises: [].}
@ -17,3 +19,9 @@ method call*(provider: Provider, tx: Transaction): Future[seq[byte]] {.base.} =
method getGasPrice*(provider: Provider): Future[UInt256] {.base.} =
doAssert false, "not implemented"
method getTransactionCount*(provider: Provider,
address: Address,
blockTag = BlockTag.latest):
Future[UInt256] {.base.} =
doAssert false, "not implemented"

View File

@ -68,6 +68,13 @@ method getGasPrice*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
let client = await provider.client
return await client.eth_gasprice()
method getTransactionCount*(provider: JsonRpcProvider,
address: Address,
blockTag = BlockTag.latest):
Future[UInt256] {.async.} =
let client = await provider.client
return await client.eth_getTransactionCount(address, blockTag)
# Signer
method provider*(signer: JsonRpcSigner): Provider =

View File

@ -2,6 +2,7 @@ import std/os
import pkg/json_rpc/rpcclient
import ../basics
import ../transaction
import ../blocktag
import ./rpccalls/conversions
const file = currentSourcePath.parentDir / "rpccalls" / "signatures.nim"

View File

@ -2,6 +2,7 @@ import std/json
import pkg/stew/byteutils
import ../../basics
import ../../transaction
import ../../blocktag
# byte sequence
@ -34,3 +35,8 @@ func fromJson*(json: JsonNode, name: string, result: var UInt256) =
func `%`*(tx: Transaction): JsonNode =
%{ "to": %tx.to, "data": %tx.data }
# BlockTag
func `%`*(blockTag: BlockTag): JsonNode =
%($blockTag)

View File

@ -2,3 +2,4 @@ proc eth_accounts: seq[Address]
proc eth_blockNumber: UInt256
proc eth_call(tx: Transaction): seq[byte]
proc eth_gasPrice(): UInt256
proc eth_getTransactionCount(address: Address, blockTag: BlockTag): UInt256

View File

@ -13,3 +13,9 @@ method getAddress*(signer: Signer): Future[Address] {.base.} =
method getGasPrice*(signer: Signer): Future[UInt256] {.base.} =
signer.provider.getGasPrice()
method getTransactionCount*(signer: Signer,
blockTag = BlockTag.latest):
Future[UInt256] {.base, async.} =
let address = await signer.getAddress()
return await signer.provider.getTransactionCount(address, blockTag)

View File

@ -4,20 +4,26 @@ import pkg/ethers
suite "JsonRpcSigner":
var provider: JsonRpcProvider
var accounts: seq[Address]
setup:
provider = JsonRpcProvider.new()
accounts = await provider.listAccounts()
test "is connected to the first account of the provider by default":
let signer = provider.getSigner()
check (await signer.getAddress()) == (await provider.listAccounts())[0]
check (await signer.getAddress()) == accounts[0]
test "can connect to a different account":
let account = (await provider.listAccounts())[1]
let signer = provider.getSigner(account)
check (await signer.getAddress()) == account
let signer = provider.getSigner(accounts[1])
check (await signer.getAddress()) == accounts[1]
test "can retrieve gas price":
let signer = provider.getSigner()
let gasprice = await signer.getGasPrice()
check gasprice > 0.u256
test "can retrieve transaction count":
let signer = provider.getSigner(accounts[9])
let count = await signer.getTransactionCount(BlockTag.pending)
check count == 0.u256