api: Allow coin type to be specified by ticker.
This commit is contained in:
parent
9b4a256b44
commit
4ea4e0656a
@ -5,22 +5,22 @@
|
||||
# file LICENSE or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
import os
|
||||
import threading
|
||||
import logging
|
||||
import threading
|
||||
import subprocess
|
||||
|
||||
import basicswap.config as cfg
|
||||
import basicswap.contrib.segwit_addr as segwit_addr
|
||||
|
||||
from .chainparams import (
|
||||
chainparams,
|
||||
Coins,
|
||||
from .rpc import (
|
||||
callrpc,
|
||||
)
|
||||
from .util import (
|
||||
pubkeyToAddress,
|
||||
)
|
||||
from .rpc import (
|
||||
callrpc,
|
||||
from .chainparams import (
|
||||
Coins,
|
||||
chainparams,
|
||||
)
|
||||
|
||||
|
||||
|
@ -46,6 +46,7 @@ from .ui import (
|
||||
PAGE_LIMIT,
|
||||
inputAmount,
|
||||
describeBid,
|
||||
getCoinType,
|
||||
setCoinFilter,
|
||||
get_data_entry,
|
||||
have_data_entry,
|
||||
@ -324,7 +325,7 @@ class HttpHandler(BaseHTTPRequestHandler):
|
||||
parsed_data['addr_from'] = None
|
||||
|
||||
try:
|
||||
page_data['coin_from'] = int(get_data_entry(form_data, 'coin_from'))
|
||||
page_data['coin_from'] = getCoinType(get_data_entry(form_data, 'coin_from'))
|
||||
coin_from = Coins(page_data['coin_from'])
|
||||
ci_from = swap_client.ci(coin_from)
|
||||
if coin_from != Coins.XMR:
|
||||
@ -334,7 +335,7 @@ class HttpHandler(BaseHTTPRequestHandler):
|
||||
errors.append('Unknown Coin From')
|
||||
|
||||
try:
|
||||
page_data['coin_to'] = int(get_data_entry(form_data, 'coin_to'))
|
||||
page_data['coin_to'] = getCoinType(get_data_entry(form_data, 'coin_to'))
|
||||
coin_to = Coins(page_data['coin_to'])
|
||||
ci_to = swap_client.ci(coin_to)
|
||||
parsed_data['coin_to'] = coin_to
|
||||
|
@ -24,6 +24,22 @@ from .basicswap import (
|
||||
PAGE_LIMIT = 50
|
||||
|
||||
|
||||
def tickerToCoinId(ticker):
|
||||
search_str = ticker.upper()
|
||||
for c in Coins:
|
||||
if c.name == search_str:
|
||||
return c.value
|
||||
raise ValueError('Unknown coin')
|
||||
|
||||
|
||||
def getCoinType(coin_type_ind):
|
||||
# coin_type_ind can be int id or str ticker
|
||||
try:
|
||||
return int(coin_type_ind)
|
||||
except Exception:
|
||||
return tickerToCoinId(coin_type_ind)
|
||||
|
||||
|
||||
def validateAmountString(amount, ci):
|
||||
if type(amount) != str:
|
||||
return
|
||||
@ -42,6 +58,7 @@ def get_data_entry(post_data, name):
|
||||
return post_data[name]
|
||||
return post_data[name.encode('utf-8')][0].decode('utf-8')
|
||||
|
||||
|
||||
def have_data_entry(post_data, name):
|
||||
if 'is_json' in post_data:
|
||||
return name in post_data
|
||||
@ -50,7 +67,7 @@ def have_data_entry(post_data, name):
|
||||
|
||||
def setCoinFilter(form_data, field_name):
|
||||
try:
|
||||
coin_type = int(get_data_entry(form_data, field_name))
|
||||
coin_type = getCoinType(get_data_entry(form_data, field_name))
|
||||
except Exception:
|
||||
return -1
|
||||
if coin_type == -1:
|
||||
@ -211,4 +228,3 @@ def describeBid(swap_client, bid, xmr_swap, offer, xmr_offer, bid_events, edit_b
|
||||
data['events'] = bid_events
|
||||
|
||||
return data
|
||||
|
||||
|
@ -52,8 +52,8 @@ class Test(XmrTestBase):
|
||||
|
||||
data = parse.urlencode({
|
||||
'addr_from': '-1',
|
||||
'coin_from': '1',
|
||||
'coin_to': '6',
|
||||
'coin_from': 'part',
|
||||
'coin_to': 'xmr',
|
||||
'amt_from': '1',
|
||||
'amt_to': '1',
|
||||
'lockhrs': '24'}).encode()
|
||||
|
Loading…
Reference in New Issue
Block a user