diff --git a/mmgen/amt.py b/mmgen/amt.py index 71146b40..14cc35fc 100755 --- a/mmgen/amt.py +++ b/mmgen/amt.py @@ -66,12 +66,8 @@ class CoinAmt(Decimal, Hilite, InitErrors): # abstract class except Exception as e: return cls.init_fail(e, num) - def to_unit(self,unit,show_decimal=False): - ret = Decimal(self) // getattr(self,unit) - if show_decimal and ret < 1: - ret = f'{ret:.8f}'.rstrip('0') - return ret + '0' if ret.endswith('.') else ret - return int(ret) + def to_unit(self, unit): + return int(Decimal(self) // getattr(self, unit)) @classmethod def fmtc(cls, *args, **kwargs): diff --git a/mmgen/proto/eth/tx/base.py b/mmgen/proto/eth/tx/base.py index 349eebfa..bfcc2c70 100755 --- a/mmgen/proto/eth/tx/base.py +++ b/mmgen/proto/eth/tx/base.py @@ -29,11 +29,20 @@ class Base(TxBase.Base): usr_contract_data = HexStr('') disable_fee_check = False - # given absolute fee in ETH, return gas price in Gwei using self.gas + def pretty_fmt_fee(self, fee): + if fee < 1: + ret = f'{fee:.8f}'.rstrip('0') + return ret + '0' if ret.endswith('.') else ret + return str(int(fee)) + + # given absolute fee in ETH, return gas price for display in selected unit def fee_abs2rel(self, abs_fee, to_unit='Gwei'): - ret = self.proto.coin_amt(int(abs_fee.toWei() // self.gas.toWei()), from_unit='wei') - self.cfg._util.dmsg(f'fee_abs2rel() ==> {ret} ETH') - return ret if to_unit == 'eth' else ret.to_unit(to_unit, show_decimal=True) + return self.pretty_fmt_fee( + self.fee_abs2gas(abs_fee).to_unit(to_unit)) + + # given absolute fee in ETH, return gas price in ETH + def fee_abs2gas(self, abs_fee): + return self.proto.coin_amt(int(abs_fee.toWei() // self.gas.toWei()), from_unit='wei') # given rel fee (gasPrice) in wei, return absolute fee using self.gas (Ethereum-only method) def fee_gasPrice2abs(self, rel_fee): diff --git a/mmgen/proto/eth/tx/bump.py b/mmgen/proto/eth/tx/bump.py index 9253ca63..b11764f1 100755 --- a/mmgen/proto/eth/tx/bump.py +++ b/mmgen/proto/eth/tx/bump.py @@ -26,7 +26,7 @@ class Bump(Completed,New,TxBase.Bump): return self.proto.coin_amt(self.fee * Decimal('1.101')) def bump_fee(self,idx,fee): - self.txobj['gasPrice'] = self.fee_abs2rel(fee,to_unit='eth') + self.txobj['gasPrice'] = self.fee_abs2gas(fee) async def get_nonce(self): return self.txobj['nonce'] diff --git a/mmgen/proto/eth/tx/info.py b/mmgen/proto/eth/tx/info.py index 6323f9f6..e9cc87e3 100755 --- a/mmgen/proto/eth/tx/info.py +++ b/mmgen/proto/eth/tx/info.py @@ -54,8 +54,8 @@ class TxInfo(TxInfo): n = t['nonce'].hl(), d = '{}... ({} bytes)'.format(td[:40],len(td)//2) if len(td) else blue('None'), c = tx.proto.dcoin if len(tx.outputs) else '', - g = yellow(str(t['gasPrice'].to_unit('Gwei',show_decimal=True))), - G = yellow(str(t['startGas'].to_unit('Kwei'))), + g = yellow(tx.pretty_fmt_fee(t['gasPrice'].to_unit('Gwei'))), + G = yellow(tx.pretty_fmt_fee(t['startGas'].to_unit('Kwei'))), t_mmid = m['outputs'] if len(tx.outputs) else '', f_mmid = m['inputs']) + '\n\n' diff --git a/mmgen/proto/eth/tx/new.py b/mmgen/proto/eth/tx/new.py index a7ae5670..ba70351e 100755 --- a/mmgen/proto/eth/tx/new.py +++ b/mmgen/proto/eth/tx/new.py @@ -54,7 +54,7 @@ class New(Base,TxBase.New): 'from': self.inputs[0].addr, 'to': self.outputs[0].addr if self.outputs else None, 'amt': self.outputs[0].amt if self.outputs else self.proto.coin_amt('0'), - 'gasPrice': self.fee_abs2rel(self.usr_fee,to_unit='eth'), + 'gasPrice': self.fee_abs2gas(self.usr_fee), 'startGas': self.start_gas, 'nonce': await self.get_nonce(), 'chainId': self.rpc.chainID,