2021-04-24 03:59:55 +00:00
|
|
|
# nim-graphql
|
2024-01-12 21:06:19 +00:00
|
|
|
# Copyright (c) 2021-2024 Status Research & Development GmbH
|
2021-04-24 03:59:55 +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
|
2023-10-18 02:16:11 +00:00
|
|
|
std/[strutils],
|
2024-05-30 12:54:03 +00:00
|
|
|
stew/byteutils, stint,
|
|
|
|
results,
|
2024-10-27 18:56:28 +00:00
|
|
|
eth/common/transaction_utils,
|
2024-10-16 01:34:12 +00:00
|
|
|
chronos,
|
2021-04-24 03:59:55 +00:00
|
|
|
graphql, graphql/graphql as context,
|
|
|
|
graphql/common/types, graphql/httpserver,
|
2021-05-10 15:19:34 +00:00
|
|
|
graphql/instruments/query_complexity,
|
2024-06-16 03:21:02 +00:00
|
|
|
../db/[ledger],
|
2024-04-16 12:52:07 +00:00
|
|
|
../rpc/rpc_utils,
|
2024-06-17 07:56:39 +00:00
|
|
|
".."/[transaction, evm/state, config, constants],
|
2022-01-23 11:39:43 +00:00
|
|
|
../transaction/call_evm,
|
2023-07-28 10:32:49 +00:00
|
|
|
../core/[tx_pool, tx_pool/tx_item],
|
2024-10-16 01:34:12 +00:00
|
|
|
../common/common,
|
|
|
|
web3/eth_api_types
|
2024-10-27 18:56:28 +00:00
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
from eth/p2p import EthereumNode
|
|
|
|
export httpserver
|
|
|
|
|
|
|
|
type
|
|
|
|
EthTypes = enum
|
|
|
|
ethAccount = "Account"
|
|
|
|
ethLog = "Log"
|
|
|
|
ethTransaction = "Transaction"
|
|
|
|
ethBlock = "Block"
|
|
|
|
ethCallResult = "CallResult"
|
|
|
|
ethSyncState = "SyncState"
|
|
|
|
ethPending = "Pending"
|
|
|
|
ethQuery = "Query"
|
|
|
|
ethMutation = "Mutation"
|
2021-05-17 13:45:37 +00:00
|
|
|
ethAccessTuple = "AccessTuple"
|
2023-07-28 10:32:49 +00:00
|
|
|
ethWithdrawal = "Withdrawal"
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2021-05-05 04:18:17 +00:00
|
|
|
HeaderNode = ref object of Node
|
2024-10-16 01:34:12 +00:00
|
|
|
header: Header
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2021-05-05 04:18:17 +00:00
|
|
|
AccountNode = ref object of Node
|
2024-10-16 01:34:12 +00:00
|
|
|
address: Address
|
2021-04-24 03:59:55 +00:00
|
|
|
account: Account
|
2024-06-16 03:21:02 +00:00
|
|
|
db: LedgerRef
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2021-05-05 04:18:17 +00:00
|
|
|
TxNode = ref object of Node
|
2021-04-24 03:59:55 +00:00
|
|
|
tx: Transaction
|
2024-06-14 07:31:08 +00:00
|
|
|
index: uint64
|
2024-10-16 01:34:12 +00:00
|
|
|
blockNumber: base.BlockNumber
|
2021-04-24 03:59:55 +00:00
|
|
|
receipt: Receipt
|
|
|
|
gasUsed: GasInt
|
2024-06-14 07:31:08 +00:00
|
|
|
baseFee: Opt[UInt256]
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2021-05-05 04:18:17 +00:00
|
|
|
LogNode = ref object of Node
|
2021-04-24 03:59:55 +00:00
|
|
|
log: Log
|
|
|
|
index: int
|
|
|
|
tx: TxNode
|
|
|
|
|
2021-05-17 13:45:37 +00:00
|
|
|
AclNode = ref object of Node
|
|
|
|
acl: AccessPair
|
|
|
|
|
2023-07-28 10:32:49 +00:00
|
|
|
WdNode = ref object of Node
|
|
|
|
wd: Withdrawal
|
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
GraphqlContextRef = ref GraphqlContextObj
|
|
|
|
GraphqlContextObj = object of Graphql
|
|
|
|
ids: array[EthTypes, Name]
|
2022-12-02 04:39:12 +00:00
|
|
|
com: CommonRef
|
2023-08-04 11:10:09 +00:00
|
|
|
chainDB: CoreDbRef
|
2021-04-24 03:59:55 +00:00
|
|
|
ethNode: EthereumNode
|
2022-01-23 11:39:43 +00:00
|
|
|
txPool: TxPoolRef
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-01-12 21:06:19 +00:00
|
|
|
{.push gcsafe, raises: [].}
|
|
|
|
{.pragma: apiRaises, raises: [].}
|
2024-06-19 01:27:54 +00:00
|
|
|
{.pragma: apiPragma, cdecl, gcsafe, apiRaises.}
|
2023-04-17 08:22:51 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc toHash(n: Node): Hash32 {.gcsafe, raises: [ValueError].} =
|
|
|
|
Hash32.fromHex(n.stringVal)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc toBlockNumber(n: Node): base.BlockNumber {.gcsafe, raises: [ValueError].} =
|
2023-07-28 10:32:49 +00:00
|
|
|
if n.kind == nkInt:
|
2024-10-16 01:34:12 +00:00
|
|
|
result = parse(n.intVal, UInt256, radix = 10).truncate(base.BlockNumber)
|
2023-07-28 10:32:49 +00:00
|
|
|
elif n.kind == nkString:
|
2024-10-16 01:34:12 +00:00
|
|
|
result = parse(n.stringVal, UInt256, radix = 16).truncate(base.BlockNumber)
|
2023-07-28 10:32:49 +00:00
|
|
|
else:
|
|
|
|
doAssert(false, "unknown node type: " & $n.kind)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc headerNode(ctx: GraphqlContextRef, header: Header): Node =
|
2021-05-05 04:18:17 +00:00
|
|
|
HeaderNode(
|
|
|
|
kind: nkMap,
|
|
|
|
typeName: ctx.ids[ethBlock],
|
|
|
|
pos: Pos(),
|
2021-04-24 03:59:55 +00:00
|
|
|
header: header
|
|
|
|
)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc accountNode(ctx: GraphqlContextRef, acc: Account, address: Address, db: LedgerRef): Node =
|
2021-05-05 04:18:17 +00:00
|
|
|
AccountNode(
|
|
|
|
kind: nkMap,
|
|
|
|
typeName: ctx.ids[ethAccount],
|
|
|
|
pos: Pos(),
|
2021-04-24 03:59:55 +00:00
|
|
|
account: acc,
|
|
|
|
address: address,
|
|
|
|
db: db
|
|
|
|
)
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc txNode(ctx: GraphqlContextRef, tx: Transaction, index: uint64, blockNumber: base.BlockNumber, baseFee: Opt[UInt256]): Node =
|
2021-05-05 04:18:17 +00:00
|
|
|
TxNode(
|
|
|
|
kind: nkMap,
|
|
|
|
typeName: ctx.ids[ethTransaction],
|
|
|
|
pos: Pos(),
|
2021-04-24 03:59:55 +00:00
|
|
|
tx: tx,
|
|
|
|
index: index,
|
2021-09-20 12:58:24 +00:00
|
|
|
blockNumber: blockNumber,
|
|
|
|
baseFee: baseFee
|
2021-04-24 03:59:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc logNode(ctx: GraphqlContextRef, log: Log, index: int, tx: TxNode): Node =
|
2021-05-05 04:18:17 +00:00
|
|
|
LogNode(
|
|
|
|
kind: nkMap,
|
|
|
|
typeName: ctx.ids[ethLog],
|
|
|
|
pos: Pos(),
|
2021-04-24 03:59:55 +00:00
|
|
|
log: log,
|
|
|
|
index: index,
|
|
|
|
tx: tx
|
|
|
|
)
|
|
|
|
|
2021-05-17 13:45:37 +00:00
|
|
|
proc aclNode(ctx: GraphqlContextRef, accessPair: AccessPair): Node =
|
|
|
|
AclNode(
|
|
|
|
kind: nkMap,
|
|
|
|
typeName: ctx.ids[ethAccessTuple],
|
|
|
|
pos: Pos(),
|
|
|
|
acl: accessPair
|
|
|
|
)
|
|
|
|
|
2023-07-28 10:32:49 +00:00
|
|
|
proc wdNode(ctx: GraphqlContextRef, wd: Withdrawal): Node =
|
|
|
|
WdNode(
|
|
|
|
kind: nkMap,
|
|
|
|
typeName: ctx.ids[ethWithdrawal],
|
|
|
|
pos: Pos(),
|
|
|
|
wd: wd
|
|
|
|
)
|
|
|
|
|
2024-10-27 18:56:28 +00:00
|
|
|
proc getStateDB(com: CommonRef, header: Header): LedgerRef {.deprecated: "LedgerRef does not support loading a particular state".} =
|
2021-04-24 03:59:55 +00:00
|
|
|
## Retrieves the account db from canonical head
|
2021-10-28 09:42:39 +00:00
|
|
|
## we don't use accounst_cache here because it's read only operations
|
2024-10-27 18:56:28 +00:00
|
|
|
# TODO the ledger initialized here refers to the base, not the given header!
|
|
|
|
LedgerRef.init(com.db)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc getBlockByNumber(ctx: GraphqlContextRef, number: Node): RespResult =
|
|
|
|
try:
|
2024-11-07 01:24:21 +00:00
|
|
|
let header = ?ctx.chainDB.getBlockHeader(toBlockNumber(number))
|
|
|
|
ok(headerNode(ctx, header))
|
|
|
|
except ValueError as exc:
|
|
|
|
err(exc.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc getBlockByNumber(ctx: GraphqlContextRef, number: base.BlockNumber): RespResult =
|
2024-11-07 01:24:21 +00:00
|
|
|
let header = ?ctx.chainDB.getBlockHeader(number)
|
|
|
|
ok(headerNode(ctx, header))
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc getBlockByHash(ctx: GraphqlContextRef, hash: Node): RespResult =
|
|
|
|
try:
|
2024-11-07 01:24:21 +00:00
|
|
|
let header = ?ctx.chainDB.getBlockHeader(toHash(hash))
|
|
|
|
ok(headerNode(ctx, header))
|
|
|
|
except ValueError as exc:
|
|
|
|
err(exc.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc getBlockByHash(ctx: GraphqlContextRef, hash: Hash32): RespResult =
|
2024-11-07 01:24:21 +00:00
|
|
|
let header = ?ctx.chainDB.getBlockHeader(hash)
|
|
|
|
ok(headerNode(ctx, header))
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc getLatestBlock(ctx: GraphqlContextRef): RespResult =
|
2024-11-07 01:24:21 +00:00
|
|
|
let header = ?ctx.chainDB.getCanonicalHead()
|
|
|
|
ok(headerNode(ctx, header))
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc getTxCount(ctx: GraphqlContextRef, txRoot: Hash32): RespResult =
|
2024-11-07 01:24:21 +00:00
|
|
|
let txCount = ctx.chainDB.getTransactionCount(txRoot)
|
|
|
|
ok(resp(txCount))
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc longNode(val: uint64 | int64): RespResult =
|
|
|
|
ok(Node(kind: nkInt, intVal: $val, pos: Pos()))
|
|
|
|
|
|
|
|
proc longNode(val: UInt256): RespResult =
|
|
|
|
ok(Node(kind: nkInt, intVal: val.toString, pos: Pos()))
|
|
|
|
|
2021-05-04 06:54:46 +00:00
|
|
|
proc stripLeadingZeros(x: string): string =
|
|
|
|
strip(x, leading = true, trailing = false, chars = {'0'})
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2021-05-04 06:54:46 +00:00
|
|
|
proc bigIntNode(val: UInt256): RespResult =
|
2021-06-17 09:11:24 +00:00
|
|
|
if val == 0.u256:
|
|
|
|
ok(Node(kind: nkString, stringVal: "0x0", pos: Pos()))
|
|
|
|
else:
|
|
|
|
let hex = stripLeadingZeros(val.toHex)
|
|
|
|
ok(Node(kind: nkString, stringVal: "0x" & hex, pos: Pos()))
|
2021-05-04 06:54:46 +00:00
|
|
|
|
|
|
|
proc bigIntNode(x: uint64 | int64): RespResult =
|
|
|
|
# stdlib toHex is not suitable for hive
|
|
|
|
const
|
|
|
|
HexChars = "0123456789abcdef"
|
2023-07-28 10:32:49 +00:00
|
|
|
|
|
|
|
if x == 0:
|
|
|
|
return ok(Node(kind: nkString, stringVal: "0x0", pos: Pos()))
|
|
|
|
|
2021-05-04 06:54:46 +00:00
|
|
|
var
|
|
|
|
n = cast[uint64](x)
|
|
|
|
r: array[2*sizeof(uint64), char]
|
|
|
|
i = 0
|
|
|
|
while n > 0:
|
|
|
|
r[i] = HexChars[int(n and 0xF)]
|
|
|
|
n = n shr 4
|
|
|
|
inc i
|
|
|
|
var hex = newString(i+2)
|
|
|
|
hex[0] = '0'
|
|
|
|
hex[1] = 'x'
|
|
|
|
while i > 0:
|
|
|
|
hex[hex.len-i] = r[i-1]
|
|
|
|
dec i
|
|
|
|
ok(Node(kind: nkString, stringVal: hex, pos: Pos()))
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc byte32Node(val: UInt256): RespResult =
|
|
|
|
ok(Node(kind: nkString, stringVal: "0x" & val.dumpHex, pos: Pos()))
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc resp(hash: Hash32 | Bytes32): RespResult =
|
2024-10-02 16:22:35 +00:00
|
|
|
ok(resp(hash.to0xHex))
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc resp(data: openArray[byte]): RespResult =
|
|
|
|
ok(resp("0x" & data.toHex))
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc getTotalDifficulty(ctx: GraphqlContextRef, blockHash: Hash32): RespResult =
|
2024-06-14 05:10:00 +00:00
|
|
|
let score = getScore(ctx.chainDB, blockHash).valueOr:
|
|
|
|
return err("can't get total difficulty")
|
|
|
|
|
|
|
|
bigIntNode(score)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc getOmmerCount(ctx: GraphqlContextRef, ommersHash: Hash32): RespResult =
|
2024-11-07 01:24:21 +00:00
|
|
|
let ommers = ?ctx.chainDB.getUnclesCount(ommersHash)
|
|
|
|
ok(resp(ommers))
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc getOmmers(ctx: GraphqlContextRef, ommersHash: Hash32): RespResult =
|
2024-11-07 01:24:21 +00:00
|
|
|
let uncles = ?ctx.chainDB.getUncles(ommersHash)
|
|
|
|
when false:
|
|
|
|
# EIP 1767 says no ommers == null
|
|
|
|
# but hive test case want empty array []
|
2021-04-24 03:59:55 +00:00
|
|
|
if uncles.len == 0:
|
|
|
|
return ok(respNull())
|
2024-11-07 01:24:21 +00:00
|
|
|
var list = respList()
|
|
|
|
for n in uncles:
|
|
|
|
list.add headerNode(ctx, n)
|
|
|
|
ok(list)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-11-07 01:24:21 +00:00
|
|
|
proc getOmmerAt(ctx: GraphqlContextRef, ommersHash: Hash32, index: int): RespResult =
|
|
|
|
let uncles = ?ctx.chainDB.getUncles(ommersHash)
|
|
|
|
if uncles.len == 0:
|
|
|
|
return ok(respNull())
|
|
|
|
if index < 0 or index >= uncles.len:
|
|
|
|
return ok(respNull())
|
|
|
|
ok(headerNode(ctx, uncles[index]))
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-11-07 01:24:21 +00:00
|
|
|
proc getTxs(ctx: GraphqlContextRef, header: Header): RespResult =
|
|
|
|
let txCount = getTransactionCount(ctx.chainDB, header.txRoot)
|
|
|
|
if txCount == 0:
|
|
|
|
return ok(respNull())
|
|
|
|
var list = respList()
|
|
|
|
var index = 0'u64
|
|
|
|
|
|
|
|
let txList = ?ctx.chainDB.getTransactions(header.txRoot)
|
|
|
|
for tx in txList:
|
|
|
|
list.add txNode(ctx, tx, index, header.number, header.baseFeePerGas)
|
|
|
|
inc index
|
|
|
|
|
|
|
|
index = 0'u64
|
|
|
|
var prevUsed = 0.GasInt
|
|
|
|
let receiptList = ?ctx.chainDB.getReceipts(header.receiptsRoot)
|
|
|
|
for r in receiptList:
|
|
|
|
let tx = TxNode(list.sons[index])
|
|
|
|
tx.receipt = r
|
|
|
|
tx.gasUsed = r.cumulativeGasUsed - prevUsed
|
|
|
|
prevUsed = r.cumulativeGasUsed
|
|
|
|
inc index
|
|
|
|
|
|
|
|
ok(list)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc getWithdrawals(ctx: GraphqlContextRef, header: Header): RespResult =
|
2024-11-07 01:24:21 +00:00
|
|
|
if header.withdrawalsRoot.isNone:
|
|
|
|
return ok(respNull())
|
|
|
|
|
|
|
|
let wds = ?ctx.chainDB.getWithdrawals(header.withdrawalsRoot.get)
|
|
|
|
var list = respList()
|
|
|
|
for wd in wds:
|
|
|
|
list.add wdNode(ctx, wd)
|
|
|
|
ok(list)
|
2023-07-28 10:32:49 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc getTxAt(ctx: GraphqlContextRef, header: Header, index: uint64): RespResult =
|
2024-11-07 01:24:21 +00:00
|
|
|
let tx = ctx.chainDB.getTransactionByIndex(header.txRoot, index.uint16).valueOr:
|
|
|
|
return ok(respNull())
|
|
|
|
|
|
|
|
let txn = txNode(ctx, tx, index, header.number, header.baseFeePerGas)
|
|
|
|
var i = 0'u64
|
|
|
|
var prevUsed = 0.GasInt
|
|
|
|
let receiptList = ?ctx.chainDB.getReceipts(header.receiptsRoot)
|
|
|
|
for r in receiptList:
|
|
|
|
if i == index:
|
|
|
|
let tx = TxNode(txn)
|
|
|
|
tx.receipt = r
|
|
|
|
tx.gasUsed = r.cumulativeGasUsed - prevUsed
|
|
|
|
prevUsed = r.cumulativeGasUsed
|
|
|
|
inc i
|
|
|
|
ok(txn)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc getTxByHash(ctx: GraphqlContextRef, hash: Hash32): RespResult =
|
2024-11-07 01:24:21 +00:00
|
|
|
let
|
|
|
|
txKey = ?ctx.chainDB.getTransactionKey(hash)
|
|
|
|
header = ?ctx.chainDB.getBlockHeader(txKey.blockNumber)
|
|
|
|
getTxAt(ctx, header, txKey.index)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc accountNode(ctx: GraphqlContextRef, header: Header, address: Address): RespResult =
|
2023-04-17 08:22:51 +00:00
|
|
|
try:
|
|
|
|
let db = getStateDB(ctx.com, header)
|
|
|
|
when false:
|
|
|
|
# EIP 1767 unclear about non existent account
|
|
|
|
# but hive test case demand something
|
|
|
|
if not db.accountExists(address):
|
|
|
|
return ok(respNull())
|
2024-06-16 03:21:02 +00:00
|
|
|
let acc = db.getEthAccount(address)
|
|
|
|
ok(accountNode(ctx, acc, address, db))
|
2023-04-17 08:22:51 +00:00
|
|
|
except RlpError as ex:
|
|
|
|
err(ex.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2023-07-28 10:32:49 +00:00
|
|
|
func hexCharToInt(c: char): uint64 =
|
|
|
|
case c
|
|
|
|
of 'a'..'f': return c.uint64 - 'a'.uint64 + 10'u64
|
|
|
|
of 'A'..'F': return c.uint64 - 'A'.uint64 + 10'u64
|
|
|
|
of '0'..'9': return c.uint64 - '0'.uint64
|
|
|
|
else: doAssert(false, "invalid hex digit: " & $c)
|
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
proc parseU64(node: Node): uint64 =
|
2023-07-28 10:32:49 +00:00
|
|
|
if node.kind == nkString:
|
|
|
|
if node.stringVal.len > 2 and node.stringVal[1] == 'x':
|
|
|
|
for i in 2..<node.stringVal.len:
|
|
|
|
let c = node.stringVal[i]
|
|
|
|
result = result * 16 + hexCharToInt(c)
|
|
|
|
else:
|
|
|
|
for c in node.stringVal:
|
|
|
|
result = result * 10 + (c.uint64 - '0'.uint64)
|
|
|
|
else:
|
|
|
|
for c in node.intVal:
|
|
|
|
result = result * 10 + (c.uint64 - '0'.uint64)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc validateHex(x: Node, minLen = 0): NodeResult =
|
|
|
|
if x.stringVal.len < 2:
|
|
|
|
return err("hex is too short")
|
|
|
|
if x.stringVal.len != 2 + minLen * 2 and minLen != 0:
|
2024-01-12 21:06:19 +00:00
|
|
|
return err("expect hex with len '" &
|
|
|
|
$(2 * minLen + 2) & "', got '" & $x.stringVal.len & "'")
|
2021-04-24 03:59:55 +00:00
|
|
|
if x.stringVal.len mod 2 != 0:
|
|
|
|
return err("hex must have even number of nibbles")
|
2021-05-04 06:54:46 +00:00
|
|
|
if x.stringVal[0] != '0' or x.stringVal[1] != 'x':
|
2021-04-24 03:59:55 +00:00
|
|
|
return err("hex should be prefixed by '0x'")
|
|
|
|
for i in 2..<x.stringVal.len:
|
|
|
|
if x.stringVal[i] notin HexDigits:
|
|
|
|
return err("invalid chars in hex")
|
|
|
|
ok(x)
|
|
|
|
|
2021-05-05 12:48:56 +00:00
|
|
|
proc padBytes(n: Node, prefixLen, minLen: int) =
|
|
|
|
let tmp = n.stringVal
|
|
|
|
if prefixLen != 0:
|
|
|
|
n.stringVal = newString(minLen + prefixLen)
|
|
|
|
for i in 0..<prefixLen:
|
|
|
|
n.stringVal[i] = tmp[i]
|
|
|
|
let zeros = minLen - tmp.len + prefixLen
|
|
|
|
for i in 0..<zeros:
|
|
|
|
n.stringVal[i + prefixLen] = '0'
|
|
|
|
for i in 2..<tmp.len:
|
|
|
|
n.stringVal[i+zeros] = tmp[i]
|
|
|
|
else:
|
|
|
|
n.stringVal = newString(minLen)
|
|
|
|
let zeros = minLen - tmp.len
|
|
|
|
for i in 0..<zeros:
|
|
|
|
n.stringVal[i] = '0'
|
|
|
|
for i in 0..<tmp.len:
|
|
|
|
n.stringVal[i+zeros] = tmp[i]
|
|
|
|
|
|
|
|
proc validateFixedLenHex(x: Node, minLen: int, kind: string, padding = false): NodeResult =
|
2021-05-04 06:54:46 +00:00
|
|
|
if x.stringVal.len < 2:
|
|
|
|
return err(kind & " hex is too short")
|
|
|
|
|
2024-01-12 21:06:19 +00:00
|
|
|
try:
|
|
|
|
var prefixLen = 0
|
|
|
|
if x.stringVal[0] == '0' and x.stringVal[1] == 'x':
|
|
|
|
prefixLen = 2
|
|
|
|
|
|
|
|
let expectedLen = minLen * 2 + prefixLen
|
|
|
|
if x.stringVal.len < expectedLen:
|
|
|
|
if not padding:
|
|
|
|
return err("$1 len is too short: expect $2 got $3" %
|
|
|
|
[kind, $expectedLen, $x.stringVal.len])
|
|
|
|
else:
|
|
|
|
padBytes(x, prefixLen, minLen * 2)
|
|
|
|
elif x.stringVal.len > expectedLen:
|
|
|
|
return err("$1 len is too long: expect $2 got $3" %
|
2021-05-05 12:48:56 +00:00
|
|
|
[kind, $expectedLen, $x.stringVal.len])
|
2021-05-04 06:54:46 +00:00
|
|
|
|
2024-01-12 21:06:19 +00:00
|
|
|
for i in prefixLen..<x.stringVal.len:
|
|
|
|
if x.stringVal[i] notin HexDigits:
|
|
|
|
return err("invalid chars in $1 hex" % [kind])
|
2021-05-04 06:54:46 +00:00
|
|
|
|
2024-01-12 21:06:19 +00:00
|
|
|
ok(x)
|
|
|
|
except ValueError as exc:
|
|
|
|
err(exc.msg)
|
2021-05-04 06:54:46 +00:00
|
|
|
|
2024-01-12 21:06:19 +00:00
|
|
|
proc scalarBytes32(ctx: GraphqlRef, typeNode, node: Node): NodeResult {.cdecl, gcsafe, noSideEffect, raises:[].} =
|
2021-04-24 03:59:55 +00:00
|
|
|
## Bytes32 is a 32 byte binary string,
|
|
|
|
## represented as 0x-prefixed hexadecimal.
|
|
|
|
if node.kind != nkString:
|
2024-01-12 21:06:19 +00:00
|
|
|
return err("expect hex string, but got '" & $node.kind & "'")
|
2021-05-05 12:48:56 +00:00
|
|
|
validateFixedLenHex(node, 32, "Bytes32", padding = true)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-01-12 21:06:19 +00:00
|
|
|
proc scalarAddress(ctx: GraphqlRef, typeNode, node: Node): NodeResult {.cdecl, gcsafe, noSideEffect, raises:[].} =
|
2021-04-24 03:59:55 +00:00
|
|
|
## Address is a 20 byte Ethereum address,
|
|
|
|
## represented as 0x-prefixed hexadecimal.
|
|
|
|
if node.kind != nkString:
|
2024-01-12 21:06:19 +00:00
|
|
|
return err("expect hex string, but got '" & $node.kind & "'")
|
2021-05-04 06:54:46 +00:00
|
|
|
validateFixedLenHex(node, 20, "Address")
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-01-12 21:06:19 +00:00
|
|
|
proc scalarBytes(ctx: GraphqlRef, typeNode, node: Node): NodeResult {.cdecl, gcsafe, noSideEffect, raises:[].} =
|
2021-04-24 03:59:55 +00:00
|
|
|
## Bytes is an arbitrary length binary string,
|
|
|
|
## represented as 0x-prefixed hexadecimal.
|
|
|
|
## An empty byte string is represented as '0x'.
|
|
|
|
## Byte strings must have an even number of hexadecimal nybbles.
|
|
|
|
if node.kind != nkString:
|
2024-01-12 21:06:19 +00:00
|
|
|
return err("expect hex string, but got '" & $node.kind & "'")
|
2021-04-24 03:59:55 +00:00
|
|
|
validateHex(node)
|
|
|
|
|
2024-01-12 21:06:19 +00:00
|
|
|
proc scalarBigInt(ctx: GraphqlRef, typeNode, node: Node): NodeResult {.cdecl, gcsafe, noSideEffect, raises:[].} =
|
2021-04-24 03:59:55 +00:00
|
|
|
## BigInt is a large integer. Input is accepted as
|
|
|
|
## either a JSON number or as a string.
|
|
|
|
## Strings may be either decimal or 0x-prefixed hexadecimal.
|
|
|
|
## Output values are all 0x-prefixed hexadecimal.
|
2021-05-04 06:54:46 +00:00
|
|
|
try:
|
|
|
|
if node.kind == nkInt:
|
2021-04-24 03:59:55 +00:00
|
|
|
# convert it into hex nkString node
|
2021-05-04 06:54:46 +00:00
|
|
|
let val = parse(node.intVal, UInt256, radix = 10)
|
|
|
|
ok(Node(kind: nkString, stringVal: "0x" & val.toHex, pos: node.pos))
|
|
|
|
elif node.kind == nkString:
|
|
|
|
if node.stringVal.len > 2 and node.stringVal[1] == 'x':
|
|
|
|
if node.stringVal[0] != '0':
|
|
|
|
return err("Big Int hex malformed")
|
|
|
|
if node.stringVal.len > 66:
|
|
|
|
# 256 bits = 32 bytes = 64 hex nibbles
|
|
|
|
# 64 hex nibbles + '0x' prefix = 66 bytes
|
|
|
|
return err("Big Int hex should not exceed 66 bytes")
|
|
|
|
for i in 2..<node.stringVal.len:
|
|
|
|
if node.stringVal[i] notin HexDigits:
|
|
|
|
return err("invalid chars in BigInt hex")
|
|
|
|
ok(node)
|
|
|
|
elif HexDigits in node.stringVal:
|
|
|
|
if node.stringVal.len > 64:
|
|
|
|
return err("Big Int hex should not exceed 64 bytes")
|
|
|
|
for i in 0..<node.stringVal.len:
|
|
|
|
if node.stringVal[i] notin HexDigits:
|
|
|
|
return err("invalid chars in BigInt hex")
|
|
|
|
ok(node)
|
|
|
|
else:
|
|
|
|
# convert it into hex nkString node
|
|
|
|
let val = parse(node.stringVal, UInt256, radix = 10)
|
|
|
|
node.stringVal = "0x" & val.toHex
|
|
|
|
ok(node)
|
|
|
|
else:
|
2024-01-12 21:06:19 +00:00
|
|
|
return err("expect hex/dec string or int, but got '" & $node.kind & "'")
|
2021-05-04 06:54:46 +00:00
|
|
|
except CatchableError as e:
|
|
|
|
err("scalar BigInt error: " & e.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2022-04-08 04:54:11 +00:00
|
|
|
proc scalarLong(ctx: GraphqlRef, typeNode, node: Node): NodeResult {.cdecl, gcsafe, noSideEffect.} =
|
2021-04-24 03:59:55 +00:00
|
|
|
## Long is a 64 bit unsigned integer.
|
|
|
|
const maxU64 = uint64.high.u256
|
2021-05-04 06:54:46 +00:00
|
|
|
try:
|
|
|
|
case node.kind
|
|
|
|
of nkString:
|
|
|
|
if node.stringVal.len > 2 and node.stringVal[1] == 'x':
|
|
|
|
let val = parse(node.stringVal, UInt256, radix = 16)
|
|
|
|
if val > maxU64:
|
|
|
|
return err("long value overflow")
|
2023-07-28 10:32:49 +00:00
|
|
|
ok(node)
|
2021-05-04 06:54:46 +00:00
|
|
|
else:
|
|
|
|
let val = parse(node.stringVal, UInt256, radix = 10)
|
|
|
|
if val > maxU64:
|
|
|
|
return err("long value overflow")
|
2023-07-28 10:32:49 +00:00
|
|
|
ok(Node(kind: nkString, pos: node.pos, stringVal: "0x" & val.toHex))
|
2021-05-04 06:54:46 +00:00
|
|
|
of nkInt:
|
|
|
|
let val = parse(node.intVal, UInt256, radix = 10)
|
|
|
|
if val > maxU64:
|
|
|
|
return err("long value overflow")
|
2023-07-28 10:32:49 +00:00
|
|
|
ok(Node(kind: nkString, pos: node.pos, stringVal: "0x" & val.toHex))
|
2021-05-04 06:54:46 +00:00
|
|
|
else:
|
2024-01-12 21:06:19 +00:00
|
|
|
err("expect int, but got '" & $node.kind & "'")
|
2021-05-04 06:54:46 +00:00
|
|
|
except CatchableError as e:
|
|
|
|
err("scalar Long error: " & e.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc accountAddress(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let acc = AccountNode(parent)
|
2024-09-29 12:37:09 +00:00
|
|
|
resp(acc.address.data)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc accountBalance(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let acc = AccountNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
bigIntNode(acc.account.balance)
|
|
|
|
|
|
|
|
proc accountTxCount(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let acc = AccountNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
longNode(acc.account.nonce)
|
|
|
|
|
|
|
|
proc accountCode(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let acc = AccountNode(parent)
|
2023-04-17 08:22:51 +00:00
|
|
|
try:
|
|
|
|
let code = acc.db.getCode(acc.address)
|
2024-06-21 07:44:10 +00:00
|
|
|
resp(code.bytes())
|
2023-04-17 08:22:51 +00:00
|
|
|
except RlpError as ex:
|
|
|
|
err(ex.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc accountStorage(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let acc = AccountNode(parent)
|
2023-04-17 08:22:51 +00:00
|
|
|
try:
|
|
|
|
let slot = parse(params[0].val.stringVal, UInt256, radix = 16)
|
2024-06-16 03:21:02 +00:00
|
|
|
let val = acc.db.getStorage(acc.address, slot)
|
2023-04-17 08:22:51 +00:00
|
|
|
byte32Node(val)
|
|
|
|
except RlpError as ex:
|
|
|
|
err(ex.msg)
|
|
|
|
except ValueError as ex:
|
|
|
|
err(ex.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
const accountProcs = {
|
2023-01-26 12:37:19 +00:00
|
|
|
# Note: Need to define it as ResolverProc else a proc with noSideEffect is
|
|
|
|
# assumed and this fails for accountCode and accountStorage.
|
|
|
|
"address": ResolverProc accountAddress,
|
2021-04-24 03:59:55 +00:00
|
|
|
"balance": accountBalance,
|
|
|
|
"transactionCount": accountTxCount,
|
|
|
|
"code": accountCode,
|
|
|
|
"storage": accountStorage
|
|
|
|
}
|
|
|
|
|
|
|
|
proc logIndex(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let log = LogNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
ok(resp(log.index))
|
|
|
|
|
|
|
|
proc logAccount(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: with block param
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let log = LogNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
let hres = ctx.getBlockByNumber(log.tx.blockNumber)
|
|
|
|
if hres.isErr:
|
|
|
|
return hres
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(hres.get())
|
2021-04-24 03:59:55 +00:00
|
|
|
ctx.accountNode(h.header, log.log.address)
|
|
|
|
|
|
|
|
proc logTopics(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let log = LogNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
var list = respList()
|
|
|
|
for n in log.log.topics:
|
|
|
|
list.add resp("0x" & n.toHex)
|
|
|
|
ok(list)
|
|
|
|
|
|
|
|
proc logData(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let log = LogNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
resp(log.log.data)
|
|
|
|
|
|
|
|
proc logTransaction(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let log = LogNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
ok(cast[Node](log.tx))
|
|
|
|
|
|
|
|
const logProcs = {
|
|
|
|
"account": logAccount,
|
|
|
|
"index": logIndex,
|
|
|
|
"topics": logTopics,
|
|
|
|
"data": logData,
|
|
|
|
"transaction": logTransaction
|
|
|
|
}
|
|
|
|
|
|
|
|
proc txHash(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let
|
2021-05-05 04:18:17 +00:00
|
|
|
tx = TxNode(parent)
|
2023-06-24 13:56:44 +00:00
|
|
|
txHash = rlpHash(tx.tx) # beware EIP-4844
|
2021-04-24 03:59:55 +00:00
|
|
|
resp(txHash)
|
|
|
|
|
|
|
|
proc txNonce(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2021-05-15 07:18:21 +00:00
|
|
|
longNode(tx.tx.nonce)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc txIndex(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2024-06-14 07:31:08 +00:00
|
|
|
ok(resp(tx.index.int))
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc txFrom(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2023-07-28 10:32:49 +00:00
|
|
|
|
|
|
|
let blockNumber = if params[0].val.kind != nkEmpty:
|
2024-06-14 07:31:08 +00:00
|
|
|
parseU64(params[0].val)
|
2023-07-28 10:32:49 +00:00
|
|
|
else:
|
|
|
|
tx.blockNumber
|
|
|
|
|
2024-10-04 14:34:31 +00:00
|
|
|
let sender = tx.tx.recoverSender.valueOr:
|
2021-04-24 03:59:55 +00:00
|
|
|
return ok(respNull())
|
2023-07-28 10:32:49 +00:00
|
|
|
let hres = ctx.getBlockByNumber(blockNumber)
|
2021-04-24 03:59:55 +00:00
|
|
|
if hres.isErr:
|
|
|
|
return hres
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(hres.get())
|
2021-04-24 03:59:55 +00:00
|
|
|
ctx.accountNode(h.header, sender)
|
|
|
|
|
|
|
|
proc txTo(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2023-07-28 10:32:49 +00:00
|
|
|
|
|
|
|
let blockNumber = if params[0].val.kind != nkEmpty:
|
2024-06-14 07:31:08 +00:00
|
|
|
parseU64(params[0].val)
|
2023-07-28 10:32:49 +00:00
|
|
|
else:
|
|
|
|
tx.blockNumber
|
|
|
|
|
2021-06-27 04:19:43 +00:00
|
|
|
if tx.tx.contractCreation:
|
2021-04-24 03:59:55 +00:00
|
|
|
return ok(respNull())
|
2023-07-28 10:32:49 +00:00
|
|
|
let hres = ctx.getBlockByNumber(blockNumber)
|
2021-04-24 03:59:55 +00:00
|
|
|
if hres.isErr:
|
|
|
|
return hres
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(hres.get())
|
2021-06-27 04:19:43 +00:00
|
|
|
ctx.accountNode(h.header, tx.tx.to.get())
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc txValue(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
bigIntNode(tx.tx.value)
|
|
|
|
|
|
|
|
proc txGasPrice(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2024-06-14 07:31:08 +00:00
|
|
|
if tx.tx.txType >= TxEip1559:
|
2021-09-20 12:58:24 +00:00
|
|
|
if tx.baseFee.isNone:
|
|
|
|
return bigIntNode(tx.tx.gasPrice)
|
|
|
|
|
|
|
|
let baseFee = tx.baseFee.get().truncate(GasInt)
|
2024-06-14 07:31:08 +00:00
|
|
|
let priorityFee = min(tx.tx.maxPriorityFeePerGas, tx.tx.maxFeePerGas - baseFee)
|
2021-09-20 12:58:24 +00:00
|
|
|
bigIntNode(priorityFee + baseFee)
|
|
|
|
else:
|
|
|
|
bigIntNode(tx.tx.gasPrice)
|
|
|
|
|
|
|
|
proc txMaxFeePerGas(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
2024-06-14 07:31:08 +00:00
|
|
|
if tx.tx.txType >= TxEip1559:
|
|
|
|
bigIntNode(tx.tx.maxFeePerGas)
|
2021-09-20 12:58:24 +00:00
|
|
|
else:
|
|
|
|
ok(respNull())
|
|
|
|
|
|
|
|
proc txMaxPriorityFeePerGas(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
2024-06-14 07:31:08 +00:00
|
|
|
if tx.tx.txType >= TxEip1559:
|
|
|
|
bigIntNode(tx.tx.maxPriorityFeePerGas)
|
2021-09-20 12:58:24 +00:00
|
|
|
else:
|
|
|
|
ok(respNull())
|
|
|
|
|
|
|
|
proc txEffectiveGasPrice(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
|
|
|
if tx.baseFee.isNone:
|
|
|
|
return bigIntNode(tx.tx.gasPrice)
|
|
|
|
|
|
|
|
let baseFee = tx.baseFee.get().truncate(GasInt)
|
2024-06-14 07:31:08 +00:00
|
|
|
let priorityFee = min(tx.tx.maxPriorityFeePerGas, tx.tx.maxFeePerGas - baseFee)
|
2021-09-20 12:58:24 +00:00
|
|
|
bigIntNode(priorityFee + baseFee)
|
|
|
|
|
|
|
|
proc txChainId(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
|
|
|
if tx.tx.txType == TxLegacy:
|
|
|
|
ok(respNull())
|
|
|
|
else:
|
|
|
|
longNode(tx.tx.chainId.uint64)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc txGas(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
longNode(tx.tx.gasLimit)
|
|
|
|
|
|
|
|
proc txInputData(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
resp(tx.tx.payload)
|
|
|
|
|
|
|
|
proc txBlock(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
ctx.getBlockByNumber(tx.blockNumber)
|
|
|
|
|
|
|
|
proc txStatus(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
if tx.receipt.hasStatus:
|
2021-06-27 04:19:43 +00:00
|
|
|
longNode(tx.receipt.status.uint64)
|
2021-04-24 03:59:55 +00:00
|
|
|
else:
|
|
|
|
ok(respNull())
|
|
|
|
|
|
|
|
proc txGasUsed(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
longNode(tx.gasUsed)
|
|
|
|
|
|
|
|
proc txCumulativeGasUsed(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
longNode(tx.receipt.cumulativeGasUsed)
|
|
|
|
|
|
|
|
proc txCreatedContract(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2024-10-04 14:34:31 +00:00
|
|
|
let sender = tx.tx.recoverSender.valueOr:
|
2021-05-05 12:18:35 +00:00
|
|
|
return err("can't calculate sender")
|
|
|
|
|
2021-06-27 04:19:43 +00:00
|
|
|
if not tx.tx.contractCreation:
|
2021-04-24 03:59:55 +00:00
|
|
|
return ok(respNull())
|
|
|
|
|
|
|
|
let hres = getBlockByNumber(ctx, tx.blockNumber)
|
|
|
|
if hres.isErr:
|
|
|
|
return hres
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(hres.get())
|
2021-06-17 09:11:24 +00:00
|
|
|
let contractAddress = generateAddress(sender, tx.tx.nonce)
|
2021-04-24 03:59:55 +00:00
|
|
|
ctx.accountNode(h.header, contractAddress)
|
|
|
|
|
|
|
|
proc txLogs(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let tx = TxNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
var list = respList()
|
|
|
|
for i, n in tx.receipt.logs:
|
|
|
|
list.add logNode(ctx, n, i, tx)
|
|
|
|
ok(list)
|
|
|
|
|
2021-05-17 13:45:37 +00:00
|
|
|
proc txR(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
|
|
|
bigIntNode(tx.tx.R)
|
|
|
|
|
|
|
|
proc txS(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
|
|
|
bigIntNode(tx.tx.S)
|
|
|
|
|
|
|
|
proc txV(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
|
|
|
bigIntNode(tx.tx.V)
|
|
|
|
|
|
|
|
proc txType(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
|
|
|
let typ = resp(ord(tx.tx.txType))
|
|
|
|
ok(typ)
|
|
|
|
|
|
|
|
proc txAccessList(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
|
|
|
let tx = TxNode(parent)
|
2021-06-27 04:19:43 +00:00
|
|
|
if tx.tx.txType == TxLegacy:
|
2021-05-17 13:45:37 +00:00
|
|
|
ok(respNull())
|
|
|
|
else:
|
|
|
|
var list = respList()
|
2021-06-27 04:19:43 +00:00
|
|
|
for x in tx.tx.accessList:
|
2021-05-17 13:45:37 +00:00
|
|
|
list.add aclNode(ctx, x)
|
|
|
|
ok(list)
|
|
|
|
|
2023-07-28 10:32:49 +00:00
|
|
|
proc txMaxFeePerBlobGas(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
2024-11-01 19:06:26 +00:00
|
|
|
if tx.tx.txType < TxEip4844:
|
2023-07-28 10:32:49 +00:00
|
|
|
ok(respNull())
|
|
|
|
else:
|
2023-08-04 12:43:30 +00:00
|
|
|
longNode(tx.tx.maxFeePerBlobGas)
|
2023-07-28 10:32:49 +00:00
|
|
|
|
|
|
|
proc txVersionedHashes(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
2024-11-01 19:06:26 +00:00
|
|
|
if tx.tx.txType < TxEip4844:
|
2023-07-28 10:32:49 +00:00
|
|
|
ok(respNull())
|
|
|
|
else:
|
|
|
|
var list = respList()
|
|
|
|
for hs in tx.tx.versionedHashes:
|
|
|
|
list.add resp("0x" & hs.data.toHex)
|
|
|
|
ok(list)
|
|
|
|
|
|
|
|
proc txRaw(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
|
|
|
let txBytes = rlp.encode(tx.tx)
|
|
|
|
resp(txBytes)
|
|
|
|
|
|
|
|
proc txRawReceipt(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let tx = TxNode(parent)
|
|
|
|
let recBytes = rlp.encode(tx.receipt)
|
|
|
|
resp(recBytes)
|
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
const txProcs = {
|
|
|
|
"from": txFrom,
|
|
|
|
"hash": txHash,
|
|
|
|
"nonce": txNonce,
|
|
|
|
"index": txIndex,
|
|
|
|
"to": txTo,
|
|
|
|
"value": txValue,
|
|
|
|
"gasPrice": txGasPrice,
|
|
|
|
"gas": txGas,
|
|
|
|
"inputData": txInputData,
|
|
|
|
"block": txBlock,
|
|
|
|
"status": txStatus,
|
|
|
|
"gasUsed": txGasUsed,
|
|
|
|
"cumulativeGasUsed": txCumulativeGasUsed,
|
|
|
|
"createdContract": txCreatedContract,
|
2021-05-17 13:45:37 +00:00
|
|
|
"logs": txLogs,
|
|
|
|
"r": txR,
|
|
|
|
"s": txS,
|
|
|
|
"v": txV,
|
|
|
|
"type": txType,
|
2021-09-20 12:58:24 +00:00
|
|
|
"accessList": txAccessList,
|
|
|
|
"maxFeePerGas": txMaxFeePerGas,
|
|
|
|
"maxPriorityFeePerGas": txMaxPriorityFeePerGas,
|
|
|
|
"effectiveGasPrice": txEffectiveGasPrice,
|
2023-07-28 10:32:49 +00:00
|
|
|
"chainID": txChainId,
|
2024-02-20 03:07:38 +00:00
|
|
|
"maxFeePerBlobGas": txMaxFeePerBlobGas,
|
2023-07-28 10:32:49 +00:00
|
|
|
"versionedHashes": txVersionedHashes,
|
|
|
|
"raw": txRaw,
|
|
|
|
"rawReceipt": txRawReceipt
|
2021-05-17 13:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proc aclAddress(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let acl = AclNode(parent)
|
2024-09-29 12:37:09 +00:00
|
|
|
resp(acl.acl.address.data)
|
2021-05-17 13:45:37 +00:00
|
|
|
|
|
|
|
proc aclStorageKeys(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let acl = AclNode(parent)
|
|
|
|
if acl.acl.storageKeys.len == 0:
|
|
|
|
ok(respNull())
|
|
|
|
else:
|
|
|
|
var list = respList()
|
|
|
|
for n in acl.acl.storageKeys:
|
2024-09-29 12:37:09 +00:00
|
|
|
list.add resp(n.data).get()
|
2021-05-17 13:45:37 +00:00
|
|
|
ok(list)
|
|
|
|
|
|
|
|
const aclProcs = {
|
|
|
|
"address": aclAddress,
|
|
|
|
"storageKeys": aclStorageKeys
|
2021-04-24 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
2023-07-28 10:32:49 +00:00
|
|
|
proc wdIndex(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let w = WdNode(parent)
|
|
|
|
longNode(w.wd.index)
|
|
|
|
|
|
|
|
proc wdValidator(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let w = WdNode(parent)
|
|
|
|
longNode(w.wd.validatorIndex)
|
|
|
|
|
|
|
|
proc wdAddress(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let w = WdNode(parent)
|
2024-09-29 12:37:09 +00:00
|
|
|
resp(w.wd.address.data)
|
2023-07-28 10:32:49 +00:00
|
|
|
|
|
|
|
proc wdAmount(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let w = WdNode(parent)
|
|
|
|
longNode(w.wd.amount)
|
|
|
|
|
|
|
|
const wdProcs = {
|
|
|
|
"index": wdIndex,
|
|
|
|
"validator": wdValidator,
|
|
|
|
"address": wdAddress,
|
|
|
|
"amount": wdAmount
|
|
|
|
}
|
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
proc blockNumberImpl(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2024-06-14 07:31:08 +00:00
|
|
|
longNode(h.header.number)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc blockHashImpl(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
let hash = blockHash(h.header)
|
|
|
|
resp(hash)
|
|
|
|
|
|
|
|
proc blockParent(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
getBlockByHash(ctx, h.header.parentHash)
|
|
|
|
|
|
|
|
proc blockNonce(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
ok(resp("0x" & h.header.nonce.toHex))
|
|
|
|
|
|
|
|
proc blockTransactionsRoot(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
resp(h.header.txRoot)
|
|
|
|
|
|
|
|
proc blockTransactionCount(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
ctx.getTxCount(h.header.txRoot)
|
|
|
|
|
|
|
|
proc blockStateRoot(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
resp(h.header.stateRoot)
|
|
|
|
|
|
|
|
proc blockReceiptsRoot(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2024-06-14 07:31:08 +00:00
|
|
|
resp(h.header.receiptsRoot)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc blockMiner(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
ctx.accountNode(h.header, h.header.coinbase)
|
|
|
|
|
|
|
|
proc blockExtraData(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
resp(h.header.extraData)
|
|
|
|
|
|
|
|
proc blockGasLimit(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
longNode(h.header.gasLimit)
|
|
|
|
|
|
|
|
proc blockGasUsed(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
longNode(h.header.gasUsed)
|
|
|
|
|
|
|
|
proc blockTimestamp(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2023-10-18 02:16:11 +00:00
|
|
|
bigIntNode(h.header.timestamp.uint64)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc blockLogsBloom(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2024-09-29 12:37:09 +00:00
|
|
|
resp(h.header.logsBloom.data)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc blockMixHash(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2024-06-14 07:31:08 +00:00
|
|
|
resp(h.header.mixHash)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc blockDifficulty(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
bigIntNode(h.header.difficulty)
|
|
|
|
|
|
|
|
proc blockTotalDifficulty(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
let hash = blockHash(h.header)
|
|
|
|
getTotalDifficulty(ctx, hash)
|
|
|
|
|
|
|
|
proc blockOmmerCount(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
getOmmerCount(ctx, h.header.ommersHash)
|
|
|
|
|
|
|
|
proc blockOmmers(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
getOmmers(ctx, h.header.ommersHash)
|
|
|
|
|
|
|
|
proc blockOmmerAt(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
let index = parseU64(params[0].val)
|
|
|
|
getOmmerAt(ctx, h.header.ommersHash, index.int)
|
|
|
|
|
|
|
|
proc blockOmmerHash(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
resp(h.header.ommersHash)
|
|
|
|
|
|
|
|
proc blockTransactions(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2023-08-04 11:10:09 +00:00
|
|
|
{.cast(noSideEffect).}:
|
|
|
|
getTxs(ctx, h.header)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc blockTransactionAt(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2023-04-17 08:22:51 +00:00
|
|
|
try:
|
|
|
|
let index = parseU64(params[0].val)
|
2023-08-04 11:10:09 +00:00
|
|
|
{.cast(noSideEffect).}:
|
2024-06-14 07:31:08 +00:00
|
|
|
getTxAt(ctx, h.header, index)
|
2023-04-17 08:22:51 +00:00
|
|
|
except ValueError as ex:
|
|
|
|
err(ex.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc blockLogs(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: stub, missing impl
|
2021-05-04 06:54:46 +00:00
|
|
|
err("not implemented")
|
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
proc blockAccount(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2023-04-17 08:22:51 +00:00
|
|
|
try:
|
2024-10-16 01:34:12 +00:00
|
|
|
let address = Address.fromHex(params[0].val.stringVal)
|
2023-04-17 08:22:51 +00:00
|
|
|
ctx.accountNode(h.header, address)
|
|
|
|
except ValueError as ex:
|
|
|
|
err(ex.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2021-10-26 15:18:08 +00:00
|
|
|
const
|
|
|
|
fFrom = 0
|
|
|
|
fTo = 1
|
|
|
|
fGasLimit = 2
|
|
|
|
fGasPrice = 3
|
|
|
|
fMaxFee = 4
|
|
|
|
fMaxPriorityFee = 5
|
|
|
|
fValue = 6
|
|
|
|
fData = 7
|
|
|
|
|
|
|
|
template isSome(n: Node, field: int): bool =
|
|
|
|
# [0] is the field's name node
|
|
|
|
# [1] is the field's value node
|
|
|
|
n[field][1].kind != nkEmpty
|
|
|
|
|
|
|
|
template fieldString(n: Node, field: int): string =
|
|
|
|
n[field][1].stringVal
|
|
|
|
|
|
|
|
template optionalAddress(dstField: untyped, n: Node, field: int) =
|
|
|
|
if isSome(n, field):
|
2024-09-29 12:37:09 +00:00
|
|
|
let address = addresses.Address.fromHex(fieldString(n, field))
|
|
|
|
dstField = Opt.some(primitives.Address address.data)
|
2021-10-26 15:18:08 +00:00
|
|
|
|
|
|
|
template optionalGasInt(dstField: untyped, n: Node, field: int) =
|
|
|
|
if isSome(n, field):
|
2024-06-14 07:31:08 +00:00
|
|
|
dstField = Opt.some(parseU64(n[field][1]).Quantity)
|
2021-10-26 15:18:08 +00:00
|
|
|
|
|
|
|
template optionalGasHex(dstField: untyped, n: Node, field: int) =
|
|
|
|
if isSome(n, field):
|
|
|
|
let gas = parse(fieldString(n, field), UInt256, radix = 16)
|
2024-06-14 07:31:08 +00:00
|
|
|
dstField = Opt.some(gas.truncate(uint64).Quantity)
|
2021-10-26 15:18:08 +00:00
|
|
|
|
|
|
|
template optionalHexU256(dstField: untyped, n: Node, field: int) =
|
|
|
|
if isSome(n, field):
|
2024-06-14 07:31:08 +00:00
|
|
|
dstField = Opt.some(parse(fieldString(n, field), UInt256, radix = 16))
|
2021-10-26 15:18:08 +00:00
|
|
|
|
|
|
|
template optionalBytes(dstField: untyped, n: Node, field: int) =
|
|
|
|
if isSome(n, field):
|
2024-06-14 07:31:08 +00:00
|
|
|
dstField = Opt.some(hexToSeqByte(fieldString(n, field)))
|
2021-10-26 15:18:08 +00:00
|
|
|
|
2024-04-16 12:52:07 +00:00
|
|
|
proc toTxArgs(n: Node): TransactionArgs {.gcsafe, raises: [ValueError].} =
|
2024-09-29 12:37:09 +00:00
|
|
|
optionalAddress(result.`from`, n, fFrom)
|
2021-10-26 15:18:08 +00:00
|
|
|
optionalAddress(result.to, n, fTo)
|
2024-04-16 12:52:07 +00:00
|
|
|
optionalGasInt(result.gas, n, fGasLimit)
|
2021-10-26 15:18:08 +00:00
|
|
|
optionalGasHex(result.gasPrice, n, fGasPrice)
|
2024-04-16 12:52:07 +00:00
|
|
|
optionalGasHex(result.maxFeePerGas, n, fMaxFee)
|
|
|
|
optionalGasHex(result.maxPriorityFeePerGas, n, fMaxPriorityFee)
|
2021-10-26 15:18:08 +00:00
|
|
|
optionalHexU256(result.value, n, fValue)
|
|
|
|
optionalBytes(result.data, n, fData)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2024-04-16 12:52:07 +00:00
|
|
|
proc makeCall(ctx: GraphqlContextRef, args: TransactionArgs,
|
2024-10-16 01:34:12 +00:00
|
|
|
header: Header): RespResult =
|
2024-06-07 08:24:32 +00:00
|
|
|
let res = rpcCallEvm(args, header, ctx.com).valueOr:
|
|
|
|
return err("Failed to call rpcCallEvm")
|
2021-04-24 03:59:55 +00:00
|
|
|
var map = respMap(ctx.ids[ethCallResult])
|
2021-10-26 15:18:08 +00:00
|
|
|
map["data"] = resp("0x" & res.output.toHex)
|
|
|
|
map["gasUsed"] = longNode(res.gasUsed).get()
|
|
|
|
map["status"] = longNode(if res.isError: 0 else: 1).get()
|
2021-04-24 03:59:55 +00:00
|
|
|
ok(map)
|
|
|
|
|
|
|
|
proc blockCall(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
let param = params[0].val
|
|
|
|
try:
|
2024-04-16 12:52:07 +00:00
|
|
|
let args = toTxArgs(param)
|
2023-08-04 11:10:09 +00:00
|
|
|
{.cast(noSideEffect).}:
|
2024-04-16 12:52:07 +00:00
|
|
|
ctx.makeCall(args, h.header)
|
2023-06-04 06:00:50 +00:00
|
|
|
except CatchableError as em:
|
2021-05-03 06:40:37 +00:00
|
|
|
err("call error: " & em.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc blockEstimateGas(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2021-05-05 04:18:17 +00:00
|
|
|
let h = HeaderNode(parent)
|
2021-04-24 03:59:55 +00:00
|
|
|
let param = params[0].val
|
|
|
|
try:
|
2024-04-16 12:52:07 +00:00
|
|
|
let args = toTxArgs(param)
|
2021-10-26 15:18:08 +00:00
|
|
|
# TODO: DEFAULT_RPC_GAS_CAP should configurable
|
2023-08-04 11:10:09 +00:00
|
|
|
{.cast(noSideEffect).}:
|
2024-06-07 08:24:32 +00:00
|
|
|
let gasUsed = rpcEstimateGas(args, h.header, ctx.com, DEFAULT_RPC_GAS_CAP).valueOr:
|
|
|
|
return err("Failed to call rpcEstimateGas")
|
2023-08-04 11:10:09 +00:00
|
|
|
longNode(gasUsed)
|
2023-06-04 06:00:50 +00:00
|
|
|
except CatchableError as em:
|
2021-04-24 03:59:55 +00:00
|
|
|
err("estimateGas error: " & em.msg)
|
|
|
|
|
2021-09-20 12:58:24 +00:00
|
|
|
proc blockBaseFeePerGas(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let h = HeaderNode(parent)
|
2024-06-14 07:31:08 +00:00
|
|
|
if h.header.baseFeePerGas.isSome:
|
|
|
|
bigIntNode(h.header.baseFeePerGas.get)
|
2021-09-20 12:58:24 +00:00
|
|
|
else:
|
|
|
|
ok(respNull())
|
|
|
|
|
2023-07-28 10:32:49 +00:00
|
|
|
proc blockWithdrawalsRoot(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let h = HeaderNode(parent)
|
|
|
|
if h.header.withdrawalsRoot.isSome:
|
|
|
|
resp(h.header.withdrawalsRoot.get)
|
|
|
|
else:
|
|
|
|
ok(respNull())
|
|
|
|
|
|
|
|
proc blockWithdrawals(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
|
|
|
let h = HeaderNode(parent)
|
|
|
|
getWithdrawals(ctx, h.header)
|
|
|
|
|
|
|
|
proc blockBlobGasUsed(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let h = HeaderNode(parent)
|
2023-08-04 12:43:30 +00:00
|
|
|
if h.header.blobGasUsed.isSome:
|
|
|
|
longNode(h.header.blobGasUsed.get)
|
2023-07-28 10:32:49 +00:00
|
|
|
else:
|
|
|
|
ok(respNull())
|
|
|
|
|
2024-02-20 03:07:38 +00:00
|
|
|
proc blockExcessBlobGas(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2023-07-28 10:32:49 +00:00
|
|
|
let h = HeaderNode(parent)
|
2023-08-04 12:43:30 +00:00
|
|
|
if h.header.excessBlobGas.isSome:
|
|
|
|
longNode(h.header.excessBlobGas.get)
|
2023-07-28 10:32:49 +00:00
|
|
|
else:
|
|
|
|
ok(respNull())
|
|
|
|
|
2023-08-30 16:29:48 +00:00
|
|
|
proc blockParentBeaconBlockRoot(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let h = HeaderNode(parent)
|
|
|
|
if h.header.parentBeaconBlockRoot.isSome:
|
|
|
|
resp(h.header.parentBeaconBlockRoot.get)
|
|
|
|
else:
|
|
|
|
ok(respNull())
|
|
|
|
|
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
const blockProcs = {
|
|
|
|
"parent": blockParent,
|
|
|
|
"number": blockNumberImpl,
|
|
|
|
"hash": blockHashImpl,
|
|
|
|
"nonce": blockNonce,
|
|
|
|
"transactionsRoot": blockTransactionsRoot,
|
|
|
|
"transactionCount": blockTransactionCount,
|
|
|
|
"stateRoot": blockStateRoot,
|
|
|
|
"receiptsRoot": blockReceiptsRoot,
|
|
|
|
"miner": blockMiner,
|
|
|
|
"extraData": blockExtraData,
|
|
|
|
"gasLimit": blockGasLimit,
|
|
|
|
"gasUsed": blockGasUsed,
|
|
|
|
"timestamp": blockTimestamp,
|
|
|
|
"logsBloom": blockLogsBloom,
|
|
|
|
"mixHash": blockMixHash,
|
|
|
|
"difficulty": blockDifficulty,
|
|
|
|
"totalDifficulty": blockTotalDifficulty,
|
|
|
|
"ommerCount": blockOmmerCount,
|
|
|
|
"ommers": blockOmmers,
|
|
|
|
"ommerAt": blockOmmerAt,
|
|
|
|
"ommerHash": blockOmmerHash,
|
|
|
|
"transactions": blockTransactions,
|
|
|
|
"transactionAt": blockTransactionAt,
|
2021-05-04 06:54:46 +00:00
|
|
|
"logs": blockLogs,
|
2021-04-24 03:59:55 +00:00
|
|
|
"account": blockAccount,
|
|
|
|
"call": blockCall,
|
2021-09-20 12:58:24 +00:00
|
|
|
"estimateGas": blockEstimateGas,
|
2023-07-28 10:32:49 +00:00
|
|
|
"baseFeePerGas": blockBaseFeePerGas,
|
|
|
|
"withdrawalsRoot": blockWithdrawalsRoot,
|
|
|
|
"withdrawals": blockWithdrawals,
|
|
|
|
"blobGasUsed": blockBlobGasUsed,
|
2023-08-30 16:29:48 +00:00
|
|
|
"excessBlobGas": blockExcessBlobGas,
|
|
|
|
"parentBeaconBlockRoot": blockParentBeaconBlockRoot,
|
2021-04-24 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proc callResultData(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
ok(parent.map[0].val)
|
|
|
|
|
|
|
|
proc callResultGasUsed(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
ok(parent.map[1].val)
|
|
|
|
|
|
|
|
proc callResultStatus(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
ok(parent.map[2].val)
|
|
|
|
|
|
|
|
const callResultProcs = {
|
|
|
|
"data": callResultData,
|
|
|
|
"gasUsed": callResultGasUsed,
|
|
|
|
"status": callResultStatus
|
|
|
|
}
|
|
|
|
|
|
|
|
proc syncStateStartingBlock(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2022-12-02 04:39:12 +00:00
|
|
|
longNode(ctx.com.syncStart)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc syncStateCurrentBlock(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2022-12-02 04:39:12 +00:00
|
|
|
longNode(ctx.com.syncCurrent)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc syncStateHighestBlock(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2022-12-02 04:39:12 +00:00
|
|
|
longNode(ctx.com.syncHighest)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc syncStatePulledStates(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: what is this ?
|
|
|
|
ok(respNull())
|
|
|
|
|
|
|
|
proc syncStateKnownStates(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: what is this ?
|
|
|
|
ok(respNull())
|
|
|
|
|
|
|
|
const syncStateProcs = {
|
|
|
|
"startingBlock": syncStateStartingBlock,
|
|
|
|
"currentBlock": syncStateCurrentBlock,
|
|
|
|
"highestBlock": syncStateHighestBlock,
|
|
|
|
"pulledStates": syncStatePulledStates,
|
|
|
|
"knownStates": syncStateKnownStates
|
|
|
|
}
|
|
|
|
|
|
|
|
proc pendingTransactionCount(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: stub, missing impl
|
2021-05-04 06:54:46 +00:00
|
|
|
err("not implemented")
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc pendingTransactions(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: stub, missing impl
|
2021-05-04 06:54:46 +00:00
|
|
|
err("not implemented")
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc pendingAccount(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: stub, missing impl
|
2021-05-04 06:54:46 +00:00
|
|
|
err("not implemented")
|
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
proc pendingCall(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: stub, missing impl
|
2021-05-04 06:54:46 +00:00
|
|
|
err("not implemented")
|
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
proc pendingEstimateGas(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: stub, missing impl
|
2021-05-04 06:54:46 +00:00
|
|
|
err("not implemented")
|
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
const pendingProcs = {
|
|
|
|
"transactionCount": pendingTransactionCount,
|
|
|
|
"transactions": pendingTransactions,
|
|
|
|
"account": pendingAccount,
|
|
|
|
"call": pendingCall,
|
|
|
|
"estimateGas": pendingEstimateGas
|
|
|
|
}
|
|
|
|
|
2024-10-16 01:34:12 +00:00
|
|
|
proc pickBlockNumber(ctx: GraphqlContextRef, number: Node): base.BlockNumber =
|
2021-05-05 12:18:35 +00:00
|
|
|
if number.kind == nkEmpty:
|
2022-12-02 04:39:12 +00:00
|
|
|
ctx.com.syncCurrent
|
2021-05-05 12:18:35 +00:00
|
|
|
else:
|
2024-06-14 07:31:08 +00:00
|
|
|
parseU64(number)
|
2021-05-05 12:18:35 +00:00
|
|
|
|
|
|
|
proc queryAccount(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2023-04-17 08:22:51 +00:00
|
|
|
try:
|
2024-10-16 01:34:12 +00:00
|
|
|
let address = Address.fromHex(params[0].val.stringVal)
|
2023-04-17 08:22:51 +00:00
|
|
|
let blockNumber = pickBlockNumber(ctx, params[1].val)
|
|
|
|
let hres = getBlockByNumber(ctx, blockNumber)
|
|
|
|
if hres.isErr:
|
|
|
|
return hres
|
|
|
|
let h = HeaderNode(hres.get())
|
|
|
|
accountNode(ctx, h.header, address)
|
|
|
|
except ValueError as ex:
|
|
|
|
err(ex.msg)
|
2021-05-05 12:18:35 +00:00
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
proc queryBlock(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
|
|
|
let number = params[0].val
|
|
|
|
let hash = params[1].val
|
2021-05-04 06:54:46 +00:00
|
|
|
if number.kind != nkEmpty and hash.kind != nkEmpty:
|
|
|
|
err("only one param allowed, number or hash, not both")
|
|
|
|
elif number.kind == nkInt:
|
2021-04-24 03:59:55 +00:00
|
|
|
getBlockByNumber(ctx, number)
|
2023-07-28 10:32:49 +00:00
|
|
|
elif number.kind == nkString:
|
|
|
|
try:
|
|
|
|
let blockNumber = toBlockNumber(number)
|
|
|
|
getBlockByNumber(ctx, blockNumber)
|
|
|
|
except ValueError as ex:
|
|
|
|
err(ex.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
elif hash.kind == nkString:
|
|
|
|
getBlockByHash(ctx, hash)
|
|
|
|
else:
|
|
|
|
getLatestBlock(ctx)
|
|
|
|
|
|
|
|
proc queryBlocks(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2024-06-14 07:31:08 +00:00
|
|
|
let fromNumber = parseU64(params[0].val)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
let to = params[1].val
|
2021-05-05 12:18:35 +00:00
|
|
|
let toNumber = pickBlockNumber(ctx, to)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
if fromNumber > toNumber:
|
2024-06-14 07:31:08 +00:00
|
|
|
return err("from(" & $fromNumber &
|
|
|
|
") is bigger than to(" & $toNumber & ")")
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
# TODO: what is the maximum number here?
|
2024-06-14 07:31:08 +00:00
|
|
|
if toNumber - fromNumber > 32'u64:
|
2021-04-24 03:59:55 +00:00
|
|
|
return err("can't get more than 32 blocks at once")
|
|
|
|
|
|
|
|
var list = respList()
|
|
|
|
var number = fromNumber
|
|
|
|
while number <= toNumber:
|
|
|
|
let n = getBlockByNumber(ctx, number)
|
|
|
|
if n.isErr:
|
|
|
|
list.add respNull()
|
|
|
|
else:
|
|
|
|
list.add n.get()
|
2024-06-14 07:31:08 +00:00
|
|
|
number += 1'u64
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
ok(list)
|
|
|
|
|
|
|
|
proc queryPending(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: stub, missing impl
|
2021-05-04 06:54:46 +00:00
|
|
|
err("not implemented")
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc queryTransaction(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2023-04-17 08:22:51 +00:00
|
|
|
try:
|
|
|
|
let hash = toHash(params[0].val)
|
2023-08-04 11:10:09 +00:00
|
|
|
{.cast(noSideEffect).}:
|
|
|
|
getTxByHash(ctx, hash)
|
2023-04-17 08:22:51 +00:00
|
|
|
except ValueError as ex:
|
|
|
|
err(ex.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
proc queryLogs(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: stub, missing impl
|
2021-05-04 06:54:46 +00:00
|
|
|
err("not implemented")
|
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
proc queryGasPrice(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
|
|
|
try:
|
2024-10-01 17:40:37 +00:00
|
|
|
bigIntNode(calculateMedianGasPrice(ctx.chainDB))
|
2023-06-04 06:00:50 +00:00
|
|
|
except CatchableError as em:
|
2021-04-24 03:59:55 +00:00
|
|
|
err("can't get gasPrice: " & em.msg)
|
|
|
|
|
|
|
|
proc queryProtocolVersion(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
|
|
|
for n in ctx.ethNode.capabilities:
|
|
|
|
if n.name == "eth":
|
2024-10-01 17:40:37 +00:00
|
|
|
return bigIntNode(n.version)
|
2021-04-24 03:59:55 +00:00
|
|
|
err("can't get protocol version")
|
|
|
|
|
|
|
|
proc querySyncing(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
|
|
|
ok(respMap(ctx.ids[ethSyncState]))
|
|
|
|
|
2021-09-20 12:58:24 +00:00
|
|
|
proc queryMaxPriorityFeePerGas(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
# TODO: stub, missing impl
|
|
|
|
err("not implemented")
|
|
|
|
|
|
|
|
proc queryChainId(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
|
|
|
let ctx = GraphqlContextRef(ud)
|
2022-12-02 04:39:12 +00:00
|
|
|
longNode(ctx.com.chainId.uint64)
|
2021-09-20 12:58:24 +00:00
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
const queryProcs = {
|
2021-05-05 12:18:35 +00:00
|
|
|
"account": queryAccount,
|
2021-04-24 03:59:55 +00:00
|
|
|
"block": queryBlock,
|
|
|
|
"blocks": queryBlocks,
|
|
|
|
"pending": queryPending,
|
|
|
|
"transaction": queryTransaction,
|
|
|
|
"logs": queryLogs,
|
|
|
|
"gasPrice": queryGasPrice,
|
|
|
|
"protocolVersion": queryProtocolVersion,
|
2021-09-20 12:58:24 +00:00
|
|
|
"syncing": querySyncing,
|
|
|
|
"maxPriorityFeePerGas": queryMaxPriorityFeePerGas,
|
|
|
|
"chainID": queryChainId
|
2021-04-24 03:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
proc sendRawTransaction(ud: RootRef, params: Args, parent: Node): RespResult {.apiPragma.} =
|
2021-05-04 09:39:07 +00:00
|
|
|
# if tx validation failed, the result will be null
|
2021-04-24 03:59:55 +00:00
|
|
|
let ctx = GraphqlContextRef(ud)
|
|
|
|
try:
|
|
|
|
let data = hexToSeqByte(params[0].val.stringVal)
|
2024-05-15 03:07:59 +00:00
|
|
|
let tx = decodePooledTx(data) # we want to know if it is a valid tx blob
|
|
|
|
let txHash = rlpHash(tx)
|
2023-07-28 10:32:49 +00:00
|
|
|
|
|
|
|
ctx.txPool.add(tx)
|
|
|
|
|
2023-10-20 08:30:05 +00:00
|
|
|
let res = ctx.txPool.inPoolAndReason(txHash)
|
|
|
|
if res.isOk:
|
2023-07-28 10:32:49 +00:00
|
|
|
return resp(txHash)
|
|
|
|
else:
|
2023-10-20 08:30:05 +00:00
|
|
|
return err(res.error)
|
2023-07-28 10:32:49 +00:00
|
|
|
|
2023-06-04 06:00:50 +00:00
|
|
|
except CatchableError as em:
|
|
|
|
return err("failed to process raw transaction: " & em.msg)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
|
|
|
const mutationProcs = {
|
|
|
|
"sendRawTransaction": sendRawTransaction
|
|
|
|
}
|
|
|
|
|
|
|
|
const
|
|
|
|
ethSchema = staticRead("ethapi.ql")
|
|
|
|
|
2021-05-10 15:19:34 +00:00
|
|
|
type
|
|
|
|
QcNames = enum
|
|
|
|
qcType = "__Type"
|
|
|
|
qcFields = "fields"
|
|
|
|
qcBlock = "block"
|
|
|
|
qcTransaction = "Transaction"
|
2021-05-17 13:45:37 +00:00
|
|
|
|
2021-05-10 15:19:34 +00:00
|
|
|
EthQueryComplexity = ref object of QueryComplexity
|
|
|
|
names: array[QcNames, Name]
|
2021-05-17 13:45:37 +00:00
|
|
|
|
|
|
|
proc calcQC(qc: QueryComplexity, field: FieldRef): int {.cdecl,
|
2023-04-17 08:22:51 +00:00
|
|
|
gcsafe, apiRaises.} =
|
2021-05-10 15:19:34 +00:00
|
|
|
let qc = EthQueryComplexity(qc)
|
|
|
|
if field.parentType.sym.name == qc.names[qcType] and
|
|
|
|
field.field.name.name == qc.names[qcFields]:
|
|
|
|
return 100
|
2021-05-17 13:45:37 +00:00
|
|
|
elif field.parentType.sym.name == qc.names[qcTransaction] and
|
2021-05-10 15:19:34 +00:00
|
|
|
field.field.name.name == qc.names[qcBlock]:
|
|
|
|
return 100
|
|
|
|
else:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
proc newQC(ctx: GraphqlContextRef): EthQueryComplexity =
|
|
|
|
const MaxQueryComplexity = 200
|
|
|
|
var qc = EthQueryComplexity()
|
|
|
|
qc.init(calcQC, MaxQueryComplexity)
|
|
|
|
for n in QcNames:
|
|
|
|
let name = ctx.createName($n)
|
|
|
|
qc.names[n] = name
|
|
|
|
qc
|
2021-05-17 13:45:37 +00:00
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
proc initEthApi(ctx: GraphqlContextRef) =
|
|
|
|
ctx.customScalar("Bytes32", scalarBytes32)
|
|
|
|
ctx.customScalar("Address", scalarAddress)
|
|
|
|
ctx.customScalar("Bytes", scalarBytes)
|
|
|
|
ctx.customScalar("BigInt", scalarBigInt)
|
|
|
|
ctx.customScalar("Long", scalarLong)
|
|
|
|
|
|
|
|
for n in EthTypes:
|
|
|
|
let name = ctx.createName($n)
|
|
|
|
ctx.ids[n] = name
|
|
|
|
|
|
|
|
ctx.addResolvers(ctx, ctx.ids[ethAccount ], accountProcs)
|
|
|
|
ctx.addResolvers(ctx, ctx.ids[ethLog ], logProcs)
|
|
|
|
ctx.addResolvers(ctx, ctx.ids[ethTransaction], txProcs)
|
|
|
|
ctx.addResolvers(ctx, ctx.ids[ethBlock ], blockProcs)
|
|
|
|
ctx.addResolvers(ctx, ctx.ids[ethCallResult ], callResultProcs)
|
|
|
|
ctx.addResolvers(ctx, ctx.ids[ethSyncState ], syncStateProcs)
|
|
|
|
ctx.addResolvers(ctx, ctx.ids[ethPending ], pendingProcs)
|
|
|
|
ctx.addResolvers(ctx, ctx.ids[ethQuery ], queryProcs)
|
|
|
|
ctx.addResolvers(ctx, ctx.ids[ethMutation ], mutationProcs)
|
2021-05-17 13:45:37 +00:00
|
|
|
ctx.addResolvers(ctx, ctx.ids[ethAccessTuple], aclProcs)
|
2023-07-28 10:32:49 +00:00
|
|
|
ctx.addResolvers(ctx, ctx.ids[ethWithdrawal ], wdProcs)
|
2021-04-24 03:59:55 +00:00
|
|
|
|
2021-05-10 15:19:34 +00:00
|
|
|
var qc = newQC(ctx)
|
|
|
|
ctx.addInstrument(qc)
|
2021-05-17 13:45:37 +00:00
|
|
|
|
2021-04-24 03:59:55 +00:00
|
|
|
let res = ctx.parseSchema(ethSchema)
|
|
|
|
if res.isErr:
|
|
|
|
echo res.error
|
|
|
|
quit(QuitFailure)
|
|
|
|
|
2022-12-02 04:39:12 +00:00
|
|
|
proc setupGraphqlContext*(com: CommonRef,
|
2022-01-23 11:39:43 +00:00
|
|
|
ethNode: EthereumNode,
|
|
|
|
txPool: TxPoolRef): GraphqlContextRef =
|
2021-04-24 03:59:55 +00:00
|
|
|
let ctx = GraphqlContextRef(
|
2022-12-02 04:39:12 +00:00
|
|
|
chainDB: com.db,
|
|
|
|
com : com,
|
2022-01-23 11:39:43 +00:00
|
|
|
ethNode: ethNode,
|
2022-12-02 04:39:12 +00:00
|
|
|
txPool : txPool
|
2021-04-24 03:59:55 +00:00
|
|
|
)
|
|
|
|
graphql.init(ctx)
|
|
|
|
ctx.initEthApi()
|
|
|
|
ctx
|
|
|
|
|
2024-01-29 13:20:04 +00:00
|
|
|
proc setupGraphqlHttpHandler*(com: CommonRef,
|
|
|
|
ethNode: EthereumNode,
|
|
|
|
txPool: TxPoolRef): GraphqlHttpHandlerRef =
|
2022-12-02 04:39:12 +00:00
|
|
|
let ctx = setupGraphqlContext(com, ethNode, txPool)
|
2024-01-29 13:20:04 +00:00
|
|
|
GraphqlHttpHandlerRef.new(ctx)
|
2023-01-31 05:13:57 +00:00
|
|
|
|
2023-08-04 11:10:09 +00:00
|
|
|
{.pop.}
|