weakhands.py (2061B)
1 import requests 2 import logging 3 4 5 quote_url = "https://sideshift.ai/api/v1/quotes" 6 swap_url = "https://sideshift.ai/api/v1/orders" 7 affiliate = "eK590V1Mh" 8 9 10 def get_quote(amount_lnbtc): 11 quote_data = { 12 "depositMethod": "ln", 13 "settleMethod": "usdtla", 14 "affiliateId": affiliate, 15 "depositAmount": str(amount_lnbtc), 16 } 17 logging.info("Getting quote to swap {:.8f} LN-BTC to USDT".format(amount_lnbtc)) 18 resp = requests.post(quote_url, json=quote_data) 19 20 if resp.status_code != 201: 21 logging.error("Failed quote request:") 22 logging.error(resp.json()) 23 return False 24 25 return resp.json() 26 27 28 def get_swap(quote, amount_lnbtc, liquid_address): 29 swap_url = "https://sideshift.ai/api/orders" 30 data = { 31 "type": "fixed", 32 "quoteId": quote["id"], 33 "settleAddress": liquid_address, 34 "affiliateId": affiliate, 35 } 36 logging.info( 37 "Creating order to swap {:.8f} LN-BTC to USDT (liquid: {})".format( 38 amount_lnbtc, liquid_address 39 ) 40 ) 41 42 resp = requests.post(swap_url, json=data) 43 44 if resp.status_code != 201: 45 logging.error("Failed to create order:") 46 logging.error(resp.json()) 47 return False 48 49 return resp.json() 50 51 52 def pay_swap(node, swap): 53 payment_req = swap["depositAddress"]["paymentRequest"] 54 logging.info("Paying invoice: {}".format(payment_req)) 55 node.pay_invoice(payment_req) 56 return True 57 58 59 def swap_lnbtc_for_lusdt(node, amount_lnbtc, liquid_address): 60 try: 61 quote = get_quote(amount_lnbtc) 62 if not quote: 63 logging.error("Quote failed, not swapping this order.") 64 return False 65 66 swap = get_swap(quote, amount_lnbtc, liquid_address) 67 if not swap: 68 logging.error("Creating order failed, not swapping this payment.") 69 return False 70 71 pay_swap(node, swap) 72 logging.info("Paid invoice! Swapped ") 73 74 except Exception as e: 75 logging.error("Error encountered during swap: {}".format(e)) 76 77 return