eth_getBalance in p2p
This commit is contained in:
parent
da8849a5a0
commit
a027ca2b25
|
@ -6,10 +6,42 @@
|
|||
# at your option.
|
||||
# This file may not be copied, modified, or distributed except according to
|
||||
# those terms.
|
||||
import nimcrypto, json_rpc/server, eth_p2p
|
||||
import ../config
|
||||
import
|
||||
nimcrypto, json_rpc/server, eth_p2p, hexstrings, strutils, stint,
|
||||
../config, ../vm_state, ../constants, eth_trie/[memdb, types],
|
||||
../db/[db_chain, state_db], eth_common
|
||||
|
||||
proc headerFromTag(chain:BaseChainDB, blockTag: string): BlockHeader =
|
||||
let tag = blockTag.string.toLowerAscii
|
||||
case tag
|
||||
of "latest": result = chain.getCanonicalHead()
|
||||
of "earliest": result = chain.getCanonicalBlockHeaderByNumber(GENESIS_BLOCK_NUMBER)
|
||||
of "pending":
|
||||
#TODO
|
||||
raise newException(ValueError, "Pending tag not yet implemented")
|
||||
else:
|
||||
# Raises are trapped and wrapped in JSON when returned to the user.
|
||||
tag.validateRaiseHexQuantity
|
||||
let blockNum = stint.fromHex(UInt256, tag)
|
||||
result = chain.getCanonicalBlockHeaderByNumber(blockNum)
|
||||
|
||||
proc setupP2PRPC*(server: EthereumNode, rpcsrv: RpcServer) =
|
||||
rpcsrv.rpc("net_version") do() -> uint:
|
||||
let conf = getConfiguration()
|
||||
result = conf.net.networkId
|
||||
|
||||
rpcsrv.rpc("eth_getBalance") do(address: array[20, byte], quantityTag: string) -> int:
|
||||
## Returns the balance of the account of given address.
|
||||
##
|
||||
## data: address to check for balance.
|
||||
## quantityTag: integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.
|
||||
## Returns integer of the current balance in wei.
|
||||
template chain: untyped = BaseChainDB(server.chain) # TODO: Sensible casting
|
||||
let
|
||||
header = chain.headerFromTag(quantityTag)
|
||||
vmState = newBaseVMState(header, chain)
|
||||
account_db = vmState.readOnlyStateDb
|
||||
balance = account_db.get_balance(address)
|
||||
|
||||
return balance.toInt
|
||||
|
||||
|
|
Loading…
Reference in New Issue