OSDN Git Service

update the vote and veto v3 logic
[bytom/Byone.git] / src / models / transaction.js
1 import bytom from "./bytom";
2 import { snakeize } from "@/utils/utils";
3
4
5 let transaction = {};
6
7 transaction.list = function(guid, asset_id, start, limit, tx_types) {
8   const filter = {asset_id}
9   if(tx_types){
10     filter.tx_types = tx_types
11   }
12   return bytom.transaction.list(guid, filter, null, start, limit);
13 };
14
15 transaction.convertArgument = function(argArray) {
16   let fn = function asyncConvert(object){
17     const type = object.type
18     const value = object.value
19     return bytom.transaction.convertArgument(type, value)
20       .then(resp => resp.value);
21   };
22
23   let actionFunction = argArray.map(fn)
24   return Promise.all(actionFunction);
25 };
26
27 transaction.chainStatus = function() {
28   return bytom.query.getblockcount();
29 };
30
31 transaction.asset = function(asset_id) {
32   return bytom.query.asset(asset_id);
33 };
34
35 transaction.build = function(address, to, asset, amount, fee, confirmations) {
36   let retPromise = new Promise((resolve, reject) => {
37     bytom.transaction
38       .buildPayment(address, to, asset, amount.toString(), fee, confirmations)
39       .then(res => {
40         resolve(res);
41       })
42       .catch(error => {
43         reject(error);
44       });
45   });
46   return retPromise;
47 };
48
49 transaction.buildCrossChain = function(address, to, asset, amount, confirmations) {
50   let retPromise = new Promise((resolve, reject) => {
51     bytom.transaction
52       .buildCrossChain(address, to, asset, amount.toString(), confirmations)
53       .then(res => {
54         resolve(res);
55       })
56       .catch(error => {
57         reject(error);
58       });
59   });
60   return retPromise;
61 };
62
63 transaction.buildVote = function(address, vote, amount, confirmations, memo) {
64   let retPromise = new Promise((resolve, reject) => {
65     bytom.transaction
66       .buildVote(address, vote, amount.toString(), confirmations, memo)
67       .then(res => {
68         resolve(res);
69       })
70       .catch(error => {
71         reject(error);
72       });
73   });
74   return retPromise;
75 };
76
77 transaction.buildVeto = function(address, vote, amount, confirmations, memo) {
78   let retPromise = new Promise((resolve, reject) => {
79     bytom.transaction
80       .buildVeto(address, vote, amount.toString(), confirmations, memo)
81       .then(res => {
82         resolve(res);
83       })
84       .catch(error => {
85         reject(error);
86       });
87   });
88   return retPromise;
89 };
90
91 transaction.buildTransaction = function(guid, inputs, outputs, gas, confirmations) {
92   let retPromise = new Promise((resolve, reject) => {
93     bytom.transaction
94       .buildTransaction(guid, inputs, outputs, gas, confirmations)
95       .then(res => {
96         resolve(res);
97       })
98       .catch(error => {
99         reject(error);
100       });
101   });
102   return retPromise;
103 };
104
105 transaction.signTransaction = function(guid, transaction, password) {
106   let retPromise = new Promise((resolve, reject) => {
107     bytom.transaction
108       .signTransaction(guid, JSON.stringify(snakeize(transaction)), password)
109       .then(res => {
110         resolve(res);
111       })
112       .catch(error => {
113         reject(error);
114       });
115   });
116   return retPromise;
117 };
118
119 transaction.transfer = function(guid, transaction, password, address) {
120   let retPromise = new Promise((resolve, reject) => {
121     bytom.transaction
122       .signTransaction(guid, JSON.stringify(snakeize(transaction)), password)
123       .then(ret => {
124         bytom.transaction
125           .submitPayment(address, ret.raw_transaction, ret.signatures)
126           .then(res3 => {
127             resolve(res3);
128           })
129           .catch(error => {
130             reject(error);
131           });
132       })
133       .catch(error => {
134         reject(error);
135       });
136   });
137
138   return retPromise;
139 };
140
141
142 transaction.signMessage = function(message, password, address) {
143   return bytom.keys.signMessage(message, password,address);
144 };
145
146 transaction.advancedTransfer = function(guid, transaction, password, arrayData) {
147   let retPromise = new Promise((resolve, reject) => {
148     bytom.transaction
149       .signTransaction(guid, JSON.stringify(transaction), password)
150       .then(ret => {
151         let signatures = ret.signatures
152         if(arrayData){
153           signatures[0] = arrayData
154         }
155         bytom.transaction
156           .submitPayment(guid, ret.raw_transaction, signatures)
157           .then(res3 => {
158             resolve(res3);
159           })
160           .catch(error => {
161             reject(error);
162           });
163       })
164       .catch(error => {
165         reject(error);
166       });
167   });
168
169   return retPromise;
170 };
171
172 export default transaction;