From eb80abaaee6572e031fea17e9d1b1edf1217ea6c Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 17 Mar 2025 19:16:46 +0300 Subject: [PATCH] amt.py: new `UniAmt` type, equivalent to `BTCAmt` --- mmgen/amt.py | 3 +++ mmgen/swap/proto/thorchain/memo.py | 5 +++-- mmgen/swap/proto/thorchain/thornode.py | 9 +++++---- mmgen/tx/new_swap.py | 7 ++++--- test/cmdtest_d/httpd/thornode.py | 3 ++- test/modtest_d/tx.py | 5 +++-- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/mmgen/amt.py b/mmgen/amt.py index e066d1b8..2b80c5b5 100755 --- a/mmgen/amt.py +++ b/mmgen/amt.py @@ -173,6 +173,9 @@ class BTCAmt(CoinAmt): atomic = satoshi units = ('satoshi',) +class UniAmt(BTCAmt): + coin = None + class BCHAmt(BTCAmt): coin = 'BCH' diff --git a/mmgen/swap/proto/thorchain/memo.py b/mmgen/swap/proto/thorchain/memo.py index 6f509fdd..7e73aa27 100755 --- a/mmgen/swap/proto/thorchain/memo.py +++ b/mmgen/swap/proto/thorchain/memo.py @@ -13,6 +13,7 @@ swap.proto.thorchain.memo: THORChain swap protocol memo class """ from ....util import die +from ....amt import UniAmt from . import name as proto_name @@ -125,9 +126,9 @@ class Memo: self.proto = proto self.chain = chain or proto.coin if trade_limit is None: - self.trade_limit = self.proto.coin_amt('0') + self.trade_limit = UniAmt('0') else: - assert type(trade_limit) is self.proto.coin_amt, f'{type(trade_limit)} != {self.proto.coin_amt}' + assert type(trade_limit) is UniAmt, f'{type(trade_limit)} != {UniAmt}' self.trade_limit = trade_limit from ....addr import is_coin_addr assert is_coin_addr(proto, addr) diff --git a/mmgen/swap/proto/thorchain/thornode.py b/mmgen/swap/proto/thorchain/thornode.py index 78d8a024..ca0eeb5d 100755 --- a/mmgen/swap/proto/thorchain/thornode.py +++ b/mmgen/swap/proto/thorchain/thornode.py @@ -13,6 +13,7 @@ swap.proto.thorchain.thornode: THORChain swap protocol network query ops """ import json +from ....amt import UniAmt class ThornodeRPCClient: @@ -73,12 +74,12 @@ class Thornode: in_coin = tx.send_proto.coin out_coin = tx.recv_proto.coin in_amt = self.in_amt - out_amt = tx.recv_proto.coin_amt(int(d['expected_amount_out']), from_unit='satoshi') + out_amt = UniAmt(int(d['expected_amount_out']), from_unit='satoshi') if trade_limit: from . import ExpInt4 e = ExpInt4(trade_limit.to_unit('satoshi')) - tl_rounded = tx.recv_proto.coin_amt(e.trunc, from_unit='satoshi') + tl_rounded = UniAmt(e.trunc, from_unit='satoshi') ratio = usr_trade_limit if type(usr_trade_limit) is float else float(tl_rounded / out_amt) direction = 'ABOVE' if ratio > 1 else 'below' mcolor, lblcolor = ( @@ -102,13 +103,13 @@ class Thornode: else: ymsg('Warning: unknown gas unit ‘{}’, cannot estimate fee'.format(d['gas_rate_units'])) - min_in_amt = tx.send_proto.coin_amt(int(d['recommended_min_amount_in']), from_unit='satoshi') + min_in_amt = UniAmt(int(d['recommended_min_amount_in']), from_unit='satoshi') gas_unit = { 'satsperbyte': 'sat/byte', }.get(d['gas_rate_units'], d['gas_rate_units']) elapsed_disp = format_elapsed_hr(d['expiry'], future_msg='from now') fees = d['fees'] - fees_t = tx.recv_proto.coin_amt(int(fees['total']), from_unit='satoshi') + fees_t = UniAmt(int(fees['total']), from_unit='satoshi') fees_pct_disp = str(fees['total_bps'] / 100) + '%' slip_pct_disp = str(fees['slippage_bps'] / 100) + '%' hdr = f'SWAP QUOTE (source: {self.rpc.host})' diff --git a/mmgen/tx/new_swap.py b/mmgen/tx/new_swap.py index 665029f3..7fa0eb27 100755 --- a/mmgen/tx/new_swap.py +++ b/mmgen/tx/new_swap.py @@ -13,6 +13,7 @@ tx.new_swap: new swap transaction class """ from .new import New +from ..amt import UniAmt class NewSwap(New): desc = 'swap transaction' @@ -28,7 +29,7 @@ class NewSwap(New): if s := self.cfg.trade_limit: self.usr_trade_limit = ( 1 - float(s[:-1]) / 100 if s.endswith('%') else - self.recv_proto.coin_amt(self.cfg.trade_limit)) + UniAmt(self.cfg.trade_limit)) else: self.usr_trade_limit = None @@ -40,11 +41,11 @@ class NewSwap(New): from ..term import get_char def get_trade_limit(): - if type(self.usr_trade_limit) is self.recv_proto.coin_amt: + if type(self.usr_trade_limit) is UniAmt: return self.usr_trade_limit elif type(self.usr_trade_limit) is float: return ( - self.recv_proto.coin_amt(int(c.data['expected_amount_out']), from_unit='satoshi') + UniAmt(int(c.data['expected_amount_out']), from_unit='satoshi') * self.usr_trade_limit) while True: diff --git a/test/cmdtest_d/httpd/thornode.py b/test/cmdtest_d/httpd/thornode.py index 131eaa59..7c770496 100755 --- a/test/cmdtest_d/httpd/thornode.py +++ b/test/cmdtest_d/httpd/thornode.py @@ -15,6 +15,7 @@ test.cmdtest_d.httpd.thornode: Thornode WSGI http server import time, re, json from mmgen.cfg import Config +from mmgen.amt import UniAmt from . import HTTPD @@ -75,7 +76,7 @@ class ThornodeServer(HTTPD): from mmgen.protocol import init_proto send_proto = init_proto(cfg, send_coin, network='regtest', need_amt=True) - in_amt = send_proto.coin_amt(int(amt_atomic), from_unit='satoshi') + in_amt = UniAmt(int(amt_atomic), from_unit='satoshi') out_amt = in_amt * (prices[send_coin] / prices[recv_coin]) addr = make_inbound_addr(send_proto, send_proto.preferred_mmtypes[0]) diff --git a/test/modtest_d/tx.py b/test/modtest_d/tx.py index 7a5cd490..7a765604 100755 --- a/test/modtest_d/tx.py +++ b/test/modtest_d/tx.py @@ -170,6 +170,7 @@ class unit_tests: def memo(self, name, ut, desc='Swap transaction memo'): from mmgen.protocol import init_proto + from mmgen.amt import UniAmt from mmgen.swap.proto.thorchain.memo import Memo for coin, addrtype in ( ('ltc', 'bech32'), @@ -186,13 +187,13 @@ class unit_tests: (None, 0), ): vmsg('\nTesting memo initialization:') - m = Memo(proto, addr, trade_limit=proto.coin_amt(limit) if limit else None) + m = Memo(proto, addr, trade_limit=UniAmt(limit) if limit else None) vmsg(f'str(memo): {m}') vmsg(f'repr(memo): {m!r}') vmsg(f'limit: {limit}') p = Memo.parse(m) - limit_dec = proto.coin_amt(p.trade_limit, from_unit='satoshi') + limit_dec = UniAmt(p.trade_limit, from_unit='satoshi') vmsg(f'limit_dec: {limit_dec.hl()}') vmsg('\nTesting memo parsing:')