-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from cryptape/v119
v119
- Loading branch information
Showing
35 changed files
with
867 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import time | ||
|
||
import requests | ||
|
||
import json | ||
|
||
|
||
class FiberRPCClient: | ||
def __init__(self, url): | ||
self.url = url | ||
|
||
def send_btc(self, btc_pay_req): | ||
return self.call("send_btc", [btc_pay_req]) | ||
|
||
def open_channel(self, param): | ||
""" | ||
curl --location 'http://127.0.0.1:8227' --header 'Content-Type: application/json' --data '{ | ||
"id": 42, | ||
"jsonrpc": "2.0", | ||
"method": "open_channel", | ||
"params": [ | ||
{ | ||
"peer_id": "QmaQSn11jsAXWLhjHtZ9EVbauD88sCmYzty3GmYcoVWP2j", | ||
"funding_amount": "0x2e90edd000" | ||
} | ||
] | ||
}' | ||
{"jsonrpc": "2.0", "result": {"temporary_channel_id": "0xbf1b507e730b08024180ed9cb5bb3655606d3a89e94476033cf34d206d352751"}, "id": 42} | ||
""" | ||
return self.call("open_channel", [param]) | ||
|
||
def list_channels(self, param): | ||
""" | ||
curl --location 'http://127.0.0.1:8227' --header 'Content-Type: application/json' --data '{ | ||
"id": 42, | ||
"jsonrpc": "2.0", | ||
"method": "list_channels", | ||
"params": [ | ||
{ | ||
"peer_id": "QmaQSn11jsAXWLhjHtZ9EVbauD88sCmYzty3GmYcoVWP2j" | ||
} | ||
] | ||
}' | ||
{"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} | ||
""" | ||
return self.call("list_channels", [param]) | ||
|
||
def accept_channel(self, param): | ||
return self.call("accept_channel", [param]) | ||
|
||
def add_tlc(self, param): | ||
return self.call("add_tlc", [param]) | ||
|
||
def remove_tlc(self, param): | ||
return self.call("remove_tlc", [param]) | ||
|
||
def shutdown_channel(self, param): | ||
return self.call("shutdown_channel", [param]) | ||
|
||
def new_invoice(self, param): | ||
return self.call("new_invoice", [param]) | ||
|
||
def parse_invoice(self, param): | ||
return self.call("parse_invoice", [param]) | ||
|
||
def connect_peer(self, param): | ||
return self.call("connect_peer", [param]) | ||
|
||
def disconnect_peer(self, param): | ||
return self.call("disconnect_peer", [param]) | ||
|
||
def send_payment(self, param): | ||
return self.call("send_payment", [param]) | ||
|
||
def call(self, method, params): | ||
headers = {"content-type": "application/json"} | ||
data = {"id": 42, "jsonrpc": "2.0", "method": method, "params": params} | ||
print( | ||
"curl --location '{url}' --header 'Content-Type: application/json' --data '{data}'".format( | ||
url=self.url, data=json.dumps(data, indent=4) | ||
) | ||
) | ||
for i in range(100): | ||
try: | ||
response = requests.post( | ||
self.url, data=json.dumps(data), headers=headers | ||
).json() | ||
print("response:\n{response}".format(response=json.dumps(response))) | ||
if "error" in response.keys(): | ||
error_message = response["error"].get("message", "Unknown error") | ||
raise Exception(f"Error: {error_message}") | ||
|
||
return response.get("result", None) | ||
except requests.exceptions.ConnectionError as e: | ||
print(e) | ||
print("request too quickly, wait 2s") | ||
time.sleep(2) | ||
continue | ||
raise Exception("request time out") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
from framework.config import UDT_CONTRACT_PATH | ||
from framework.helper.contract import deploy_ckb_contract, CkbContract | ||
from framework.helper.miner import miner_until_tx_committed | ||
from framework.test_node import CkbNode | ||
from framework.util import ( | ||
ckb_hash_script, | ||
to_big_uint128_le_compatible, | ||
to_int_from_big_uint128_le, | ||
) | ||
from framework.helper.contract import get_ckb_contract_codehash | ||
|
||
|
||
class UdtContract(CkbContract): | ||
|
||
def __init__(self, contract_hash=None, contract_tx_index=None): | ||
self.contract_hash = contract_hash | ||
self.contract_tx_index = contract_tx_index | ||
if contract_hash is None: | ||
self.deployed = False | ||
self.contract_path = UDT_CONTRACT_PATH | ||
self.method = {"demo": {"args": "0x", "data": "0x"}} | ||
|
||
def deploy(self, account_private, node: CkbNode): | ||
if self.deployed: | ||
return | ||
self.contract_path = deploy_ckb_contract( | ||
account_private, self.contract_path, api_url=node.getClient().url | ||
) | ||
self.contract_tx_index = 0 | ||
miner_until_tx_committed(node, self.contract_path) | ||
self.deployed = True | ||
|
||
def get_deploy_hash_and_index(self) -> (str, int): | ||
if not self.deployed: | ||
raise Exception("pls deploy first") | ||
return self.contract_path, self.contract_tx_index | ||
|
||
@classmethod | ||
def issue(cls, own_arg, amount) -> (str, str): | ||
return ckb_hash_script(own_arg), to_big_uint128_le_compatible(amount) | ||
|
||
@classmethod | ||
def transfer(cls, own_arg, amount) -> (str, str): | ||
return ckb_hash_script(own_arg), to_big_uint128_le_compatible(amount) | ||
|
||
def balance(self, client, own_arg, query_arg): | ||
pass | ||
|
||
def list_cell(self, client, own_arg, query_arg): | ||
code_hash = get_ckb_contract_codehash( | ||
self.contract_path, | ||
self.contract_tx_index, | ||
enable_type_id=True, | ||
api_url=client.url, | ||
) | ||
cells = client.get_cells( | ||
{ | ||
"script": { | ||
"code_hash": "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8", | ||
"hash_type": "type", | ||
"args": query_arg, | ||
}, | ||
"script_type": "lock", | ||
"filter": { | ||
"script": { | ||
"code_hash": code_hash, | ||
"hash_type": "type", | ||
"args": ckb_hash_script(own_arg), | ||
} | ||
}, | ||
}, | ||
"asc", | ||
"0x64", | ||
None, | ||
) | ||
info = [] | ||
for cell in cells["objects"]: | ||
info.append( | ||
{ | ||
"input_cell": { | ||
"tx_hash": cell["out_point"]["tx_hash"], | ||
"index": int(cell["out_point"]["index"], 16), | ||
}, | ||
"balance": to_int_from_big_uint128_le(cell["output_data"]), | ||
} | ||
) | ||
return info | ||
|
||
def get_arg_and_data(self, key) -> (str, str): | ||
if key not in self.method.keys(): | ||
# return "0x0","0x0" | ||
raise Exception("key not exist in method list") | ||
return self.method[key]["args"], self.method[key]["data"] |
Oops, something went wrong.