OSDN Git Service

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