2021-11-14 07:52:22 +00:00
|
|
|
# Nimbus
|
2024-02-21 09:14:20 +00:00
|
|
|
# Copyright (c) 2021-2024 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
|
2024-10-16 01:34:12 +00:00
|
|
|
eth/common/[eth_types_rlp],
|
|
|
|
eth/rlp,
|
2021-11-14 07:52:22 +00:00
|
|
|
chronos, stint,
|
|
|
|
json_rpc/[rpcclient],
|
2022-12-02 04:39:12 +00:00
|
|
|
../../../nimbus/transaction,
|
|
|
|
../../../nimbus/utils/utils,
|
2023-12-08 09:35:50 +00:00
|
|
|
../../../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
|
|
|
|
2024-05-15 03:07:59 +00:00
|
|
|
proc sendTransaction*(
|
|
|
|
client: RpcClient, tx: PooledTransaction): Future[bool] {.async.} =
|
2021-11-14 07:52:22 +00:00
|
|
|
let data = rlp.encode(tx)
|
2024-10-16 01:34:12 +00:00
|
|
|
let txHash = keccak256(data)
|
2023-12-08 09:35:50 +00:00
|
|
|
let hex = await client.eth_sendRawTransaction(data)
|
2024-10-16 01:34:12 +00:00
|
|
|
let decodedHash = 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()
|
2023-12-08 09:35:50 +00:00
|
|
|
result = hex.uint64
|
2021-11-14 07:52:22 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc balanceAt*(client: RpcClient, address: Address, number: uint64): Future[UInt256] {.async.} =
|
|
|
|
let hex = await client.eth_getBalance(address, blockId(number))
|
2023-12-08 09:35:50 +00:00
|
|
|
result = hex
|
2021-11-14 07:52:22 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc balanceAt*(client: RpcClient, address: Address): Future[UInt256] {.async.} =
|
|
|
|
let hex = await client.eth_getBalance(address, blockId("latest"))
|
2023-12-08 09:35:50 +00:00
|
|
|
result = hex
|
2022-05-31 08:42:01 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc nonceAt*(client: RpcClient, address: Address): Future[AccountNonce] {.async.} =
|
|
|
|
let hex = await client.eth_getTransactionCount(address, blockId("latest"))
|
2023-12-08 09:35:50 +00:00
|
|
|
result = hex.AccountNonce
|
2022-05-31 08:42:01 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
func toTopics(list: openArray[Hash32]): seq[eth_types.Topic] =
|
|
|
|
result = newSeqOfCap[eth_types.Topic](list.len)
|
2023-06-25 01:40:15 +00:00
|
|
|
for x in list:
|
2024-10-16 01:34:12 +00:00
|
|
|
result.add eth_types.Topic(x)
|
2023-12-08 09:35:50 +00:00
|
|
|
|
|
|
|
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(
|
2024-10-16 01:34:12 +00:00
|
|
|
address: x.address,
|
2023-06-25 01:40:15 +00:00
|
|
|
data: x.data,
|
2024-10-03 11:42:24 +00:00
|
|
|
topics: x.topics
|
2023-06-25 01:40:15 +00:00
|
|
|
)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc txReceipt*(client: RpcClient, txHash: eth_types.Hash32): Future[Option[Receipt]] {.async.} =
|
|
|
|
let rc = await client.eth_getTransactionReceipt(txHash)
|
2023-12-08 09:35:50 +00:00
|
|
|
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,
|
2024-10-16 01:34:12 +00:00
|
|
|
hash : rc.root.get(default(Hash32)),
|
2023-12-08 09:35:50 +00:00
|
|
|
cumulativeGasUsed: rc.cumulativeGasUsed.GasInt,
|
2024-10-16 01:34:12 +00:00
|
|
|
logsBloom : 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)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc gasUsed*(client: RpcClient, txHash: eth_types.Hash32): Future[Option[GasInt]] {.async.} =
|
|
|
|
let rc = await client.eth_getTransactionReceipt(txHash)
|
2023-12-08 09:35:50 +00:00
|
|
|
if rc.isNil:
|
2022-05-31 08:42:01 +00:00
|
|
|
return none(GasInt)
|
|
|
|
|
2023-12-08 09:35:50 +00:00
|
|
|
result = some(rc.gasUsed.GasInt)
|