OSDN Git Service

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