2021-11-14 07:52:22 +00:00
|
|
|
# Nimbus
|
|
|
|
# Copyright (c) 2021 Status Research & Development GmbH
|
|
|
|
# Licensed under either of
|
|
|
|
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
|
|
|
|
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
|
|
# at your option.
|
|
|
|
# This file may not be copied, modified, or distributed except according to
|
|
|
|
# those terms.
|
|
|
|
|
|
|
|
import
|
|
|
|
std/strutils,
|
|
|
|
eth/[common, keys, rlp],
|
|
|
|
stew/byteutils,
|
|
|
|
chronos, stint,
|
|
|
|
json_rpc/[rpcclient],
|
2022-12-02 04:39:12 +00:00
|
|
|
../../../nimbus/transaction,
|
|
|
|
../../../nimbus/utils/utils,
|
2021-11-14 07:52:22 +00:00
|
|
|
../../../nimbus/rpc/hexstrings,
|
2022-04-20 07:57:50 +00:00
|
|
|
../../../tests/rpcclient/eth_api
|
|
|
|
|
|
|
|
export eth_api
|
2021-11-14 07:52:22 +00:00
|
|
|
|
|
|
|
proc fromHex(x: type Hash256, hex: EthHashStr): Hash256 =
|
|
|
|
hexToByteArray(hex.string, result.data)
|
|
|
|
|
|
|
|
proc sendTransaction*(client: RpcClient, tx: Transaction): Future[bool] {.async.} =
|
|
|
|
let data = rlp.encode(tx)
|
|
|
|
let txHash = keccakHash(data)
|
|
|
|
let hex = await client.eth_sendRawTransaction(hexDataStr(data))
|
|
|
|
let decodedHash = Hash256.fromHex(hex)
|
|
|
|
result = decodedHash == txHash
|
|
|
|
|
|
|
|
proc blockNumber*(client: RpcClient): Future[uint64] {.async.} =
|
|
|
|
let hex = await client.eth_blockNumber()
|
|
|
|
result = parseHexInt(hex.string).uint64
|
|
|
|
|
|
|
|
proc balanceAt*(client: RpcClient, address: EthAddress, blockNumber: uint64): Future[UInt256] {.async.} =
|
|
|
|
let hex = await client.eth_getBalance(ethAddressStr(address), encodeQuantity(blockNumber).string)
|
|
|
|
result = UInt256.fromHex(hex.string)
|
|
|
|
|
|
|
|
proc balanceAt*(client: RpcClient, address: EthAddress): Future[UInt256] {.async.} =
|
|
|
|
let hex = await client.eth_getBalance(ethAddressStr(address), "latest")
|
|
|
|
result = UInt256.fromHex(hex.string)
|
2022-05-31 08:42:01 +00:00
|
|
|
|
|
|
|
proc nonceAt*(client: RpcClient, address: EthAddress): Future[AccountNonce] {.async.} =
|
|
|
|
let hex = await client.eth_getTransactionCount(ethAddressStr(address), "latest")
|
|
|
|
result = parseHexInt(hex.string).AccountNonce
|
|
|
|
|
|
|
|
proc txReceipt*(client: RpcClient, txHash: Hash256): Future[Option[Receipt]] {.async.} =
|
|
|
|
let rr = await client.eth_getTransactionReceipt(txHash)
|
|
|
|
if rr.isNone:
|
|
|
|
return none(Receipt)
|
|
|
|
|
|
|
|
let rc = rr.get()
|
|
|
|
let rec = Receipt(
|
|
|
|
receiptType: LegacyReceipt,
|
|
|
|
isHash : rc.root.isSome,
|
|
|
|
status : rc.status.isSome,
|
|
|
|
hash : rc.root.get(Hash256()),
|
|
|
|
cumulativeGasUsed: parseHexInt(rc.cumulativeGasUsed.string).GasInt,
|
|
|
|
bloom : BloomFilter(rc.logsBloom),
|
|
|
|
logs : rc.logs
|
|
|
|
)
|
|
|
|
result = some(rec)
|
|
|
|
|
|
|
|
proc gasUsed*(client: RpcClient, txHash: Hash256): Future[Option[GasInt]] {.async.} =
|
|
|
|
let rr = await client.eth_getTransactionReceipt(txHash)
|
|
|
|
if rr.isNone:
|
|
|
|
return none(GasInt)
|
|
|
|
|
|
|
|
let rc = rr.get()
|
|
|
|
result = some(parseHexInt(rc.gasUsed.string).GasInt)
|