Skip to content

Commit

Permalink
Merge pull request #85 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 f85f924 + 024f69c commit 87a03ff
Show file tree
Hide file tree
Showing 9 changed files with 595 additions and 7 deletions.
297 changes: 297 additions & 0 deletions backend/abi/aave.v2.protocol.data.provider.abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
[
{
"inputs": [
{
"internalType": "contract ILendingPoolAddressesProvider",
"name": "addressesProvider",
"type": "address"
}
],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "ADDRESSES_PROVIDER",
"outputs": [
{
"internalType": "contract ILendingPoolAddressesProvider",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getAllATokens",
"outputs": [
{
"components": [
{
"internalType": "string",
"name": "symbol",
"type": "string"
},
{
"internalType": "address",
"name": "tokenAddress",
"type": "address"
}
],
"internalType": "struct AaveProtocolDataProvider.TokenData[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getAllReservesTokens",
"outputs": [
{
"components": [
{
"internalType": "string",
"name": "symbol",
"type": "string"
},
{
"internalType": "address",
"name": "tokenAddress",
"type": "address"
}
],
"internalType": "struct AaveProtocolDataProvider.TokenData[]",
"name": "",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "asset",
"type": "address"
}
],
"name": "getReserveConfigurationData",
"outputs": [
{
"internalType": "uint256",
"name": "decimals",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "ltv",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "liquidationThreshold",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "liquidationBonus",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "reserveFactor",
"type": "uint256"
},
{
"internalType": "bool",
"name": "usageAsCollateralEnabled",
"type": "bool"
},
{
"internalType": "bool",
"name": "borrowingEnabled",
"type": "bool"
},
{
"internalType": "bool",
"name": "stableBorrowRateEnabled",
"type": "bool"
},
{
"internalType": "bool",
"name": "isActive",
"type": "bool"
},
{
"internalType": "bool",
"name": "isFrozen",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "asset",
"type": "address"
}
],
"name": "getReserveData",
"outputs": [
{
"internalType": "uint256",
"name": "availableLiquidity",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "totalStableDebt",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "totalVariableDebt",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "liquidityRate",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "variableBorrowRate",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "stableBorrowRate",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "averageStableBorrowRate",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "liquidityIndex",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "variableBorrowIndex",
"type": "uint256"
},
{
"internalType": "uint40",
"name": "lastUpdateTimestamp",
"type": "uint40"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "asset",
"type": "address"
}
],
"name": "getReserveTokensAddresses",
"outputs": [
{
"internalType": "address",
"name": "aTokenAddress",
"type": "address"
},
{
"internalType": "address",
"name": "stableDebtTokenAddress",
"type": "address"
},
{
"internalType": "address",
"name": "variableDebtTokenAddress",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "asset",
"type": "address"
},
{
"internalType": "address",
"name": "user",
"type": "address"
}
],
"name": "getUserReserveData",
"outputs": [
{
"internalType": "uint256",
"name": "currentATokenBalance",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "currentStableDebt",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "currentVariableDebt",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "principalStableDebt",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "scaledVariableDebt",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "stableBorrowRate",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "liquidityRate",
"type": "uint256"
},
{
"internalType": "uint40",
"name": "stableRateLastUpdated",
"type": "uint40"
},
{
"internalType": "bool",
"name": "usageAsCollateralEnabled",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
}
]
2 changes: 2 additions & 0 deletions backend/api/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const consoleLib = require("../lib/console.lib")
const responseLib = require("../lib/response.lib")

const cmtRoutes= require("./routes/cmt.route")
const healthFactorRoutes = require("./routes/health.factor.route")
const userRoutes = require("./routes/user.route")
const feedRoutes = require("./routes/feed.route")
const alertRoutes= require("./routes/alert.route")
Expand Down Expand Up @@ -36,6 +37,7 @@ const port = 3001
app.use("/token", tokenRoutes)
app.use("/feed", feedRoutes)
app.use("/alert", alertRoutes)
app.use("/health/factor", healthFactorRoutes)

app.listen(port, () => {
consoleLib.logInfo(`API listening at http://localhost:${port}`)
Expand Down
70 changes: 70 additions & 0 deletions backend/api/controllers/health.factor.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const responseLib = require("../../lib/response.lib")
const mongoLib = require("../../lib/mongo.lib")

const healthFactorResolver = require("../resolvers/health.factor.resolver")
const validatorUtil = require("../../util/validators.util")
const positionModel = require("../../model/position.model")

const resStatusEnum = require("../../enum/res.status.enum")
const web3Lib = require("../../lib/web3.lib");

async function defaultHealthFactor(req, res) {
try {
let web3 = await web3Lib.getWebSocketWeb3Instance(process.env.ETH_NODE_WS_URL);
let {address} = req.params

if (validatorUtil.isEmpty(address)) {
return responseLib.sendResponse(res, null, "Missing user address", resStatusEnum.VALIDATION_ERROR)
}

address = address.toLowerCase()

const position = await mongoLib.findOneByQuery(positionModel, {owner: address})

if (validatorUtil.isEmpty(position)) {
return responseLib.sendResponse(res, null, "No position found", resStatusEnum.VALIDATION_ERROR)
}

const healthFactor = await healthFactorResolver.defaultHealthFactor(position, web3)

return responseLib.sendResponse(res, healthFactor, null, resStatusEnum.SUCCESS)

} catch (error) {
return responseLib.sendResponse(res, null, error, resStatusEnum.INTERNAL_SERVER_ERROR)
}
}

async function calculateHealthFactor(req, res) {
try {
let web3 = await web3Lib.getWebSocketWeb3Instance(process.env.ETH_NODE_WS_URL);
let {address} = req.params
let {
collateralAssets = [],
collateralAmounts = [],
collateralPrices = [],
debtAssets = [],
debtAmounts = [],
debtPrices = [],
liquidationThresholds = []
} = req.body

if (validatorUtil.isEmpty(address)) {
return responseLib.sendResponse(res, null, "Missing user address", resStatusEnum.VALIDATION_ERROR)
}

address = address.toLowerCase()

const healthFactor = await healthFactorResolver.calculateHealthFactor(web3, collateralAssets, collateralAmounts, collateralPrices, debtAssets, debtAmounts, debtPrices, liquidationThresholds)

return responseLib.sendResponse(res, healthFactor, null, resStatusEnum.SUCCESS)


} catch (error) {
return responseLib.sendResponse(res, null, error, resStatusEnum.INTERNAL_SERVER_ERROR)
}
}

module.exports = {
calculateHealthFactor: calculateHealthFactor,
defaultHealthFactor: defaultHealthFactor
}
5 changes: 2 additions & 3 deletions backend/api/controllers/token.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ async function getTokenInfo(req, res) {
try {
tokenPrice = (await priceLib.getTokenPriceFromChainlinkPriceOracle("eth", tokenAddress, web3)).usdPrice
} catch (error) {
const usdPrice = await priceLib.getPriceFromAavePriceOracle([tokenAddress], web3)
const decimals = await priceLib.getDecimalsForAsset(tokenAddress, web3)
const ethAmount = parseFloat(usdPrice[0]) / 10 ** decimals
const ethPrice = await priceLib.getPriceFromAavePriceOracle([tokenAddress], web3)
const ethAmount = parseFloat(ethPrice[0]) / 10 ** 18
tokenPrice = await priceLib.convertEthAmountToUsd(ethAmount, web3)
}

Expand Down
Loading

0 comments on commit 87a03ff

Please sign in to comment.