Skip to content

Commit

Permalink
indexer added for trading volume
Browse files Browse the repository at this point in the history
  • Loading branch information
SibghatCodora committed Mar 29, 2024
1 parent 18b2293 commit c2a0dc3
Show file tree
Hide file tree
Showing 12 changed files with 1,420 additions and 522 deletions.
9 changes: 8 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ export const DB_COLLECTION = process.env.DB_COLLECTION;
export const DB_COLLECTION_STAKING_SNAPSHOT = process.env.DB_COLLECTION_STAKING_SNAPSHOT;
export const DB_COLLECTION_SNAP_CONFIG_BALANCE = process.env.DB_COLLECTION_SNAP_CONFIG_BALANCE;
export const DB_COLLECTION_SNAP_HODL_CONFIG = process.env.DB_COLLECTION_SNAP_HODL_CONFIG;
export const DB_COLLECTION_TRADING_SNAP_HODL_CONFIG =
process.env.DB_COLLECTION_TRADING_SNAP_HODL_CONFIG;
export const DB_COLLECTION_TRADING_SNAPSHOT =
process.env.DB_COLLECTION_TRADING_SNAPSHOT;
export const CRON_SCHEDULE = process.env.CRON_SCHEDULE;
export const VOLUME_CRON_SCHEDULE = process.env.VOLUME_CRON_SCHEDULE;
export const PORT = process.env.PORT;

// An array to hold the environment variables
Expand All @@ -22,7 +27,9 @@ const envVariables = [
{ name: 'DB_COLLECTION', value: DB_COLLECTION },
{ name: 'DB_COLLECTION_STAKING_SNAPSHOT', value: DB_COLLECTION_STAKING_SNAPSHOT },
{ name: 'DB_COLLECTION_SNAP_CONFIG_BALANCE', value: DB_COLLECTION_SNAP_CONFIG_BALANCE },
{ name: 'CRON_SCHEDULE', value: CRON_SCHEDULE }
{ name: 'CRON_SCHEDULE', value: CRON_SCHEDULE },
{ name: 'VOLUME_CRON_SCHEDULE', value: VOLUME_CRON_SCHEDULE },
{ name: 'PORT', value: PORT },
];

// Check if each environment variable is set
Expand Down
309 changes: 206 additions & 103 deletions src/controllers/snapHodlConfigController.ts
Original file line number Diff line number Diff line change
@@ -1,153 +1,256 @@
// src/controllers/snapHodlConfigController.ts

import { Request, Response } from 'express';
import TradingSnapHodlModel from '../models/TradingSnapshot';
import SnapHodlConfigModel from '../models/SnapHodlConfig';
import SnapHodlConfigBalanceModel from '../models/SnapHodlConfigBalance';
import { SnapHodlConfig } from '../types';
import BigNumber from 'bignumber.js';
import mongoose from 'mongoose';


function sortStakingContractData(data: any[]) {
return data.sort((a, b) => a.stakingPoolName.localeCompare(b.stakingPoolName));
return data.sort((a, b) => a.stakingPoolName.localeCompare(b.stakingPoolName));
}

function toLowerCaseStakingContractData(data: any[]) {
return data.map(item => ({
...item,
stakingContractAddress: item.stakingContractAddress.toLowerCase(),
tokenContractAddress: item.tokenContractAddress.toLowerCase(),
stakingPoolType: item.stakingPoolType.toLowerCase(),
}));
return data.map((item) => ({
...item,
stakingContractAddress: item.stakingContractAddress.toLowerCase(),
tokenContractAddress: item.tokenContractAddress.toLowerCase(),
stakingPoolType: item.stakingPoolType.toLowerCase(),
}));
}


export const retrieveSnapHodlConfigs = async (): Promise<SnapHodlConfig[]> => {
export const getChainTradingSnapShotsTotalBySnapHodlConfigIdAndChainId = async (
req: Request,
res: Response
) => {
if (req.query.snapHodlConfigId && req.query.chainId) {
try {
return await SnapHodlConfigModel.find();
} catch (err) {
if (err instanceof Error) {
throw new Error(err.message);
} else {
throw new Error('An error occurred when attempting to fetch SnapHodlConfigs');
const snapHodlConfig: SnapHodlConfig | null =
await SnapHodlConfigModel.findOne({
_id: { $eq: req.query.snapHodlConfigId },
});
if (
snapHodlConfig &&
snapHodlConfig.tradingVolumeContractData &&
snapHodlConfig.tradingVolumeContractData?.length > 0
) {
let total = new BigNumber("0");
let result: any[] = [];
for (
let i = 0;
i < snapHodlConfig.tradingVolumeContractData.length;
i++
) {
if (
snapHodlConfig.tradingVolumeContractData[i].chainId ===
req.query.chainId
) {
const tradingSnapshot = await TradingSnapHodlModel.findOne({
chainId: { $eq: req.query.chainId },
liquidityPoolAddress: {
$eq: snapHodlConfig.tradingVolumeContractData[i]
.liquidityPoolAddress,
},
tokenContractAddress: {
$eq: snapHodlConfig.tradingVolumeContractData[i]
.tokenContractAddress,
},
});

if (tradingSnapshot && tradingSnapshot?.tradesVolumeBalances) {
console.log(tradingSnapshot?.tradesVolumeBalances);
Object.entries<string>(tradingSnapshot?.tradesVolumeBalances).forEach(
([address, balance]) => {
console.log(balance)
total = total.plus(balance);
});
}
}
}
return res.json({totalTradingByChainId:total});
}
} catch (error) {
if (error instanceof Error) {
return res.status(500).json({ message: error.message });
} else {
return res.status(500).json({ message: "An unexpected error occurred." });
}
}
}
return res.json({total : 0});
};

export const retrieveSnapHodlConfigs = async (): Promise<SnapHodlConfig[]> => {
try {
return await SnapHodlConfigModel.find();
} catch (err) {
if (err instanceof Error) {
throw new Error(err.message);
} else {
throw new Error('An error occurred when attempting to fetch SnapHodlConfigs');
}
}
};



export const getSnapHodlConfigs = async (req: Request, res: Response) => {
try {
const snapHodlConfigs = await retrieveSnapHodlConfigs();
res.json(snapHodlConfigs);
} catch (err) {
if (err instanceof Error) {
res.status(500).json({ message: err.message });
} else {
res.status(500).json({ message: 'An error occurred when attempting to request SnapHodlConfigs to be retrieved' });
}
try {
const snapHodlConfigs = await retrieveSnapHodlConfigs();
res.json(snapHodlConfigs);
} catch (err) {
if (err instanceof Error) {
res.status(500).json({ message: err.message });
} else {
res.status(500).json({ message: 'An error occurred when attempting to request SnapHodlConfigs to be retrieved' });
}
}
};


export const createSnapHodlConfig = async (req: Request, res: Response) => {
const { snapShotConfigName, isActive, stakingContractData } = req.body;
const { snapShotConfigName, isActive, stakingContractData } = req.body;

// Convert stakingContractData values to lower case
const lowerCaseStakingContractData = toLowerCaseStakingContractData(stakingContractData);
// Convert stakingContractData values to lower case
const lowerCaseStakingContractData = toLowerCaseStakingContractData(stakingContractData);

// Sort stakingContractData by stakingPoolName for consistent comparison
const sortedStakingContractData = sortStakingContractData(lowerCaseStakingContractData);
// Sort stakingContractData by stakingPoolName for consistent comparison
const sortedStakingContractData = sortStakingContractData(lowerCaseStakingContractData);

// Check for duplicate stakingContractData
const duplicateConfig = await SnapHodlConfigModel.findOne({
stakingContractData: { $eq: sortedStakingContractData },
});
// Check for duplicate stakingContractData
const duplicateConfig = await SnapHodlConfigModel.findOne({
stakingContractData: { $eq: sortedStakingContractData },
});

if (duplicateConfig) {
return res.status(400).json({
message: `Duplicate stakingContractData detected. Please use the ${duplicateConfig.snapShotConfigName} config, which has the same stakingContractData, or change the stakingContractData for the new config.`
});
}

const newSnapHodlConfig = new SnapHodlConfigModel({
snapShotConfigName,
isActive,
stakingContractData: sortedStakingContractData,
if (duplicateConfig) {
return res.status(400).json({
message: `Duplicate stakingContractData detected. Please use the ${duplicateConfig.snapShotConfigName} config, which has the same stakingContractData, or change the stakingContractData for the new config.`
});
}

try {
const savedSnapHodlConfig = await newSnapHodlConfig.save();
res.json(savedSnapHodlConfig);
} catch (err) {
if (err instanceof Error) {
res.status(500).json({ message: err.message });
} else {
res.status(500).json({ message: 'An error occurred' });
}
const newSnapHodlConfig = new SnapHodlConfigModel({
snapShotConfigName,
isActive,
stakingContractData: sortedStakingContractData,
});

try {
const savedSnapHodlConfig = await newSnapHodlConfig.save();
res.json(savedSnapHodlConfig);
} catch (err) {
if (err instanceof Error) {
res.status(500).json({ message: err.message });
} else {
res.status(500).json({ message: 'An error occurred' });
}
}
};

export const getSnapShotBySnapShotIdAndAddress = async (req: Request, res: Response) => {
try {
let { snapShotId, address } = req.params;
const { raw } = req.query;
try {
let { snapShotId, address } = req.params;
const { raw } = req.query;

address = address.toLowerCase();
address = address.toLowerCase();

const snapHodlConfigBalance = await SnapHodlConfigBalanceModel.findOne({ snapHodlConfigId: snapShotId });
const snapHodlConfigBalance = await SnapHodlConfigBalanceModel.findOne({ snapHodlConfigId: snapShotId });

if (!snapHodlConfigBalance) {
return res.status(404).json({ message: 'SnapShot not found' });
}
if (!snapHodlConfigBalance) {
return res.status(404).json({ message: 'SnapShot not found' });
}

const snapShotBalance = snapHodlConfigBalance.totalStakedBalance.get(address);
const snapShotBalance = snapHodlConfigBalance.totalStakedBalance.get(address);

if (!snapShotBalance) {
return res.status(404).json({ message: 'Address not found in SnapShot' });
}
if (!snapShotBalance) {
return res.status(404).json({ message: 'Address not found in SnapShot' });
}

if (raw === 'true') {
return res.send(snapShotBalance.toString());
} else {
const result = {
snapShotConfigName: snapHodlConfigBalance.snapShotConfigName,
address: address,
snapShotBalance: snapShotBalance,
updatedAt: snapHodlConfigBalance.updatedAt,
};

return res.json(result);
}
} catch (error) {
if (error instanceof Error) {
return res.status(500).json({ message: error.message });
} else {
return res.status(500).json({ message: "An unexpected error occurred." });
}
if (raw === 'true') {
return res.send(snapShotBalance.toString());
} else {
const result = {
snapShotConfigName: snapHodlConfigBalance.snapShotConfigName,
address: address,
snapShotBalance: snapShotBalance,
updatedAt: snapHodlConfigBalance.updatedAt,
};

return res.json(result);
}
} catch (error) {
if (error instanceof Error) {
return res.status(500).json({ message: error.message });
} else {
return res.status(500).json({ message: "An unexpected error occurred." });
}
}
};

// New function to retrieve all documents from DB_COLLECTION_SNAP_CONFIG_BALANCE
export const getAllSnapShots = async (req: Request, res: Response) => {
try {
const query: { [key: string]: any } = {};
if(req.query.snapHodlConfigId){
query.snapHodlConfigId = req.query.snapHodlConfigId;
}
const page = parseInt(req.query.page as string) || 1; // defaults to 1 if not provided
const limit = parseInt(req.query.limit as string) || 10; // defaults to 10 if not provided
const skip = (page - 1) * limit;

// Find all documents in the collection with pagination
const snapHodlConfigBalances = await SnapHodlConfigBalanceModel.find(query).skip(skip).limit(limit);

// Send the result as a JSON response
return res.json(snapHodlConfigBalances);
} catch (error) {
if (error instanceof Error) {
return res.status(500).json({ message: error.message });
} else {
return res.status(500).json({ message: "An unexpected error occurred." });
}
try {
const query: { [key: string]: any } = {};
if(req.query.snapHodlConfigId){
query.snapHodlConfigId = req.query.snapHodlConfigId;
}
};
const page = parseInt(req.query.page as string) || 1; // defaults to 1 if not provided
const limit = parseInt(req.query.limit as string) || 10; // defaults to 10 if not provided
const skip = (page - 1) * limit;

// Find all documents in the collection with pagination
const snapHodlConfigBalances = await SnapHodlConfigBalanceModel.find(query).skip(skip).limit(limit);

// Send the result as a JSON response
return res.json(snapHodlConfigBalances);
} catch (error) {
if (error instanceof Error) {
return res.status(500).json({ message: error.message });
} else {
return res.status(500).json({ message: "An unexpected error occurred." });
}
}
};

// export async function saveBulkUserTransactionVolume(
// data: any
// ): Promise<any | null> {
// // const client = new MongoClient(connectionString);
// // let leaderboard = null;
// try {
// // await client.connect();
// // const database = client.db(dbName);
// // console.log("database: ", database);
// // const collection = database.collection(dbCollection);

// // console.log("collection: ", collection);

// const bulkOperations: any = [];

// // Prepare update operations for existing documents
// Object.keys(data).forEach((item) => {
// bulkOperations.push({
// updateOne: {
// filter: { walletAddress: item }, // Filter by unique key
// update: {
// $set: { walletAddress: item, totalVolume: data[item] },
// }, // Update all fields in the document
// upsert: true, // Insert a new document if no match is found
// },
// });
// });

// // You can add additional insertOne operations for new documents here
// console.log("bulkOperations: ", bulkOperations);
// const result = await LeaderboardModel.bulkWrite(bulkOperations);
// console.log("result: ", result);
// } catch (err) {
// console.error(
// "Error while saving the data against address the database:",
// err
// );
// }
// // return leaderboard;
// }
Loading

0 comments on commit c2a0dc3

Please sign in to comment.