-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
177 lines (157 loc) · 6.21 KB
/
main.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
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
#!/usr/bin/env python3
"""
Created: 11.03.2021
Updated: 20.03.2021
Updated: 26.03.2021 - Token Transactions
Updated: 27.03.2021 - Bug fixes, validation, tests
"""
import json
import datetime
from library import *
if __name__ == '__main__':
# transform lovelace amounts from integers to dictionaries
amounts, spend_amounts = transform_amounts(AMOUNTS)
# 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]
_, _ = cardano_cli_cmd(cmd)
# 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)
# set transaction expire time in TRANSACTION_EXPIRE seconds (default 300)
expire = json.loads(out)['slot'] + TRANSACTION_EXPIRE
# get the list of transactions from all source addresses
src_transactions = []
src_token_transactions = []
src_address = ''
tokens_amounts = {}
for address in SRC_ADDRESSES:
try:
with open(address, 'r') as f:
src_addr = f.read()
if src_address == '':
# keep the first source address for change, if required
src_address = src_addr
except Exception as err:
print('Error while opening the file: %s' % err)
sys.exit(1)
trans, token_trans, out, err = get_transactions(src_addr, tokens_amounts, MAX_IN_UTXOS)
if err:
print(err)
sys.exit(1)
else:
src_transactions += trans
src_token_transactions += token_trans
# debug
if len(src_transactions) == 0 and len(src_token_transactions) == 0:
print('No source transactions (UTXOs)!')
sys.exit(1)
# print('Source transactions: %s' % src_transactions)
# print('Source token transactions: %s' % src_token_transactions)
# print('Change address (if required): %s' % src_address)
#print('Tokens amounts: %s' % tokens_amounts)
#print("Amounts to spend: %s" % spend_amounts)
# validate transation
if not validate_transaction(spend_amounts, tokens_amounts):
print("Spending more than existing amounts is not possible!")
sys.exit(1)
# get the list of destination addresses
dst_addresses = []
for address in DST_ADDRESSES:
try:
with open(address, 'r') as f:
dst_addr = f.read()
except Exception as err:
print('Error while opening the file: %s' % err)
sys.exit(1)
dst_addresses += dst_addr.splitlines()
# debug
if len(dst_addresses) == 0:
print('No destination addresses!')
sys.exit(1)
#print('Destination addresses: %s\n' % dst_addresses)
# create draft transaction
_, err, incount, outcount, cmd_transaction = create_transaction(src_transactions, src_token_transactions,
src_address, dst_addresses, 200000, 'tx.draft',
expire, amounts, tokens_amounts)
if err:
print(err)
sys.exit(1)
# calculate transaction fee
out, err = calculate_fee('tx.draft', incount, outcount, len(SRC_KEYS))
if not err:
fee = int(out.split()[0])
else:
print(err)
sys.exit(1)
#print('Transaction fee: %s\n' % fee)
# create transaction
_, err, incount, outcount, cmd_transaction = create_transaction(src_transactions, src_token_transactions,
src_address, dst_addresses, fee, 'tx.raw',
expire, amounts, tokens_amounts)
if err:
print(err)
sys.exit(1)
# sign transaction
_, err = sign_transaction(SRC_KEYS, 'tx.raw', 'tx.signed')
if err:
print(err)
sys.exit(1)
# Transaction
print('Tokens amounts: %s' % tokens_amounts)
print('Amounts to spend: %s' % spend_amounts)
print('Destination addresses: %s\n' % dst_addresses)
print('Transaction fee: %s\n' % fee)
print('Command to execute: %s\n' % cmd_transaction)
if len(MAGIC_NUMBER) == 0:
cmd = ["cardano-cli", "transaction", "submit", "--tx-file", 'tx.signed', CARDANO_NET]
else:
cmd = ["cardano-cli", "transaction", "submit", "--tx-file", 'tx.signed', CARDANO_NET, str(MAGIC_NUMBER)]
# submit transaction
"""
out, err = cardano_cli_cmd(cmd)
if err:
print(err)
sys.exit(1)
print('Transaction executed')
print(out)
"""
with(open(LOG_FILE, 'a')) as lf:
lf.write('------------------------------------------\n')
lf.write('%s\n' % datetime.datetime.now().strftime("%d.%b %Y %H:%M:%S"))
lf.write('Tokens amounts: %s\n' % tokens_amounts)
lf.write('Amounts to spend: %s\n' % spend_amounts)
lf.write('Destination addresses: %s\n' % dst_addresses)
lf.write('Transaction fee: %s\n' % fee)
lf.write('Command to execute: %s\n' % cmd_transaction)
#lf.write(out)
lf.write('------------------------------------------\n')
#sys.exit(0)
# ask for confirmation before sending the transaction
while True:
reply = input('Confirm? [y/n] ')
if reply.lower() in ('y', 'yes'):
"""
if len(MAGIC_NUMBER) == 0:
cmd = ["cardano-cli", "transaction", "submit", "--tx-file", 'tx.signed', CARDANO_NET]
else:
cmd = ["cardano-cli", "transaction", "submit", "--tx-file", 'tx.signed', CARDANO_NET, str(MAGIC_NUMBER)]
"""
# submit transaction
out, err = cardano_cli_cmd(cmd)
if err:
print(err)
sys.exit(1)
print('Transaction executed')
print(out)
break
elif reply.lower() in ('n', 'no'):
print('Transaction cancelled')
break
else:
print('Invalid answer, please input "y" or "n":')