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';
8 function transactionSDK(bytom) {
9 this.http = bytom.serverHttp;
15 * List all the transactions related to a wallet or an address.
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
25 transactionSDK.prototype.list = function(address, filter,sort, start, limit) {
26 let net = this.bytom.net;
37 let url = 'merchant/transactions';
38 let args = new URLSearchParams();
39 if (typeof start !== 'undefined') {
40 args.append('start', start);
43 args.append('limit', limit);
46 args.append('address', address);
48 url = url + '?' + args.toString();
49 return this.http.request(url, pm, net);
53 * List all the transactions related to a wallet or an address.
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
62 transactionSDK.prototype.listDelayTransaction = function(baseUrl, address, start, limit) {
63 const _http = new http(baseUrl)
66 let path = '/lingerdesire/v1/list-delay-transfers';
67 let args = new URLSearchParams();
68 if (typeof start !== 'undefined') {
69 args.append('start', start);
72 args.append('limit', limit);
75 args.append('address', address);
77 path = path + '?' + args.toString();
78 return _http.request(path, pm);
82 * Submit a signed transaction to the chain.
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
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);
96 * Build a raw transaction transfered from the wallet.
97 * May use all available addresses (under the wallet) as source addresses if not specified.
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
108 transactionSDK.prototype.buildPayment = function(address, to, asset, amount, confirmations, memo, forbidChainTx) {
109 let net = this.bytom.net;
113 forbid_chain_tx: false
116 pm['recipients'][`${to}`] = amount;
122 pm.forbid_chain_tx = forbidChainTx;
125 pm.confirmations = confirmations;
127 return this.http.request(`merchant/build-payment?address=${address}`, pm, net);
131 * Build a cross chain transaction.
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
140 transactionSDK.prototype.buildCrossChain = function(address, to, asset, amount, confirmations, forbidChainTx) {
141 let net = this.bytom.net;
146 forbid_chain_tx: false
149 pm['recipients'][`${to}`] = amount;
152 pm.forbid_chain_tx = forbidChainTx;
155 return this.http.request(`merchant/build-crosschain?address=${address}`, pm, net);
159 * Build a Vote transaction.
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
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};
172 pm.confirmations = confirmations;
178 pm.forbid_chain_tx = forbidChainTx;
181 return this.http.request(`merchant/build-vote?address=${address}`, pm, net);
184 transactionSDK.prototype.estimateFee = function(address, asset_amounts, confirmations=1) {
185 let net = this.bytom.net;
186 let pm = {asset_amounts, confirmations};
188 return this.http.request(`merchant/estimate-tx-fee?address=${address}`, pm, net);
192 * Build a Veto transaction.
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
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};
205 pm.confirmations = confirmations;
211 pm.forbid_chain_tx = forbidChainTx;
214 return this.http.request(`merchant/build-veto?address=${address}`, pm, net);
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.
221 * @param {String} guid unique id for each wallet
222 * @param {Number} fee transaction fee amount
225 transactionSDK.prototype.buildTransaction = function(address, inputs, outputs, fee, confirmations, forbid_chain_tx = true) {
226 let net = this.bytom.net;
236 pm.confirmations = confirmations;
238 return this.http.request(`merchant/build-advanced-tx?address=${address}`, pm, net);
242 transactionSDK.prototype._signTransactionJs = function( transaction, password, key) {
243 let tx = camelize(JSON.parse(transaction));
245 return signJs(tx, password, key);
248 transactionSDK.prototype._signTransactionJsPromise = function( transaction, password, key) {
249 let retPromise = new Promise((resolve, reject) => {
251 let result = this._signTransactionJs(transaction, password, key);
265 * @param {String} type - type.
266 * @param {String} value - value.
268 transactionSDK.prototype.convertArgument = function(type, value) {
269 let retPromise = new Promise((resolve, reject) => {
272 data.raw_data = JSON.stringify({value});
274 const result = convertArgument(data).data
285 export default transactionSDK;