Add eth adapter to tx helper

This commit is contained in:
nolash
2021-01-10 18:27:44 +01:00
parent 347e117eb9
commit f0e00152b7
6 changed files with 97 additions and 20 deletions

View File

@@ -0,0 +1 @@
from .tx import EthTxExecutor

View File

@@ -0,0 +1,49 @@
# standard imports
import logging
# local imports
from crypto_dev_signer.helper import TxExecutor
logg = logging.getLogger()
logging.getLogger('web3').setLevel(logging.CRITICAL)
logging.getLogger('urllib3').setLevel(logging.CRITICAL)
class EthTxExecutor(TxExecutor):
def __init__(self, w3, sender, signer, chain_id, verifier=None, block=False):
self.w3 = w3
nonce = self.w3.eth.getTransactionCount(sender, 'pending')
super(EthTxExecutor, self).__init__(sender, signer, self.translator, self.dispatcher, self.reporter, nonce, chain_id, self.fee_helper, self.fee_price_helper, verifier, block)
def fee_helper(self, tx):
estimate = self.w3.eth.estimateGas(tx)
if estimate < 21000:
estimate = 21000
logg.debug('estimate {} {}'.format(tx, estimate))
return estimate
def fee_price_helper(self):
return self.w3.eth.gasPrice
def dispatcher(self, tx):
return self.w3.eth.sendRawTransaction(tx)
def reporter(self, tx):
return self.w3.eth.getTransactionReceipt(tx)
def translator(self, tx):
if tx.get('feePrice') != None:
tx['gasPrice'] = tx['feePrice']
del tx['feePrice']
if tx.get('feeUnits') != None:
tx['gas'] = tx['feeUnits']
del tx['feeUnits']
return tx

View File

@@ -13,8 +13,9 @@ logg = logging.getLogger()
class TxExecutor:
def __init__(self, sender, signer, dispatcher, reporter, nonce, chain_id, fee_helper=None, fee_price_helper=None, verifier=None, block=False):
def __init__(self, sender, signer, translator, dispatcher, reporter, nonce, chain_id, fee_helper=None, fee_price_helper=None, verifier=None, block=False):
self.sender = sender
self.translator = translator
self.nonce = nonce
self.signer = signer
self.dispatcher = dispatcher
@@ -33,7 +34,7 @@ class TxExecutor:
self.verifier = verifier
def noop_fee_helper(self, sender, code, inputs):
def noop_fee_helper(self, tx):
return 1
@@ -45,21 +46,29 @@ class TxExecutor:
return rcpt
def noop_translator(self, tx):
return tx
def sign_and_send(self, builder, force_wait=False):
fee_units = self.fee_helper(self.sender, None, None)
fee_price = self.fee_price_helper()
tx_tpl = {
'from': self.sender,
'chainId': self.chain_id,
'feeUnits': fee_units,
'feePrice': self.fee_price_helper(),
'feeUnits': 0, #fee_units,
'feePrice': fee_price,
'nonce': self.nonce,
}
tx = tx_tpl
tx = self.translator(tx_tpl)
for b in builder:
tx = b(tx)
tx['feeUnits'] = self.fee_helper(tx)
tx = self.translator(tx)
logg.debug('from {} nonce {} tx {}'.format(self.sender, self.nonce, tx))
chain_tx = EIP155Transaction(tx, self.nonce, self.chain_id)