|
| 1 | +import time |
| 2 | + |
| 3 | +import requests |
| 4 | + |
| 5 | +import json |
| 6 | + |
| 7 | + |
| 8 | +class FiberRPCClient: |
| 9 | + def __init__(self, url): |
| 10 | + self.url = url |
| 11 | + |
| 12 | + def send_btc(self, btc_pay_req): |
| 13 | + return self.call("send_btc", [btc_pay_req]) |
| 14 | + |
| 15 | + def open_channel(self, param): |
| 16 | + """ |
| 17 | + curl --location 'http://127.0.0.1:8227' --header 'Content-Type: application/json' --data '{ |
| 18 | + "id": 42, |
| 19 | + "jsonrpc": "2.0", |
| 20 | + "method": "open_channel", |
| 21 | + "params": [ |
| 22 | + { |
| 23 | + "peer_id": "QmaQSn11jsAXWLhjHtZ9EVbauD88sCmYzty3GmYcoVWP2j", |
| 24 | + "funding_amount": "0x2e90edd000" |
| 25 | + } |
| 26 | + ] |
| 27 | + }' |
| 28 | + {"jsonrpc": "2.0", "result": {"temporary_channel_id": "0xbf1b507e730b08024180ed9cb5bb3655606d3a89e94476033cf34d206d352751"}, "id": 42} |
| 29 | + """ |
| 30 | + return self.call("open_channel", [param]) |
| 31 | + |
| 32 | + def list_channels(self, param): |
| 33 | + """ |
| 34 | + curl --location 'http://127.0.0.1:8227' --header 'Content-Type: application/json' --data '{ |
| 35 | + "id": 42, |
| 36 | + "jsonrpc": "2.0", |
| 37 | + "method": "list_channels", |
| 38 | + "params": [ |
| 39 | + { |
| 40 | + "peer_id": "QmaQSn11jsAXWLhjHtZ9EVbauD88sCmYzty3GmYcoVWP2j" |
| 41 | + } |
| 42 | + ] |
| 43 | + }' |
| 44 | + {"jsonrpc": "2.0", "result": {"channels": [{"channel_id": "0x2329a1ced09d0c9eff46068ac939596bb657a984b1d6385db563f2de837b8879", "peer_id": "QmaQSn11jsAXWLhjHtZ9EVbauD88sCmYzty3GmYcoVWP2j", "state": {"state_name": "NEGOTIATING_FUNDING", "state_flags": "OUR_INIT_SENT | THEIR_INIT_SENT"}, "local_balance": "0x2d1f615200", "sent_tlc_balance": "0x0", "remote_balance": "0x0", "received_tlc_balance": "0x0", "created_at": "0x620a0b7b1676b"}]}, "id": 42} |
| 45 | + """ |
| 46 | + return self.call("list_channels", [param]) |
| 47 | + |
| 48 | + def accept_channel(self, param): |
| 49 | + return self.call("accept_channel", [param]) |
| 50 | + |
| 51 | + def add_tlc(self, param): |
| 52 | + return self.call("add_tlc", [param]) |
| 53 | + |
| 54 | + def remove_tlc(self, param): |
| 55 | + return self.call("remove_tlc", [param]) |
| 56 | + |
| 57 | + def shutdown_channel(self, param): |
| 58 | + return self.call("shutdown_channel", [param]) |
| 59 | + |
| 60 | + def new_invoice(self, param): |
| 61 | + return self.call("new_invoice", [param]) |
| 62 | + |
| 63 | + def parse_invoice(self, param): |
| 64 | + return self.call("parse_invoice", [param]) |
| 65 | + |
| 66 | + def connect_peer(self, param): |
| 67 | + return self.call("connect_peer", [param]) |
| 68 | + |
| 69 | + def disconnect_peer(self, param): |
| 70 | + return self.call("disconnect_peer", [param]) |
| 71 | + |
| 72 | + def send_payment(self, param): |
| 73 | + return self.call("send_payment", [param]) |
| 74 | + |
| 75 | + def call(self, method, params): |
| 76 | + headers = {"content-type": "application/json"} |
| 77 | + data = {"id": 42, "jsonrpc": "2.0", "method": method, "params": params} |
| 78 | + print( |
| 79 | + "curl --location '{url}' --header 'Content-Type: application/json' --data '{data}'".format( |
| 80 | + url=self.url, data=json.dumps(data, indent=4) |
| 81 | + ) |
| 82 | + ) |
| 83 | + for i in range(100): |
| 84 | + try: |
| 85 | + response = requests.post( |
| 86 | + self.url, data=json.dumps(data), headers=headers |
| 87 | + ).json() |
| 88 | + print("response:\n{response}".format(response=json.dumps(response))) |
| 89 | + if "error" in response.keys(): |
| 90 | + error_message = response["error"].get("message", "Unknown error") |
| 91 | + raise Exception(f"Error: {error_message}") |
| 92 | + |
| 93 | + return response.get("result", None) |
| 94 | + except requests.exceptions.ConnectionError as e: |
| 95 | + print(e) |
| 96 | + print("request too quickly, wait 2s") |
| 97 | + time.sleep(2) |
| 98 | + continue |
| 99 | + raise Exception("request time out") |
0 commit comments