1 import {convertArgument, signTransaction} from '../wasm/func';
2 import { handleApiError, handleAxiosError } from '../utils/http';
3 import { getDB } from '../db/db';
5 function transactionSDK(bytom) {
6 this.http = bytom.serverHttp;
12 * List all the transactions related to a wallet or an address.
14 * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmmerchantlist-transactions
15 * @param {String} guid unique id for each wallet
16 * @param {String} filter (optional) if provided, will only return transactions the filter condition is related to
17 * @param {String} sort (optional) if provided, will only return transactions the sort condition is related to
18 * @param {Number} start page start
19 * @param {Number} limit page limit
22 transactionSDK.prototype.list = function(address, filter,sort, start, limit) {
23 let net = this.bytom.net;
34 let url = 'merchant/transactions';
35 let args = new URLSearchParams();
36 if (typeof start !== 'undefined') {
37 args.append('start', start);
40 args.append('limit', limit);
43 args.append('address', address);
45 url = url + '?' + args.toString();
46 return this.http.request(url, pm, net);
50 * Submit a signed transaction to the chain.
52 * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmmerchantsubmit-payment
53 * @param {String} guid unique id for each wallet
54 * @param {String} raw_transaction raw transaction bytes encoded to string
55 * @param {Array} signatures signed data of each signing instruction
57 transactionSDK.prototype.submitPayment = function(address, raw_transaction, signatures) {
58 let net = this.bytom.net;
59 let pm = {raw_transaction: raw_transaction, signatures: signatures};
60 return this.http.request(`merchant/submit-payment?address=${address}`, pm, net);
64 * Build a raw transaction transfered from the wallet.
65 * May use all available addresses (under the wallet) as source addresses if not specified.
67 * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmmerchantbuild-payment
68 * @param {String} guid unique id for each wallet
69 * @param {String} to destination address
70 * @param {String} asset hexdecimal asset id
71 * @param {Number} amount transfer amount
72 * @param {Number} fee transaction fee amount
73 * @param {Number} confirmations - transaction confirmations
76 transactionSDK.prototype.buildPayment = function(address, to, asset, amount, confirmations, memo, forbidChainTx) {
77 let net = this.bytom.net;
81 forbid_chain_tx: false
84 pm['recipients'][`${to}`] = amount;
90 pm.forbid_chain_tx = forbidChainTx;
93 pm.confirmations = confirmations;
95 return this.http.request(`merchant/build-payment?address=${address}`, pm, net);
99 * Build a cross chain transaction.
101 * @param {String} guid unique id for each wallet
102 * @param {String} to destination address
103 * @param {String} asset hexdecimal asset id
104 * @param {Number} amount transfer amount
105 * @param {Number} confirmations - transaction confirmations
108 transactionSDK.prototype.buildCrossChain = function(address, to, asset, amount, confirmations, forbidChainTx) {
109 let net = this.bytom.net;
114 forbid_chain_tx: false
117 pm['recipients'][`${to}`] = amount;
120 pm.forbid_chain_tx = forbidChainTx;
123 return this.http.request(`merchant/build-crosschain?address=${address}`, pm, net);
127 * Build a Vote transaction.
129 * @param {String} guid unique id for each wallet
130 * @param {String} vote pubkey vote
131 * @param {String} memo memo key
132 * @param {Number} amount transfer amount
133 * @param {Number} confirmations - transaction confirmations
136 transactionSDK.prototype.buildVote = function(address, vote, amount, confirmations, memo, forbidChainTx) {
137 let net = this.bytom.net;
138 let pm = {vote, amount: amount, forbid_chain_tx: false};
140 pm.confirmations = confirmations;
146 pm.forbid_chain_tx = forbidChainTx;
149 return this.http.request(`merchant/build-vote?address=${address}`, pm, net);
152 transactionSDK.prototype.estimateFee = function(address, asset_amounts, confirmations=1) {
153 let net = this.bytom.net;
154 let pm = {asset_amounts, confirmations};
156 return this.http.request(`merchant/estimate-tx-fee?address=${address}`, pm, net);
160 * Build a Veto transaction.
162 * @param {String} guid unique id for each wallet
163 * @param {String} vote pubkey vote
164 * @param {String} memo memo key
165 * @param {Number} amount transfer amount
166 * @param {Number} confirmations - transaction confirmations
169 transactionSDK.prototype.buildVeto = function(address, vote, amount, confirmations, memo, forbidChainTx) {
170 let net = this.bytom.net;
171 let pm = { vote, amount: amount, forbid_chain_tx: false};
173 pm.confirmations = confirmations;
179 pm.forbid_chain_tx = forbidChainTx;
182 return this.http.request(`merchant/build-veto?address=${address}`, pm, net);
186 * Advanced Build a raw transaction transfered from the wallet.
187 * May use all available addresses (under the wallet) as source addresses if not specified.
189 * @param {String} guid unique id for each wallet
190 * @param {Number} fee transaction fee amount
193 transactionSDK.prototype.buildTransaction = function(address, inputs, outputs, fee, confirmations, forbid_chain_tx = true) {
194 let net = this.bytom.net;
204 pm.confirmations = confirmations;
206 return this.http.request(`merchant/build-advanced-tx?address=${address}`, pm, net);
211 * @param {String} guid
212 * @param {String} transaction
213 * @param {String} password
214 * @returns {Object} signed data
216 transactionSDK.prototype.signTransaction = function(guid, transaction, password, getKeyByXPub) {
217 let bytom = this.bytom;
218 let retPromise = new Promise((resolve, reject) => {
220 let getRequest = db.transaction(['accounts-server'], 'readonly')
221 .objectStore('accounts-server')
224 getRequest.onsuccess = function(e) {
225 if (!e.target.result) {
226 reject(new Error('not found guid'));
229 const res = getKeyByXPub(e.target.result.rootXPub)[0];
231 reject('not found xpub');
234 let pm = {transaction: transaction, password: password, key: key};
235 signTransaction(pm).then(res => {
236 resolve(JSON.parse(res.data));
242 getRequest.onerror = function() {
243 reject(getRequest.error);
252 transactionSDK.prototype._signTransaction = function( transaction, password, key) {
253 let pm = {transaction: transaction, password: password, key: key};
254 return signTransaction(pm).then(res => {
255 return JSON.parse(res.data);
264 * @param {String} type - type.
265 * @param {String} value - value.
267 transactionSDK.prototype.convertArgument = function(type, value) {
268 let retPromise = new Promise((resolve, reject) => {
271 data.raw_data = JSON.stringify({value});
272 convertArgument(data).then((res) => {
273 let jsonData = JSON.parse(res.data);
282 export default transactionSDK;