Skip to content

Commit

Permalink
Merge pull request #73 from cd-sigma/main
Browse files Browse the repository at this point in the history
Build
  • Loading branch information
cd-simarpreet committed Dec 9, 2023
2 parents 7598ec5 + 5c370fb commit 57c3c7f
Show file tree
Hide file tree
Showing 25 changed files with 2,982 additions and 237 deletions.
Empty file.
2 changes: 2 additions & 0 deletions backend/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const mongoLib = require("../lib/mongo.lib")
const consoleLib = require("../lib/console.lib")
const responseLib = require("../lib/response.lib")

const cmtRoutes= require("./routes/cmt.route")
const userRoutes = require("./routes/user.route")
const feedRoutes = require("./routes/feed.route")
const alertRoutes= require("./routes/alert.route")
Expand All @@ -30,6 +31,7 @@ const port = 3001
return responseLib.sendResponse(res, {status: 200, health: "GREEN"}, null, 200)
})

app.use("/cmt", cmtRoutes)
app.use("/user", userRoutes)
app.use("/token", tokenRoutes)
app.use("/feed", feedRoutes)
Expand Down
34 changes: 34 additions & 0 deletions backend/api/controllers/cmt.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const mongoLib = require("../../lib/mongo.lib");
const responseLib = require("../../lib/response.lib");
const validatorUtil = require("../../util/validators.util");

const cmtOrderModel = require("../../model/cmt.order.model");
const resStatusEnum = require("../../enum/res.status.enum");

async function addCmtOrder(req, res) {
try {
const {positionId, collateralAddress, collateralAmount} = req.body;
let {address} = req.user.payload;
address = address.toLowerCase();

if (validatorUtil.isEmpty(collateralAddress) || validatorUtil.isEmpty(collateralAmount)) {
return responseLib.sendResponse(res, null, `collateralAddress and collateralAmount are required`
, resStatusEnum.VALIDATION_ERROR);
}

await mongoLib.createDoc(cmtOrderModel, {
ownerAddress: address,
positionId: positionId,
collateralAddress: collateralAddress,
collateralAmount: collateralAmount,
})

return responseLib.sendResponse(res, "CMT order added successfully!", null, resStatusEnum.SUCCESS);
} catch (error) {
return responseLib.sendResponse(res, null, error, resStatusEnum.INTERNAL_SERVER_ERROR)
}
}

module.exports = {
addCmtOrder: addCmtOrder
}
9 changes: 9 additions & 0 deletions backend/api/routes/cmt.route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require("express")
const router = express.Router()

const cmtController = require("../controllers/cmt.controller")
const authMiddleware = require("../../middleware/jwt.auth.middleware")

router.post("/order/create", authMiddleware.isLoggedIn, cmtController.addCmtOrder);

module.exports = router
4 changes: 4 additions & 0 deletions backend/config/service.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,9 @@
"defaultBatchProcessingLimit": 50,
"defaultSleepTime": 5000
}
},
"cmtOrderExecutor": {
"defaultBatchProcessingLimit": 50,
"defaultSleepTime": 5000
}
}
12 changes: 7 additions & 5 deletions backend/enum/dp.enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ const dpEnum = Object.freeze({
EMAIL_ALERTING_SERVICE_SLEEPTIME: "EMAIL_ALERTING_SERVICE_SLEEPTIME",
DISCORD_ALERT_PROCESSING_BATCH_LIMIT: "DISCORD_ALERT_PROCESSING_BATCH_LIMIT",
EMAIL_ALERT_PROCESSING_BATCH_LIMIT: "EMAIL_ALERT_PROCESSING_BATCH_LIMIT",
PUSH_ALERT_PROCESSING_BATCH_LIMIT:"PUSH_ALERT_PROCESSING_BATCH_LIMIT",
PUSH_ALERTING_SERVICE_SLEEPTIME:"PUSH_ALERTING_SERVICE_SLEEPTIME",
SLACK_ALERT_PROCESSING_BATCH_LIMIT:"SLACK_ALERT_PROCESSING_BATCH_LIMIT",
SLACK_ALERTING_SERVICE_SLEEPTIME:"SLACK_ALERTING_SERVICE_SLEEPTIME",
AAVE_V2_HEALTH_FACTOR_THRESHOLD:"AAVE_V2_HEALTH_FACTOR_THRESHOLD"
PUSH_ALERT_PROCESSING_BATCH_LIMIT: "PUSH_ALERT_PROCESSING_BATCH_LIMIT",
PUSH_ALERTING_SERVICE_SLEEPTIME: "PUSH_ALERTING_SERVICE_SLEEPTIME",
SLACK_ALERT_PROCESSING_BATCH_LIMIT: "SLACK_ALERT_PROCESSING_BATCH_LIMIT",
SLACK_ALERTING_SERVICE_SLEEPTIME: "SLACK_ALERTING_SERVICE_SLEEPTIME",
AAVE_V2_HEALTH_FACTOR_THRESHOLD: "AAVE_V2_HEALTH_FACTOR_THRESHOLD",
CMT_ORDER_PROCESSING_LIMIT: "CMT_ORDER_PROCESSING_LIMIT",
CMT_ORDER_PROCESSING_SLEEP_TIME:"CMT_ORDER_PROCESSING_SLEEP_TIME"
})

module.exports = dpEnum;
14 changes: 8 additions & 6 deletions backend/global.const.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const ERROR_STACK_TRACE_LIMIT = 1000;
const NULL_ADDRESS = "0x0000000000000000000000000000000000000000";
const AAVE_V2_PRICE_ORACLE_ADDRESS =
"0xa50ba011c48153de246e5192c8f9258a2ba79ca9";
"0xa50ba011c48153de246e5192c8f9258a2ba79ca9";
const FINSAFE_EXECUTOR_ADDRESS = "0x7d2768de32b0b80b7a3454c06bdac94a69ddc7a9"; //TODO: change this to the actual address
const EMAIL_HOST = "mail.privateemail.com";
const EMAIL_PORT = 465;
module.exports = {
ERROR_STACK_TRACE_LIMIT: ERROR_STACK_TRACE_LIMIT,
NULL_ADDRESS: NULL_ADDRESS,
AAVE_V2_PRICE_ORACLE_ADDRESS: AAVE_V2_PRICE_ORACLE_ADDRESS,
EMAIL_HOST: EMAIL_HOST,
EMAIL_PORT: EMAIL_PORT,
ERROR_STACK_TRACE_LIMIT: ERROR_STACK_TRACE_LIMIT,
FINSAFE_EXECUTOR_ADDRESS: FINSAFE_EXECUTOR_ADDRESS,
NULL_ADDRESS: NULL_ADDRESS,
AAVE_V2_PRICE_ORACLE_ADDRESS: AAVE_V2_PRICE_ORACLE_ADDRESS,
EMAIL_HOST: EMAIL_HOST,
EMAIL_PORT: EMAIL_PORT,
};
39 changes: 39 additions & 0 deletions backend/lib/cmt.lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const globalConst=require("../global.const");
const errorUtil = require("../util/error.util");
const validatorUtil = require("../util/validators.util");

const finsafeExecutorAbi= require("../abi/finsafe.executor.abi.json");

function validateOrder(order) {
try {
if (validatorUtil.isEmpty(order.ownerAddress) || validatorUtil.isEmpty(order.positionId) || validatorUtil.isEmpty(order.timestamp) || validatorUtil.isEmpty(order.collateralAddress) || validatorUtil.isEmpty(order.collateralAmount)) {
return false;
}
return true;
} catch (error) {
throw error;
}
}


async function executeOrder(order, web3) {
try {
if(validatorUtil.isEmpty(order) || validatorUtil.isEmpty(web3)) {
errorUtil.throwErr("Invalid order or web3!");
}

if(!validateOrder(order)) {
errorUtil.throwErr("Invalid order!");
}

const lendingPoolContract= new web3.eth.Contract(finsafeExecutorAbi, globalConst.FINSAFE_EXECUTOR_ADDRESS);
//TODO: execute order
// await lendingPoolContract.methods.executeOrder(order.ownerAddress, order.collateralAddress, order.collateralAmount)
} catch (error) {
throw error;
}
}

module.exports = {
executeOrder: executeOrder
}
37 changes: 37 additions & 0 deletions backend/model/cmt.order.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const mongoose = require("mongoose")
const dbEnum = require("../enum/db.enum")

const cmtOrderSchema = new mongoose.Schema({
ownerAddress: {
type: String,
required: true
},
positionId: {
type: String,
required: true
},
timestamp: {
type: Number,
required: true
},
collateralAddress: {
type: String,
required: true
},
collateralAmount: {
type: Number,
required: true
},
isExecuted: {
type: Boolean,
default: false
},
isFailed: {
type: Boolean,
default: false
}
}, {
timestamps: true
})

module.exports = mongoose.connection.useDb(dbEnum.FINSAFE).model("cmt_order", cmtOrderSchema)
Loading

0 comments on commit 57c3c7f

Please sign in to comment.