diff --git a/app.js b/app.js index 38a8f038..4859c39f 100644 --- a/app.js +++ b/app.js @@ -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")); @@ -83,6 +84,7 @@ app.use("/", erc20Router); app.use("/", coinsmarketcapapiRouter); app.use("/", pathRouter); app.use("/", readWasmRouter); +app.use("/", setUserForRemoveLiquidityCSPRRouter); app.use( "/graphql", diff --git a/models/approveData.js b/models/approveData.js deleted file mode 100644 index 9eb0559b..00000000 --- a/models/approveData.js +++ /dev/null @@ -1,14 +0,0 @@ -var mongoose = require("mongoose"); -var Schema = mongoose.Schema; - -const approveDataSchema = new Schema({ - user: { - type: String, - }, - deployHash: { - type: String, - }, -}); - -var approveData = mongoose.model("approveData", approveDataSchema); -module.exports = approveData; diff --git a/models/removeReservesData.js b/models/removeReservesData.js index c5c2c4b6..b76b7d2f 100644 --- a/models/removeReservesData.js +++ b/models/removeReservesData.js @@ -7,16 +7,8 @@ const removeReservesDataSchema = new Schema({ }, deployHash: { type: String, - }, - pair: { - type: String, - }, - reserve0: { - type: String, - }, - reserve1: { - type: String, - }, + } + }); var removeReservesData = mongoose.model( diff --git a/routes/listenerroutes.js b/routes/listenerroutes.js index 6b2910e8..3ef413b7 100644 --- a/routes/listenerroutes.js +++ b/routes/listenerroutes.js @@ -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) { @@ -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); @@ -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({ diff --git a/routes/setUserForRemoveLiquidityCSPR.js b/routes/setUserForRemoveLiquidityCSPR.js new file mode 100644 index 00000000..0d92136e --- /dev/null +++ b/routes/setUserForRemoveLiquidityCSPR.js @@ -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;