Background process for close-fund

This commit is contained in:
sairajzero 2022-10-11 16:56:42 +05:30
parent 8a82a0b675
commit d3f08dd921
2 changed files with 50 additions and 5 deletions

View File

@ -2,6 +2,7 @@
const blockchain = require('./blockchain');
const conversion_rates = require('./services/conversion').getRate;
const bond_util = require('./services/bonds').util;
const fund_util = require('./services/bobs-fund').util;
const {
LAUNCH_SELLER_TAG,
@ -186,6 +187,8 @@ function confirmWithdrawalToken() {
DB.query("SELECT id, floID, token, amount, txid FROM WithdrawToken WHERE status=?", ["WAITING_CONFIRMATION"]).then(results => {
results.forEach(r => {
floTokenAPI.getTx(r.txid).then(tx => {
if (!tx.transactionDetails.blockheight || !tx.transactionDetails.confirmations) //Still not confirmed
return;
DB.query("UPDATE WithdrawToken SET status=? WHERE id=?", ["SUCCESS", r.id])
.then(result => console.debug("Token withdrawed:", r.floID, r.token, r.amount))
.catch(error => console.error(error));
@ -270,6 +273,8 @@ function confirmConvert() {
}).catch(error => console.error(error));
else if (mode == _sql.CONVERT_MODE_PUT)
floTokenAPI.getTx(r.out_txid).then(tx => {
if (!tx.transactionDetails.blockheight || !tx.transactionDetails.confirmations) //Still not confirmed
return;
DB.query("UPDATE DirectConvert SET status=? WHERE id=?", ["SUCCESS", r.id])
.then(result => console.debug(`${r.floID} converted ${r.quantity} BTC to ${amount}`))
.catch(error => console.error(error));
@ -288,6 +293,8 @@ function confirmBondClosing() {
DB.query("SELECT * FROM CloseBondTransact WHERE status=?", ["WAITING_CONFIRMATION"]).then(result => {
results.forEach(r => {
floTokenAPI.getTx(r.txid).then(tx => {
if (!tx.transactionDetails.blockheight || !tx.transactionDetails.confirmations) //Still not confirmed
return;
let closeBondString = bond_util.stringify.end(r.bond_id, r.end_date, r.btc_net, r.usd_net, r.amount, r.ref_sign, r.txid);
floBlockchainAPI.writeData(global.myFloID, closeBondString, global.myPrivKey, bond_util.config.adminID).then(txid => {
DB.query("UPDATE CloseBondTransact SET status=?, close_id=? WHERE id=?", ["SUCCESS", txid, r.id])
@ -296,7 +303,30 @@ function confirmBondClosing() {
}).catch(error => console.error(error))
}).catch(error => console.error(error));
})
}).catch(error => reject(error))
}).catch(error => console.error(error))
}
function retryFundClosing() {
DB.query("SELECT id, floID, amount FROM CloseFundTransact WHERE status=?", ["PENDING"]).then(results => {
results.forEach(r => blockchain.fundTransact.retry(r.floID, r.amount, r.id))
}).catch(error => console.error(error))
}
function confirmFundClosing() {
DB.query("SELECT * FROM CloseFundTransact WHERE status=?", ["WAITING_CONFIRMATION"]).then(result => {
results.forEach(r => {
floTokenAPI.getTx(r.txid).then(tx => {
if (!tx.transactionDetails.blockheight || !tx.transactionDetails.confirmations) //Still not confirmed
return;
let closeFundString = fund_util.stringify.end(r.fund_id, r.floID, r.end_date, r.btc_net, r.usd_net, r.amount, r.ref_sign, r.txid);
floBlockchainAPI.writeData(global.myFloID, closeFundString, global.myPrivKey, fund_util.config.adminID).then(txid => {
DB.query("UPDATE CloseFundTransact SET status=?, close_id=? WHERE id=?", ["SUCCESS", txid, r.id])
.then(result => console.debug("Fund investment closed:", r.fund_id))
.catch(error => console.error(error));
}).catch(error => console.error(error))
}).catch(error => console.error(error));
})
}).catch(error => console.error(error))
}
function processAll() {
@ -312,6 +342,8 @@ function processAll() {
confirmConvert();
retryBondClosing();
confirmBondClosing();
retryFundClosing();
confirmFundClosing();
}
module.exports = {

View File

@ -7,7 +7,8 @@ var DB; //container for database
const TYPE_TOKEN = "TOKEN",
TYPE_COIN = "COIN",
TYPE_CONVERT = "CONVERT",
TYPE_BOND = "BOND";
TYPE_BOND = "BOND",
TYPE_FUND = "BOB-FUND";
const balance_locked = {},
balance_cache = {},
@ -15,7 +16,8 @@ const balance_locked = {},
[TYPE_COIN]: {},
[TYPE_TOKEN]: {},
[TYPE_CONVERT]: {},
[TYPE_BOND]: {}
[TYPE_BOND]: {},
[TYPE_FUND]: {}
};
function getBalance(sinkID, asset) {
@ -61,7 +63,8 @@ const WITHDRAWAL_MESSAGE = {
[TYPE_COIN]: "(withdrawal from market)",
[TYPE_TOKEN]: "(withdrawal from market)",
[TYPE_CONVERT]: "(convert coin)",
[TYPE_BOND]: "(bond closing)"
[TYPE_BOND]: "(bond closing)",
[TYPE_FUND]: "(fund investment closing)"
}
function sendTx(floID, asset, quantity, sinkID, sinkKey, message) {
@ -81,7 +84,8 @@ const updateSyntax = {
[TYPE_COIN]: "UPDATE WithdrawCoin SET status=?, txid=? WHERE id=?",
[TYPE_TOKEN]: "UPDATE WithdrawToken SET status=?, txid=? WHERE id=?",
[TYPE_CONVERT]: "UPDATE DirectConvert SET status=?, out_txid=? WHERE id=?",
[TYPE_BOND]: "UPDATE CloseBondTransact SET status=?, txid=? WHERE id=?"
[TYPE_BOND]: "UPDATE CloseBondTransact SET status=?, txid=? WHERE id=?",
[TYPE_FUND]: "UPDATE CloseFundTransact SET status=?, txid=? WHERE id=?"
};
function sendAsset(floID, asset, quantity, type, id) {
@ -161,6 +165,12 @@ function bondTransact_retry(floID, amount, id) {
else sendAsset(floID, floGlobals.currency, amount, TYPE_BOND, id);
}
function fundTransact_retry(floID, amount, id) {
if (id in callbackCollection[TYPE_FUND])
console.debug("A callback is already pending for this Fund investment closing");
else sendAsset(floID, floGlobals.currency, amount, TYPE_FUND, id);
}
module.exports = {
set collectAndCall(fn) {
collectAndCall = fn;
@ -190,6 +200,9 @@ module.exports = {
bondTransact: {
retry: bondTransact_retry
},
fundTransact: {
retry: fundTransact_retry
},
set DB(db) {
DB = db;
}