chainlib/chainlib/eth/gas.py

56 lines
1.3 KiB
Python
Raw Normal View History

2021-02-09 09:42:46 +01:00
# third-party imports
from hexathon import (
add_0x,
strip_0x,
)
from crypto_dev_signer.eth.transaction import EIP155Transaction
# local imports
2021-02-11 08:52:59 +01:00
from chainlib.eth.rpc import jsonrpc_template
from chainlib.eth.tx import TxFactory
2021-02-11 12:43:54 +01:00
from chainlib.hash import keccak256_hex_to_hex
2021-02-09 09:42:46 +01:00
def price():
o = jsonrpc_template()
o['method'] = 'eth_gasPrice'
return o
def balance(address):
o = jsonrpc_template()
o['method'] = 'eth_getBalance'
o['params'].append(address)
return o
class GasTxFactory(TxFactory):
def create(self, sender, recipient, value):
2021-02-15 19:29:08 +01:00
tx = self.template(sender, recipient, use_nonce=True)
tx['value'] = value
2021-02-09 09:42:46 +01:00
txe = EIP155Transaction(tx, tx['nonce'], tx['chainId'])
self.signer.signTransaction(txe)
tx_raw = txe.rlp_serialize()
tx_raw_hex = add_0x(tx_raw.hex())
tx_hash_hex = add_0x(keccak256_hex_to_hex(tx_raw_hex))
o = jsonrpc_template()
o['method'] = 'eth_sendRawTransaction'
o['params'].append(tx_raw_hex)
return (tx_hash_hex, o)
class DefaultGasOracle:
def __init__(self, conn):
self.conn = conn
def get(self):
o = price()
r = self.conn.do(o)
n = strip_0x(r)
return int(n, 16)