proto.eth.tx: fee display cleanups

This commit is contained in:
The MMGen Project 2024-10-18 10:32:04 +00:00
commit 8ddd94c125
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
5 changed files with 19 additions and 14 deletions

View file

@ -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):

View file

@ -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):

View file

@ -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']

View file

@ -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'

View file

@ -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,