Adds Signer.getTransactionCount()
This commit is contained in:
parent
54c66db827
commit
30bea4759c
|
@ -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
|
|
@ -1,8 +1,10 @@
|
||||||
import ./basics
|
import ./basics
|
||||||
import ./transaction
|
import ./transaction
|
||||||
|
import ./blocktag
|
||||||
|
|
||||||
export basics
|
export basics
|
||||||
export transaction
|
export transaction
|
||||||
|
export blocktag
|
||||||
|
|
||||||
push: {.upraises: [].}
|
push: {.upraises: [].}
|
||||||
|
|
||||||
|
@ -17,3 +19,9 @@ method call*(provider: Provider, tx: Transaction): Future[seq[byte]] {.base.} =
|
||||||
|
|
||||||
method getGasPrice*(provider: Provider): Future[UInt256] {.base.} =
|
method getGasPrice*(provider: Provider): Future[UInt256] {.base.} =
|
||||||
doAssert false, "not implemented"
|
doAssert false, "not implemented"
|
||||||
|
|
||||||
|
method getTransactionCount*(provider: Provider,
|
||||||
|
address: Address,
|
||||||
|
blockTag = BlockTag.latest):
|
||||||
|
Future[UInt256] {.base.} =
|
||||||
|
doAssert false, "not implemented"
|
||||||
|
|
|
@ -68,6 +68,13 @@ method getGasPrice*(provider: JsonRpcProvider): Future[UInt256] {.async.} =
|
||||||
let client = await provider.client
|
let client = await provider.client
|
||||||
return await client.eth_gasprice()
|
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
|
# Signer
|
||||||
|
|
||||||
method provider*(signer: JsonRpcSigner): Provider =
|
method provider*(signer: JsonRpcSigner): Provider =
|
||||||
|
|
|
@ -2,6 +2,7 @@ import std/os
|
||||||
import pkg/json_rpc/rpcclient
|
import pkg/json_rpc/rpcclient
|
||||||
import ../basics
|
import ../basics
|
||||||
import ../transaction
|
import ../transaction
|
||||||
|
import ../blocktag
|
||||||
import ./rpccalls/conversions
|
import ./rpccalls/conversions
|
||||||
|
|
||||||
const file = currentSourcePath.parentDir / "rpccalls" / "signatures.nim"
|
const file = currentSourcePath.parentDir / "rpccalls" / "signatures.nim"
|
||||||
|
|
|
@ -2,6 +2,7 @@ import std/json
|
||||||
import pkg/stew/byteutils
|
import pkg/stew/byteutils
|
||||||
import ../../basics
|
import ../../basics
|
||||||
import ../../transaction
|
import ../../transaction
|
||||||
|
import ../../blocktag
|
||||||
|
|
||||||
# byte sequence
|
# byte sequence
|
||||||
|
|
||||||
|
@ -34,3 +35,8 @@ func fromJson*(json: JsonNode, name: string, result: var UInt256) =
|
||||||
|
|
||||||
func `%`*(tx: Transaction): JsonNode =
|
func `%`*(tx: Transaction): JsonNode =
|
||||||
%{ "to": %tx.to, "data": %tx.data }
|
%{ "to": %tx.to, "data": %tx.data }
|
||||||
|
|
||||||
|
# BlockTag
|
||||||
|
|
||||||
|
func `%`*(blockTag: BlockTag): JsonNode =
|
||||||
|
%($blockTag)
|
||||||
|
|
|
@ -2,3 +2,4 @@ proc eth_accounts: seq[Address]
|
||||||
proc eth_blockNumber: UInt256
|
proc eth_blockNumber: UInt256
|
||||||
proc eth_call(tx: Transaction): seq[byte]
|
proc eth_call(tx: Transaction): seq[byte]
|
||||||
proc eth_gasPrice(): UInt256
|
proc eth_gasPrice(): UInt256
|
||||||
|
proc eth_getTransactionCount(address: Address, blockTag: BlockTag): UInt256
|
||||||
|
|
|
@ -13,3 +13,9 @@ method getAddress*(signer: Signer): Future[Address] {.base.} =
|
||||||
|
|
||||||
method getGasPrice*(signer: Signer): Future[UInt256] {.base.} =
|
method getGasPrice*(signer: Signer): Future[UInt256] {.base.} =
|
||||||
signer.provider.getGasPrice()
|
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)
|
||||||
|
|
|
@ -4,20 +4,26 @@ import pkg/ethers
|
||||||
suite "JsonRpcSigner":
|
suite "JsonRpcSigner":
|
||||||
|
|
||||||
var provider: JsonRpcProvider
|
var provider: JsonRpcProvider
|
||||||
|
var accounts: seq[Address]
|
||||||
|
|
||||||
setup:
|
setup:
|
||||||
provider = JsonRpcProvider.new()
|
provider = JsonRpcProvider.new()
|
||||||
|
accounts = await provider.listAccounts()
|
||||||
|
|
||||||
test "is connected to the first account of the provider by default":
|
test "is connected to the first account of the provider by default":
|
||||||
let signer = provider.getSigner()
|
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":
|
test "can connect to a different account":
|
||||||
let account = (await provider.listAccounts())[1]
|
let signer = provider.getSigner(accounts[1])
|
||||||
let signer = provider.getSigner(account)
|
check (await signer.getAddress()) == accounts[1]
|
||||||
check (await signer.getAddress()) == account
|
|
||||||
|
|
||||||
test "can retrieve gas price":
|
test "can retrieve gas price":
|
||||||
let signer = provider.getSigner()
|
let signer = provider.getSigner()
|
||||||
let gasprice = await signer.getGasPrice()
|
let gasprice = await signer.getGasPrice()
|
||||||
check gasprice > 0.u256
|
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
|
||||||
|
|
Loading…
Reference in New Issue