From 42f30a12bbba9769697f3b22b7f1f70a9e29ea69 Mon Sep 17 00:00:00 2001 From: sairajzero Date: Fri, 19 May 2023 01:08:21 +0530 Subject: [PATCH] Fix for flosight changes --- args/schema.sql | 2 +- docs/scripts/floExchangeAPI.js | 14 ++++++++------ src/main.js | 19 +++++++++++-------- src/services/bobs-fund.js | 31 ++++++++++++++++++------------- src/services/bonds.js | 29 +++++++++++++++++------------ 5 files changed, 55 insertions(+), 40 deletions(-) diff --git a/args/schema.sql b/args/schema.sql index 40cf6e8..16b4d97 100644 --- a/args/schema.sql +++ b/args/schema.sql @@ -2,7 +2,7 @@ CREATE TABLE LastTx( floID CHAR(34) NOT NULL, - num INT, + txid VARCHAR(128), PRIMARY KEY(floID) ); diff --git a/docs/scripts/floExchangeAPI.js b/docs/scripts/floExchangeAPI.js index 44ae667..c737d89 100644 --- a/docs/scripts/floExchangeAPI.js +++ b/docs/scripts/floExchangeAPI.js @@ -1737,13 +1737,15 @@ trusted = new Set(); assets = new Set(); tags = new Set(); - lastTx = 0; + lastTx = undefined; } - floBlockchainAPI.readData(DEFAULT.marketID, { - ignoreOld: lastTx, - sentOnly: true, - pattern: DEFAULT.marketApp - }).then(result => { + + var query_options = { sentOnly: true, pattern: DEFAULT.marketApp }; + if (typeof lastTx == 'string' && /^[0-9a-f]{64}/i.test(lastTx))//lastTx is txid of last tx + query_options.after = lastTx; + else if (!isNaN(lastTx))//lastTx is tx count (*backward support) + query_options.ignoreOld = parseInt(lastTx); + floBlockchainAPI.readData(DEFAULT.marketID, query_options).then(result => { result.data.reverse().forEach(data => { var content = JSON.parse(data)[DEFAULT.marketApp]; //Node List diff --git a/src/main.js b/src/main.js index 15b6186..95b2ad2 100644 --- a/src/main.js +++ b/src/main.js @@ -40,13 +40,16 @@ function refreshData(startup = false) { function refreshDataFromBlockchain() { return new Promise((resolve, reject) => { - DB.query("SELECT num FROM LastTx WHERE floID=?", [floGlobals.adminID]).then(result => { - let lastTx = result.length ? result[0].num : 0; - floBlockchainAPI.readData(floGlobals.adminID, { - ignoreOld: lastTx, - sentOnly: true, - pattern: floGlobals.application - }).then(result => { + DB.query("SELECT txid FROM LastTx WHERE floID=?", [floGlobals.adminID]).then(result => { + var query_options = { sentOnly: true, pattern: floGlobals.application }; + + let lastTx = result.length ? result[0].txid : undefined; + if (typeof lastTx == 'string' && /^[0-9a-f]{64}/i.test(lastTx))//lastTx is txid of last tx + query_options.after = lastTx; + else if (!isNaN(lastTx))//lastTx is tx count (*backward support) + query_options.ignoreOld = parseInt(lastTx); + + floBlockchainAPI.readData(floGlobals.adminID, query_options).then(result => { let promises = [], nodes_change = false, assets_change = false, @@ -96,7 +99,7 @@ function refreshDataFromBlockchain() { promises.push(`UPDATE TagList WHERE tag=? SET ${a}=?`, [t, content.Tag.update[t][a]]); } }); - promises.push(DB.query("INSERT INTO LastTx (floID, num) VALUE (?) ON DUPLICATE KEY UPDATE num=?", [[floGlobals.adminID, result.totalTxs], result.totalTxs])); + promises.push(DB.query("INSERT INTO LastTx (floID, txid) VALUE (?) ON DUPLICATE KEY UPDATE txid=?", [[floGlobals.adminID, result.lastItem], result.lastItem])); //Check if all save process were successful Promise.allSettled(promises).then(results => { //console.debug(results.filter(r => r.status === "rejected")); diff --git a/src/services/bobs-fund.js b/src/services/bobs-fund.js index d4705c9..042100a 100644 --- a/src/services/bobs-fund.js +++ b/src/services/bobs-fund.js @@ -203,16 +203,21 @@ bobsFund.config = { function refreshBlockchainData(nodeList = []) { return new Promise((resolve, reject) => { - DB.query("SELECT num FROM LastTx WHERE floID=?", [bobsFund.config.adminID]).then(result => { - let lastTx = result.length ? result[0].num : 0; - floBlockchainAPI.readData(bobsFund.config.adminID, { - ignoreOld: lastTx, - senders: nodeList.concat(bobsFund.config.adminID), //sentOnly: true, - tx: true, - filter: d => d.startsWith(bobsFund.productStr) - }).then(result => { + DB.query("SELECT txid FROM LastTx WHERE floID=?", [bobsFund.config.adminID]).then(result => { + + var query_options = { + senders: nodeList.concat(bobsFund.config.adminID), + tx: true, filter: d => d.startsWith(bobsFund.productStr) + }; + let lastTx = result.length ? result[0].txid : undefined; + if (typeof lastTx == 'string' && /^[0-9a-f]{64}/i.test(lastTx))//lastTx is txid of last tx + query_options.after = lastTx; + else if (!isNaN(lastTx))//lastTx is tx count (*backward support) + query_options.ignoreOld = parseInt(lastTx); + + floBlockchainAPI.readData(bobsFund.config.adminID, query_options).then(result => { let txQueries = []; - result.data.reverse().forEach(d => { + result.items.reverse().forEach(d => { let fund = bobsFund.parse(d.data); if (d.senders.has(bobsFund.config.adminID) && !/close:/.test(d.data)) { let fund_id = d.data.match(/continue: [a-z0-9]{64}\|/); @@ -221,7 +226,7 @@ function refreshBlockchainData(nodeList = []) { let values = [fund_id, fund.start_date, fund.BTC_base, fund.USD_base, fund.fee, fund.duration]; if (fund.tapoutInterval) values.push(fund.topoutWindow, fund.tapoutInterval.join(',')); - else + else values.push(null, null); txQueries.push(["INSERT INTO BobsFund(fund_id, begin_date, btc_base, usd_base, fee, duration, tapout_window, tapout_interval) VALUE (?) ON DUPLICATE KEY UPDATE fund_id=fund_id", [values]]) } else @@ -240,10 +245,10 @@ function refreshBlockchainData(nodeList = []) { } } }); - txQueries.push(["INSERT INTO LastTx (floID, num) VALUE (?) ON DUPLICATE KEY UPDATE num=?", - [[bobsFund.config.adminID, result.totalTxs], result.totalTxs]]) + txQueries.push(["INSERT INTO LastTx (floID, txid) VALUE (?) ON DUPLICATE KEY UPDATE txid=?", + [[bobsFund.config.adminID, result.lastItem], result.lastItem]]) DB.transaction(txQueries) - .then(_ => resolve(result.totalTxs)) + .then(_ => resolve(result.lastItem)) .catch(error => reject(["Bobs-Fund refresh data failed!", error])); }).catch(error => reject(error)) }).catch(error => reject(error)) diff --git a/src/services/bonds.js b/src/services/bonds.js index 4b43466..3c47ab8 100644 --- a/src/services/bonds.js +++ b/src/services/bonds.js @@ -186,16 +186,21 @@ blockchainBond.config = { function refreshBlockchainData(nodeList = []) { return new Promise((resolve, reject) => { - DB.query("SELECT num FROM LastTx WHERE floID=?", [blockchainBond.config.adminID]).then(result => { - let lastTx = result.length ? result[0].num : 0; - floBlockchainAPI.readData(blockchainBond.config.adminID, { - ignoreOld: lastTx, - senders: nodeList.concat(blockchainBond.config.adminID), //sentOnly: true, - tx: true, - filter: d => d.startsWith(blockchainBond.productStr) - }).then(result => { + DB.query("SELECT txid FROM LastTx WHERE floID=?", [blockchainBond.config.adminID]).then(result => { + + var query_options = { + senders: nodeList.concat(blockchainBond.config.adminID), + tx: true, filter: d => d.startsWith(blockchainBond.productStr) + }; + let lastTx = result.length ? result[0].txid : undefined; + if (typeof lastTx == 'string' && /^[0-9a-f]{64}/i.test(lastTx))//lastTx is txid of last tx + query_options.after = lastTx; + else if (!isNaN(lastTx))//lastTx is tx count (*backward support) + query_options.ignoreOld = parseInt(lastTx); + + floBlockchainAPI.readData(blockchainBond.config.adminID, query_options).then(result => { let txQueries = []; - result.data.reverse().forEach(d => { + result.items.reverse().forEach(d => { let bond = d.senders.has(blockchainBond.config.adminID) ? blockchainBond.parse.main(d.data) : null; if (bond && bond.amount) txQueries.push(["INSERT INTO BlockchainBonds(bond_id, floID, amount_in, begin_date, btc_base, usd_base, gain_cut, min_ipa, max_period, lockin_period) VALUE (?) ON DUPLICATE KEY UPDATE bond_id=bond_id", @@ -207,10 +212,10 @@ function refreshBlockchainData(nodeList = []) { [d.txid, details.amountFinal, details.bondID]]); } }); - txQueries.push(["INSERT INTO LastTx (floID, num) VALUE (?) ON DUPLICATE KEY UPDATE num=?", - [[blockchainBond.config.adminID, result.totalTxs], result.totalTxs]]) + txQueries.push(["INSERT INTO LastTx (floID, txid) VALUE (?) ON DUPLICATE KEY UPDATE txid=?", + [[blockchainBond.config.adminID, result.lastItem], result.lastItem]]) DB.transaction(txQueries) - .then(_ => resolve(result.totalTxs)) + .then(_ => resolve(result.lastItem)) .catch(error => reject(["Blockchain-bonds refresh data failed!", error])); }).catch(error => reject(error)) }).catch(error => reject(error))