tx.base: new swap_proto_mod, send_asset, recv_asset properties

This commit is contained in:
The MMGen Project 2025-04-21 14:01:16 +00:00
commit 6649fab18b
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
7 changed files with 30 additions and 30 deletions

View file

@ -13,17 +13,15 @@ proto.btc.tx.new_swap: Bitcoin new swap transaction class
"""
from ....tx.new_swap import NewSwap as TxNewSwap
from ....tx.new_swap import get_swap_proto_mod
from .new import New
class NewSwap(New, TxNewSwap):
desc = 'Bitcoin swap transaction'
def update_data_output(self, trade_limit):
sp = get_swap_proto_mod(self.swap_proto)
o = self.data_output._asdict()
parsed_memo = sp.Memo.parse(o['data'].decode())
memo = sp.Memo(
parsed_memo = self.swap_proto_mod.Memo.parse(o['data'].decode())
memo = self.swap_proto_mod.Memo(
self.recv_proto,
self.recv_asset,
self.recv_proto.coin_addr(parsed_memo.address),

View file

@ -13,17 +13,15 @@ proto.eth.tx.new_swap: Ethereum new swap transaction class
"""
from ....tx.new_swap import NewSwap as TxNewSwap
from ....tx.new_swap import get_swap_proto_mod
from .new import New
class NewSwap(New, TxNewSwap):
desc = 'Ethereum swap transaction'
def update_data_output(self, trade_limit):
sp = get_swap_proto_mod(self.swap_proto)
data = bytes.fromhex(self.txobj['data']) if self.is_bump else self.usr_contract_data
parsed_memo = sp.Memo.parse(data.decode())
memo = sp.Memo(
parsed_memo = self.swap_proto_mod.Memo.parse(data.decode())
memo = self.swap_proto_mod.Memo(
self.recv_proto,
self.recv_asset,
self.recv_proto.coin_addr(parsed_memo.address),

View file

@ -26,7 +26,7 @@ from ..obj import (
)
from ..amt import CoinAmtChk
from ..addr import MMGenID, CoinAddr
from ..util import msg, ymsg, fmt, remove_dups, make_timestamp, die
from ..util import msg, ymsg, fmt, remove_dups, make_timestamp, die, cached_property
class MMGenTxIO(MMGenListItem):
vout = ListItemAttr(NonNegativeInt)
@ -220,3 +220,19 @@ class Base(MMGenObject):
if not self.cfg.yes:
from ..ui import keypress_confirm
keypress_confirm(self.cfg, 'Continue?', default_yes=True, do_exit=True)
# swap methods:
@cached_property
def swap_proto_mod(self):
from .new_swap import get_swap_proto_mod
return get_swap_proto_mod(self.swap_proto)
@cached_property
def send_asset(self):
spec = self.proto.coin + (f'.{self.proto.tokensym}' if self.proto.tokensym else '')
return self.swap_proto_mod.SwapAsset(spec, 'send')
@cached_property
def recv_asset(self):
return self.swap_proto_mod.SwapAsset(self.swap_recv_asset_spec, 'recv')

View file

@ -12,7 +12,7 @@
tx.bump: transaction bump class
"""
from .new_swap import NewSwap, get_swap_proto_mod
from .new_swap import NewSwap
from .completed import Completed
from ..util import msg, ymsg, is_int, die
from ..color import pink
@ -36,16 +36,11 @@ class Bump(Completed, NewSwap):
self.new_outputs = new_outputs
self.orig_rel_fee = self.get_orig_rel_fee()
if self.is_swap:
if new_outputs:
if new_outputs:
if self.is_swap:
from .base import Base
for attr in self.swap_attrs:
setattr(self, attr, getattr(Base, attr))
else:
sp = get_swap_proto_mod(self.swap_proto)
self.recv_asset = sp.SwapAsset(self.swap_recv_asset_spec, 'recv')
if new_outputs:
self.outputs = self.OutputList(self)
self.cfg = kwargs['cfg'] # must use current cfg opts, not those from orig_tx

View file

@ -77,6 +77,8 @@ class Completed(Base):
text = data.decode('ascii')
p = Memo.parse(text)
assert p.function == 'SWAP', f'{p.function}’: unsupported function in swap memo ‘{text}'
aname = p.chain + (f'.{p.asset}' if p.asset != p.chain else '')
assert aname == self.recv_asset.name, f'invalid memo: {aname} != {self.recv_asset.name}'
assert p.chain == p.asset, f'{p.chain} != {p.asset}: chain/asset mismatch in swap memo ‘{text}'
proto = init_proto(self.cfg, p.asset, network=self.cfg.network, need_amt=True)
if self.swap_recv_addr_mmid:

View file

@ -80,7 +80,7 @@ class TxInfo:
if Memo.is_partial_memo(data):
p = Memo.parse(data.decode('ascii'))
yield ' {} {}\n'.format(magenta('DEX Protocol:'), blue(name))
yield ' Swap: {}\n'.format(orange(f'{tx.proto.coin} => {p.asset}'))
yield ' Swap: {}\n'.format(orange(f'{tx.send_asset.name} => {tx.recv_asset.name}'))
yield ' Dest: {}{}\n'.format(
cyan(p.address),
orange(f' ({tx.swap_recv_addr_mmid})') if tx.swap_recv_addr_mmid else '')

View file

@ -35,11 +35,7 @@ def get_send_proto(cfg):
arg = cfg._args.pop(0)
except:
cfg._usage()
global send_asset
send_asset = get_swap_proto_mod(cfg.swap_proto).SwapAsset(arg, 'send')
return init_swap_proto(cfg, send_asset)
return init_swap_proto(cfg, get_swap_proto_mod(cfg.swap_proto).SwapAsset(arg, 'send'))
class NewSwap(New):
desc = 'swap transaction'
@ -110,7 +106,6 @@ class NewSwap(New):
# arg 4: recv_coin
self.swap_recv_asset_spec = arg # this goes into the transaction file
self.recv_asset = sp.SwapAsset(arg, 'recv')
self.recv_proto = init_swap_proto(self.cfg, self.recv_asset)
# arg 5: recv_spec (receive address spec)
@ -120,7 +115,7 @@ class NewSwap(New):
if args_in: # done parsing, all args consumed
self.cfg._usage()
sp = get_swap_proto_mod(self.swap_proto)
sp = self.swap_proto_mod
args_in = list(cmd_args)
args = CmdlineArgs()
parse()
@ -149,9 +144,6 @@ class NewSwap(New):
memo = sp.Memo(self.recv_proto, self.recv_asset, recv_output.addr)
self.is_token_swap = self.proto.tokensym or self.recv_asset.asset
self.send_asset = send_asset
# this goes into the transaction file:
self.swap_recv_addr_mmid = recv_output.mmid
@ -176,8 +168,7 @@ class NewSwap(New):
self.outputs[vault_idx] = self.Output(self.proto, **o)
async def update_vault_output(self, amt, *, deduct_est_fee=False):
sp = get_swap_proto_mod(self.swap_proto)
c = sp.rpc_client(self, amt)
c = self.swap_proto_mod.rpc_client(self, amt)
from ..util import msg
from ..term import get_char