diff --git a/allways/cli/swap_commands/helpers.py b/allways/cli/swap_commands/helpers.py index 26293e96..fb316dbe 100644 --- a/allways/cli/swap_commands/helpers.py +++ b/allways/cli/swap_commands/helpers.py @@ -171,6 +171,27 @@ def from_rao(amount_rao: int) -> float: return amount_rao / TAO_TO_RAO +def to_smallest_unit(amount: float, chain_id: str) -> int: + """Convert a human-readable amount to the smallest unit for a chain. + + Uses Decimal to avoid IEEE 754 float artifacts (e.g. 0.1 * 10^9 = 99999999). + """ + from decimal import Decimal + + from allways.chain_defs import get_chain + + chain = get_chain(chain_id) + return int(Decimal(str(amount)) * (10**chain.decimals)) + + +def from_smallest_unit(amount: int, chain_id: str) -> float: + """Convert from smallest unit to human-readable amount.""" + from allways.chain_defs import get_chain + + chain = get_chain(chain_id) + return amount / (10**chain.decimals) + + def load_cli_config() -> dict: """Load CLI configuration from ~/.allways/config.json.""" if not CONFIG_FILE.exists(): diff --git a/allways/cli/swap_commands/quote.py b/allways/cli/swap_commands/quote.py index 5a452f26..19f00b9d 100644 --- a/allways/cli/swap_commands/quote.py +++ b/allways/cli/swap_commands/quote.py @@ -1,7 +1,5 @@ """alw swap quote - Preview rates and estimated receive amounts before swapping.""" -from decimal import Decimal - import click from rich.table import Table @@ -13,6 +11,7 @@ get_cli_context, loading, read_miner_commitments, + to_smallest_unit, ) from allways.constants import FEE_DIVISOR from allways.contract_client import ContractError @@ -73,8 +72,7 @@ def quote_command(from_chain: str, to_chain: str, amount: float): netuid = config['netuid'] # Convert to smallest units - src_chain_def = get_chain(from_chain) - from_amount = int(Decimal(str(amount)) * (10**src_chain_def.decimals)) + from_amount = to_smallest_unit(amount, from_chain) fee_divisor = FEE_DIVISOR fee_pct = 100 / fee_divisor diff --git a/allways/cli/swap_commands/swap.py b/allways/cli/swap_commands/swap.py index 37729845..a9470acb 100644 --- a/allways/cli/swap_commands/swap.py +++ b/allways/cli/swap_commands/swap.py @@ -15,6 +15,8 @@ from allways.classes import SwapStatus from allways.cli.dendrite_lite import broadcast_synapse, discover_validators, get_ephemeral_wallet from allways.cli.help import StyledGroup + +# to_smallest_unit and from_smallest_unit moved to helpers.py from allways.cli.swap_commands.helpers import ( PendingSwapState, blocks_to_minutes_str, @@ -22,6 +24,7 @@ console, find_matching_miners, from_rao, + from_smallest_unit, get_cli_context, is_local_network, load_pending_swap, @@ -31,6 +34,7 @@ resolve_source_tx_block, save_pending_swap, sign_or_prompt_external, + to_smallest_unit, ) from allways.cli.validator_rejections import RejectionInfo, render_and_aggregate from allways.commitments import read_miner_commitments @@ -40,24 +44,6 @@ from allways.utils.proofs import reserve_proof_message, swap_proof_message from allways.utils.rate import apply_fee_deduction, calculate_to_amount, check_swap_viability, derive_tao_leg - -def to_smallest_unit(amount: float, chain_id: str) -> int: - """Convert a human-readable amount to the smallest unit for a chain. - - Uses Decimal to avoid IEEE 754 float artifacts (e.g. 0.1 * 10^9 = 99999999). - """ - from decimal import Decimal - - chain = get_chain(chain_id) - return int(Decimal(str(amount)) * (10**chain.decimals)) - - -def from_smallest_unit(amount: int, chain_id: str) -> float: - """Convert from smallest unit to human-readable amount.""" - chain = get_chain(chain_id) - return amount / (10**chain.decimals) - - # ========================================================================= # Shared functions (used by swap command, post_tx command) # =========================================================================