From 4b26ad60bd8653e0554f69405ca32bd71c1229a9 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Tue, 25 Oct 2022 02:12:58 +0530 Subject: [PATCH] Bug fix - Adding rate column to DirectConvert table to store the rate of conversion - Fixed: withdrawConvertFundCurrency not working correctly - Fixed: closeBlockchainBond and closeBobsFundInvestment syntax bugs - Adding request_timeout after with convert request will expire if tx from user is still unconfirmed - Fixed: conversion result value has more than 8 decimal places - Fixed minor syntax bugs --- args/schema.sql | 1 + docs/scripts/floExchangeAPI.js | 12 +++--- src/_constants.js | 1 + src/background.js | 69 ++++++++++++++++++---------------- src/blockchain.js | 12 ++++-- 5 files changed, 52 insertions(+), 43 deletions(-) diff --git a/args/schema.sql b/args/schema.sql index 71275f7..8bc523f 100644 --- a/args/schema.sql +++ b/args/schema.sql @@ -277,6 +277,7 @@ CREATE TABLE DirectConvert( coin VARCHAR(8) NOT NULL, quantity DECIMAL(16, 8), mode TINYINT NOT NULL, + rate DECIMAL(16, 2), in_txid VARCHAR(128), out_txid VARCHAR(128), locktime TIMESTAMP DEFAULT CURRENT_TIMESTAMP, diff --git a/docs/scripts/floExchangeAPI.js b/docs/scripts/floExchangeAPI.js index b51e678..e84c2f6 100644 --- a/docs/scripts/floExchangeAPI.js +++ b/docs/scripts/floExchangeAPI.js @@ -1352,7 +1352,7 @@ request.sign = signRequest({ type: "deposit_convert_coin_fund", coin: request.coin, - txid: data.txid, + txid: txid, timestamp: request.timestamp }, privKey); console.debug(request); @@ -1407,7 +1407,7 @@ }) } - exchangeAPI.withdrawConvertFundCurrency = function (quantity, floID, privKey) { + exchangeAPI.withdrawConvertFundCoin = function (quantity, floID, privKey) { return new Promise((resolve, reject) => { if (!floCrypto.verifyPrivKey(privKey, floID)) return reject(ExchangeError(ExchangeError.BAD_REQUEST_CODE, "Invalid Private Key", errorCode.INVALID_PRIVATE_KEY)); @@ -1454,8 +1454,8 @@ request.pubKey = floCrypto.getPubKeyHex(privKey); request.sign = signRequest({ type: "close_blockchain_bond", - bond_id: data.bond_id, - timestamp: data.timestamp + bond_id: request.bond_id, + timestamp: request.timestamp }, privKey); console.debug(request); @@ -1485,8 +1485,8 @@ request.pubKey = floCrypto.getPubKeyHex(privKey); request.sign = signRequest({ type: "close_bobs_fund", - fund_id: data.fund_id, - timestamp: data.timestamp + fund_id: request.fund_id, + timestamp: request.timestamp }, privKey); console.debug(request); diff --git a/src/_constants.js b/src/_constants.js index 50d64d2..9efffa8 100644 --- a/src/_constants.js +++ b/src/_constants.js @@ -9,6 +9,7 @@ module.exports = { market: { PERIOD_INTERVAL: 5 * 60 * 1000, //5 min, WAIT_TIME: 2 * 60 * 1000, //2 mins, + REQUEST_TIMEOUT: 24 * 60 * 60 * 1000, //1 day LAUNCH_SELLER_TAG: "launch-seller", MAXIMUM_LAUNCH_SELL_CHIPS: 100000, TRADE_HASH_PREFIX: "z1", diff --git a/src/background.js b/src/background.js index 3c9ae6b..1623842 100644 --- a/src/background.js +++ b/src/background.js @@ -8,6 +8,7 @@ const pCode = require('../docs/scripts/floExchangeAPI').processCode; const { LAUNCH_SELLER_TAG, MAXIMUM_LAUNCH_SELL_CHIPS, + REQUEST_TIMEOUT, } = require('./_constants')["market"]; var DB, assetList; //container for database and allowed assets @@ -209,37 +210,39 @@ verifyTx.BTC = function (sender, txid) { } function verifyConvert() { - DB.query("SELECT id, floID, mode, in_txid, amount, quantity FROM DirectConvert WHERE r_status=? AND coin=?", [pCode.STATUS_PENDING, "BTC"]).then(results => { - results.forEach(r => { - if (r.mode == pCode.CONVERT_MODE_GET) { - verifyTx.token(r.floID, r.in_txid, true).then(({ amount }) => { - if (r.amount !== amount) - throw ([true, "Transaction amount mismatched in blockchain"]); - conversion_rates.BTC_INR().then(rate => { - blockchain.convertToCoin.init(r.floID, "BTC", amount / rate, r.id) - }).catch(error => console.error(error)) - }).catch(error => { - console.error(error); - if (error[0]) - DB.query("UPDATE DirectConvert SET r_status=? WHERE id=?", [pCode.STATUS_REJECTED, r.id]) - .then(_ => null).catch(error => console.error(error)); - }); - } else if (r.mode == pCode.CONVERT_MODE_PUT) { - verifyTx.BTC(r.floID, r.in_txid).then(quantity => { - if (r.quantity !== quantity) - throw ([true, "Transaction quantity mismatched in blockchain"]); - conversion_rates.BTC_INR().then(rate => { - blockchain.convertFromCoin.init(r.floID, quantity * rate, r.id) - }).catch(error => console.error(error)) - }).catch(error => { - console.error(error); - if (error[0]) - DB.query("UPDATE DirectConvert SET r_status=? WHERE id=?", [pCode.STATUS_REJECTED, r.id]) - .then(_ => null).catch(error => console.error(error)); - }); - } - }) - }).catch(error => console.error(error)) + DB.query("UPDATE DirectConvert SET r_status=? WHERE r_status=? AND locktime { + DB.query("SELECT id, floID, mode, in_txid, amount, quantity FROM DirectConvert WHERE r_status=? AND coin=?", [pCode.STATUS_PENDING, "BTC"]).then(results => { + results.forEach(r => { + if (r.mode == pCode.CONVERT_MODE_GET) { + verifyTx.token(r.floID, r.in_txid, true).then(({ amount }) => { + if (r.amount !== amount) + throw ([true, "Transaction amount mismatched in blockchain"]); + conversion_rates.BTC_INR().then(rate => { + blockchain.convertToCoin.init(r.floID, "BTC", amount, rate, r.id) + }).catch(error => console.error(error)) + }).catch(error => { + console.error(error); + if (error[0]) + DB.query("UPDATE DirectConvert SET r_status=? WHERE id=?", [pCode.STATUS_REJECTED, r.id]) + .then(_ => null).catch(error => console.error(error)); + }); + } else if (r.mode == pCode.CONVERT_MODE_PUT) { + verifyTx.BTC(r.floID, r.in_txid).then(quantity => { + if (r.quantity !== quantity) + throw ([true, "Transaction quantity mismatched in blockchain"]); + conversion_rates.BTC_INR().then(rate => { + blockchain.convertFromCoin.init(r.floID, quantity, rate, r.id) + }).catch(error => console.error(error)) + }).catch(error => { + console.error(error); + if (error[0]) + DB.query("UPDATE DirectConvert SET r_status=? WHERE id=?", [pCode.STATUS_REJECTED, r.id]) + .then(_ => null).catch(error => console.error(error)); + }); + } + }) + }).catch(error => console.error(error)) + }).catch(error => reject(error)) } function retryConvert() { @@ -261,7 +264,7 @@ function confirmConvert() { if (!tx.blockhash || !tx.confirmations) //Still not confirmed return; DB.query("UPDATE DirectConvert SET r_status=? WHERE id=?", [pCode.STATUS_SUCCESS, r.id]) - .then(result => console.info(`${r.floID} converted ${amount} to ${r.quantity} BTC`)) + .then(result => console.info(`${r.floID} converted ${r.amount} to ${r.quantity} BTC`)) .catch(error => console.error(error)) }).catch(error => console.error(error)); else if (r.mode == pCode.CONVERT_MODE_PUT) @@ -269,7 +272,7 @@ function confirmConvert() { if (!tx.transactionDetails.blockheight || !tx.transactionDetails.confirmations) //Still not confirmed return; DB.query("UPDATE DirectConvert SET r_status=? WHERE id=?", [pCode.STATUS_SUCCESS, r.id]) - .then(result => console.info(`${r.floID} converted ${r.quantity} BTC to ${amount}`)) + .then(result => console.info(`${r.floID} converted ${r.quantity} BTC to ${r.amount}`)) .catch(error => console.error(error)); }).catch(error => console.error(error)); }) diff --git a/src/blockchain.js b/src/blockchain.js index 758cb2f..60f9e42 100644 --- a/src/blockchain.js +++ b/src/blockchain.js @@ -118,6 +118,7 @@ function sendAsset(floID, asset, quantity, type, id) { } function withdrawAsset_init(floID, asset, amount) { + amount = parseFloat(amount.toFixed(8)); let asset_type = ["FLO", "BTC"].includes(asset) ? pCode.ASSET_TYPE_COIN : pCode.ASSET_TYPE_TOKEN; DB.query("INSERT INTO VaultTransactions (floID, mode, asset_type, asset, amount, r_status) VALUES (?)", [[floID, pCode.VAULT_MODE_WITHDRAW, asset_type, asset, amount, pCode.STATUS_PENDING]]) .then(result => sendAsset(floID, asset, amount, TYPE_VAULT, result.insertId)) @@ -130,8 +131,9 @@ function withdrawAsset_retry(floID, asset, amount, id) { else sendAsset(floID, asset, amount, TYPE_VAULT, id); } -function convertToCoin_init(floID, coin, coin_quantity, id) { - DB.query("UPDATE DirectConvert SET quantity=?, r_status=?, locktime=DEFAULT WHERE id=?", [coin_quantity, pCode.STATUS_PROCESSING, id]) +function convertToCoin_init(floID, coin, currency_amount, rate, id) { + let coin_quantity = parseFloat((currency_amount / rate).toFixed(8)); + DB.query("UPDATE DirectConvert SET quantity=?, r_status=?, rate=?, locktime=DEFAULT WHERE id=?", [coin_quantity, pCode.STATUS_PROCESSING, rate, id]) .then(result => sendAsset(floID, coin, coin_quantity, TYPE_CONVERT, id)) .catch(error => console.error(error)) } @@ -142,8 +144,9 @@ function convertToCoin_retry(floID, coin, coin_quantity, id) { else sendAsset(floID, coin, coin_quantity, TYPE_CONVERT, id); } -function convertFromCoin_init(floID, currency_amount, id) { - DB.query("UPDATE DirectConvert SET amount=?, r_status=?, locktime=DEFAULT WHERE id=?", [currency_amount, pCode.STATUS_PROCESSING, id]) +function convertFromCoin_init(floID, coin_quantity, rate, id) { + let currency_amount = parseFloat((coin_quantity * rate).toFixed(8)); + DB.query("UPDATE DirectConvert SET amount=?, r_status=?, rate=?, locktime=DEFAULT WHERE id=?", [currency_amount, pCode.STATUS_PROCESSING, rate, id]) .then(result => sendAsset(floID, floGlobals.currency, currency_amount, TYPE_CONVERT, id)) .catch(error => console.error(error)) } @@ -173,6 +176,7 @@ function fundTransact_retry(floID, amount, id) { } function refundTransact_init(floID, amount, id) { + amount = parseFloat(amount.toFixed(8)); DB.query("UPDATE RefundTransact SET amount=?, r_status=?, locktime=DEFAULT WHERE id=?", [amount, pCode.STATUS_PROCESSING, id]) .then(result => sendAsset(floID, floGlobals.currency, amount, TYPE_REFUND, id)) .catch(error => console.error(error))