diff --git a/routes/deploypair.js b/routes/deploypair.js index abc6fd8a..2634d9b7 100644 --- a/routes/deploypair.js +++ b/routes/deploypair.js @@ -5,8 +5,6 @@ var pair = require("../JsClients/PAIR/test/install.ts"); var pairsagainstuser = require("../models/pairsagainstuser"); var pairagainstuser = require("../models/pairagainstuser"); var pairsModel = require("../models/pair"); -var pair = require("../JsClients/PAIR/test/installed.ts"); -const { reset } = require("nodemon"); router.route("/makedeploypaircontract").post(async function (req, res, next) { try { @@ -149,29 +147,15 @@ router.route("/getpairagainstuser").post(async function (req, res, next) { message: "This user has not added liquidity against any pair.", }); } else { - for (var i = 0; i < result.length; i++) { - let liquidity = await pair.balanceOf( - result[i].pair, - result[i].id.toLowerCase() - ); - if (liquidity == "0") { - await pairagainstuser.deleteOne({ _id: result[i]._id }); - } - } - let result1 = await pairagainstuser.find({ - id: req.body.user.toLowerCase(), - }); - let pairsdata = []; - for (var i = 0; i < result1.length; i++) { - let pairsresult = await pairsModel.findOne({ id: result1[i].pair }); + for (var i = 0; i < result.length; i++) { + let pairsresult = await pairsModel.findOne({ id: result[i].pair }); pairsdata.push(pairsresult); } - return res.status(200).json({ success: true, message: "This User has added liquidity against following pairs.", - userpairs: result1, + userpairs: result, pairsdata: pairsdata, }); } diff --git a/routes/listenerroutes.js b/routes/listenerroutes.js index 00329820..cabf4ec9 100644 --- a/routes/listenerroutes.js +++ b/routes/listenerroutes.js @@ -7,6 +7,7 @@ var pairModel = require("../models/pair"); var hashesofpairsModel = require("../models/hashesofpairs"); var eventsModel = require("../models/events"); var pairagainstuser = require("../models/pairagainstuser"); +var pair = require("../JsClients/PAIR/test/installed.ts"); function splitdata(data) { var temp = data.split("("); @@ -680,6 +681,10 @@ router.route("/geteventsdata").post(async function (req, res, next) { reserve1: reserve1, }); await pairagainstuser.create(newData); + return res.status(200).json({ + success: true, + message: "Pair against User with reserves created Successfully.", + }); } else { pairagainstuserresult.reserve0 = ( BigInt(pairagainstuserresult.reserve0) + BigInt(reserve0) @@ -688,6 +693,10 @@ router.route("/geteventsdata").post(async function (req, res, next) { BigInt(pairagainstuserresult.reserve1) + BigInt(reserve1) ).toString(); await pairagainstuserresult.save(); + return res.status(200).json({ + success: true, + message: "User Reserves against pair Added Successfully.", + }); } } else if (eventName == "removereserves") { console.log(eventName + " Event result: "); @@ -713,21 +722,35 @@ router.route("/geteventsdata").post(async function (req, res, next) { pair: pair, }); if (pairagainstuserresult == null) { - let newData = new pairagainstuser({ - id: user, - pair: pair, - reserve0: reserve0, - reserve1: reserve1, + return res.status(400).json({ + success: false, + message: "There is no pair against this user to remove reserves.", }); - await pairagainstuser.create(newData); } else { - pairagainstuserresult.reserve0 = ( - BigInt(pairagainstuserresult.reserve0) - BigInt(reserve0) - ).toString(); - pairagainstuserresult.reserve1 = ( - BigInt(pairagainstuserresult.reserve1) - BigInt(reserve1) - ).toString(); - await pairagainstuserresult.save(); + let liquidity = await pair.balanceOf( + pairagainstuserresult.pair, + pairagainstuserresult.id.toLowerCase() + ); + if (liquidity == "0") { + await pairagainstuser.deleteOne({ _id: pairagainstuserresult._id }); + return res.status(200).json({ + success: true, + message: + "Record deleted because this pair against user has zero liquidity.", + }); + } else { + pairagainstuserresult.reserve0 = ( + BigInt(pairagainstuserresult.reserve0) - BigInt(reserve0) + ).toString(); + pairagainstuserresult.reserve1 = ( + BigInt(pairagainstuserresult.reserve1) - BigInt(reserve1) + ).toString(); + await pairagainstuserresult.save(); + return res.status(200).json({ + success: true, + message: "User Reserves against pair removed Successfully.", + }); + } } } } catch (error) {