modify estimateGas in rpc_utils.nim and it can be reusable for graphql too

This commit is contained in:
jangko 2021-04-24 10:56:22 +07:00
parent 82d4f6a2d4
commit 3dfc1501f0
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 12 additions and 12 deletions

View File

@ -274,7 +274,8 @@ proc setupEthRpc*(node: EthereumNode, chain: BaseChainDB , server: RpcServer) =
let
header = chain.headerFromTag(quantityTag)
callData = callData(call, false, chain)
result = estimateGas(callData, header, chain, call.gas.isSome)
gasUsed = estimateGas(callData, header, chain, call.gas.isSome)
result = encodeQuantity(gasUsed.uint64)
server.rpc("eth_getBlockByHash") do(data: EthHashStr, fullTransactions: bool) -> Option[BlockObject]:
## Returns information about a block by hash.

View File

@ -24,13 +24,13 @@ type
contractCreation* {.rlpIgnore.}: bool
CallData* = object
source: EthAddress
to: EthAddress
gas: GasInt
gasPrice: GasInt
value: UInt256
data: seq[byte]
contractCreation: bool
source*: EthAddress
to*: EthAddress
gas*: GasInt
gasPrice*: GasInt
value*: UInt256
data*: seq[byte]
contractCreation*: bool
proc read(rlp: var Rlp, t: var UnsignedTx, _: type EthAddress): EthAddress {.inline.} =
if rlp.blobLen != 0:
@ -188,7 +188,7 @@ proc callData*(call: EthCall, callMode: bool = true, chain: BaseChainDB): CallDa
if call.data.isSome:
result.data = hexToSeqByte(call.data.get.string)
proc setupComputation(vmState: BaseVMState, call: CallData, fork: Fork) : Computation =
proc setupComputation*(vmState: BaseVMState, call: CallData, fork: Fork) : Computation =
vmState.setupTxContext(
origin = call.source,
gasPrice = call.gasPrice,
@ -221,7 +221,7 @@ proc doCall*(call: CallData, header: BlockHeader, chain: BaseChainDB): HexDataSt
# TODO: handle revert and error
# TODO: handle contract ABI
proc estimateGas*(call: CallData, header: BlockHeader, chain: BaseChainDB, haveGasLimit: bool): HexQuantityStr =
proc estimateGas*(call: CallData, header: BlockHeader, chain: BaseChainDB, haveGasLimit: bool): GasInt =
var
# we use current header stateRoot, unlike block validation
# which use previous block stateRoot
@ -239,8 +239,7 @@ proc estimateGas*(call: CallData, header: BlockHeader, chain: BaseChainDB, haveG
var dbTx = chain.db.beginTransaction()
defer: dbTx.dispose()
let gasUsed = processTransaction(tx, call.source, vmState, fork)
result = encodeQuantity(gasUsed.uint64)
result = processTransaction(tx, call.source, vmState, fork)
dbTx.dispose()
# TODO: handle revert and error