SwapAsset: new sanitize_input param

This commit is contained in:
The MMGen Project 2026-10-12 10:04:48 +00:00
commit fcb5924fe8
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
3 changed files with 10 additions and 5 deletions

View file

@ -142,8 +142,8 @@ class THORChainRemoteRPCClient(RemoteRPCClient):
def quote(self, send_asset: str, recv_asset: str, amt: Decimal):
"get a swap quote for a given send asset, receive asset and amount"
from ....swap.util import init_swap_proto, get_swap_asset
from_asset = get_swap_asset(self.cfg, send_asset, direction='send')
to_asset = get_swap_asset(self.cfg, recv_asset, direction='recv')
from_asset = get_swap_asset(self.cfg, send_asset, direction='send', sanitize_input=True)
to_asset = get_swap_asset(self.cfg, recv_asset, direction='recv', sanitize_input=True)
send_amt = init_swap_proto(self.cfg, from_asset).coin_amt(str(amt))
return self.do_quote(from_asset, to_asset, send_amt)

View file

@ -93,7 +93,12 @@ class SwapAsset:
def memo_asset_name(self):
return self.data.abbr or self.data.full_name
def __init__(self, name, direction):
def __init__(self, name, direction, sanitize_input=False):
if sanitize_input:
name = name.upper()
if '.' in name and (ss := name.split('.', 1)) and ss[0] == ss[1]: # BTC.BTC -> BTC
name = ss[0]
if name not in self.assets_data:
die('SwapAssetError', f'{name!r}: unrecognized asset')

View file

@ -27,7 +27,7 @@ def init_swap_proto(cfg, asset):
tokensym = asset.tokensym,
need_amt = True)
def get_swap_asset(cfg, asset, *, direction='send'):
def get_swap_asset(cfg, asset, *, direction='send', sanitize_input=False):
"given an asset symbol, and optionally direction, return a swap asset instance"
assert isinstance(asset, str), '‘asset’ must be a string'
return get_swap_proto_mod(cfg.swap_proto).SwapAsset(asset, direction)
return get_swap_proto_mod(cfg.swap_proto).SwapAsset(asset, direction, sanitize_input=sanitize_input)