Skip to content

Commit

Permalink
Merge pull request #66 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 338f487 + 4934a02 commit 7598ec5
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 87 deletions.
9 changes: 9 additions & 0 deletions backend/api/controllers/alert.controller.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const mongoLib = require("../../lib/mongo.lib");
const emailLib = require("../../lib/email.lib");
const slackLib = require("../../lib/slack.lib");
const discordLib = require("../../lib/discord.lib");
const responseLib = require("../../lib/response.lib");
const validatorUtil = require("../../util/validators.util");

Expand All @@ -20,6 +23,8 @@ async function subscribeToEmail(req, res) {
$addToSet: {alertPreferences: alertTypeEnum.EMAIL}, email: email
});

await emailLib.sendEmail(email, "Welcome To finSafe", "You have successfully subscribed to finSafe for alerts.")

return responseLib.sendResponse(res, "Subscribed to email successfully!", null, resStatusEnum.SUCCESS);
} catch (error) {
return responseLib.sendResponse(res, null, error, resStatusEnum.INTERNAL_SERVER_ERROR)
Expand All @@ -40,6 +45,8 @@ async function subscribeToSlack(req, res) {
$addToSet: {alertPreferences: alertTypeEnum.SLACK}, slackWebhook: webhook
});

await slackLib.sendAlert(webhook, "Welcome To finSafe", "You have successfully subscribed to finSafe for alerts.")

return responseLib.sendResponse(res, "Subscribed to slack successfully!", null, resStatusEnum.SUCCESS);
} catch (error) {
return responseLib.sendResponse(res, null, error, resStatusEnum.INTERNAL_SERVER_ERROR)
Expand All @@ -60,6 +67,8 @@ async function subscribeToDiscord(req, res) {
$addToSet: {alertPreferences: alertTypeEnum.DISCORD}, discordWebhook: webhook
});

await discordLib.sendAlert(webhook, "Welcome To finSafe", "You have successfully subscribed to finSafe for alerts.")

return responseLib.sendResponse(res, "Subscribed to discord successfully!", null, resStatusEnum.SUCCESS);
} catch (error) {
return responseLib.sendResponse(res, null, error, resStatusEnum.INTERNAL_SERVER_ERROR)
Expand Down
23 changes: 15 additions & 8 deletions backend/api/controllers/token.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ const priceLib = require("../../lib/price.lib")
const responseLib = require("../../lib/response.lib")
const validatorsUtil = require("../../util/validators.util")
const resStatusEnum = require("../../enum/res.status.enum")

const tokenLogoModel = require("../../model/token.logo.model");
const { mongo } = require("mongoose");
const mongoLib = require("../../lib/mongo.lib");
async function getTokenInfo(req, res) {
try {
let web3 = await web3Lib.getWebSocketWeb3Instance(process.env.ETH_NODE_WS_URL);
Expand All @@ -16,23 +18,28 @@ async function getTokenInfo(req, res) {
return responseLib.sendResponse(res, null, "Missing token Address", resStatusEnum.VALIDATION_ERROR)
}
tokenAddress = tokenAddress.toLowerCase()
const url = `https://api.1inch.dev/token/v1.2/1/custom/${tokenAddress}`;
const tokenPrice = await priceLib.getTokenPriceFromChainlinkPriceOracle("eth", tokenAddress, web3)
const logoExist = await mongoLib.findOneByQuery(tokenLogoModel, {address : tokenAddress} )

if(validatorsUtil.isNotEmpty(logoExist)){
return responseLib.sendResponse(res,{ logo: logoExist.logo, price: tokenPrice.usdPrice } , null ,resStatusEnum.SUCCESS)
}

const url = `https://api.1inch.dev/token/v1.2/1/custom/${tokenAddress}`;
const config = {
headers: {
"Authorization": `Bearer ${apiKeyConfig["1inch"]}`
}, params: {}
};

const response = await axios.get(url, config);

if (validatorsUtil.isEmpty(response) || validatorsUtil.isEmpty(response.data)) {
return responseLib.sendResponse(res, null, "token not available", resStatusEnum.INTERNAL_SERVER_ERROR)
}

const tokenPrice = await priceLib.getTokenPriceFromChainlinkPriceOracle("eth", tokenAddress, web3)
response.data.price = tokenPrice.usdPrice
return responseLib.sendResponse(res, response.data, null, resStatusEnum.SUCCESS)


await mongoLib.findOneAndUpdate(tokenLogoModel , { address : response.data.address.toLowerCase() } , {logo :response.data.logoURI } ,{upsert : true})
return responseLib.sendResponse(res, { logo : response.data.logoURI , price : tokenPrice.usdPrice }, null, resStatusEnum.SUCCESS)

} catch (error) {
return responseLib.sendResponse(res, null, error, resStatusEnum.INTERNAL_SERVER_ERROR)
}
Expand Down
8 changes: 4 additions & 4 deletions backend/lib/alert.lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ async function generateAlert(address, timestamp, title, body) {

function isValidPushAlert(alert) {
try {
if (validatorUtil.isEmpty(alert) || validatorUtil.isEmpty(alert.address) || validatorUtil.isEmpty(alert.title) || validatorUtil.isEmpty(alert.body) || validatorUtil.isEmpty(alert.timestamp)) {
if (validatorUtil.isEmpty(alert) || validatorUtil.isEmpty(alert.address) || validatorUtil.isEmpty(alert.title) || validatorUtil.isEmpty(alert.body) || validatorUtil.isNil(alert.timestamp)) {
return false;
}
return true;
Expand All @@ -99,7 +99,7 @@ function isValidPushAlert(alert) {

function isValidEmailAlert(alert) {
try {
if (validatorUtil.isEmpty(alert) || validatorUtil.isEmpty(alert.email) || validatorUtil.isEmpty(alert.subject) || validatorUtil.isEmpty(alert.body) || validatorUtil.isEmpty(alert.timestamp)) {
if (validatorUtil.isEmpty(alert) || validatorUtil.isEmpty(alert.email) || validatorUtil.isEmpty(alert.subject) || validatorUtil.isEmpty(alert.body) || validatorUtil.isNil(alert.timestamp)) {
return false;
}
return true;
Expand All @@ -110,7 +110,7 @@ function isValidEmailAlert(alert) {

function isValidSlackAlert(alert) {
try {
if (validatorUtil.isEmpty(alert) || validatorUtil.isEmpty(alert.title) || validatorUtil.isEmpty(alert.body) || validatorUtil.isEmpty(alert.webhook) || validatorUtil.isEmpty(alert.timestamp)) {
if (validatorUtil.isEmpty(alert) || validatorUtil.isEmpty(alert.title) || validatorUtil.isEmpty(alert.body) || validatorUtil.isEmpty(alert.webhook) || validatorUtil.isNil(alert.timestamp)) {
return false;
}
return true;
Expand All @@ -121,7 +121,7 @@ function isValidSlackAlert(alert) {

function isValidDiscordAlert(alert) {
try {
if (validatorUtil.isEmpty(alert) || validatorUtil.isEmpty(alert.body) || validatorUtil.isEmpty(alert.webhook) || validatorUtil.isEmpty(alert.timestamp)) {
if (validatorUtil.isEmpty(alert) || validatorUtil.isEmpty(alert.body) || validatorUtil.isEmpty(alert.webhook) || validatorUtil.isNil(alert.timestamp)) {
return false;
}
return true;
Expand Down
18 changes: 18 additions & 0 deletions backend/model/token.logo.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mongoose = require("mongoose")
const dbEnum = require("../enum/db.enum")

const tokenLogo = new mongoose.Schema(
{
address: {
type: String,
},
logo: {
type: String,
}
},
{timestamps: true},
)

tokenLogo.index({address: 1})

module.exports = mongoose.connection.useDb(dbEnum.FINSAFE).model("logo", tokenLogo)
6 changes: 6 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"@testing-library/user-event": "^13.5.0",
"axios": "^1.6.2",
"body-parser": "^1.20.2",
"dayjs": "^1.11.10",
"express": "^4.18.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/api/axios.instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ let tokenAxios = axios.create({
baseURL: "https://finsafe-backend.insidefi.io/token/"
});

export { userAxios, tokenAxios };
let feedAxios = axios.create({
baseURL:"https://finsafe-backend.insidefi.io/feed/"
});

export { userAxios, tokenAxios ,feedAxios};
13 changes: 12 additions & 1 deletion frontend/src/api/profile.api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { userAxios, tokenAxios } from "./axios.instance";
import {userAxios, tokenAxios, feedAxios} from "./axios.instance";

export const getPortfolioDetails = async (address) => {
try {
Expand All @@ -22,3 +22,14 @@ export const getTokenDetails = async (token) => {
console.log(`Error occured while fetching token details`, err);
}
};
export const getFeedDetails = async (address) => {
try {
const response = await feedAxios.get(`${address}?limit=20`);
const { result } = response.data;
console.log(result);
const alert=result.data.alerts;
return alert;
} catch (err) {
console.log(`Error occured while fetching feed details`, err);
}
}
15 changes: 8 additions & 7 deletions frontend/src/components/HomePage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ import React, {useState} from "react";
import Web3 from "web3";
import ConnectWalletButton from "./ConnectWalltet/Walltet";
import axios from "axios";
import {useUserStore} from "../store/userStore";
import Onboarding from "./Onboarding/Onboarding";

const HomePage = () => {
const [loading, setLoading] = useState(false);
const [address, setAddress] = useState("");
const userAddress = useUserStore((state) => state.userAddress);
const setUserAddress = useUserStore((state) => state.setUserAddress)
const [open, setOpen] = useState(false);
const onPressConnect = async () => {
setLoading(true);
Expand All @@ -18,7 +21,7 @@ const HomePage = () => {
});

let account = Web3.utils.toChecksumAddress(accounts[0]);
setAddress(account);
setUserAddress(account);
account = account.toLowerCase();
// get nonce
const nonce = await axios.get(
Expand All @@ -35,8 +38,6 @@ const HomePage = () => {
params: [message, account],
});

console.log(signature);
// send signature to backend

const response = await axios.post(
"https://finsafe-backend.insidefi.io/user/validate/signature",
Expand All @@ -45,7 +46,6 @@ const HomePage = () => {
signature: signature,
}
);

if (response.data.status === 200) {
setOpen(true);
}
Expand All @@ -56,8 +56,9 @@ const HomePage = () => {

setLoading(false);
};
console.log(userAddress)

const onPressLogout = () => setAddress("");
const onPressLogout = () => setUserAddress("");
return (
<div
style={{
Expand All @@ -75,7 +76,7 @@ const HomePage = () => {
onPressConnect={onPressConnect}
onPressLogout={onPressLogout}
loading={loading}
address={address}
address={userAddress}
/>
<Onboarding open={open} setOpen={setOpen} />
</div>
Expand Down
102 changes: 51 additions & 51 deletions frontend/src/components/PortfolioDetails.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import React, { useState, useEffect } from "react";
import {
Accordion,
AccordionSummary,
AccordionDetails,
Typography,
Table,
TableBody,
TableCell,
Expand All @@ -12,28 +8,37 @@ import {
TableRow,
Paper,
} from "@mui/material";
import ExpandMoreIcon from "@mui/icons-material/ExpandMore";
import { useUserStore } from "../store/userStore";

import { getPortfolioDetails } from "../api/profile.api";
import { getPortfolioDetails, getTokenDetails } from "../api/profile.api";

const PortfolioDetails = () => {
const [expanded, setExpanded] = useState(false);
const [suppliedDetails, setSuppliedDetails] = useState([]);
const [borrowedDetails, setBorrowedDetails] = useState([]);
const handleAccordionChange = () => {
setExpanded((prevExpanded) => !prevExpanded);
};
let userAddress = useUserStore((state) => state.userAddress);
userAddress = "0x464c71f6c2f760dda6093dcb91c24c39e5d6e18c";

const convertSuppliedToDesiredFormat = (array) => {
let result = [];
// array.map((value) => {
// let obj = {};
// obj.supplied =
// })
array.map(async (value) => {
const { token, balance, symbol } = value;
// const moreDetails = await getTokenDetails(token);
const moreDetails = {
logoURI:
"https://tokens-data.1inch.io/images/0xdac17f958d2ee523a2206206994597c13d831ec7.png",
price: 1,
};
let obj = {};
obj.logo = moreDetails?.logoURI;
obj.symbol = symbol;
obj.balance = balance.toFixed(2);
obj.price = (moreDetails?.price * balance).toFixed(2);
result.push(obj);
});
return result;
};
const callApis = async () => {
const details = await getPortfolioDetails(
"0x464c71f6c2f760dda6093dcb91c24c39e5d6e18c"
);
const details = await getPortfolioDetails(userAddress);
const { metadata } = details[0];
const { supplied, borrowed } = metadata;
const derivedSupplied = convertSuppliedToDesiredFormat(supplied);
Expand All @@ -45,42 +50,37 @@ const PortfolioDetails = () => {
}, []);
console.log(suppliedDetails);
return (
<Accordion expanded={expanded} onChange={handleAccordionChange}>
<AccordionSummary
expandIcon={<ExpandMoreIcon />}
aria-controls="panel-content"
id="panel-header"
>
<Typography>Supplied</Typography>
</AccordionSummary>
<AccordionDetails>
<TableContainer component={Paper}>
<Table>
<TableHead>
<TableRow>
<TableCell>Supplied</TableCell>
<TableCell>Balance</TableCell>
<TableCell>USD Value</TableCell>
</TableRow>
</TableHead>
<TableBody>
{/* Add your table rows here */}
<TableRow>
<TableCell>Item 1</TableCell>
<TableCell>10</TableCell>
<TableCell>$100</TableCell>
</TableRow>
<TableContainer component={Paper}>
<Table>
<TableHead style={{marginBottom:"50px"}}>
<TableRow>
<TableCell>Supplied</TableCell>
<TableCell>Balance</TableCell>
<TableCell>USD Value</TableCell>
</TableRow>
</TableHead>
<TableBody>
{/* Add your table rows here */}
{suppliedDetails.map((value) => {
const { logo, balance, price, symbol } = value;
return (
<TableRow>
<TableCell>Item 2</TableCell>
<TableCell>20</TableCell>
<TableCell>$200</TableCell>
<TableCell>
<div style={{ display: "flex", alignItems: "center" }}>
<img src={logo} style={{ width: "30px" }} />
<span style={{ marginLeft: "10px" }}>{symbol}</span>
</div>
</TableCell>
<TableCell>
{balance} {symbol}
</TableCell>
<TableCell>${price}</TableCell>
</TableRow>
{/* Add more rows as needed */}
</TableBody>
</Table>
</TableContainer>
</AccordionDetails>
</Accordion>
);
})}
</TableBody>
</Table>
</TableContainer>
);
};

Expand Down
Loading

0 comments on commit 7598ec5

Please sign in to comment.