OSDN Git Service

Merge pull request #2 from Bytom/dev
[bytom/Bytom-JS-SDK.git] / src / sdk / transaction.js
1 import {signTransaction as signJs} from '../utils/transaction/signTransaction';
2 import { camelize } from '../utils/utils';
3 import {convertArgument} from '../utils/convertArguement';
4 import {http} from '../http.js';
5
6
7
8 function transactionSDK(bytom) {
9     this.http = bytom.serverHttp;
10     this.bytom = bytom;
11 }
12
13
14 /**
15  * List all the transactions related to a wallet or an address.
16  *
17  * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmmerchantlist-transactions
18  * @param {String} guid unique id for each wallet
19  * @param {String} filter (optional) if provided, will only return transactions the filter condition is related to
20  * @param {String} sort (optional) if provided, will only return transactions the sort condition is related to
21  * @param {Number} start page start
22  * @param {Number} limit page limit
23  * @returns {Promise}
24  */
25 transactionSDK.prototype.list = function(address, filter,sort, start, limit) {
26     let net = this.bytom.net;
27     let pm = {};
28
29     if (filter) {
30         pm.filter = filter;
31     }
32
33     if (sort) {
34         pm.sort = sort;
35     }
36
37     let url = 'merchant/transactions';
38     let args = new URLSearchParams();
39     if (typeof start !== 'undefined') {
40         args.append('start', start);
41     }
42     if (limit) {
43         args.append('limit', limit);
44     }
45     if (address) {
46         args.append('address', address);
47     }
48     url = url + '?' + args.toString();
49     return this.http.request(url, pm, net);
50 };
51
52 /**
53  * List all the transactions related to a wallet or an address.
54  *
55  * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmmerchantlist-transactions
56  * @param {String} baseUrl request url,
57  * @param {String} address transaction address
58  * @param {Number} start page start
59  * @param {Number} limit page limit
60  * @returns {Promise}
61  */
62 transactionSDK.prototype.listDelayTransaction = function(baseUrl, address, start, limit) {
63     const _http = new http(baseUrl)
64     let pm = {};
65
66     let path = '/lingerdesire/v1/list-delay-transfers';
67     let args = new URLSearchParams();
68     if (typeof start !== 'undefined') {
69         args.append('start', start);
70     }
71     if (limit) {
72         args.append('limit', limit);
73     }
74     if (address) {
75         args.append('address', address);
76     }
77     path = path + '?' + args.toString();
78     return _http.request(path, pm);
79 };
80
81 /**
82  * Submit a signed transaction to the chain.
83  *
84  * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmmerchantsubmit-payment
85  * @param {String} guid unique id for each wallet
86  * @param {String} raw_transaction raw transaction bytes encoded to string
87  * @param {Array} signatures signed data of each signing instruction
88  */
89 transactionSDK.prototype.submitPayment = function(address, raw_transaction, signatures) {
90     let net = this.bytom.net;
91     let pm = {raw_transaction: raw_transaction, signatures: signatures};
92     return this.http.request(`merchant/submit-payment?address=${address}`, pm, net);
93 };
94
95 /**
96  * Build a raw transaction transfered from the wallet. 
97  * May use all available addresses (under the wallet) as source addresses if not specified.
98  * 
99  * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmmerchantbuild-payment
100  * @param {String} guid unique id for each wallet
101  * @param {String} to destination address
102  * @param {String} asset hexdecimal asset id
103  * @param {Number} amount transfer amount
104  * @param {Number} fee transaction fee amount
105  * @param {Number} confirmations - transaction confirmations
106  * @returns {Promise}
107  */
108 transactionSDK.prototype.buildPayment = function(address, to, asset, amount, confirmations, memo, forbidChainTx) {
109     let net = this.bytom.net;
110     let pm = {
111         asset: asset,
112         recipients: {},
113         forbid_chain_tx: false
114     };
115
116     pm['recipients'][`${to}`] = amount;
117
118     if (memo) {
119         pm.memo = memo;
120     }
121     if (forbidChainTx) {
122         pm.forbid_chain_tx = forbidChainTx;
123     }
124     if (confirmations) {
125         pm.confirmations = confirmations;
126     }
127     return this.http.request(`merchant/build-payment?address=${address}`, pm, net);
128 };
129
130 /**
131  * Build a cross chain transaction.
132  *
133  * @param {String} guid unique id for each wallet
134  * @param {String} to destination address
135  * @param {String} asset hexdecimal asset id
136  * @param {Number} amount transfer amount
137  * @param {Number} confirmations - transaction confirmations
138  * @returns {Promise}
139  */
140 transactionSDK.prototype.buildCrossChain = function(address, to, asset, amount, confirmations, forbidChainTx) {
141     let net = this.bytom.net;
142
143     let pm = {
144         asset: asset,
145         recipients: {},
146         forbid_chain_tx: false
147     };
148
149     pm['recipients'][`${to}`] = amount;
150
151     if (forbidChainTx) {
152         pm.forbid_chain_tx = forbidChainTx;
153     }
154
155     return this.http.request(`merchant/build-crosschain?address=${address}`, pm, net);
156 };
157
158 /**
159  * Build a Vote transaction.
160  *
161  * @param {String} guid unique id for each wallet
162  * @param {String} vote pubkey vote
163  * @param {String} memo memo key
164  * @param {Number} amount transfer amount
165  * @param {Number} confirmations - transaction confirmations
166  * @returns {Promise}
167  */
168 transactionSDK.prototype.buildVote = function(address, vote, amount, confirmations, memo, forbidChainTx) {
169     let net = this.bytom.net;
170     let pm = {vote, amount: amount, forbid_chain_tx: false};
171     if (confirmations) {
172         pm.confirmations = confirmations;
173     }
174     if (memo) {
175         pm.memo = memo;
176     }
177     if (forbidChainTx) {
178         pm.forbid_chain_tx = forbidChainTx;
179     }
180
181     return this.http.request(`merchant/build-vote?address=${address}`, pm, net);
182 };
183
184 transactionSDK.prototype.estimateFee = function(address, asset_amounts, confirmations=1) {
185     let net = this.bytom.net;
186     let pm = {asset_amounts, confirmations};
187
188     return this.http.request(`merchant/estimate-tx-fee?address=${address}`, pm, net);
189 };
190
191 /**
192  * Build a Veto transaction.
193  *
194  * @param {String} guid unique id for each wallet
195  * @param {String} vote pubkey vote
196  * @param {String} memo memo key
197  * @param {Number} amount transfer amount
198  * @param {Number} confirmations - transaction confirmations
199  * @returns {Promise}
200  */
201 transactionSDK.prototype.buildVeto = function(address, vote, amount, confirmations, memo,  forbidChainTx) {
202     let net = this.bytom.net;
203     let pm = { vote, amount: amount, forbid_chain_tx: false};
204     if (confirmations) {
205         pm.confirmations = confirmations;
206     }
207     if (memo) {
208         pm.memo = memo;
209     }
210     if (forbidChainTx) {
211         pm.forbid_chain_tx = forbidChainTx;
212     }
213
214     return this.http.request(`merchant/build-veto?address=${address}`, pm, net);
215 };
216
217 /**
218  * Advanced Build a raw transaction transfered from the wallet.
219  * May use all available addresses (under the wallet) as source addresses if not specified.
220  *
221  * @param {String} guid unique id for each wallet
222  * @param {Number} fee transaction fee amount
223  * @returns {Promise}
224  */
225 transactionSDK.prototype.buildTransaction = function(address, inputs, outputs, fee, confirmations, forbid_chain_tx = true) {
226     let net = this.bytom.net;
227     let pm = {
228         inputs,
229         outputs,
230         forbid_chain_tx
231     };
232     if (fee) {
233         pm.fee = fee;
234     }
235     if (confirmations) {
236         pm.confirmations = confirmations;
237     }
238     return this.http.request(`merchant/build-advanced-tx?address=${address}`, pm, net);
239 };
240
241
242 transactionSDK.prototype._signTransactionJs = function( transaction, password, key) {
243     let tx = camelize(JSON.parse(transaction));
244
245     return signJs(tx, password, key);
246 };
247
248 transactionSDK.prototype._signTransactionJsPromise = function( transaction, password, key) {
249     let retPromise = new Promise((resolve, reject) => {
250         try{
251             let result = this._signTransactionJs(transaction, password, key);
252             resolve(result);
253         }
254         catch(error) {
255             reject(error);
256         }
257     });
258
259     return retPromise;
260 };
261
262 /**
263  * Convert arguement.
264  *
265  * @param {String} type - type.
266  * @param {String} value - value.
267  */
268 transactionSDK.prototype.convertArgument = function(type, value) {
269     let retPromise = new Promise((resolve, reject) => {
270         let data = {};
271         data.type = type;
272         data.raw_data = JSON.stringify({value});
273         try{
274             const result = convertArgument(data).data
275             resolve(result);
276         }
277         catch(error){
278             reject(error);
279         }
280     });
281     return retPromise;
282 };
283
284
285 export default transactionSDK;