proto.rune.tx.protobuf: replace build_swap_tx() function

This commit is contained in:
The MMGen Project 2026-09-29 09:23:31 +00:00
commit 4e825a58ef
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
4 changed files with 66 additions and 102 deletions

View file

@ -1 +1 @@
16.3.0dev10
16.3.0dev11

View file

@ -171,21 +171,21 @@ def tx_info(tx, proto):
b = tx.body.messages[0].body
s = tx.authInfo.signerInfos[0]
fee = parse_fee(proto, tx)
match msg_type := tx.body.messages[0].id.removeprefix('/types.'):
case 'MsgSend':
match msg_type := type(tx.body.messages[0]):
case Messages.MsgSend:
from_addr = proto.encode_addr_bech32x(b.fromAddress)
to_addr = proto.encode_addr_bech32x(b.toAddress)
asset = b.amount[0].denom.upper()
memo = tx.body.memo
amt = base_unit_to_amt(int(b.amount[0].amount), decimals=8)
case 'MsgDeposit':
case Messages.MsgDeposit:
from_addr = proto.encode_addr_bech32x(b.signer)
to_addr = 'None'
asset = b.coins[0].asset.symbol
memo = b.memo
amt = base_unit_to_amt(int(b.coins[0].amount), decimals=b.coins[0].decimals or 8)
yield f'TxID: {tx.txid}'
yield f'Type: {msg_type}'
yield f'Type: {msg_type.__name__}'
yield f'From: {from_addr}'
yield f'To: {to_addr}'
yield f'Asset: {asset}'
@ -196,40 +196,9 @@ def tx_info(tx, proto):
yield f'Memo: {memo}'
yield f'Pubkey: {s.publicKey.key.data.hex()}'
def build_swap_tx(cfg, proto, parms, *, skip_body_memo=False):
p = parms
assert type(p) == swap_tx_parms, f'{p}: invalid ‘parms’ (not swap_tx_parms instance)'
return build_tx(
cfg,
proto,
deposit_tx_parms(
chain = 'THOR',
symbol = 'RUNE',
ticker = 'RUNE',
from_addr = p.from_addr,
amt = p.amt,
gas_limit = p.gas_limit,
account_number = p.account_number,
sequence = p.sequence,
decimals = 8,
fee = p.fee,
synth = p.synth,
trade = p.trade,
secured = p.secured,
memo = p.memo,
pubkey = p.pubkey,
wifkey = p.wifkey,
signature = p.signature),
skip_body_memo = skip_body_memo)
def build_tx(cfg, proto, parms, *, null_fee=False, skip_body_memo=False):
p = parms
assert type(p) in (send_tx_parms, deposit_tx_parms), f'{p}: invalid ‘parms’ (not *_tx_parms instance)'
msg_type = 'MsgSend' if type(p) == send_tx_parms else 'MsgDeposit'
if p.wifkey:
assert p.pubkey is None and p.signature is None
@ -241,34 +210,48 @@ def build_tx(cfg, proto, parms, *, null_fee=False, skip_body_memo=False):
assert p.pubkey and p.signature
pubkey = p.pubkey
cls = getattr(Messages, msg_type)
match type(p).__name__:
case 'rune_send_tx_parms':
message = Messages.MsgSend(
id = '/types.MsgSend',
body = Messages.MsgSend.Body(
fromAddress = proto.decode_addr(p.from_addr).bytes,
toAddress = proto.decode_addr(p.to_addr).bytes,
amount = [Coin(denom='rune', amount=str(amt_to_base_unit(p.amt, decimals=8)))]))
fee_amt = None
case 'rune_deposit_tx_parms':
coin_data = CoinWithAsset(
asset = Asset(
chain = p.chain,
symbol = p.symbol,
ticker = p.ticker,
synth = p.synth,
trade = p.trade,
secured = p.secured),
amount = str(amt_to_base_unit(p.amt, decimals=p.decimals or 8)),
decimals = p.decimals)
case 'rune_swap_tx_parms':
coin_data = CoinWithAsset(
asset = Asset(
chain = 'THOR',
symbol = 'RUNE',
ticker = 'RUNE',
synth = p.synth,
trade = p.trade,
secured = p.secured),
amount = str(amt_to_base_unit(p.amt, decimals=8)),
decimals = 8)
match type(p).__name__:
case 'rune_deposit_tx_parms' | 'rune_swap_tx_parms':
message = Messages.MsgDeposit(
id = '/types.MsgDeposit',
body = Messages.MsgDeposit.Body(
coins = [coin_data],
memo = p.memo,
signer = proto.decode_addr(p.from_addr).bytes))
fee_amt = None if null_fee else [Coin(denom='rune', amount=str(p.fee))]
if msg_type == 'MsgSend':
message = cls(
id = '/types.MsgSend',
body = cls.Body(
fromAddress = proto.decode_addr(p.from_addr).bytes,
toAddress = proto.decode_addr(p.to_addr).bytes,
amount = [Coin(denom='rune', amount=str(amt_to_base_unit(p.amt, decimals=8)))]))
fee_amt = None
elif msg_type == 'MsgDeposit':
coin_data = CoinWithAsset(
asset = Asset(
chain = p.chain,
symbol = p.symbol,
ticker = p.ticker,
synth = p.synth,
trade = p.trade,
secured = p.secured),
amount = str(amt_to_base_unit(p.amt, decimals=p.decimals or 8)),
decimals = p.decimals)
message = cls(
id = '/types.MsgDeposit',
body = cls.Body(
coins = [coin_data],
memo = p.memo,
signer = proto.decode_addr(p.from_addr).bytes))
fee_amt = None if null_fee else [Coin(denom='rune', amount=str(p.fee))]
signer_info = SignerInfo(
publicKey = PublicKey(

View file

@ -35,30 +35,24 @@ class Unsigned(VmUnsigned, Completed, TxBase.Unsigned):
'chain_id': d['chain_id']}
async def do_sign(self, o, wif):
if self.is_swap:
from .protobuf import swap_tx_parms, build_swap_tx as build_tx
parms = swap_tx_parms(
o['from'],
o['amt'],
o['gas'],
o['account_number'],
o['sequence'],
self.swap_memo,
fee = 0 if o['fee'] is None else o['fee'].to_unit('atomic'),
wifkey = wif)
else:
from .protobuf import send_tx_parms, build_tx
parms = send_tx_parms(
o['from'],
o['to'],
o['amt'],
o['gas'],
o['account_number'],
o['sequence'],
fee = 0 if o['fee'] is None else o['fee'].to_unit('atomic'),
wifkey = wif)
parms = {
'from_addr': o['from'],
'amt': o['amt'],
'gas_limit': o['gas'],
'account_number': o['account_number'],
'sequence': o['sequence'],
'fee': 0 if o['fee'] is None else o['fee'].to_unit('atomic'),
'wifkey': wif}
tx = build_tx(self.cfg, self.proto, parms)
if self.is_swap:
from .protobuf import swap_tx_parms as tx_parms
add_parms = {'memo': self.swap_memo}
else:
from .protobuf import send_tx_parms as tx_parms
add_parms = {'to_addr': o['to']}
from .protobuf import build_tx
tx = build_tx(self.cfg, self.proto, tx_parms(**(parms | add_parms)))
self.serialized = bytes(tx).hex()
self.coin_txid = CoinTxID(tx.txid)
tx.verify_sig(self.cfg, self.proto, o['account_number'])

View file

@ -11,7 +11,6 @@ from mmgen.proto.cosmos.tx.protobuf import RawTx
from mmgen.proto.rune.tx.protobuf import (
RuneTx,
build_tx,
build_swap_tx,
tx_info,
send_tx_parms,
deposit_tx_parms,
@ -40,14 +39,12 @@ parse_vectors = [
_pv(
'mainnet-tx-msgdeposit1.binpb',
'1089bbd54746bbc6a40e264d3ce8085561978739094c9c5aac59c569b4c28ba9',
deposit_tx_parms(
'THOR', 'RUNE', 'RUNE',
swap_tx_parms(
'thor1lukwlve7hayy66qrdkp4k7sh0emjqwergy7tl3',
'299.23861844',
600000000,
125632,
348388,
decimals = 8,
memo = '=:LTC~LTC:thor1lukwlve7hayy66qrdkp4k7sh0emjqwergy7tl3:605926421/0/1',
pubkey = '03da157f891abfe7822efb91f59667aa6cc6c3768a7e280caeb9ae243c969eb3e7',
signature = '869399bcc2ccb9c9c286bdf214439ad132221cb8206547ceb012e06efbc3ff3e' # r
@ -115,11 +112,8 @@ build_vectors = [
37,
decimals = 8,
memo = '=:MEMO',
wifkey = 'Ky9bSjPUD35uUaY3JReXiESivnfxV6rLMsW1wTFyvVZwYXpX95vF'))
]
swap_build_vectors = [
_bv(
wifkey = 'Ky9bSjPUD35uUaY3JReXiESivnfxV6rLMsW1wTFyvVZwYXpX95vF')),
_bv( # same as above, only via swap_tx_parms()
'0d41e0ee40cd18a991cd8f0ef0e60e4c5bea898c53d54e00b6dddc0c9ce7edb7',
'444e026fe5d0988da602dc22f0ff6172c080f1d2e2d66012d86f4afd314b78d6',
swap_tx_parms(
@ -141,7 +135,7 @@ def test_tx(src, cfg, vec):
pubkey = bytes.fromhex(parms.pubkey),
signature = bytes.fromhex(parms.signature))
assert src in ('parse', 'build', 'swapbuild')
assert src in ('parse', 'build')
match src:
case 'parse':
@ -153,8 +147,6 @@ def test_tx(src, cfg, vec):
assert bytes(tx) == tx_in
case 'build':
tx = build_tx(cfg, proto, parms, null_fee=vec.null_fee)
case 'swapbuild':
tx = build_swap_tx(cfg, proto, parms)
vmsg(pp_fmt(tx))
@ -198,11 +190,6 @@ class unit_tests:
test_tx('build', test_cfg, vec)
return True
def swaptxbuild(self, name, ut, desc='transaction building and signing (Swap TX)'):
for vec in swap_build_vectors:
test_tx('swapbuild', test_cfg, vec)
return True
def rpc(self, name, ut, desc='remote RPC operations'):
import asyncio
from mmgen.rpc import rpc_init