From c24315ff53b70c9b63c1317d51a4fe3cd031bbf4 Mon Sep 17 00:00:00 2001 From: jony376 Date: Thu, 28 May 2026 16:32:24 -0700 Subject: [PATCH] fix(rate): strip whitespace from swap rate strings in calculate_to_amount Co-authored-by: Cursor --- allways/utils/rate.py | 1 + tests/test_rate.py | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/allways/utils/rate.py b/allways/utils/rate.py index 38f3af6..1384313 100644 --- a/allways/utils/rate.py +++ b/allways/utils/rate.py @@ -37,6 +37,7 @@ def calculate_to_amount( to_decimals: Decimal places for canonical dest chain (e.g. 9 for TAO) from_decimals: Decimal places for canonical source chain (e.g. 8 for BTC) """ + rate = rate.strip() rate_fixed = int(Decimal(rate) * RATE_PRECISION) if rate_fixed == 0: return 0 diff --git a/tests/test_rate.py b/tests/test_rate.py index 372e195..6effa06 100644 --- a/tests/test_rate.py +++ b/tests/test_rate.py @@ -14,6 +14,16 @@ class TestBtcToTao: """BTC → TAO: forward direction, multiply by rate.""" + def test_rate_string_whitespace_stripped(self): + source = int(Decimal('0.01') * BTC_TO_SAT) + padded = calculate_to_amount( + source, ' 345 ', is_reverse=False, to_decimals=TAO_DEC, from_decimals=BTC_DEC + ) + plain = calculate_to_amount( + source, '345', is_reverse=False, to_decimals=TAO_DEC, from_decimals=BTC_DEC + ) + assert padded == plain + def test_standard_rate(self): # 0.01 BTC @ rate 345 (1 BTC = 345 TAO) → 3.45 TAO source = int(Decimal('0.01') * BTC_TO_SAT) # 1_000_000 sat