OSDN Git Service

feat(input option): change getopt to argparse and add -c(output count) input option
[bytom/bytom-spanner.git] / btmtransaction.py
1 import os
2 import sys
3 import httprequest
4
5 miner_fee = 40000000
6 max_output_count = 1500
7
8
9 def handle_input(_path, _account_id, _password, _output_count):
10     if _output_count <= 0:
11         _output_count = max_output_count
12     lines = list()
13     with open(_path, 'r', encoding='utf-8') as file:
14         for line in file:
15             line = line.strip()
16             if len(lines) < _output_count:
17                 lines.append(line)
18             else:
19                 handle_transaction(lines, _path, _account_id, _password)
20                 lines.clear()
21                 lines.append(line)
22     if len(lines) > 0:
23         handle_transaction(lines, _path, _account_id, _password)
24     print('Transactions are completed.Please check completed.txt')
25
26
27 def handle_transaction(lines, _path, _account_id, _password):
28     data = build_transaction(lines, _account_id)
29     data = sign_transaction(_password, data)
30     if data['sign_complete']:
31         data = submit_transaction(data)
32         txid = data['tx_id']
33         if txid:
34             print('transaction_id:\n' + txid)
35             write_lines_to_file(lines, _path)
36     else:
37         print('Sign transaction is failed.Please check account password.')
38         sys.exit(1)
39
40
41 # parameter lines: max_output line list
42 def build_transaction(_lines, _account_id):
43     amount_sum = 0
44     action_list = list()
45     for line in _lines:
46         data = line.strip().split(',')
47         address = data[0]
48         amount = int(data[1])
49         amount_sum += amount
50         address_dict = get_address_dict(amount, address)
51         action_list.append(address_dict)
52     amount_sum += miner_fee
53     spend_dict = get_spend_dict(_account_id, int(amount_sum))
54     action_list.insert(0, spend_dict)
55     parameter = {'base_transaction': None, 'actions': action_list, 'ttl': 0}
56     return httprequest.post('build-transaction', parameter)
57
58
59 # parameter transaction: dict from build_transaction function return
60 def sign_transaction(_password, _transaction):
61     parameter = {'password': _password, 'transaction': _transaction}
62     return httprequest.post('sign-transaction', parameter)
63
64
65 def submit_transaction(_transaction):
66     parameter = {'raw_transaction': _transaction['transaction']['raw_transaction']}
67     return httprequest.post('submit-transaction', parameter)
68
69
70 # write complete transactions lines to file
71 def write_lines_to_file(lines, _path):
72     _path = os.path.abspath(os.path.dirname(_path)) + os.path.sep + 'completed.txt'
73     with open(_path, 'a', encoding='utf-8') as file:
74         file.write('\n'.join(lines) + '\n\n')
75
76
77 # control_address action
78 def get_address_dict(_amount, _address):
79     return {'amount': _amount,
80             'asset_id': 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
81             'address': _address,
82             'type': 'control_address'}
83
84
85 # spend_account action
86 def get_spend_dict(_account_id, _amount_sum):
87     return {'account_id': _account_id,
88             'amount': _amount_sum,
89             'asset_id': 'ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff',
90             'type': 'spend_account'}
91
92 # if __name__ == "__main__":
93 #     path = '/Users/john/Desktop/btm.txt'
94 #     account_id = '0F0BV1OLG0A04'
95 #     password = '123456'
96 #     sys.exit(handle_input(path, account_id, password))