Skip to content

Commit

Permalink
remove indexing stats
Browse files Browse the repository at this point in the history
  • Loading branch information
ewansheldon committed Sep 7, 2023
1 parent 3088c56 commit fe5c272
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 51 deletions.
3 changes: 1 addition & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ const http = require('http');
require('dotenv').config();
const { scheduleLiquidation } = require('./src/liquidation');
const { getPrices } = require('./src/pricing');
const { getStats, scheduleStatIndexing } = require('./src/stats');
const { getStats } = require('./src/stats');

const port = process.env.PORT || 3000;

scheduleLiquidation();
scheduleStatIndexing();

const server = http.createServer(async (req, res) => {
console.log(`${new Date().toISOString()} | ${req.method} ${req.url}`);
Expand Down
51 changes: 2 additions & 49 deletions src/stats.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
const schedule = require('node-schedule');
const ethers = require('ethers');
const { createClient } = require('redis');
const { getContract, getERC20 } = require('./contractFactory');
const { getNetwork } = require('./networks');

const redisHost = process.env.REDIS_HOST || '127.0.0.1';
const redisPort = process.env.REDIS_PORT || '6379';
Expand All @@ -12,60 +8,17 @@ const redis = createClient({
});
redis.on('error', err => console.log('Redis Client Error', err));


const indexStats = async _ => {
await redis.connect();

const network = getNetwork('arbitrum');
const provider = new ethers.getDefaultProvider(network.rpc)
const wallet = new ethers.Wallet(process.env.WALLET_PRIVATE_KEY, provider);
const tokens = await (await getContract(network.name, 'TokenManager')).connect(wallet).getAcceptedTokens();
const eventData = await (await getContract(network.name, 'SmartVaultManager')).connect(wallet)
.queryFilter('*');
const vaultAddresses = eventData.filter(e => e.args).map(e => e.args[0]);
const tvl = [];
for (let i = 0; i < tokens.length; i++) {
const { addr } = tokens[i];
let assetTvl = 0n;
for (let j = 0; j < vaultAddresses.length; j++) {
const vaultAddress = vaultAddresses[j];
if (addr === ethers.ZeroAddress) {
assetTvl += await provider.getBalance(vaultAddress);
} else {
assetTvl += await (await getERC20(addr)).connect(wallet).balanceOf(vaultAddress);
}
}
tvl.push({address: addr, assetTvl: assetTvl.toString()});
}

let multi = redis.MULTI()
.DEL('assets')
.SADD('assets', tvl.map(asset => asset.address));

tvl.forEach(asset => {
multi = multi.SETEX(`tvl:${asset.address}`, 3600, asset.assetTvl);
});

await multi.EXEC();
}

const scheduleStatIndexing = async _ => {
schedule.scheduleJob(`*/10 * * * *`, async _ => {
await indexStats();
});
}

const getStats = async _ => {
await redis.connect();
const assets = await redis.SMEMBERS('assets');
const tvl = [];
for (let i = 0; i < assets.length; i++) {
tvl.push({address: assets[i], amount: await redis.GET(`tvl:${assets[i]}`)});
}
await redis.disconnect();
return {tvl}
}

module.exports = {
getStats,
scheduleStatIndexing
getStats
}

0 comments on commit fe5c272

Please sign in to comment.