mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-01-10 15:30:55 +00:00
src/trezor: refactor format_amount into utils, use it where possible
This commit is contained in:
parent
340f926804
commit
fedfde7e99
@ -1,6 +1,6 @@
|
||||
from apps.common.confirm import *
|
||||
from trezor import wire, ui
|
||||
from trezor.utils import chunks
|
||||
from trezor.utils import chunks, format_amount
|
||||
from trezor.messages import ButtonRequestType
|
||||
from trezor.ui.text import Text
|
||||
from ubinascii import hexlify
|
||||
@ -10,7 +10,7 @@ from . import networks
|
||||
async def confirm_tx(ctx, to, value, chain_id, token=None): # TODO: wording
|
||||
str_to = '0x' + hexlify(to).decode() # TODO: use ethereum address format
|
||||
content = Text('Confirm transaction', ui.ICON_RESET,
|
||||
ui.BOLD, format_amount(value, token, chain_id),
|
||||
ui.BOLD, format_ethereum_amount(value, token, chain_id),
|
||||
ui.NORMAL, 'to',
|
||||
ui.MONO, *split_address(str_to))
|
||||
return await confirm(ctx, content, ButtonRequestType.SignTx) # we use SignTx, not ConfirmOutput, for compatibility with T1
|
||||
@ -18,9 +18,9 @@ async def confirm_tx(ctx, to, value, chain_id, token=None): # TODO: wording
|
||||
|
||||
async def confirm_fee(ctx, spending, gas_price, gas_limit, chain_id, token=None): # TODO: wording
|
||||
content = Text('Confirm fee', ui.ICON_RESET,
|
||||
'Sending: %s' % format_amount(spending, token, chain_id),
|
||||
'Gas: %s' % format_amount(gas_price, token, chain_id),
|
||||
'Limit: %s' % format_amount(gas_limit, token, chain_id))
|
||||
'Sending: %s' % format_ethereum_amount(spending, token, chain_id),
|
||||
'Gas: %s' % format_ethereum_amount(gas_price, token, chain_id),
|
||||
'Limit: %s' % format_ethereum_amount(gas_limit, token, chain_id))
|
||||
return await hold_to_confirm(ctx, content, ButtonRequestType.SignTx)
|
||||
|
||||
|
||||
@ -36,7 +36,7 @@ def split_address(address):
|
||||
return chunks(address, 17)
|
||||
|
||||
|
||||
def format_amount(value, token, chain_id):
|
||||
def format_ethereum_amount(value, token, chain_id):
|
||||
value = int.from_bytes(value, 'big')
|
||||
if token:
|
||||
suffix = token.ticker
|
||||
@ -48,8 +48,4 @@ def format_amount(value, token, chain_id):
|
||||
decimals = 18
|
||||
suffix = networks.suffix_by_chain_id(chain_id)
|
||||
|
||||
d = pow(10, decimals)
|
||||
value = ('%d.%0*d' % (value // d , decimals, value % d)).rstrip('0')
|
||||
if value.endswith('.'):
|
||||
value += '0'
|
||||
return '%s %s' % (value, suffix)
|
||||
return '%s %s' % (format_amount(value, decimals), suffix)
|
||||
|
@ -1,5 +1,5 @@
|
||||
from trezor import ui
|
||||
from trezor.utils import chunks
|
||||
from trezor.utils import chunks, format_amount
|
||||
from trezor.ui.text import Text
|
||||
from trezor.messages import ButtonRequestType
|
||||
from trezor.messages import OutputScriptType
|
||||
@ -7,12 +7,8 @@ from apps.common.confirm import confirm
|
||||
from apps.common.confirm import hold_to_confirm
|
||||
|
||||
|
||||
def format_amount(amount, coin):
|
||||
d = pow(10, 8)
|
||||
amount = ('%d.%08d' % (amount // d , amount % d)).rstrip('0')
|
||||
if amount.endswith('.'):
|
||||
amount += '0'
|
||||
return '%s %s' % (amount, coin.coin_shortcut)
|
||||
def format_coin_amount(amount, coin):
|
||||
return '%s %s' % (format_amount(amount, 8), coin.coin_shortcut)
|
||||
|
||||
|
||||
def split_address(address):
|
||||
@ -25,7 +21,7 @@ async def confirm_output(ctx, output, coin):
|
||||
else:
|
||||
address = output.address
|
||||
content = Text('Confirm output', ui.ICON_RESET,
|
||||
ui.BOLD, format_amount(output.amount, coin),
|
||||
ui.BOLD, format_coin_amount(output.amount, coin),
|
||||
ui.NORMAL, 'to',
|
||||
ui.MONO, *split_address(address))
|
||||
return await confirm(ctx, content, ButtonRequestType.ConfirmOutput)
|
||||
@ -33,12 +29,12 @@ async def confirm_output(ctx, output, coin):
|
||||
|
||||
async def confirm_total(ctx, spending, fee, coin):
|
||||
content = Text('Confirm transaction', ui.ICON_RESET,
|
||||
'Sending: %s' % format_amount(spending, coin),
|
||||
'Fee: %s' % format_amount(fee, coin))
|
||||
'Sending: %s' % format_coin_amount(spending, coin),
|
||||
'Fee: %s' % format_coin_amount(fee, coin))
|
||||
return await hold_to_confirm(ctx, content, ButtonRequestType.SignTx)
|
||||
|
||||
|
||||
async def confirm_feeoverthreshold(ctx, fee, coin):
|
||||
content = Text('Confirm high fee:', ui.ICON_RESET,
|
||||
ui.BOLD, format_amount(fee, coin))
|
||||
ui.BOLD, format_coin_amount(fee, coin))
|
||||
return await confirm(ctx, content, ButtonRequestType.FeeOverThreshold)
|
||||
|
@ -26,3 +26,11 @@ def chunks(items, size):
|
||||
def ensure(cond):
|
||||
if not cond:
|
||||
raise AssertionError()
|
||||
|
||||
|
||||
def format_amount(amount, decimals):
|
||||
d = pow(10, decimals)
|
||||
amount = ('%d.%0*d' % (amount // d, decimals, amount % d)).rstrip('0')
|
||||
if amount.endswith('.'):
|
||||
amount += '0'
|
||||
return amount
|
||||
|
Loading…
Reference in New Issue
Block a user