Skip to content

Commit

Permalink
add test script
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill committed May 15, 2024
1 parent cc5378e commit 91862a4
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 50 deletions.
1 change: 1 addition & 0 deletions indexer/services/roundtable/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "",
Expand Down
171 changes: 121 additions & 50 deletions indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down Expand Up @@ -101,62 +101,133 @@ export async function getPnlTicksCreateObjects(
);
const getFundingIndexStart: number = Date.now();
const blockHeightToFundingIndexMap: _.Dictionary<FundingIndexMap> = 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,
Expand Down
37 changes: 37 additions & 0 deletions indexer/services/roundtable/src/pnl-script.ts
Original file line number Diff line number Diff line change
@@ -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);
});

0 comments on commit 91862a4

Please sign in to comment.