-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlibrary.py
More file actions
290 lines (268 loc) · 10.4 KB
/
library.py
File metadata and controls
290 lines (268 loc) · 10.4 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
import subprocess
import json
from config import *
def parse_airdrop_data(data):
"""
parse the airdrop data
"""
airdrops_list = []
out = ''
err = ''
try:
json_data = json.loads(data)
for item in json_data:
address = list(item.keys())[0]
amount = list(item.values())[0]
aird = {}
aird['address'] = address
aird['tokens_amount'] = amount
airdrops_list.append(aird)
out = 'json parsed'
except json.decoder.JSONDecodeError:
for line in data.splitlines():
wallet = line.split(',')
item = {}
item['address'] = wallet[0]
item['tokens_amount'] = wallet[1]
airdrops_list.append(item)
out = 'csv parsed'
except Exception as e:
err = 'exception parsing data: %s' % str(e)
return airdrops_list, [], [], [], out, err
"""
DST_ADDRESSES = list of destination wallet addresses for the airdrop
AMOUNTS = dictionary of wallet addresses and amounts to airdrop for each wallet address
"""
dst_addresses = []
amounts = {}
total_lovelace = 0
total_tokens = 0
for item in airdrops_list:
# read the values from the airdrops_list
address = item['address']
t_amount = int(item['tokens_amount'])
# create the dictionary with the final airdrops list
amount = []
lovelace_amount = {}
tokens_amount = {}
lovelace_amount['token'] = 'lovelace'
lovelace_amount['amount'] = LOVELACE_AMOUNT
tokens_amount['token'] = TOKEN_NAME
tokens_amount['amount'] = t_amount
amount.append(lovelace_amount)
amount.append(tokens_amount)
total_lovelace += LOVELACE_AMOUNT
total_tokens += int(t_amount)
amounts[address] = amount
dst_addresses.append(address)
spend_amounts = {}
spend_amounts['lovelace'] = total_lovelace
spend_amounts[TOKEN_NAME] = total_tokens
return airdrops_list, spend_amounts, dst_addresses, amounts, out, err
def generate_protocol_file():
"""
generate the protocol file
"""
if len(MAGIC_NUMBER) == 0:
cmd = ["cardano-cli", "query", "protocol-parameters", CARDANO_NET, "--out-file", PROTOCOL_FILE]
else:
cmd = ["cardano-cli", "query", "protocol-parameters", CARDANO_NET, str(MAGIC_NUMBER),
"--out-file", PROTOCOL_FILE]
out, err = cardano_cli_cmd(cmd)
return out, err
def get_tip():
"""
query tip
"""
if len(MAGIC_NUMBER) == 0:
cmd = ["cardano-cli", "query", "tip", CARDANO_NET]
else:
cmd = ["cardano-cli", "query", "tip", CARDANO_NET, str(MAGIC_NUMBER)]
out, err = cardano_cli_cmd(cmd)
return out, err
def get_available_amounts(src_addresses):
"""
Get the amount of available ADA and tokens at the src_addresses
"""
# source UTxOs grouped by source address
source_transactions = {}
# get the list of transactions from all source addresses
src_transactions = []
src_token_transactions = []
tokens_amounts = []
for src_addr in src_addresses:
src_trans, src_token_trans, tok_amounts, out, err = get_transactions(src_addr)
if err:
msg = {}
msg['error'] = err
return [], [], [], [], msg
else:
utxos = {}
utxos['src_transactions'] = src_trans
utxos['src_token_transactions'] = src_token_trans
source_transactions[src_addr] = utxos
src_transactions += src_trans
src_token_transactions += src_token_trans
tokens_amounts.append(tok_amounts)
#
total_available = {}
for t in tokens_amounts:
for k in t.keys():
if k in total_available:
total_available[k] += t[k]
else:
total_available[k] = t[k]
tokens_amounts = total_available
return source_transactions, src_transactions, src_token_transactions, tokens_amounts, ''
def get_utxo_list(address):
"""
Get the list of UTxOs from the given addresses.
:param address: Cardano Blockchain address to search for UTXOs
:return: utxo_list: list of UTxOs
"""
if len(MAGIC_NUMBER) == 0:
cmd = ["cardano-cli", "query", "utxo", "--address", address, CARDANO_NET]
else:
cmd = ["cardano-cli", "query", "utxo", "--address", address, CARDANO_NET, str(MAGIC_NUMBER)]
out, err = cardano_cli_cmd(cmd)
utxo_list = []
if not err:
for line in out.splitlines():
if 'lovelace' in line:
trans = line.split()
utxo_list.append(trans[0])
return utxo_list
def get_transactions(address, max_utxos=MAX_IN_UTXOS):
"""
Get the list of transactions from the given addresses. They will be used as input UTxOs.
:param address: Cardano Blockchain address to search for UTXOs
:param max_utxos: Maximum number of UTxOs to read from the address. if the number is too big,
the transaction size will be over the maximum transaction size allowed by Cardano.
:return: ada_transactions, token_transactions
ada_transactions: list of transactions with lovelace only
token_transactions: list of transactions including custom tokens
"""
if len(MAGIC_NUMBER) == 0:
cmd = ["cardano-cli", "query", "utxo", "--address", address, CARDANO_NET]
else:
cmd = ["cardano-cli", "query", "utxo", "--address", address, CARDANO_NET, str(MAGIC_NUMBER)]
out, err = cardano_cli_cmd(cmd)
tokens_amounts = {}
ada_transactions = []
token_transactions = []
nr_utxos = 0
if not err:
for line in out.splitlines():
nr_utxos += 1
# exit loop if the max number of UTxOs has been reached
if nr_utxos > max_utxos:
break
if 'lovelace' in line:
transaction = {}
trans = line.split()
# if this is an UTxO with only lovelace in it
if len(trans) == 6:
transaction['hash'] = trans[0]
transaction['id'] = trans[1]
transaction['amount'] = trans[2]
ada_transactions.append(transaction)
# add the lovelace to total amounts to spend
if 'lovelace' in tokens_amounts:
tokens_amounts['lovelace'] += int(transaction['amount'])
else:
tokens_amounts['lovelace'] = int(transaction['amount'])
# if there are also other tokens
else:
transaction['hash'] = trans[0]
transaction['id'] = trans[1]
transaction['amounts'] = []
tr_amount = {}
tr_amount['token'] = trans[3]
tr_amount['amount'] = trans[2]
transaction['amounts'].append(tr_amount)
# for each token
for i in range(0, int((len(trans) - 4) / 3)):
tr_amount = {}
tr_amount['token'] = trans[3 + i * 3 + 3]
tr_amount['amount'] = trans[3 + i * 3 + 2]
transaction['amounts'].append(tr_amount)
token_transactions.append(transaction)
# add the tokens to total amounts to spend
for t in transaction['amounts']:
if t['token'] in tokens_amounts:
tokens_amounts[t['token']] += int(t['amount'])
else:
tokens_amounts[t['token']] = int(t['amount'])
return ada_transactions, token_transactions, tokens_amounts, out, err
def get_airdrop_details(cur, airdrop_id):
"""
Return all the details about an airdrop from the database
:param airdrop_id:
:return: a disctionary with all the airdrop details
"""
airdrop_details = {}
airdrop_transactions = []
cur.execute("SELECT hash, status, date, id FROM airdrops WHERE id = ?", (airdrop_id, ))
airdrop = cur.fetchone()
airdrop_details['airdrop_id'] = airdrop[0]
airdrop_details['status'] = airdrop[1]
airdrop_details['date'] = airdrop[2]
cur.execute("SELECT hash, name, status, date FROM transactions WHERE airdrop_id = ?", (airdrop[3], ))
transactions = cur.fetchall()
for trans in transactions:
airdrop_transaction = {}
airdrop_transaction['transaction_hash'] = trans[0]
airdrop_transaction['transaction_name'] = trans[1]
airdrop_transaction['transaction_status'] = trans[2]
airdrop_transaction['transaction_data'] = trans[3]
airdrop_transactions.append(airdrop_transaction)
airdrop_details['transactions'] = airdrop_transactions
return airdrop_details
def validate_transaction(spend_amounts, tokens_amounts):
"""
A transaction is considered valid here if the amounts of tokens
in the source UTXOs are greater than or equal to the amounts to spend.
:param spend_amounts: amounts to spend
:param tokens_amounts: existing amounts to spend from
:return: True if transaction is valid, otherwise False
"""
for am in spend_amounts:
if am not in tokens_amounts or spend_amounts[am] > tokens_amounts[am]:
return False
return True
def sign_transaction(payment_skeys, infile, outfile):
"""
Sign a raw transaction file.
:param payment_skeys: payment signing keys
:param infile: transaction raw file
:param outfile: transaction signed file
:return:
"""
cmd = ["cardano-cli", "transaction", "sign", "--tx-body-file", infile]
for pkey in payment_skeys:
cmd += ["--signing-key-file", pkey]
if len(MAGIC_NUMBER) == 0:
cmd += [CARDANO_NET, "--out-file", outfile]
else:
cmd += [CARDANO_NET, str(MAGIC_NUMBER), "--out-file", outfile]
out, err = cardano_cli_cmd(cmd)
return out, err
def cardano_cli_cmd(cmd):
"""
Execute a cardano-cli command.
:param cmd: command to execute
:return: output of the command and error message, if any
"""
out, err = subprocess.Popen(
cmd, env=env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE).communicate()
out = out.decode('utf-8')
err = err.decode('utf-8')
"""
if err and 'Warning' not in err and 'Ok.' not in err:
print(cmd)
print(err)
sys.exit(1)
"""
return out, err