From 00f978ef7c5d904a132fc5ceb505c8f17378c113 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Thu, 24 Jun 2021 17:15:23 +0000 Subject: [PATCH] MMGenTX: new 'fee' property, replacing get_fee() --- mmgen/altcoins/eth/tx.py | 23 +++++++++-------------- mmgen/main_txbump.py | 3 +-- mmgen/tx.py | 18 +++++++++--------- mmgen/txfile.py | 2 +- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/mmgen/altcoins/eth/tx.py b/mmgen/altcoins/eth/tx.py index a869a8e1..97ca1218 100755 --- a/mmgen/altcoins/eth/tx.py +++ b/mmgen/altcoins/eth/tx.py @@ -49,9 +49,6 @@ class EthereumMMGenTX: dmsg('fee_abs2rel() ==> {} ETH'.format(ret)) return ret if to_unit == 'eth' else ret.to_unit(to_unit,show_decimal=True) - def get_fee(self): - return self.fee - def get_hex_locktime(self): return None # TODO @@ -76,7 +73,6 @@ class EthereumMMGenTX: fee_fail_fs = 'Network fee estimation failed' no_chg_msg = 'Warning: Transaction leaves account with zero balance' usr_fee_prompt = 'Enter transaction fee or gas price: ' - usr_rel_fee = None # not in MMGenTX def __init__(self,*args,**kwargs): MMGenTX.New.__init__(self,*args,**kwargs) @@ -97,7 +93,7 @@ class EthereumMMGenTX: 'from': self.inputs[0].addr, 'to': self.outputs[0].addr if self.outputs else Str(''), 'amt': self.outputs[0].amt if self.outputs else ETHAmt('0'), - 'gasPrice': self.usr_rel_fee or self.fee_abs2rel(self.fee,to_unit='eth'), + 'gasPrice': self.fee_abs2rel(self.usr_fee,to_unit='eth'), 'startGas': self.start_gas, 'nonce': await self.get_nonce(), 'chainId': Int(await self.rpc.call(chain_id_method),16), @@ -233,6 +229,10 @@ class EthereumMMGenTX: """) fmt_keys = ('from','to','amt','nonce') + @property + def fee(self): + return self.fee_gasPrice2abs(self.txobj['gasPrice'].toWei()) + def check_txfile_hex_data(self): pass @@ -262,8 +262,7 @@ class EthereumMMGenTX: f_mmid = m['inputs'] ) def format_view_abs_fee(self): - fee = self.fee_gasPrice2abs(self.txobj['gasPrice'].toWei()) - return fee.hl() + (' (max)' if self.txobj['data'] else '') + return self.fee.hl() + (' (max)' if self.txobj['data'] else '') def format_view_rel_fee(self,terse): return '' @@ -300,7 +299,6 @@ class EthereumMMGenTX: 'chainId': Int(d['chainId']), 'data': HexStr(d['data']) } self.tx_gas = o['startGas'] # approximate, but better than nothing - self.fee = self.fee_gasPrice2abs(o['gasPrice'].toWei()) self.txobj = o return d # 'token_addr','decimals' required by Token subclass @@ -379,7 +377,6 @@ class EthereumMMGenTX: txid = CoinTxID(etx.hash.hex()) assert txid == self.coin_txid,"txid in tx.hex doesn't match value in MMGen transaction file" self.tx_gas = o['startGas'] # approximate, but better than nothing - self.fee = self.fee_gasPrice2abs(o['gasPrice'].toWei()) self.txobj = o return d # 'token_addr','decimals' required by Token subclass @@ -421,11 +418,9 @@ class EthereumMMGenTX: self.check_correct_chain() - fee = self.fee_gasPrice2abs(self.txobj['gasPrice'].toWei()) - - if not self.disable_fee_check and (fee > self.proto.max_tx_fee): + if not self.disable_fee_check and (self.fee > self.proto.max_tx_fee): die(2,'Transaction fee ({}) greater than {} max_tx_fee ({} {})!'.format( - fee, + self.fee, self.proto.name, self.proto.max_tx_fee, self.proto.coin )) @@ -472,7 +467,7 @@ class EthereumMMGenTX: return ETHAmt(self.fee * Decimal('1.101')) def bump_fee(self,idx,fee): - self.fee = fee + self.txobj['gasPrice'] = self.fee_abs2rel(fee,to_unit='eth') async def get_nonce(self): return self.txobj['nonce'] diff --git a/mmgen/main_txbump.py b/mmgen/main_txbump.py index 6bc29ded..a8df5546 100755 --- a/mmgen/main_txbump.py +++ b/mmgen/main_txbump.py @@ -154,8 +154,7 @@ async def main(): tx.bump_fee(output_idx,tx.usr_fee) tx.update_send_amt() - d = tx.get_fee() - assert d == tx.usr_fee and d <= tx.proto.max_tx_fee + assert tx.fee <= tx.proto.max_tx_fee if tx.proto.base_proto == 'Bitcoin': tx.outputs.sort_bip69() # output amts have changed, so re-sort diff --git a/mmgen/tx.py b/mmgen/tx.py index bb825686..6392de92 100755 --- a/mmgen/tx.py +++ b/mmgen/tx.py @@ -457,9 +457,6 @@ class MMGenTX: unit = getattr(self.proto.coin_amt,to_unit or 'min_coin_unit') return int(abs_fee // unit // self.estimate_size()) - def get_fee(self): - return self.sum_inputs() - self.sum_outputs() - def get_hex_locktime(self): return int(bytes.fromhex(self.hex[-8:])[::-1].hex(),16) @@ -577,7 +574,6 @@ class MMGenTX: # given tx size, rel fee and units, return absolute fee def fee_rel2abs(self,tx_size,units,amt,unit): - self.usr_rel_fee = None # TODO if tx_size: return self.proto.coin_amt(amt * tx_size * getattr(self.proto.coin_amt,units[unit])) else: @@ -1083,15 +1079,19 @@ class MMGenTX: + ''.join(format_io('inputs')) + ''.join(format_io('outputs')) ) + @property + def fee(self): + return self.sum_inputs() - self.sum_outputs() + def format_view_rel_fee(self,terse): return ' ({} {}, {} of spend amount)'.format( - pink(str(self.fee_abs2rel(self.get_fee()))), + pink(str(self.fee_abs2rel(self.fee))), self.rel_fee_disp, - pink('{:0.6f}%'.format( self.get_fee() / self.send_amt * 100 )) + pink('{:0.6f}%'.format( self.fee / self.send_amt * 100 )) ) def format_view_abs_fee(self): - return self.proto.coin_amt(self.get_fee()).hl() + return self.proto.coin_amt(self.fee).hl() def format_view_verbose_footer(self): tsize = len(self.hex)//2 if self.hex else 'unknown' @@ -1418,9 +1418,9 @@ class MMGenTX: die(2,'Transaction has Segwit outputs, but this blockchain does not support Segwit' + ' at the current height') - if self.get_fee() > self.proto.max_tx_fee: + if self.fee > self.proto.max_tx_fee: die(2,'Transaction fee ({}) greater than {} max_tx_fee ({} {})!'.format( - self.get_fee(), + self.fee, self.proto.name, self.proto.max_tx_fee, self.proto.coin )) diff --git a/mmgen/txfile.py b/mmgen/txfile.py index a84b0748..8e749986 100755 --- a/mmgen/txfile.py +++ b/mmgen/txfile.py @@ -141,7 +141,7 @@ class MMGenTxFile: yield '-' + tx.dcoin yield f'[{tx.send_amt!s}' if tx.is_replaceable(): - yield ',{}'.format(tx.fee_abs2rel(tx.get_fee(),to_unit=tx.fn_fee_unit)) + yield ',{}'.format(tx.fee_abs2rel(tx.fee,to_unit=tx.fn_fee_unit)) if tx.get_hex_locktime(): yield ',tl={}'.format(tx.get_hex_locktime()) yield ']'