OSDN Git Service

update transfer page.
[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(), 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(address, inputs, outputs, gas, confirmations) {
92   let retPromise = new Promise((resolve, reject) => {
93     bytom.transaction
94       .buildTransaction(address, 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, context) {
106   let retPromise = new Promise((resolve, reject) => {
107       signTx(
108         guid,
109         JSON.stringify(snakeize(transaction)),
110         password,
111         context
112       )
113       .then(res => {
114         resolve(res);
115       })
116       .catch(error => {
117         reject(error);
118       });
119   });
120   return retPromise;
121 };
122
123 transaction.decodeTransaction = function(rawTx) {
124   let retPromise = new Promise((resolve, reject) => {
125     bytom.transaction
126       .decodeTransaction(rawTx)
127       .then(res => {
128         resolve(res);
129       })
130       .catch(error => {
131         reject(error);
132       });
133   });
134   return retPromise;
135 };
136
137 transaction.transfer = function(transaction, password, address, context) {
138   let retPromise = new Promise((resolve, reject) => {
139
140     const {to, asset, amount, confirmations} = transaction
141     bytom.transaction
142       .buildPayment(address, to, asset, amount.toString(), confirmations)
143       .then(result => {
144         Promise.all(result.map( (data) => 
145           signSubmit( data, password, address, context)))
146             .then((ret )=>{
147               resolve(ret)
148             })
149             .catch(error => {
150               throw error
151             });
152       })
153       .catch(error => {
154         reject(error);
155       });
156   })
157
158   return retPromise;
159 };
160
161 function signSubmit (txObject, password, address, context) {
162   let retPromise = new Promise((resolve, reject) => {
163         signTx(
164           address,
165           JSON.stringify(snakeize(txObject)),
166           password,
167           context
168         )
169           .then(ret => {
170             bytom.transaction
171               .submitPayment(address, ret.raw_transaction, ret.signatures)
172               .then(res3 => {
173                 const object = {
174                   transactionHash: res3.txHash
175                 }
176                 resolve(object);
177               })
178               .catch(error => {
179                 throw error
180               });
181           })
182           .catch(error => {
183             reject(error);
184           });
185       });
186   return retPromise;
187 };
188
189
190 transaction.signMessage = function(message, password, address) {
191   return bytom.keys.signMessage(message, password,address);
192 };
193
194 transaction.advancedTransfer = function(guid, transaction, password, arrayData, address, context) {
195   let retPromise = new Promise((resolve, reject) => {
196       signTx(
197         guid,
198         JSON.stringify(snakeize(transaction)),
199         password,
200         context
201       )
202       .then(ret => {
203         let signatures = ret.signatures
204         if(arrayData){
205           signatures[0] = arrayData
206         }
207         bytom.transaction
208           .submitPayment(address, ret.raw_transaction, signatures)
209           .then(res3 => {
210             const object ={
211               transactionHash: res3.txHash
212             }
213             resolve(object);
214           })
215           .catch(error => {
216             reject(error);
217           });
218       })
219       .catch(error => {
220         reject(error);
221       });
222   });
223
224   return retPromise;
225 };
226
227
228
229 function signTx(address, transaction, password, context){
230   const keyArray = context.bytom.keychain.findByAddress(address);
231   if(!keyArray){
232     throw 'Account not found.'
233   }else{
234     const key = JSON.stringify(keyArray.keystore)
235     return bytom.transaction
236       ._signTransaction(
237         transaction,
238         password,
239         key
240       )
241   }
242 }
243
244 transaction.estimateFee = function(address, asset_amounts, confirmations){
245   return bytom.transaction.estimateFee(address, asset_amounts, confirmations)
246 }
247
248 export default transaction;