Python based Bytom wallet tools
Revision | 50901e6bb336bb14c2e9f75d1d77d3fa71572809 (tree) |
---|---|
Zeit | 2018-06-21 18:33:14 |
Autor | successli <successli@outl...> |
Commiter | successli |
utxos list divide not mature utxo
@@ -55,16 +55,17 @@ class Transaction(object): | ||
55 | 55 | resp_json = json.loads(response.text) |
56 | 56 | |
57 | 57 | if resp_json['status'] == 'success': |
58 | - return resp_json['data'] | |
58 | + return resp_json['data'], 1 | |
59 | 59 | elif resp_json['status'] == 'fail': |
60 | - return resp_json['msg'] | |
60 | + return resp_json['msg'], -1 | |
61 | 61 | else: |
62 | - return resp_json | |
62 | + return resp_json, 0 | |
63 | 63 | |
64 | 64 | @staticmethod |
65 | 65 | def build_transaction(connection, actions): |
66 | + # ttl: 15min=900000ms | |
66 | 67 | body_json = {"base_transaction": None, "actions": actions, |
67 | - "ttl": 0, "time_range": 0} | |
68 | + "ttl": 1, "time_range": 0} | |
68 | 69 | response = connection.request("/build-transaction", body_json) |
69 | 70 | |
70 | 71 | resp_json = json.loads(response.text) |
@@ -1,5 +1,6 @@ | ||
1 | 1 | import json |
2 | 2 | import os |
3 | +import time | |
3 | 4 | |
4 | 5 | from Account import Account |
5 | 6 | from Asset import Asset |
@@ -9,6 +10,19 @@ from Transaction import Action, Transaction | ||
9 | 10 | class UnspentOutputs(object): |
10 | 11 | |
11 | 12 | @staticmethod |
13 | + def get_block_height(connection): | |
14 | + response = connection.request("/get-block-count") | |
15 | + | |
16 | + resp_json = json.loads(response.text) | |
17 | + | |
18 | + if resp_json['status'] == 'success': | |
19 | + return resp_json['data']['block_count'], 1 | |
20 | + elif resp_json['status'] == 'fail': | |
21 | + return resp_json['msg'], -1 | |
22 | + else: | |
23 | + return resp_json, 0 | |
24 | + | |
25 | + @staticmethod | |
12 | 26 | def list_UTXO(connection): |
13 | 27 | response = connection.request("/list-unspent-outputs") |
14 | 28 |
@@ -22,13 +36,24 @@ class UnspentOutputs(object): | ||
22 | 36 | return resp_json, 0 |
23 | 37 | |
24 | 38 | @staticmethod |
25 | - def list_by_account_asset(connection, account_alias, asset_alias): | |
26 | - utxos, ret = UnspentOutputs.list_UTXO(connection) | |
39 | + def list_mature_UTXO(connection): | |
27 | 40 | result = [] |
28 | - if ret == 1: | |
41 | + utxos, ret = UnspentOutputs.list_UTXO(connection=connection) | |
42 | + block_height, ret_code = UnspentOutputs.get_block_height(connection=connection) | |
43 | + if ret == 1 and ret_code == 1: | |
29 | 44 | for utxo in utxos: |
30 | - if utxo['account_alias'] == account_alias and utxo['asset_alias'] == asset_alias: | |
45 | + # append mature utxo to set | |
46 | + if utxo['valid_height'] < block_height: | |
31 | 47 | result.append(utxo) |
48 | + return result | |
49 | + | |
50 | + @staticmethod | |
51 | + def list_by_account_asset(connection, account_alias, asset_alias): | |
52 | + utxos = UnspentOutputs.list_mature_UTXO(connection) | |
53 | + result = [] | |
54 | + for utxo in utxos: | |
55 | + if utxo['account_alias'] == account_alias and utxo['asset_alias'] == asset_alias: | |
56 | + result.append(utxo) | |
32 | 57 | |
33 | 58 | return result |
34 | 59 |
@@ -74,14 +99,33 @@ class UnspentOutputs(object): | ||
74 | 99 | @staticmethod |
75 | 100 | def combine_utxos(connection, account_alias, password, asset_alias='BTM', max_amount=5000000000, size=20): |
76 | 101 | actions = UnspentOutputs.combine_actions(connection, account_alias, asset_alias, max_amount, size) |
77 | - print(actions) | |
102 | + # print(actions) | |
78 | 103 | issuance = Transaction.build_transaction(connection, actions) |
79 | - print("issuance:", issuance) | |
104 | + # print("issuance:", issuance) | |
80 | 105 | # sign transaction |
81 | 106 | signed_raw_transaction = Transaction.sign_transaction(connection, password, issuance) |
82 | - print("signed_raw_transaction:", signed_raw_transaction) | |
107 | + # print("signed_raw_transaction:", signed_raw_transaction) | |
83 | 108 | # submit transaction |
84 | - tx_id = Transaction.submit_transaction(connection, signed_raw_transaction) | |
109 | + tx_submit = Transaction.submit_transaction(connection, signed_raw_transaction) | |
110 | + tx_id = tx_submit['tx_id'] | |
85 | 111 | print("tx_id:", tx_id) |
86 | 112 | if tx_id is not None: |
87 | 113 | print("success to combine utxos.") |
114 | + | |
115 | + print("wait combine tx to chain...(about 2.5 minutes)") | |
116 | + | |
117 | + while True: | |
118 | + tx, ret = Transaction.get_transaction(connection=connection, tx_id=tx_id) | |
119 | + if ret == 1: | |
120 | + # print(tx) | |
121 | + if int(tx['block_index']) > 0: | |
122 | + print("combine tx to chain. You can combine next utxos.") | |
123 | + break | |
124 | + elif ret == -1: | |
125 | + print("tx:", tx) | |
126 | + break | |
127 | + time.sleep(2) | |
128 | + else: | |
129 | + print(actions) | |
130 | + print("issuance:", issuance) | |
131 | + print("signed_raw_transaction:", signed_raw_transaction) |
@@ -27,10 +27,11 @@ class TestTransactionMethods(unittest.TestCase): | ||
27 | 27 | print(tx) |
28 | 28 | |
29 | 29 | def test_get_transaction(self): |
30 | - con = Connection("http://127.0.0.1:9888") | |
31 | - tx_id = "5120dc0b2fe372e8f551b7e02ce5a44eb2a7b37108303d15ef8b601bfc9c1d5b" | |
32 | - tx = Transaction.get_transaction(con, tx_id) | |
30 | + con = Connection("http://116.62.130.184:9888") | |
31 | + tx_id = "eb535916e05ecee1a193ce489e5151c873f459639fcabffdc2df69f7f76fc550" | |
32 | + tx, ret = Transaction.get_transaction(con, tx_id) | |
33 | 33 | print(tx) |
34 | + print(ret) | |
34 | 35 | |
35 | 36 | def test_issue_transaction(self): |
36 | 37 | # build transaction |
@@ -7,6 +7,12 @@ from UnspentOutputs import UnspentOutputs | ||
7 | 7 | |
8 | 8 | class TestUTXOMethods(unittest.TestCase): |
9 | 9 | |
10 | + def test_get_block_height(self): | |
11 | + con = Connection("http://127.0.0.1:9888") | |
12 | + block_height, ret = UnspentOutputs.get_block_height(connection=con) | |
13 | + if ret == 1: | |
14 | + print(block_height) | |
15 | + | |
10 | 16 | def test_list_UTXO(self): |
11 | 17 | con = Connection("http://127.0.0.1:9888") |
12 | 18 | utxos, ret = UnspentOutputs.list_UTXO(con) |
@@ -47,7 +53,7 @@ class TestUTXOMethods(unittest.TestCase): | ||
47 | 53 | con = Connection("http://127.0.0.1:9888") |
48 | 54 | account_alias = "zhangsan" |
49 | 55 | asset_alias = "BTM" |
50 | - UnspentOutputs.combine_utxos(connection=con, account_alias=account_alias, password='123456', max_amount=5000000000, size=20) | |
56 | + UnspentOutputs.combine_utxos(connection=con, account_alias=account_alias, password='123456', max_amount=50, size=20) | |
51 | 57 | |
52 | 58 | if __name__ == '__main__': |
53 | 59 | unittest.main() |
@@ -0,0 +1,55 @@ | ||
1 | +import sys, getopt | |
2 | +from UnspentOutputs import UnspentOutputs | |
3 | +from connection import Connection | |
4 | + | |
5 | + | |
6 | +def main(argv): | |
7 | + try: | |
8 | + opts, args = getopt.getopt(argv, "hc:a:p:m:s:", | |
9 | + ["connection=", "account=", "password=", "max_amount=", "size="]) | |
10 | + except getopt.GetoptError: | |
11 | + print('python test.py -c <connection> -a <account_alias> -p <password>', | |
12 | + '[optional] -m <max_amount> -s <size>') | |
13 | + print( | |
14 | + 'python test.py --connection <connection> --account <account_alias> --password <password>', | |
15 | + '[optional] --max_amount <max_amount> --size <size>') | |
16 | + sys.exit(2) | |
17 | + | |
18 | + # print("opts", opts) | |
19 | + | |
20 | + connection = Connection.generate() | |
21 | + account_alias = "" | |
22 | + password = "" | |
23 | + max_amount = "" | |
24 | + size = "" | |
25 | + | |
26 | + for op, value in opts: | |
27 | + if op in ("-c", "--connection"): | |
28 | + connection = Connection(value) | |
29 | + # print(connection) | |
30 | + elif op in ("-a", "--account"): | |
31 | + account_alias = value | |
32 | + # print(account_alias) | |
33 | + elif op in ("-p", "--password"): | |
34 | + password = value | |
35 | + # print(password) | |
36 | + elif op in ("-m", "--max_amount"): | |
37 | + max_amount = int(value) | |
38 | + # print(max_amount) | |
39 | + elif op in ("-s", "--size"): | |
40 | + size = int(value) | |
41 | + # print(size) | |
42 | + elif op in ("-h", "--help"): | |
43 | + print('python test.py -c <connection> -a <account_alias> -p <password>', | |
44 | + '[optional] -m <max_amount> -s <size>') | |
45 | + print( | |
46 | + 'python test.py --connection <connection> --account <account_alias> --password <password>', | |
47 | + '[optional] --max_amount <max_amount> --size <size>') | |
48 | + sys.exit() | |
49 | + | |
50 | + UnspentOutputs.combine_utxos(connection=connection, account_alias=account_alias, | |
51 | + password=password, max_amount=max_amount, size=size) | |
52 | + | |
53 | + | |
54 | +if __name__ == '__main__': | |
55 | + main(sys.argv[1:]) |