1 import {signTransaction as signJs} from '../utils/transaction/signTransaction';
2 import { camelize } from '../utils/utils';
3 import {convertArgument} from '../utils/convertArguement';
6 function transactionSDK(bytom) {
7 this.http = bytom.serverHttp;
13 * List all the transactions related to a wallet or an address.
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
23 transactionSDK.prototype.list = function(address, filter,sort, start, limit) {
24 let net = this.bytom.net;
35 let url = 'merchant/transactions';
36 let args = new URLSearchParams();
37 if (typeof start !== 'undefined') {
38 args.append('start', start);
41 args.append('limit', limit);
44 args.append('address', address);
46 url = url + '?' + args.toString();
47 return this.http.request(url, pm, net);
51 * Submit a signed transaction to the chain.
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
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);
65 * Build a raw transaction transfered from the wallet.
66 * May use all available addresses (under the wallet) as source addresses if not specified.
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
77 transactionSDK.prototype.buildPayment = function(address, to, asset, amount, confirmations, memo, forbidChainTx) {
78 let net = this.bytom.net;
82 forbid_chain_tx: false
85 pm['recipients'][`${to}`] = amount;
91 pm.forbid_chain_tx = forbidChainTx;
94 pm.confirmations = confirmations;
96 return this.http.request(`merchant/build-payment?address=${address}`, pm, net);
100 * Build a cross chain transaction.
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
109 transactionSDK.prototype.buildCrossChain = function(address, to, asset, amount, confirmations, forbidChainTx) {
110 let net = this.bytom.net;
115 forbid_chain_tx: false
118 pm['recipients'][`${to}`] = amount;
121 pm.forbid_chain_tx = forbidChainTx;
124 return this.http.request(`merchant/build-crosschain?address=${address}`, pm, net);
128 * Build a Vote transaction.
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
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};
141 pm.confirmations = confirmations;
147 pm.forbid_chain_tx = forbidChainTx;
150 return this.http.request(`merchant/build-vote?address=${address}`, pm, net);
153 transactionSDK.prototype.estimateFee = function(address, asset_amounts, confirmations=1) {
154 let net = this.bytom.net;
155 let pm = {asset_amounts, confirmations};
157 return this.http.request(`merchant/estimate-tx-fee?address=${address}`, pm, net);
161 * Build a Veto transaction.
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
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};
174 pm.confirmations = confirmations;
180 pm.forbid_chain_tx = forbidChainTx;
183 return this.http.request(`merchant/build-veto?address=${address}`, pm, net);
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.
190 * @param {String} guid unique id for each wallet
191 * @param {Number} fee transaction fee amount
194 transactionSDK.prototype.buildTransaction = function(address, inputs, outputs, fee, confirmations, forbid_chain_tx = true) {
195 let net = this.bytom.net;
205 pm.confirmations = confirmations;
207 return this.http.request(`merchant/build-advanced-tx?address=${address}`, pm, net);
211 transactionSDK.prototype._signTransactionJs = function( transaction, password, key) {
212 let tx = camelize(JSON.parse(transaction));
214 return signJs(tx, password, key);
217 transactionSDK.prototype._signTransactionJsPromise = function( transaction, password, key) {
218 let retPromise = new Promise((resolve, reject) => {
220 let result = this._signTransactionJs(transaction, password, key);
234 * @param {String} type - type.
235 * @param {String} value - value.
237 transactionSDK.prototype.convertArgument = function(type, value) {
238 let retPromise = new Promise((resolve, reject) => {
241 data.raw_data = JSON.stringify({value});
243 const result = convertArgument(data).data
254 export default transactionSDK;