Skip to content

Commit

Permalink
index liquidation pool data
Browse files Browse the repository at this point in the history
  • Loading branch information
ewansheldon committed Feb 27, 2024
1 parent 75463e7 commit b55cd67
Showing 1 changed file with 42 additions and 14 deletions.
56 changes: 42 additions & 14 deletions src/liquidationPools.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ const Pool = require('pg-pool');
const { getNetwork } = require("./networks");
const { getContract } = require('./contractFactory');
const { formatEther, parseUnits } = require('ethers/lib/utils');
const { createClient } = require('redis');
const { WALLET_PRIVATE_KEY } = process.env;

const EUROS_ADDRESS = '0x643b34980E635719C15a2D4ce69571a258F940E9';
const TST_ADDRESS = '0xf5A27E55C748bCDdBfeA5477CB9Ae924f0f7fd2e';
const EUROS_ADDRESS = '0x643b34980e635719c15a2d4ce69571a258f940e9';
const TST_ADDRESS = '0xf5a27e55c748bcddbfea5477cb9ae924f0f7fd2e';

const redisHost = process.env.REDIS_HOST || '127.0.0.1';
const redisPort = process.env.REDIS_PORT || '6379';

const {
POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DB, POSTGRES_USERNAME, POSTGRES_PASSWORD
Expand All @@ -23,23 +27,47 @@ let pool = new Pool({
port: POSTGRES_PORT
});

const redis = createClient({
url: `redis://${redisHost}:${redisPort}`
});
redis.on('error', err => console.log('Redis Client Error', err));

const convert = (amount, price, dec) => {
return Number(formatUnits(amount.mul(price)
.div(BigNumber.from(10).pow(8)), dec)).toFixed(2);
};

const cachedPrice = async address => {
await redis.connect();
const cached = await redis.GET(`cached:${address}`);
await redis.disconnect();
return cached;
}

const cachePrice = async (address, price) => {
await redis.connect();
await redis.SET(`cached:${address}`, price);
await redis.disconnect();
}

const fetchDexPrice = async address => {
return new Promise(resolve => {
return new Promise(async resolve => {
https.get(`https://api.dexscreener.com/latest/dex/tokens/${address}`, res => {
let json = '';

res.on('data', data => {
json += data;
});

res.on('end', _ => {
console.log(json)
resolve(parseUnits(JSON.parse(json).pairs[0].priceUsd, 8).toString());
res.on('end', async _ => {
try {
const price = parseUnits(JSON.parse(json).pairs[0].priceUsd, 8).toString();
await cachePrice(address, price);
resolve(price);
} catch (e) {
console.log(e);
resolve(await cachedPrice(address));
}
});
});
});
Expand All @@ -57,7 +85,7 @@ const fetchPrices = async (networkName, wallet) => {
};

const scheduleLiquidationPoolData = async _ => {
schedule.scheduleJob('* * * * *', async _ => {
schedule.scheduleJob('20 * * * *', async _ => {
console.log('indexing liquidation pool snapshots')
const network = getNetwork('arbitrum');
const provider = new getDefaultProvider(network.rpc);
Expand All @@ -81,13 +109,13 @@ const scheduleLiquidationPoolData = async _ => {
const client = await pool.connect();
try {
console.log(POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DB, POSTGRES_USERNAME, POSTGRES_PASSWORD.length)
// const query = 'INSERT INTO user_pool_snapshots (user_address,assets,snapshot_at) VALUES ($1,$2,$3);';
// const data = [
// holderAddress.toLowerCase(),
// assets,
// now
// ];
// await client.query(query, data);
const query = 'INSERT INTO user_pool_snapshots (user_address,assets,snapshot_at) VALUES ($1,$2,$3);';
const data = [
holderAddress.toLowerCase(),
assets,
now
];
await client.query(query, data);
} catch (e) {
console.log(e);
} finally {
Expand Down

0 comments on commit b55cd67

Please sign in to comment.