From 91862a4bbfd764ac1623871120d1359ed5317f10 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 15 May 2024 16:55:17 -0400 Subject: [PATCH] add test script --- indexer/services/roundtable/package.json | 1 + .../src/helpers/pnl-ticks-helper.ts | 171 +++++++++++++----- indexer/services/roundtable/src/pnl-script.ts | 37 ++++ 3 files changed, 159 insertions(+), 50 deletions(-) create mode 100644 indexer/services/roundtable/src/pnl-script.ts diff --git a/indexer/services/roundtable/package.json b/indexer/services/roundtable/package.json index 7b856e1135..2df3d11c8b 100644 --- a/indexer/services/roundtable/package.json +++ b/indexer/services/roundtable/package.json @@ -11,6 +11,7 @@ "coverage": "pnpm test -- --coverage", "lint": "eslint --ext .ts,.js .", "lint:fix": "eslint --ext .ts,.js . --fix", + "run-pnl": "ts-node src/pnl-script.ts", "test": "NODE_ENV=test jest --runInBand --forceExit" }, "author": "", diff --git a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts index 121743676e..d86f987fd2 100644 --- a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts +++ b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts @@ -64,7 +64,7 @@ export async function getPnlTicksCreateObjects( SubaccountFromDatabase[], ] = await Promise.all([ getMostRecentPnlTicksForEachAccount(), - SubaccountTable.getSubaccountsWithTransfers(blockHeight, {readReplica: true, txId}), + SubaccountTable.getSubaccountsWithTransfers(blockHeight, { readReplica: true, txId }), ]); stats.timing( `${config.SERVICE_NAME}_get_ticks_relevant_accounts`, @@ -101,62 +101,133 @@ export async function getPnlTicksCreateObjects( ); const getFundingIndexStart: number = Date.now(); const blockHeightToFundingIndexMap: _.Dictionary = await - getBlockHeightToFundingIndexMap( - subaccountsWithTransfers, - accountsToUpdate, - txId, - ); + getBlockHeightToFundingIndexMap( + subaccountsWithTransfers, + accountsToUpdate, + txId, + ); stats.timing( `${config.SERVICE_NAME}_get_ticks_funding_indices`, new Date().getTime() - getFundingIndexStart, ); const getAccountInfoStart: number = Date.now(); - const [ - subaccountTotalTransfersMap, - openPerpetualPositions, - usdcAssetPositions, - netUsdcTransfers, - markets, - currentFundingIndexMap, - ]: [ - SubaccountAssetNetTransferMap, - SubaccountToPerpetualPositionsMap, - { [subaccountId: string]: Big }, - SubaccountUsdcTransferMap, - PriceMap, - FundingIndexMap, - ] = await Promise.all([ - TransferTable.getNetTransfersPerSubaccount( - blockHeight, - { - readReplica: true, - txId, - }, - ), - PerpetualPositionTable.findOpenPositionsForSubaccounts( - accountsToUpdate, - { - readReplica: true, - txId, - }, - ), - AssetPositionTable.findUsdcPositionForSubaccounts( - accountsToUpdate, - { - readReplica: true, - txId, - }, - ), - getUsdcTransfersSinceLastPnlTick( - accountsToUpdate, - mostRecentPnlTicks, - blockHeight, + // const [ + // subaccountTotalTransfersMap, + // openPerpetualPositions, + // usdcAssetPositions, + // netUsdcTransfers, + // markets, + // currentFundingIndexMap, + // ]: [ + // SubaccountAssetNetTransferMap, + // SubaccountToPerpetualPositionsMap, + // { [subaccountId: string]: Big }, + // SubaccountUsdcTransferMap, + // PriceMap, + // FundingIndexMap, + // ] = await Promise.all([ + // TransferTable.getNetTransfersPerSubaccount( + // blockHeight, + // { + // readReplica: true, + // txId, + // }, + // ), + // PerpetualPositionTable.findOpenPositionsForSubaccounts( + // accountsToUpdate, + // { + // readReplica: true, + // txId, + // }, + // ), + // AssetPositionTable.findUsdcPositionForSubaccounts( + // accountsToUpdate, + // { + // readReplica: true, + // txId, + // }, + // ), + // getUsdcTransfersSinceLastPnlTick( + // accountsToUpdate, + // mostRecentPnlTicks, + // blockHeight, + // txId, + // ), + // OraclePriceTable.findLatestPrices(blockHeight), + // FundingIndexUpdatesTable.findFundingIndexMap(blockHeight), + // ]); + + // Retrieve net transfers per subaccount + const subaccountTotalTransfersMap: SubaccountAssetNetTransferMap = await + TransferTable.getNetTransfersPerSubaccount( + blockHeight, + { + readReplica: true, txId, - ), - OraclePriceTable.findLatestPrices(blockHeight), - FundingIndexUpdatesTable.findFundingIndexMap(blockHeight), - ]); + }, + ); + logger.info({ + at: 'pnl-ticks-helper#computePnl', + message: 'got subaccountTotalTransfersMap', + }); + + // Find open positions for subaccounts + const openPerpetualPositions: SubaccountToPerpetualPositionsMap = await + PerpetualPositionTable.findOpenPositionsForSubaccounts( + accountsToUpdate, + { + readReplica: true, + txId, + }, + ); + logger.info({ + at: 'pnl-ticks-helper#computePnl', + message: 'got openPerpetualPositions', + }); + + // Find USDC positions for subaccounts + const usdcAssetPositions: { [subaccountId: string]: Big } = await + AssetPositionTable.findUsdcPositionForSubaccounts( + accountsToUpdate, + { + readReplica: true, + txId, + }, + ); + logger.info({ + at: 'pnl-ticks-helper#computePnl', + message: 'got usdcAssetPositions', + }); + + // Get USDC transfers since the last PnL tick + const netUsdcTransfers: SubaccountUsdcTransferMap = await + getUsdcTransfersSinceLastPnlTick( + accountsToUpdate, + mostRecentPnlTicks, + blockHeight, + txId, + ); + logger.info({ + at: 'pnl-ticks-helper#computePnl', + message: 'got netUsdcTransfers', + }); + + // Retrieve latest market prices + const markets: PriceMap = await OraclePriceTable.findLatestPrices(blockHeight); + logger.info({ + at: 'pnl-ticks-helper#computePnl', + message: 'got markets', + }); + + // Find funding index map + const currentFundingIndexMap: FundingIndexMap = await + FundingIndexUpdatesTable.findFundingIndexMap(blockHeight); + logger.info({ + at: 'pnl-ticks-helper#computePnl', + message: 'got currentFundingIndexMap', + }); + stats.timing( `${config.SERVICE_NAME}_get_ticks_account_info`, new Date().getTime() - getAccountInfoStart, diff --git a/indexer/services/roundtable/src/pnl-script.ts b/indexer/services/roundtable/src/pnl-script.ts new file mode 100644 index 0000000000..c895da9b28 --- /dev/null +++ b/indexer/services/roundtable/src/pnl-script.ts @@ -0,0 +1,37 @@ +import { logger } from '@dydxprotocol-indexer/base'; +import { IsoString, PnlTicksCreateObject, Transaction } from '@dydxprotocol-indexer/postgres'; + +import { getPnlTicksCreateObjects } from './helpers/pnl-ticks-helper'; + +async function main() { + + const blockHeight: string = '12428756'; + const blockTime: IsoString = '2024-05-15T20:36:34.543Z'; + const txId: number = await Transaction.start(); + try { + const newTicksToCreate: PnlTicksCreateObject[] = await + getPnlTicksCreateObjects(blockHeight, blockTime, txId); + logger.info({ + at: 'create-pnl-ticks#runTask', + message: 'New PNL ticks created', + newTicksToCreate, + txId, + }); + } catch (error) { + logger.error({ + at: 'create-pnl-ticks#runTask', + message: 'Error when getting pnl ticks', + error, + txId, + }); + return; + } finally { + await Transaction.rollback(txId); + } +} + +main().then(() => { + console.log('Process completed.'); +}).catch((error) => { + console.error('Failed to run main function:', error); +});