From f1e965800710787f7749409d43d732ac709326da Mon Sep 17 00:00:00 2001 From: sairajzero Date: Mon, 6 Dec 2021 22:13:37 +0530 Subject: [PATCH] Audit Transaction Store the following values upon successful transaction - Timestamp - Quanity, unit value, total cost - Old/New Balance of Rupee and FLO for both seller and buyer --- args/schema.sql | 19 +++++++++++++++++ src/coupling.js | 56 ++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 68 insertions(+), 7 deletions(-) diff --git a/args/schema.sql b/args/schema.sql index c970b8b..f394531 100644 --- a/args/schema.sql +++ b/args/schema.sql @@ -137,6 +137,25 @@ CREATE TABLE priceHistory ( rec_time DATETIME DEFAULT CURRENT_TIMESTAMP ); +CREATE TABLE auditTransaction( + rec_time DATETIME DEFAULT CURRENT_TIMESTAMP, + unit_price FLOAT NOT NULL, + quantity FLOAT NOT NULL, + total_cost FLOAT NOT NULL, + sellerID CHAR(34) NOT NULL, + FLO_seller_old FLOAT NOT NULL, + FLO_seller_new FLOAT NOT NULL, + Rupee_seller_old FLOAT NOT NULL, + Rupee_seller_new FLOAT NOT NULL, + buyerID CHAR(34) NOT NULL, + FLO_buyer_old FLOAT NOT NULL, + FLO_buyer_new FLOAT NOT NULL, + Rupee_buyer_old FLOAT NOT NULL, + Rupee_buyer_new FLOAT NOT NULL, + FOREIGN KEY (sellerID) REFERENCES Users(floID), + FOREIGN KEY (buyerID) REFERENCES Users(floID) +); + /* Backup feature (Table and Triggers) */ CREATE TABLE _backup ( diff --git a/src/coupling.js b/src/coupling.js index 6fc9f4e..72099bd 100644 --- a/src/coupling.js +++ b/src/coupling.js @@ -25,13 +25,17 @@ function processCoupling(bestPairQueue) { clear_sell = spend_result.incomplete && !spend_result.flag_baseNull; //clear_sell can be true only if an order is placed without enough FLO processOrders(seller_best, buyer_best, txQueries, tx_quantity, clear_sell); updateBalance(seller_best, buyer_best, txQueries, bestPairQueue.cur_rate, tx_quantity); - //process txn query in SQL - DB.transaction(txQueries).then(_ => { - bestPairQueue.next(tx_quantity, spend_result.incomplete, spend_result.flag_baseNull); - console.log(`Transaction was successful! BuyOrder:${buyer_best.id}| SellOrder:${seller_best.id}`); - price.updateLastTime(); - //Since a tx was successful, match again - processCoupling(bestPairQueue); + //begin audit + beginAudit(seller_best.floID, buyer_best.floID, bestPairQueue.cur_rate, tx_quantity).then(audit => { + //process txn query in SQL + DB.transaction(txQueries).then(_ => { + bestPairQueue.next(tx_quantity, spend_result.incomplete, spend_result.flag_baseNull); + console.log(`Transaction was successful! BuyOrder:${buyer_best.id}| SellOrder:${seller_best.id}`); + audit.end(); + price.updateLastTime(); + //Since a tx was successful, match again + processCoupling(bestPairQueue); + }).catch(error => console.error(error)); }).catch(error => console.error(error)); }).catch(error => console.error(error)); }).catch(error => { @@ -111,6 +115,44 @@ function updateBalance(seller_best, buyer_best, txQueries, cur_price, quantity) txQueries.push(["INSERT INTO Transactions (seller, buyer, quantity, unitValue) VALUES (?, ?, ?, ?)", [seller_best.floID, buyer_best.floID, quantity, cur_price]]); } +function beginAudit(sellerID, buyerID, unit_price, quantity) { + return new Promise((resolve, reject) => { + auditBalance(sellerID, buyerID).then(old_bal => resolve({ + end: () => endAudit(sellerID, buyerID, old_bal, unit_price, quantity) + })).catch(error => reject(error)) + }) +} + +function endAudit(sellerID, buyerID, old_bal, unit_price, quantity) { + auditBalance(sellerID, buyerID).then(new_bal => { + DB.query("INSERT INTO auditTransaction (sellerID, buyerID, quantity, unit_price, total_cost, " + + " Rupee_seller_old, Rupee_seller_new, Rupee_buyer_old, Rupee_buyer_new," + + " FLO_seller_old, FLO_seller_new, FLO_buyer_old, FLO_buyer_new) " + + " Value (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [sellerID, buyerID, quantity, unit_price, quantity * unit_price, + old_bal[sellerID].Rupee, new_bal[sellerID].Rupee, old_bal[buyerID].Rupee, new_bal[buyerID].Rupee, + old_bal[sellerID].FLO, new_bal[sellerID].FLO, old_bal[buyerID].FLO, new_bal[buyerID].FLO, + ]).then(_ => null).catch(error => console.error(error)) + }).catch(error => console.error(error)); +} + +function auditBalance(sellerID, buyerID) { + return new Promise((resolve, reject) => { + let balance = { + [sellerID]: {}, + [buyerID]: {} + }; + DB.query("SELECT floID, rupeeBalance FROM Cash WHERE floID IN (?, ?)", [sellerID, buyerID]).then(result => { + for (let i in result) + balance[result[i].floID].Rupee = result[i].rupeeBalance; + DB.query("SELECT floID, SUM(quantity) as floBal FROM Vault WHERE floID IN (?, ?) GROUP BY floID", [sellerID, buyerID]).then(result => { + for (let i in result) + balance[result[i].floID].FLO = result[i].floBal; + resolve(balance); + }).catch(error => reject(error)) + }).catch(error => reject(error)) + }) +} + module.exports = { initiate, group,