OSDN Git Service

0e2009570c1abed3390c1ef3390b4cc56997dfe2
[bytom/bytom-spanner.git] / utxomerger / Transaction.py
1 import json
2
3 from Account import Account
4
5
6 class Transaction(object):
7
8     @staticmethod
9     def list_transactions(connection):
10         response = connection.request("/list-transactions")
11
12         resp_json = json.loads(response.text)
13
14         if resp_json['status'] == 'success':
15             return resp_json['data']
16         elif resp_json['status'] == 'fail':
17             return resp_json['msg']
18         else:
19             return resp_json
20
21     @staticmethod
22     def list_by_account_id(connection, account_id):
23         body_json = {"account_id": account_id}
24         response = connection.request("/list-transactions", body_json)
25
26         resp_json = json.loads(response.text)
27
28         if resp_json['status'] == 'success':
29             return resp_json['data']
30         elif resp_json['status'] == 'fail':
31             return resp_json['msg']
32         else:
33             return resp_json
34
35     @staticmethod
36     def list_by_account_alias(connection, account_alias):
37         account = Account.find_by_alias(connection, account_alias)
38         body_json = {"account_id": account.id}
39         response = connection.request("/list-transactions", body_json)
40
41         resp_json = json.loads(response.text)
42
43         if resp_json['status'] == 'success':
44             return resp_json['data']
45         elif resp_json['status'] == 'fail':
46             return resp_json['msg']
47         else:
48             return resp_json
49
50     @staticmethod
51     def get_transaction(connection, tx_id):
52         body_json = {"tx_id": tx_id}
53         response = connection.request("/get-transaction", body_json)
54
55         resp_json = json.loads(response.text)
56
57         if resp_json['status'] == 'success':
58             return resp_json['data'], 1
59         elif resp_json['status'] == 'fail':
60             return resp_json['msg'], -1
61         else:
62             return resp_json, 0
63
64     @staticmethod
65     def build_transaction(connection, actions):
66         # ttl: 15min=900000ms
67         body_json = {"base_transaction": None, "actions": actions,
68                      "ttl": 1, "time_range": 0}
69         response = connection.request("/build-transaction", body_json)
70
71         resp_json = json.loads(response.text)
72
73         if resp_json['status'] == 'success':
74             return resp_json['data']
75         elif resp_json['status'] == 'fail':
76             return resp_json['msg']
77         else:
78             return resp_json
79
80     @staticmethod
81     def sign_transaction(connection, password, transaction):
82         body_json = {"password": password, "transaction": transaction}
83         response = connection.request("/sign-transaction", body_json)
84
85         resp_json = json.loads(response.text)
86
87         if resp_json['status'] == 'success':
88             return resp_json['data']
89         elif resp_json['status'] == 'fail':
90             return resp_json['msg']
91         else:
92             return resp_json
93
94     @staticmethod
95     def submit_transaction(connection, raw_transaction):
96         body_json = {"raw_transaction": raw_transaction}
97         response = connection.request("/submit-transaction", body_json)
98
99         resp_json = json.loads(response.text)
100
101         if resp_json['status'] == 'success':
102             return resp_json['data']
103
104
105 class Action(object):
106
107     @staticmethod
108     def spend_account(amount, account_id, asset_id):
109         return {
110             'amount': amount,
111             'account_id': account_id,
112             'asset_id': asset_id,
113             'type': 'spend_account'
114         }
115
116     @staticmethod
117     def issue(amount, asset_id):
118         return {
119             'amount': amount,
120             'asset_id': asset_id,
121             'type': 'issue'
122         }
123
124     @staticmethod
125     def gas(amount, account_id, asset_id):
126         return {
127             'amount': amount,
128             'account_id': account_id,
129             'asset_id': asset_id,
130             'type': 'spend_account'
131         }
132
133     @staticmethod
134     def control_address(amount, asset_id, address):
135         return {
136             'amount': amount,
137             'asset_id': asset_id,
138             'address': address,
139             'type': 'control_address'
140         }
141
142     @staticmethod
143     def unspent_output(output_id):
144         return {
145             'type': 'spend_account_unspent_output',
146             'output_id': output_id
147         }