nimbus-eth1/hive_integration/nodocker/rpc/client.nim

82 lines
2.6 KiB
Nim
Raw Normal View History

2021-11-14 07:52:22 +00:00
# Nimbus
# Copyright (c) 2021-2023 Status Research & Development GmbH
2021-11-14 07:52:22 +00:00
# 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,
../../../nimbus/beacon/web3_eth_conv,
web3/eth_api
2022-04-20 07:57:50 +00:00
export eth_api
2021-11-14 07:52:22 +00:00
proc sendTransaction*(client: RpcClient, tx: Transaction): Future[bool] {.async.} =
let data = rlp.encode(tx)
let txHash = keccakHash(data)
let hex = await client.eth_sendRawTransaction(data)
let decodedHash = ethHash(hex)
2021-11-14 07:52:22 +00:00
result = decodedHash == txHash
proc blockNumber*(client: RpcClient): Future[uint64] {.async.} =
let hex = await client.eth_blockNumber()
result = hex.uint64
2021-11-14 07:52:22 +00:00
proc balanceAt*(client: RpcClient, address: EthAddress, number: uint64): Future[UInt256] {.async.} =
let hex = await client.eth_getBalance(w3Addr(address), blockId(number))
result = hex
2021-11-14 07:52:22 +00:00
proc balanceAt*(client: RpcClient, address: EthAddress): Future[UInt256] {.async.} =
let hex = await client.eth_getBalance(w3Addr(address), blockId("latest"))
result = hex
2022-05-31 08:42:01 +00:00
proc nonceAt*(client: RpcClient, address: EthAddress): Future[AccountNonce] {.async.} =
let hex = await client.eth_getTransactionCount(w3Addr(address), blockId("latest"))
result = hex.AccountNonce
2022-05-31 08:42:01 +00:00
func toTopics(list: openArray[Web3Hash]): seq[common.Topic] =
result = newSeqOfCap[common.Topic](list.len)
2023-06-25 01:40:15 +00:00
for x in list:
result.add common.Topic(x)
func toLogs(list: openArray[LogObject]): seq[Log] =
2023-06-25 01:40:15 +00:00
result = newSeqOfCap[Log](list.len)
for x in list:
result.add Log(
address: ethAddr x.address,
2023-06-25 01:40:15 +00:00
data: x.data,
topics: toTopics(x.topics)
)
proc txReceipt*(client: RpcClient, txHash: common.Hash256): Future[Option[Receipt]] {.async.} =
let rc = await client.eth_getTransactionReceipt(w3Hash txHash)
if rc.isNil:
2022-05-31 08:42:01 +00:00
return none(Receipt)
let rec = Receipt(
receiptType: LegacyReceipt,
isHash : rc.root.isSome,
status : rc.status.isSome,
hash : ethHash rc.root.get(w3Hash()),
cumulativeGasUsed: rc.cumulativeGasUsed.GasInt,
2022-05-31 08:42:01 +00:00
bloom : BloomFilter(rc.logsBloom),
2023-06-25 01:40:15 +00:00
logs : toLogs(rc.logs)
2022-05-31 08:42:01 +00:00
)
result = some(rec)
proc gasUsed*(client: RpcClient, txHash: common.Hash256): Future[Option[GasInt]] {.async.} =
let rc = await client.eth_getTransactionReceipt(w3Hash txHash)
if rc.isNil:
2022-05-31 08:42:01 +00:00
return none(GasInt)
result = some(rc.gasUsed.GasInt)