forked from Blockstream/green_cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsideswap.py
132 lines (112 loc) · 3.68 KB
/
sideswap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
# coding: utf-8
import json
import sys
import requests
from websockets.sync.client import connect
import requests
import greenaddress as ga
import subprocess
MAINNET = {
# USDt mainnet
"asset": "ce091c998b83c78bb71a632313ba3760f1763d9cfcffae02258ffa9865a37bd2",
"api_url": "wss://api.sideswap.io/json-rpc-ws"
}
SUBACCOUNT = 0
NET = MAINNET
def start_swap(net):
with connect(net["api_url"]) as ws:
ws.send(json.dumps(
{
"id": 1,
"method": "subscribe_price_stream",
"params": {
"asset": net["asset"],
"send_bitcoins": True,
"send_amount": 1666
}
}
))
response = ws.recv()
result = json.loads(response)["result"]
ws.send(json.dumps(
{
"id": 2,
"method": "start_swap_web",
"params": {
"send_bitcoins": True,
"price": result["price"],
"asset": net["asset"],
"send_amount": result["send_amount"],
"recv_amount": result["recv_amount"],
}
}
))
response = ws.recv()
print(json.loads(response))
result = json.loads(response)["result"]
return result
############## START SWAP (WEBSOCKET) ##############
swap_data = start_swap(NET)
print(swap_data)
with open('/tmp/1.dat', "w") as f:
json.dump(swap_data, f)
with open('/tmp/1.dat', "r") as f:
data = json.load(f)
print(data)
data['subaccount']=0
with open('/tmp/1.dat', "w") as f:
json.dump(data, f)
############## CREATE PSET (CLI) ###################
process = subprocess.Popen("green-cli --network electrum-liquid createpset /tmp/1.dat", shell=True, stdout=subprocess.PIPE)
output, _ = process.communicate()
print(output)
print(json.loads(output))
create_pset_result = json.loads(output)
############## UPLOAD SWAP INPUTS (HTTP POST) ##############
swap_start_request ={
"id": None,
"method": "swap_start",
"params": {
"change_addr": create_pset_result['change_addr'],
"recv_addr": create_pset_result['recv_addr'],
"inputs": create_pset_result['inputs'],
"order_id": swap_data['order_id'],
"recv_amount": swap_data['recv_amount'],
"recv_asset": swap_data['recv_asset'],
"send_amount": swap_data['send_amount'],
"send_asset": swap_data['send_asset']
}
}
sideswap_pset_data = requests.post(swap_data["upload_url"], json=swap_start_request).json()['result']
print(sideswap_pset_data)
########### SIGN PSET ##########################
sign_pset_req = {
"subaccount": SUBACCOUNT,
"send_asset": swap_data["send_asset"],
"send_amount": swap_data["send_amount"],
"recv_asset": swap_data["recv_asset"],
"recv_amount": swap_data["recv_amount"],
"pset": sideswap_pset_data["pset"]
}
#sign_result = session.sign_pset(sign_pset_req).resolve()
with open('/tmp/2.dat', "w") as f:
json.dump(sign_pset_req, f)
process = subprocess.Popen("green-cli --network electrum-liquid signpset /tmp/2.dat", shell=True, stdout=subprocess.PIPE)
output, _ = process.communicate()
print(output)
print(json.loads(output))
sign_result = json.loads(output)
print(sign_result)
########### SUBMIT SIGNED PSET (HTTP POST) ##########################
upload_request = {
"id": None,
"method": "swap_sign",
"params": {
"order_id": swap_data["order_id"],
"pset": sign_result["pset"],
"submit_id": sideswap_pset_data["submit_id"]
}
}
response = requests.post(swap_data["upload_url"], json=upload_request)
print(response.json())