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

Commit

Permalink
endpoint made for removeLiquidityCSPR and removereserves event check …
Browse files Browse the repository at this point in the history
…updated
  • Loading branch information
Hammad-Mubeen committed Apr 21, 2022
1 parent 9a00a8f commit 0343ce1
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 140 deletions.
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var erc20Router = require("./routes/erc20routes");
var coinsmarketcapapiRouter = require("./routes/coinsmarketcapapi");
var pathRouter = require("./routes/pathroutes");
var readWasmRouter = require("./routes/readWasm");
var setUserForRemoveLiquidityCSPRRouter = require("./routes/setUserForRemoveLiquidityCSPR");

// view engine setup
app.set("views", path.join(__dirname, "views"));
Expand Down Expand Up @@ -83,6 +84,7 @@ app.use("/", erc20Router);
app.use("/", coinsmarketcapapiRouter);
app.use("/", pathRouter);
app.use("/", readWasmRouter);
app.use("/", setUserForRemoveLiquidityCSPRRouter);

app.use(
"/graphql",
Expand Down
14 changes: 0 additions & 14 deletions models/approveData.js

This file was deleted.

12 changes: 2 additions & 10 deletions models/removeReservesData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,8 @@ const removeReservesDataSchema = new Schema({
},
deployHash: {
type: String,
},
pair: {
type: String,
},
reserve0: {
type: String,
},
reserve1: {
type: String,
},
}

});

var removeReservesData = mongoose.model(
Expand Down
126 changes: 10 additions & 116 deletions routes/listenerroutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var eventsModel = require("../models/events");
var pairagainstuser = require("../models/pairagainstuser");
var paircontract = require("../JsClients/PAIR/test/installed.ts");
var allcontractsDataModel = require("../models/allcontractsData");
var ApproveDataModel = require("../models/approveData");
var RemoveReservesDataModel = require("../models/removeReservesData");

function splitdata(data) {
Expand Down Expand Up @@ -257,100 +256,11 @@ router.route("/geteventsdata").post(async function (req, res, next) {
console.log(newData[3][0].data + " = " + newData[3][1].data);
console.log(newData[4][0].data + " = " + newData[4][1].data);

let user = splitdata(newData[2][1].data);
let newData = new ApproveDataModel({
user: user,
deployHash: deployHash,
});
await ApproveDataModel.create(newData);

let removeReserveData = await RemoveReservesDataModel.findOne({
deployHash: deployHash,
});
if (removeReserveData == null) {
return res.status(200).json({
return res.status(200).json({
success: true,
message:
"Approved Event emitted, there were no removereserve Event against this deployHash.",
});
} else {
let pair = removeReserveData.pair;
let reserve0 = removeReserveData.reserve0;
let reserve1 = removeReserveData.reserve1;
// await RemoveReservesDataModel.deleteOne({ _id: removeReserveData._id });
let pairagainstuserresult = await pairagainstuser.findOne({
id: user,
pair: pair,
});
if (pairagainstuserresult == null) {
return res.status(400).json({
success: false,
message: "There is no pair against this user to remove reserves.",
});
} else {
let data = await allcontractsDataModel.findOne({
packageHash: pairagainstuserresult.pair,
});

let liquidity = await paircontract.balanceOf(
data.contractHash,
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 {
if (
BigInt(pairagainstuserresult.reserve0) >
BigInt(pairagainstuserresult.reserve1)
) {
if (BigInt(reserve0) > BigInt(reserve1)) {
pairagainstuserresult.reserve0 = (
BigInt(pairagainstuserresult.reserve0) - BigInt(reserve0)
).toString();
pairagainstuserresult.reserve1 = (
BigInt(pairagainstuserresult.reserve1) - BigInt(reserve1)
).toString();
await pairagainstuserresult.save();
} else {
pairagainstuserresult.reserve0 = (
BigInt(pairagainstuserresult.reserve0) - BigInt(reserve1)
).toString();
pairagainstuserresult.reserve1 = (
BigInt(pairagainstuserresult.reserve1) - BigInt(reserve0)
).toString();
await pairagainstuserresult.save();
}
} else {
if (BigInt(reserve1) > BigInt(reserve0)) {
pairagainstuserresult.reserve0 = (
BigInt(pairagainstuserresult.reserve0) - BigInt(reserve0)
).toString();
pairagainstuserresult.reserve1 = (
BigInt(pairagainstuserresult.reserve1) - BigInt(reserve1)
).toString();
await pairagainstuserresult.save();
} else {
pairagainstuserresult.reserve0 = (
BigInt(pairagainstuserresult.reserve0) - BigInt(reserve1)
).toString();
pairagainstuserresult.reserve1 = (
BigInt(pairagainstuserresult.reserve1) - BigInt(reserve0)
).toString();
await pairagainstuserresult.save();
}
}
return res.status(200).json({
success: true,
message: "User Reserves against pair removed Successfully.",
});
}
}
}
"Approved Event emitted",
});
} else if (eventName == "erc20_transfer") {
console.log(eventName + " Event result: ");
console.log(newData[0][0].data + " = " + newData[0][1].data);
Expand Down Expand Up @@ -915,31 +825,15 @@ router.route("/geteventsdata").post(async function (req, res, next) {
console.log("routerPackageHash: ", routerPackageHash);

if (routerPackageHash == user) {
let approveResult = await ApproveDataModel.findOne({
let RemoveReservesResult = await RemoveReservesDataModel.findOne({
deployHash: deployHash,
});
if (approveResult == null) {
let newData = new RemoveReservesDataModel({
user: user,
deployHash: deployHash,
pair: pair,
reserve0: reserve0,
reserve1: reserve1,
});
await RemoveReservesDataModel.create(newData);
return res.status(200).json({
success: true,
message:
"There is no Approve Event against this deployHash, saving removereserves Event data .",
});
} else {
user = approveResult.user;
console.log(
"User from approve Event saved to removereserves user:",
user
);
//await ApproveDataModel.deleteOne({ _id: approveResult._id });
}

user = RemoveReservesResult.user;
console.log(
"User from approve Event saved to removereserves user:",
user
);
}

let pairagainstuserresult = await pairagainstuser.findOne({
Expand Down
40 changes: 40 additions & 0 deletions routes/setUserForRemoveLiquidityCSPR.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
require("dotenv").config();
var express = require("express");
var router = express.Router();
var RemoveReservesDataModel = require("../models/removeReservesData");

router
.route("/setUserForRemoveLiquidityCSPR")
.post(async function (req, res, next) {
try {
if (!req.body.user) {
return res.status(400).json({
success: false,
message: "user did not found in the request body.",
});
}
if (!req.body.deployHash) {
return res.status(400).json({
success: false,
message: "deployHash did not found in the request body."
});
}
let newData = new RemoveReservesDataModel({
user: req.body.user,
deployHash: req.body.deployHash,
});
await RemoveReservesDataModel.create(newData);
return res.status(200).json({
success: true,
message: "User successfully stored for removed liquidity CSPR."
});
} catch (error) {
console.log("error (try-catch) : " + error);
return res.status(500).json({
success: false,
err: error,
});
}
});

module.exports = router;

0 comments on commit 0343ce1

Please sign in to comment.