swap: minor changes and cleanups

This commit is contained in:
The MMGen Project 2025-04-21 14:01:15 +00:00
commit 6473b41d43
Signed by: mmgen
GPG key ID: 3F8B1861E32B7DA2
5 changed files with 47 additions and 25 deletions

View file

@ -43,6 +43,10 @@ class SwapAsset:
def full_name(self):
return self.data.full_name or f'{self.data.name}.{self.data.name}'
@property
def short_name(self):
return self.asset or self.chain
@property
def memo_asset_name(self):
return self.data.abbr or self.data.full_name

View file

@ -18,12 +18,12 @@ class THORChainSwapAsset(SwapAsset):
_ad = SwapAsset._ad
assets_data = {
'BTC': _ad('Bitcoin', 'BTC', None, 'b'),
'LTC': _ad('Litecoin', 'LTC', None, 'l'),
'BCH': _ad('Bitcoin Cash', 'BCH', None, 'c'),
'ETH': _ad('Ethereum', 'ETH', None, 'e'),
'DOGE': _ad('Dogecoin', 'DOGE', None, 'd'),
'RUNE': _ad('THORChain Rune', 'RUNE', 'THOR.RUNE', 'r'),
'BTC': _ad('Bitcoin', 'BTC', None, 'b'),
'LTC': _ad('Litecoin', 'LTC', None, 'l'),
'BCH': _ad('Bitcoin Cash', 'BCH', None, 'c'),
'ETH': _ad('Ethereum', 'ETH', None, 'e'),
'DOGE': _ad('Dogecoin', 'DOGE', None, 'd'),
'RUNE': _ad('Rune (THORChain)', 'RUNE', 'THOR.RUNE', 'r'),
}
send = ('BTC', 'LTC', 'BCH', 'ETH')

View file

@ -78,8 +78,8 @@ class Thornode:
d = self.data
tx = self.tx
in_coin = tx.send_asset.chain
out_coin = tx.recv_asset.chain
in_coin = tx.send_asset.short_name
out_coin = tx.recv_asset.short_name
in_amt = self.in_amt
out_amt = UniAmt(int(d['expected_amount_out']), from_unit='satoshi')
gas_unit = d['gas_rate_units']
@ -130,7 +130,7 @@ class Thornode:
return f"""
{cyan(hdr)}
Protocol: {blue(name)}
Direction: {orange(f'{in_coin} => {out_coin}')}
Direction: {orange(f'{tx.send_asset.name} => {tx.recv_asset.name}')}
Vault address: {cyan(self.inbound_address)}
Quote expires: {pink(elapsed_disp)} [{make_timestr(d['expiry'])}]
{_amount_in_label:<22} {in_amt.hl()} {in_coin}

View file

@ -175,14 +175,11 @@ class CmdTestEthSwap(CmdTestSwapMethods, CmdTestRegtest):
def swaptxsign1(self):
return self._swaptxsign()
def swaptxsign2(self):
return self._swaptxsign()
def swaptxsend1(self):
return self._swaptxsend()
def swaptxsend2(self):
return self._swaptxsend()
swaptxsign2 = swaptxsign1
swaptxsend2 = swaptxsend1
def swaptxbump1(self): # create one-output TX back to self to rescue funds
return self._swaptxbump('40s', output_args=[f'{dfl_sid}:B:1'])

View file

@ -28,8 +28,7 @@ prices = {'BTC': 97000, 'LTC': 115, 'BCH': 330, 'ETH': 2304}
gas_rate_units = {'ETH': 'gwei', 'BTC': 'satsperbyte'}
recommended_gas_rate = {'ETH': '1', 'BTC': '6'}
data_template = {
'inbound_address': None,
data_template_btc = {
'inbound_confirmation_blocks': 4,
'inbound_confirmation_seconds': 2400,
'outbound_delay_blocks': 5,
@ -43,19 +42,37 @@ data_template = {
'slippage_bps': 31,
'total_bps': 34
},
'expiry': None,
'warning': 'Do not cache this response. Do not send funds after the expiry.',
'notes': 'First output should be to inbound_address, second output should be change back to self, third output should be OP_RETURN, limited to 80 bytes. Do not send below the dust threshold. Do not use exotic spend scripts, locks or address formats.',
'dust_threshold': '10000',
'recommended_min_amount_in': '1222064',
'recommended_gas_rate': '6',
'gas_rate_units': 'satsperbyte',
'expected_amount_out': None,
'max_streaming_quantity': 0,
'streaming_swap_blocks': 0,
'total_swap_seconds': 2430
}
data_template_eth = {
'inbound_confirmation_blocks': 2,
'inbound_confirmation_seconds': 24,
'outbound_delay_blocks': 0,
'outbound_delay_seconds': 0,
'fees': {
'asset': 'BTC.BTC',
'affiliate': '0',
'outbound': '1097',
'liquidity': '77',
'total': '1174',
'slippage_bps': 15,
'total_bps': 237
},
'router': '0xD37BbE5744D730a1d98d8DC97c42F0Ca46aD7146',
'warning': 'Do not cache this response. Do not send funds after the expiry.',
'notes': 'Base Asset: Send the inbound_address the asset with the memo encoded in hex in the data field. Tokens: First approve router to spend tokens from user: asset.approve(router, amount). Then call router.depositWithExpiry(inbound_address, asset, amount, memo, expiry). Asset is the token contract address. Amount should be in native asset decimals (eg 1e18 for most tokens). Do not swap to smart contract addresses.',
'recommended_gas_rate': '1',
'max_streaming_quantity': 0,
'streaming_swap_blocks': 0,
'total_swap_seconds': 24
}
def make_inbound_addr(proto, mmtype):
from mmgen.tool.coin import tool_cmd
n = int(time.time()) // (60 * 60 * 24) # increments once every 24 hrs
@ -77,16 +94,20 @@ class ThornodeServer(HTTPD):
m = re.search(request_pat, request_uri(environ))
send_chain, send_asset, recv_chain, recv_asset, amt_atomic = m.groups()
from mmgen.protocol import init_proto
send_proto = init_proto(cfg, send_chain, network='regtest', need_amt=True)
in_amt = UniAmt(int(amt_atomic), from_unit='satoshi')
out_amt = in_amt * (prices[send_asset] / prices[recv_asset])
addr = make_inbound_addr(send_proto, send_proto.preferred_mmtypes[0])
data_template = (
data_template_eth if send_asset == 'ETH' else
data_template_btc)
from mmgen.protocol import init_proto
send_proto = init_proto(cfg, send_chain, network='regtest', need_amt=True)
data = data_template | {
'recommended_min_amount_in': str(int(70 * 10**8 / prices[send_asset])), # $70
'expected_amount_out': str(out_amt.to_unit('satoshi')),
'expiry': int(time.time()) + (10 * 60),
'inbound_address': addr,
'inbound_address': make_inbound_addr(send_proto, send_proto.preferred_mmtypes[0]),
'gas_rate_units': gas_rate_units[send_proto.base_proto_coin],
'recommended_gas_rate': recommended_gas_rate[send_proto.base_proto_coin],
}