OSDN Git Service

update error code
[bytom/Bytom-JS-SDK.git] / src / sdk / transaction.js
index 5c88275..ee9fdc5 100644 (file)
@@ -1,6 +1,7 @@
-import { signTransaction1 } from '../wasm/func';
-import { handleApiError, handleAxiosError } from '../utils/http';
-import { getDB } from '../db/db';
+import {signTransaction as signJs} from '../utils/transaction/signTransaction';
+import { camelize } from '../utils/utils';
+import {convertArgument} from '../utils/convertArguement';
+
 
 function transactionSDK(bytom) {
     this.http = bytom.serverHttp;
@@ -13,34 +14,37 @@ function transactionSDK(bytom) {
  *
  * @see https://gist.github.com/HAOYUatHZ/0c7446b8f33e7cddd590256b3824b08f#apiv1btmmerchantlist-transactions
  * @param {String} guid unique id for each wallet
- * @param {String} address (optional) if provided, will only return transactions the address is related to
+ * @param {String} filter (optional) if provided, will only return transactions the filter condition is related to
+ * @param {String} sort (optional) if provided, will only return transactions the sort condition is related to
  * @param {Number} start page start
  * @param {Number} limit page limit
  * @returns {Promise}
  */
-transactionSDK.prototype.list = function(guid, address, start, limit) {
+transactionSDK.prototype.list = function(address, filter,sort, start, limit) {
     let net = this.bytom.net;
-    let retPromise = new Promise((resolve, reject) => {
-        let pm = {guid: guid};
-        if (address) {
-            pm.address = address;
-        }
-        let url = 'merchant/list-transactions';
-        let args = new URLSearchParams();
-        if (typeof start !== 'undefined') {
-            args.append('start', start);
-        }
-        if (limit) {
-            args.append('limit', limit);
-        }
-        url = url + '?' + args.toString();
-        this.http.request(url, pm, net).then(resp => {
-            resolve(resp.data);
-        }).catch(err => {
-            reject(handleAxiosError(err));
-        });
-    });
-    return retPromise;
+    let pm = {};
+
+    if (filter) {
+        pm.filter = filter;
+    }
+
+    if (sort) {
+        pm.sort = sort;
+    }
+
+    let url = 'merchant/transactions';
+    let args = new URLSearchParams();
+    if (typeof start !== 'undefined') {
+        args.append('start', start);
+    }
+    if (limit) {
+        args.append('limit', limit);
+    }
+    if (address) {
+        args.append('address', address);
+    }
+    url = url + '?' + args.toString();
+    return this.http.request(url, pm, net);
 };
 
 /**
@@ -51,21 +55,10 @@ transactionSDK.prototype.list = function(guid, address, start, limit) {
  * @param {String} raw_transaction raw transaction bytes encoded to string
  * @param {Array} signatures signed data of each signing instruction
  */
-transactionSDK.prototype.submitPayment = function(guid, raw_transaction, signatures) {
+transactionSDK.prototype.submitPayment = function(address, raw_transaction, signatures) {
     let net = this.bytom.net;
-    let retPromise = new Promise((resolve, reject) => {
-        let pm = {guid: guid, raw_transaction: raw_transaction, signatures: signatures};
-        this.http.request('merchant/submit-payment', pm, net).then(resp => {
-            if (resp.status !== 200 || resp.data.code !== 200) {
-                reject(handleApiError(resp));
-                return;
-            }
-            resolve(resp.data);
-        }).catch(err => {
-            reject(handleAxiosError(err));
-        });
-    });
-    return retPromise;
+    let pm = {raw_transaction: raw_transaction, signatures: signatures};
+    return this.http.request(`merchant/submit-payment?address=${address}`, pm, net);
 };
 
 /**
@@ -77,72 +70,185 @@ transactionSDK.prototype.submitPayment = function(guid, raw_transaction, signatu
  * @param {String} to destination address
  * @param {String} asset hexdecimal asset id
  * @param {Number} amount transfer amount
- * @param {String} from source address
+ * @param {Number} fee transaction fee amount
+ * @param {Number} confirmations - transaction confirmations
+ * @returns {Promise}
+ */
+transactionSDK.prototype.buildPayment = function(address, to, asset, amount, confirmations, memo, forbidChainTx) {
+    let net = this.bytom.net;
+    let pm = {
+        asset: asset,
+        recipients: {},
+        forbid_chain_tx: false
+    };
+
+    pm['recipients'][`${to}`] = amount;
+
+    if (memo) {
+        pm.memo = memo;
+    }
+    if (forbidChainTx) {
+        pm.forbid_chain_tx = forbidChainTx;
+    }
+    if (confirmations) {
+        pm.confirmations = confirmations;
+    }
+    return this.http.request(`merchant/build-payment?address=${address}`, pm, net);
+};
+
+/**
+ * Build a cross chain transaction.
+ *
+ * @param {String} guid unique id for each wallet
+ * @param {String} to destination address
+ * @param {String} asset hexdecimal asset id
+ * @param {Number} amount transfer amount
+ * @param {Number} confirmations - transaction confirmations
+ * @returns {Promise}
+ */
+transactionSDK.prototype.buildCrossChain = function(address, to, asset, amount, confirmations, forbidChainTx) {
+    let net = this.bytom.net;
+
+    let pm = {
+        asset: asset,
+        recipients: {},
+        forbid_chain_tx: false
+    };
+
+    pm['recipients'][`${to}`] = amount;
+
+    if (forbidChainTx) {
+        pm.forbid_chain_tx = forbidChainTx;
+    }
+
+    return this.http.request(`merchant/build-crosschain?address=${address}`, pm, net);
+};
+
+/**
+ * Build a Vote transaction.
+ *
+ * @param {String} guid unique id for each wallet
+ * @param {String} vote pubkey vote
+ * @param {String} memo memo key
+ * @param {Number} amount transfer amount
+ * @param {Number} confirmations - transaction confirmations
+ * @returns {Promise}
+ */
+transactionSDK.prototype.buildVote = function(address, vote, amount, confirmations, memo, forbidChainTx) {
+    let net = this.bytom.net;
+    let pm = {vote, amount: amount, forbid_chain_tx: false};
+    if (confirmations) {
+        pm.confirmations = confirmations;
+    }
+    if (memo) {
+        pm.memo = memo;
+    }
+    if (forbidChainTx) {
+        pm.forbid_chain_tx = forbidChainTx;
+    }
+
+    return this.http.request(`merchant/build-vote?address=${address}`, pm, net);
+};
+
+transactionSDK.prototype.estimateFee = function(address, asset_amounts, confirmations=1) {
+    let net = this.bytom.net;
+    let pm = {asset_amounts, confirmations};
+
+    return this.http.request(`merchant/estimate-tx-fee?address=${address}`, pm, net);
+};
+
+/**
+ * Build a Veto transaction.
+ *
+ * @param {String} guid unique id for each wallet
+ * @param {String} vote pubkey vote
+ * @param {String} memo memo key
+ * @param {Number} amount transfer amount
+ * @param {Number} confirmations - transaction confirmations
+ * @returns {Promise}
+ */
+transactionSDK.prototype.buildVeto = function(address, vote, amount, confirmations, memo,  forbidChainTx) {
+    let net = this.bytom.net;
+    let pm = { vote, amount: amount, forbid_chain_tx: false};
+    if (confirmations) {
+        pm.confirmations = confirmations;
+    }
+    if (memo) {
+        pm.memo = memo;
+    }
+    if (forbidChainTx) {
+        pm.forbid_chain_tx = forbidChainTx;
+    }
+
+    return this.http.request(`merchant/build-veto?address=${address}`, pm, net);
+};
+
+/**
+ * Advanced Build a raw transaction transfered from the wallet.
+ * May use all available addresses (under the wallet) as source addresses if not specified.
+ *
+ * @param {String} guid unique id for each wallet
  * @param {Number} fee transaction fee amount
  * @returns {Promise}
  */
-transactionSDK.prototype.buildPayment = function(guid, to, asset, amount, from, fee) {
+transactionSDK.prototype.buildTransaction = function(address, inputs, outputs, fee, confirmations, forbid_chain_tx = true) {
     let net = this.bytom.net;
+    let pm = {
+        inputs,
+        outputs,
+        forbid_chain_tx
+    };
+    if (fee) {
+        pm.fee = fee;
+    }
+    if (confirmations) {
+        pm.confirmations = confirmations;
+    }
+    return this.http.request(`merchant/build-advanced-tx?address=${address}`, pm, net);
+};
+
+
+transactionSDK.prototype._signTransactionJs = function( transaction, password, key) {
+    let tx = camelize(JSON.parse(transaction));
+
+    return signJs(tx, password, key);
+};
+
+transactionSDK.prototype._signTransactionJsPromise = function( transaction, password, key) {
     let retPromise = new Promise((resolve, reject) => {
-        let pm = {guid: guid, to: to, asset: asset, amount: amount};
-        if (from) {
-            pm.from = from;
+        try{
+            let result = this._signTransactionJs(transaction, password, key);
+            resolve(result);
         }
-        if (fee) {
-            pm.fee = fee;
+        catch(error) {
+            reject(error);
         }
-        this.http.request('merchant/build-payment', pm, net).then(resp => {
-            if (resp.status !== 200 || resp.data.code !== 200) {
-                reject(handleApiError(resp));
-                return;
-            }
-            resolve(resp.data);
-        }).catch(err => {
-            reject(handleAxiosError(err));
-        });
     });
+
     return retPromise;
 };
 
 /**
- * sign transaction
- * @param {String} guid
- * @param {String} transaction
- * @param {String} password
- * @returns {Object} signed data
+ * Convert arguement.
+ *
+ * @param {String} type - type.
+ * @param {String} value - value.
  */
-transactionSDK.prototype.signTransaction = function(guid, transaction, password) {
-    let bytom = this.bytom;
+transactionSDK.prototype.convertArgument = function(type, value) {
     let retPromise = new Promise((resolve, reject) => {
-        getDB().then(db => {
-            let getRequest = db.transaction(['accounts-server'], 'readonly')
-                .objectStore('accounts-server')
-                .index('guid')
-                .get(guid);
-            getRequest.onsuccess = function(e) {
-                if (!e.target.result) {
-                    reject(new Error('not found guid'));
-                    return;
-                }
-                bytom.sdk.keys.getKeyByXPub(e.target.result.rootXPub).then(res => {
-                    let pm = {transaction: transaction, password: password, key: res};
-                    signTransaction1(pm).then(res => {
-                        resolve(JSON.parse(res.data));
-                    }).catch(err => {
-                        reject(err);
-                    });
-                }).catch(err => {
-                    reject(err);
-                });
-            };
-            getRequest.onerror = function() {
-                reject(getRequest.error);
-            };
-        }).catch(error => {
+        let data = {};
+        data.type = type;
+        data.raw_data = JSON.stringify({value});
+        try{
+            const result = convertArgument(data).data
+            resolve(result);
+        }
+        catch(error){
             reject(error);
-        });
+        }
     });
     return retPromise;
 };
 
+
 export default transactionSDK;
\ No newline at end of file