Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
user reserves against pair deletion timecomplexity issue resolved wit…
Browse files Browse the repository at this point in the history
…h new logic
  • Loading branch information
Hammad-Mubeen committed Feb 13, 2022
1 parent 6be8ec5 commit 26474a2
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 32 deletions.
22 changes: 3 additions & 19 deletions routes/deploypair.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
});
}
Expand Down
49 changes: 36 additions & 13 deletions routes/listenerroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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("(");
Expand Down Expand Up @@ -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)
Expand All @@ -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: ");
Expand All @@ -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) {
Expand Down

0 comments on commit 26474a2

Please sign in to comment.