mirror of
https://github.com/basicswap/basicswap.git
synced 2025-11-06 02:38:11 +01:00
Move js functions to new file.
This commit is contained in:
@@ -38,6 +38,15 @@ from .basicswap import (
|
||||
SEQUENCE_LOCK_TIME,
|
||||
ABS_LOCK_TIME,
|
||||
)
|
||||
from .js_server import (
|
||||
js_error,
|
||||
js_wallets,
|
||||
js_offers,
|
||||
js_sentoffers,
|
||||
js_bids,
|
||||
js_sentbids,
|
||||
js_index,
|
||||
)
|
||||
|
||||
|
||||
def format_timestamp(value):
|
||||
@@ -250,126 +259,6 @@ class HttpHandler(BaseHTTPRequestHandler):
|
||||
self.server.last_form_id[name] = form_id
|
||||
return form_data
|
||||
|
||||
def js_error(self, error_str):
|
||||
error_str_json = json.dumps({'error': error_str})
|
||||
return bytes(error_str_json, 'UTF-8')
|
||||
|
||||
def js_wallets(self, url_split, post_string):
|
||||
return bytes(json.dumps(self.server.swap_client.getWalletsInfo()), 'UTF-8')
|
||||
|
||||
def js_offers(self, url_split, post_string, sent=False):
|
||||
if len(url_split) > 3:
|
||||
if url_split[3] == 'new':
|
||||
if post_string == '':
|
||||
raise ValueError('No post data')
|
||||
form_data = urllib.parse.parse_qs(post_string)
|
||||
offer_id = self.postNewOffer(form_data)
|
||||
rv = {'offer_id': offer_id.hex()}
|
||||
return bytes(json.dumps(rv), 'UTF-8')
|
||||
offer_id = bytes.fromhex(url_split[3])
|
||||
|
||||
filters = {
|
||||
'coin_from': -1,
|
||||
'coin_to': -1,
|
||||
'page_no': 1,
|
||||
'limit': PAGE_LIMIT,
|
||||
'sort_by': 'created_at',
|
||||
'sort_dir': 'desc',
|
||||
}
|
||||
if post_string != '':
|
||||
post_data = urllib.parse.parse_qs(post_string)
|
||||
filters['coin_from'] = setCoinFilter(form_data, b'coin_from')
|
||||
filters['coin_to'] = setCoinFilter(form_data, b'coin_to')
|
||||
|
||||
if b'sort_by' in post_data:
|
||||
sort_by = post_data[b'sort_by'][0].decode('utf-8')
|
||||
assert(sort_by in ['created_at', 'rate']), 'Invalid sort by'
|
||||
filters['sort_by'] = sort_by
|
||||
if b'sort_dir' in post_data:
|
||||
sort_dir = post_data[b'sort_dir'][0].decode('utf-8')
|
||||
assert(sort_dir in ['asc', 'desc']), 'Invalid sort dir'
|
||||
filters['sort_dir'] = sort_dir
|
||||
|
||||
if b'offset' in post_data:
|
||||
filters['offset'] = int(post_data[b'offset'][0])
|
||||
if b'limit' in post_data:
|
||||
filters['limit'] = int(post_data[b'limit'][0])
|
||||
assert(filters['limit'] > 0 and filters['limit'] <= PAGE_LIMIT), 'Invalid limit'
|
||||
|
||||
offers = self.server.swap_client.listOffers(sent, filters)
|
||||
rv = []
|
||||
for o in offers:
|
||||
rv.append({
|
||||
'offer_id': o.offer_id.hex(),
|
||||
'created_at': time.strftime('%Y-%m-%d %H:%M', time.localtime(o.created_at)),
|
||||
'coin_from': getCoinName(Coins(o.coin_from)),
|
||||
'coin_to': getCoinName(Coins(o.coin_to)),
|
||||
'amount_from': format8(o.amount_from),
|
||||
'amount_to': format8((o.amount_from * o.rate) // COIN),
|
||||
'rate': format8(o.rate)
|
||||
})
|
||||
|
||||
return bytes(json.dumps(rv), 'UTF-8')
|
||||
|
||||
def js_sentoffers(self, url_split, post_string):
|
||||
return self.js_offers(url_split, post_string, True)
|
||||
|
||||
def js_bids(self, url_split, post_string):
|
||||
swap_client = self.server.swap_client
|
||||
if len(url_split) > 3:
|
||||
if url_split[3] == 'new':
|
||||
if post_string == '':
|
||||
raise ValueError('No post data')
|
||||
post_data = urllib.parse.parse_qs(post_string)
|
||||
|
||||
offer_id = bytes.fromhex(post_data[b'offer_id'][0].decode('utf-8'))
|
||||
assert(len(offer_id) == 28)
|
||||
|
||||
amount_from = inputAmount(post_data[b'amount_from'][0].decode('utf-8'))
|
||||
|
||||
addr_from = None
|
||||
if b'addr_from' in post_data:
|
||||
addr_from = post_data[b'addr_from'][0].decode('utf-8')
|
||||
if addr_from == '-1':
|
||||
addr_from = None
|
||||
|
||||
bid_id = swap_client.postBid(offer_id, amount_from, addr_send_from=addr_from).hex()
|
||||
|
||||
rv = {'bid_id': bid_id}
|
||||
return bytes(json.dumps(rv), 'UTF-8')
|
||||
|
||||
bid_id = bytes.fromhex(url_split[3])
|
||||
assert(len(bid_id) == 28)
|
||||
|
||||
if post_string != '':
|
||||
post_data = urllib.parse.parse_qs(post_string)
|
||||
if b'accept' in post_data:
|
||||
swap_client.acceptBid(bid_id)
|
||||
|
||||
bid, offer = swap_client.getBidAndOffer(bid_id)
|
||||
assert(bid), 'Unknown bid ID'
|
||||
|
||||
edit_bid = False
|
||||
show_txns = False
|
||||
data = describeBid(swap_client, bid, offer, edit_bid, show_txns)
|
||||
|
||||
return bytes(json.dumps(data), 'UTF-8')
|
||||
|
||||
bids = swap_client.listBids()
|
||||
return bytes(json.dumps([{
|
||||
'bid_id': b[1].hex(),
|
||||
'offer_id': b[2].hex(),
|
||||
'created_at': time.strftime('%Y-%m-%d %H:%M', time.localtime(b[0])),
|
||||
'amount_from': format8(b[3]),
|
||||
'bid_state': strBidState(b[4])
|
||||
} for b in bids]), 'UTF-8')
|
||||
|
||||
def js_sentbids(self, url_split, post_string):
|
||||
return bytes(json.dumps(self.server.swap_client.listBids(sent=True)), 'UTF-8')
|
||||
|
||||
def js_index(self, url_split, post_string):
|
||||
return bytes(json.dumps(self.server.swap_client.getSummary()), 'UTF-8')
|
||||
|
||||
def page_explorers(self, url_split, post_string):
|
||||
swap_client = self.server.swap_client
|
||||
|
||||
@@ -615,16 +504,19 @@ class HttpHandler(BaseHTTPRequestHandler):
|
||||
|
||||
coin_from = Coins(offer.coin_from)
|
||||
coin_to = Coins(offer.coin_to)
|
||||
ci_from = swap_client.ci(coin_from)
|
||||
ci_to = swap_client.ci(coin_to)
|
||||
ticker_from = swap_client.getTicker(coin_from)
|
||||
ticker_to = swap_client.getTicker(coin_to)
|
||||
|
||||
data = {
|
||||
'tla_from': swap_client.getTicker(coin_from),
|
||||
'tla_to': swap_client.getTicker(coin_to),
|
||||
'state': strOfferState(offer.state),
|
||||
'coin_from': getCoinName(coin_from),
|
||||
'coin_to': getCoinName(coin_to),
|
||||
'amt_from': format8(offer.amount_from),
|
||||
'amt_to': format8((offer.amount_from * offer.rate) // COIN),
|
||||
'coin_from': ci_from.coin_name(),
|
||||
'coin_to': ci_to.coin_name(),
|
||||
'amt_from': ci_from.format_amount(offer.amount_from),
|
||||
'amt_to': ci_to.format_amount((offer.amount_from * offer.rate) // COIN),
|
||||
'rate': format8(offer.rate),
|
||||
'lock_type': getLockName(offer.lock_type),
|
||||
'lock_value': offer.lock_value,
|
||||
@@ -852,19 +744,19 @@ class HttpHandler(BaseHTTPRequestHandler):
|
||||
if len(url_split) > 1 and url_split[1] == 'json':
|
||||
try:
|
||||
self.putHeaders(status_code, 'text/plain')
|
||||
func = self.js_index
|
||||
func = js_index
|
||||
if len(url_split) > 2:
|
||||
func = {'wallets': self.js_wallets,
|
||||
'offers': self.js_offers,
|
||||
'sentoffers': self.js_sentoffers,
|
||||
'bids': self.js_bids,
|
||||
'sentbids': self.js_sentbids,
|
||||
}.get(url_split[2], self.js_index)
|
||||
return func(url_split, post_string)
|
||||
func = {'wallets': js_wallets,
|
||||
'offers': js_offers,
|
||||
'sentoffers': js_sentoffers,
|
||||
'bids': js_bids,
|
||||
'sentbids': js_sentbids,
|
||||
}.get(url_split[2], js_index)
|
||||
return func(self, url_split, post_string)
|
||||
except Exception as ex:
|
||||
if self.server.swap_client.debug is True:
|
||||
traceback.print_exc()
|
||||
return self.js_error(str(ex))
|
||||
return js_error(self, str(ex))
|
||||
try:
|
||||
self.putHeaders(status_code, 'text/html')
|
||||
if len(url_split) > 1:
|
||||
|
||||
Reference in New Issue
Block a user