add logging + small bugfix in unsettled pnl script

This commit is contained in:
lowkeynicc
2024-08-12 17:13:19 -04:00
parent bab84b58a1
commit d66a7c0042

View File

@@ -11,7 +11,6 @@ import {
Wallet, Wallet,
ZERO, ZERO,
calculateClaimablePnl, calculateClaimablePnl,
calculatePositionPNL,
decodeUser, decodeUser,
} from '@drift-labs/sdk'; } from '@drift-labs/sdk';
import { Connection, Keypair } from '@solana/web3.js'; import { Connection, Keypair } from '@solana/web3.js';
@@ -41,7 +40,7 @@ const endpoint = process.env.ENDPOINT as string;
const wsEndpoint = process.env.WS_ENDPOINT as string; const wsEndpoint = process.env.WS_ENDPOINT as string;
const driftEnv = process.env.ENV as string; const driftEnv = process.env.ENV as string;
const chunkSize = Number(process.env.CHUNK_SIZE) || 100; const chunkSize = Number(process.env.CHUNK_SIZE) || 100;
const sleepTimeMs = Number(process.env.SLEEP_TIME_MS) || 5000; const sleepTimeMs = Number(process.env.SLEEP_TIME_MS) || 500;
const delayMs = Number(process.env.DEFAULT_DELAY_MS) || 100; const delayMs = Number(process.env.DEFAULT_DELAY_MS) || 100;
const connection = new Connection(endpoint, { const connection = new Connection(endpoint, {
@@ -96,6 +95,7 @@ const main = async () => {
finishedCount += chunkSize; finishedCount += chunkSize;
logger.info(`Wrote ${finishedCount} users, sleeping for ${sleepTimeMs}ms`); logger.info(`Wrote ${finishedCount} users, sleeping for ${sleepTimeMs}ms`);
await sleep(sleepTimeMs); await sleep(sleepTimeMs);
} }
@@ -109,6 +109,12 @@ const main = async () => {
} in ${Date.now() - startTime} ms` } in ${Date.now() - startTime} ms`
); );
await sleep(5000);
logger.info('==== TEST PRINT LOSERS FROM BTC-PERP ====');
const losers = await dlobRedisClient.getRaw(`perp_market_1_losers`);
logger.info(losers);
process.exit(); process.exit();
}; };
@@ -120,16 +126,23 @@ const writeToDlobRedis = async (userPnlMap: UserPnlMap): Promise<boolean> => {
const losersRedisKey = `perp_market_${perpMarketIndex}_losers`; const losersRedisKey = `perp_market_${perpMarketIndex}_losers`;
// write the new lists // write the new lists
await dlobRedisClient.setRaw( await Promise.all([
dlobRedisClient.setRaw(
gainersRedisKey, gainersRedisKey,
JSON.stringify(userPnlMap[perpMarketIndex].gain) JSON.stringify(userPnlMap[perpMarketIndex].gain)
); ),
await dlobRedisClient.setRaw( dlobRedisClient.setRaw(
losersRedisKey, losersRedisKey,
JSON.stringify(userPnlMap[perpMarketIndex].loss) JSON.stringify(userPnlMap[perpMarketIndex].loss)
),
]).then(([_winnersResult, _losersResult]) => {
logger.info(
`Wrote ${userPnlMap[perpMarketIndex].gain.length} users to redis key: ${gainersRedisKey}`
); );
logger.info(
return; `Wrote ${userPnlMap[perpMarketIndex].loss.length} users to redis key: ${losersRedisKey}`
);
});
}) })
); );
@@ -151,7 +164,7 @@ const createMarketPnlLeaderboards = (allPnlUsers: AllPnlUsers): UserPnlMap => {
// store the top 20 winners and losers in each perp market // store the top 20 winners and losers in each perp market
pnlMap[perpMarketIndex] = { pnlMap[perpMarketIndex] = {
gain: sortedPnls.slice(0, 20), gain: sortedPnls.slice(0, 20),
loss: sortedPnls.slice(-20, -1), loss: sortedPnls.slice(-20).reverse(),
}; };
} catch (e) { } catch (e) {
logger.info( logger.info(
@@ -209,21 +222,12 @@ const buildUserMarketLists = (
false false
)[0]; )[0];
let marketPnl = calculatePositionPNL( const marketPnl = calculateClaimablePnl(
perpMarketAccount,
perpPositionWithLpSettle,
true,
oraclePriceData
);
if (marketPnl.gt(ZERO)) {
marketPnl = calculateClaimablePnl(
perpMarketAccount, perpMarketAccount,
usdcSpotMarket, usdcSpotMarket,
perpPositionWithLpSettle, perpPositionWithLpSettle,
oraclePriceData oraclePriceData
); );
}
return marketPnl.eq(ZERO) return marketPnl.eq(ZERO)
? [] ? []