From da2248573597a43d00684278c49aa6b1b3b4f219 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 11:42:29 -0400 Subject: [PATCH 01/15] add logs --- .../src/helpers/pnl-ticks-helper.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts index ab4ab6957c..44343f474b 100644 --- a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts +++ b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts @@ -73,15 +73,32 @@ export async function getPnlTicksCreateObjects( mostRecentPnlTicks, (pnlTick: PnlTicksCreateObject) => pnlTick.blockTime, ); + logger.info({ + at: 'pnl-ticks-helper#getPnlTicksCreateObjects', + message: 'Account to last updated block time', + accountToLastUpdatedBlockTime, + }); const subaccountIdsWithTranfers: string[] = _.map(subaccountsWithTransfers, 'id'); const newSubaccountIds: string[] = _.difference( subaccountIdsWithTranfers, _.keys(accountToLastUpdatedBlockTime), ); + logger.info({ + at: 'pnl-ticks-helper#getPnlTicksCreateObjects', + message: 'New subaccounts with transfers', + newSubaccountIds, + }); // get accounts to update based on last updated block height const accountsToUpdate: string[] = [ ...getAccountsToUpdate(accountToLastUpdatedBlockTime, blockTime), ...newSubaccountIds, ].slice(0, config.PNL_TICK_MAX_ACCOUNTS_PER_RUN); + logger.info({ + at: 'pnl-ticks-helper#getPnlTicksCreateObjects', + message: 'Accounts to update', + accountsToUpdate, + blockHeight, + blockTime, + }); stats.gauge( `${config.SERVICE_NAME}_get_ticks_accounts_to_update`, accountsToUpdate.length, @@ -183,6 +200,11 @@ export async function getPnlTicksCreateObjects( }); } }); + logger.info({ + at: 'pnl-ticks-helper#getPnlTicksCreateObjects', + message: 'New ticks to create', + subaccountIds: _.map(newTicksToCreate, 'subaccountId'), + }); stats.timing( `${config.SERVICE_NAME}_get_ticks_compute_pnl`, new Date().getTime() - computePnlStart, @@ -290,6 +312,13 @@ export function getNewPnlTick( lastUpdatedFundingIndexMap: FundingIndexMap, currentFundingIndexMap: FundingIndexMap, ): PnlTicksCreateObject { + logger.info({ + at: 'createPnlTicks#getNewPnlTick', + message: 'Creating new PNL tick', + subaccountId, + latestBlockHeight, + latestBlockTime, + }); const currentEquity: Big = calculateEquity( usdcPositionSize, openPerpetualPositionsForSubaccount, From 418b451043c1be954bc4f0194d5e2e96d14ba829 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 11:43:33 -0400 Subject: [PATCH 02/15] gh wf --- .github/workflows/indexer-build-and-push-dev-staging.yml | 1 + .github/workflows/indexer-build-and-push-mainnet.yml | 1 + 2 files changed, 2 insertions(+) diff --git a/.github/workflows/indexer-build-and-push-dev-staging.yml b/.github/workflows/indexer-build-and-push-dev-staging.yml index 5a98b72552..649e88ca30 100644 --- a/.github/workflows/indexer-build-and-push-dev-staging.yml +++ b/.github/workflows/indexer-build-and-push-dev-staging.yml @@ -3,6 +3,7 @@ name: Indexer Build & Push Images to AWS ECR for Dev / Staging branches on: # yamllint disable-line rule:truthy push: branches: + - 'wl/pnldbg' - main - 'release/indexer/v[0-9]+.[0-9]+.x' # e.g. release/indexer/v0.1.x - 'release/indexer/v[0-9]+.x' # e.g. release/indexer/v1.x diff --git a/.github/workflows/indexer-build-and-push-mainnet.yml b/.github/workflows/indexer-build-and-push-mainnet.yml index 78d54d2f68..d94a0456f2 100644 --- a/.github/workflows/indexer-build-and-push-mainnet.yml +++ b/.github/workflows/indexer-build-and-push-mainnet.yml @@ -3,6 +3,7 @@ name: Indexer Build & Push Images to AWS ECR for Mainnet on: # yamllint disable-line rule:truthy push: branches: + - 'wl/pnldbg' - main - 'release/indexer/v[0-9]+.[0-9]+.x' # e.g. release/indexer/v0.1.x - 'release/indexer/v[0-9]+.x' # e.g. release/indexer/v1.x From 52e3a2e45a7df482b2ed2fa199e6e593a4c74547 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 12:05:47 -0400 Subject: [PATCH 03/15] add more logs --- .../roundtable/src/helpers/pnl-ticks-helper.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts index 44343f474b..ede3a3c303 100644 --- a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts +++ b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts @@ -193,6 +193,7 @@ export async function getPnlTicksCreateObjects( logger.error({ at: 'pnl-ticks-helper#getPnlTicksCreateObjects', message: 'Error when getting new pnl tick', + error, account, pnlTicksToBeCreatedAt, blockHeight, @@ -357,7 +358,7 @@ export function getNewPnlTick( }); } - return { + const pnlTick: PnlTicksCreateObject = { totalPnl: totalPnl.toFixed(6), netTransfers: usdcNetTransfersSinceLastPnlTick.toFixed(6), subaccountId, @@ -366,6 +367,13 @@ export function getNewPnlTick( blockHeight: latestBlockHeight, blockTime: latestBlockTime, }; + logger.info({ + at: 'createPnlTicks#getNewPnlTick', + message: 'New PNL tick', + subaccountId, + pnlTick, + }); + return pnlTick; } /** From ab15341ec08ccd04125ed6cbb6a9af1d2c24de0a Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 12:55:46 -0400 Subject: [PATCH 04/15] add more dbg logs --- .../roundtable/src/helpers/pnl-ticks-helper.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts index ede3a3c303..40566b97d9 100644 --- a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts +++ b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts @@ -319,6 +319,11 @@ export function getNewPnlTick( subaccountId, latestBlockHeight, latestBlockTime, + usdcPositionSize, + openPerpetualPositionsForSubaccount: JSON.stringify(openPerpetualPositionsForSubaccount), + marketPrices: JSON.stringify(marketPrices), + lastUpdatedFundingIndexMap: JSON.stringify(lastUpdatedFundingIndexMap), + currentFundingIndexMap: JSON.stringify(currentFundingIndexMap), }); const currentEquity: Big = calculateEquity( usdcPositionSize, @@ -332,12 +337,19 @@ export function getNewPnlTick( currentEquity, subaccountTotalTransfersMap[subaccountId][USDC_ASSET_ID], ); + logger.info({ + at: 'createPnlTicks#getNewPnlTick', + message: 'Calculated equity and total pnl', + subaccountId, + currentEquity: currentEquity.toFixed(), + totalPnl: totalPnl.toFixed(), + }); const mostRecentPnlTick: PnlTicksCreateObject | undefined = mostRecentPnlTicks[subaccountId]; // if there has been a significant chagne in equity or totalPnl, log it for debugging purposes. if ( - mostRecentPnlTick && + mostRecentPnlTick !== undefined && Big(mostRecentPnlTick.equity).gt(0) && currentEquity.div(mostRecentPnlTick.equity).gt(2) && totalPnl.gte(10000) && From 87c1707091f19dd10d7d0b4bdcfdf51a2b74a7a8 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 15:11:27 -0400 Subject: [PATCH 05/15] add script --- .../services/roundtable/src/helpers/script.ts | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 indexer/services/roundtable/src/helpers/script.ts diff --git a/indexer/services/roundtable/src/helpers/script.ts b/indexer/services/roundtable/src/helpers/script.ts new file mode 100644 index 0000000000..fba312010b --- /dev/null +++ b/indexer/services/roundtable/src/helpers/script.ts @@ -0,0 +1,106 @@ +// Sample data from the logs +import {PerpetualPositionFromDatabase, PerpetualPositionStatus, PositionSide} from '@dydxprotocol-indexer/postgres'; +import Big from 'big.js'; + +const subaccountId = '070180e5-7da1-58b2-b7ae-15d690160a0c'; +const latestBlockHeight = 19010585; +const latestBlockTime = new Date('2024-06-26T18:56:28.918Z'); +const usdcPositionSize = new Big('63947.007662'); + +const openPerpetualPositionsForSubaccount: PerpetualPositionFromDatabase[] = [ + { + id: '623d0608-240b-5cd0-a5b3-6dd1d5f22c5c', + subaccountId, + perpetualId: '3', + side: PositionSide.SHORT, + status: PerpetualPositionStatus.OPEN, + size: '-8770', + maxSize: '-3500', + entryPrice: '0.57064218928164196123', + exitPrice: undefined, + sumOpen: '8770', + sumClose: '0', + createdAt: '2024-06-19T14:55:32.510Z', + closedAt: undefined, + createdAtHeight: '18488696', + closedAtHeight: undefined, + openEventId: Buffer.from([1, 26, 29, 120, 0, 0, 0, 2, 0, 0, 0, 0]), + closeEventId: undefined, + lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), + settledFunding: '0.047358', + }, + // ... (other positions) +]; + +const marketPrices = { + '0': '60909.76231', + '1': '3361.77635', + '2': '13.9680125', + '3': '0.552134848', + // ... (other prices) +}; + +const lastUpdatedFundingIndexMap = { + '0': '10061.34', + '1': '543.858', + '2': '2.985764', + '3': '0.1285959', + // ... (other indices) +}; + +const currentFundingIndexMap = { + '0': '10055.39', + '1': '543.467', + '2': '2.982482', + '3': '0.1286097', + // ... (other indices) +}; + +const subaccountTotalTransfersMap: { [key: string]: { [key: string]: Big } } = { + '070180e5-7da1-58b2-b7ae-15d690160a0c': { + 'USDC': new Big('63947.007662'), + }, +}; + +const USDC_ASSET_ID = 'USDC'; + +// Dummy implementations of calculateEquity and calculateTotalPnl +function calculateEquity( + usdcPositionSize: Big, + openPerpetualPositionsForSubaccount: PerpetualPosition[], + marketPrices: { [key: string]: string }, + lastUpdatedFundingIndexMap: { [key: string]: string }, + currentFundingIndexMap: { [key: string]: string } +): Big { + // Implement your equity calculation logic here + return new Big(0); +} + +function calculateTotalPnl( + currentEquity: Big, + subaccountTotalTransfers: Big +): Big { + // Implement your PnL calculation logic here + return new Big(0); +} + +// Call the functions with the provided data +const currentEquity: Big = calculateEquity( + usdcPositionSize, + openPerpetualPositionsForSubaccount, + marketPrices, + lastUpdatedFundingIndexMap, + currentFundingIndexMap +); + +const totalPnl: Big = calculateTotalPnl( + currentEquity, + subaccountTotalTransfersMap[subaccountId][USDC_ASSET_ID] +); + +// Log the results +console.log('Calculated equity and total PnL:', { + subaccountId, + currentEquity: currentEquity.toFixed(), + totalPnl: totalPnl.toFixed(), +}); From db7e0a7fdc67de4c00f4d51fecddc8ed314db6b3 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 15:48:41 -0400 Subject: [PATCH 06/15] found root cause --- indexer/services/roundtable/package.json | 1 + .../src/helpers/pnl-ticks-helper.ts | 10 +- .../services/roundtable/src/helpers/script.ts | 489 +++++++++++++++--- 3 files changed, 439 insertions(+), 61 deletions(-) diff --git a/indexer/services/roundtable/package.json b/indexer/services/roundtable/package.json index ad23413ca1..c354af144c 100644 --- a/indexer/services/roundtable/package.json +++ b/indexer/services/roundtable/package.json @@ -7,6 +7,7 @@ "start": "node --heapsnapshot-signal=SIGUSR2 -r dd-trace/init -r dotenv-flow/config build/src/index.js", "build": "rm -rf build/ && tsc", "build:prod": "pnpm run build", + "test-script": "ts-node src/helpers/script.ts", "build:watch": "pnpm run build -- --watch", "coverage": "pnpm test -- --coverage", "lint": "eslint --ext .ts,.js .", diff --git a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts index 40566b97d9..2d62968ddc 100644 --- a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts +++ b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts @@ -324,6 +324,7 @@ export function getNewPnlTick( marketPrices: JSON.stringify(marketPrices), lastUpdatedFundingIndexMap: JSON.stringify(lastUpdatedFundingIndexMap), currentFundingIndexMap: JSON.stringify(currentFundingIndexMap), + subaccountTotalTransfersMap: JSON.stringify(subaccountTotalTransfersMap), }); const currentEquity: Big = calculateEquity( usdcPositionSize, @@ -453,9 +454,12 @@ export function calculateEquity( const signedPositionNotional: Big = positions.reduce( (acc: Big, position: PerpetualPositionFromDatabase) => { - const positionNotional: Big = Big(position.size).times( - marketPrices[Number(position.perpetualId)], - ); + console.log(`position.size: ${position.size}`); + const price: Big = Number(position.perpetualId) in marketPrices + ? Big(marketPrices[Number(position.perpetualId)]) + : Big(0); + console.log(`price: ${marketPrices[Number(position.perpetualId)]}`); + const positionNotional: Big = Big(position.size).times(price); // Add positionNotional to the accumulator return acc.plus(positionNotional); }, diff --git a/indexer/services/roundtable/src/helpers/script.ts b/indexer/services/roundtable/src/helpers/script.ts index fba312010b..d41d479082 100644 --- a/indexer/services/roundtable/src/helpers/script.ts +++ b/indexer/services/roundtable/src/helpers/script.ts @@ -1,10 +1,18 @@ // Sample data from the logs -import {PerpetualPositionFromDatabase, PerpetualPositionStatus, PositionSide} from '@dydxprotocol-indexer/postgres'; +import { + FundingIndexMap, + PerpetualPositionFromDatabase, + PerpetualPositionStatus, + PositionSide, + PriceMap, +} from '@dydxprotocol-indexer/postgres'; import Big from 'big.js'; +import { calculateEquity, calculateTotalPnl } from './pnl-ticks-helper'; + const subaccountId = '070180e5-7da1-58b2-b7ae-15d690160a0c'; -const latestBlockHeight = 19010585; -const latestBlockTime = new Date('2024-06-26T18:56:28.918Z'); +// const latestBlockHeight = 19010585; +// const latestBlockTime = new Date('2024-06-26T18:56:28.918Z'); const usdcPositionSize = new Big('63947.007662'); const openPerpetualPositionsForSubaccount: PerpetualPositionFromDatabase[] = [ @@ -29,60 +37,429 @@ const openPerpetualPositionsForSubaccount: PerpetualPositionFromDatabase[] = [ lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), settledFunding: '0.047358', }, - // ... (other positions) + { + id: '5c7a2e82-6cfc-5115-8a7f-25224d134780', + subaccountId, + perpetualId: '5', + side: PositionSide.SHORT, + status: PerpetualPositionStatus.OPEN, + size: '-37.3', + maxSize: '-15', + entryPrice: '133.98402144772117962466', + exitPrice: undefined, + sumOpen: '37.3', + sumClose: '0', + createdAt: '2024-06-23T12:28:28.384Z', + closedAt: undefined, + createdAtHeight: '18768820', + closedAtHeight: undefined, + openEventId: Buffer.from([1, 30, 99, 180, 0, 0, 0, 2, 0, 0, 0, 3]), + closeEventId: undefined, + lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), + settledFunding: '-0.930262', + }, + { + id: 'b7383524-a615-5bad-b708-9b8e093b973e', + subaccountId, + perpetualId: '7', + side: PositionSide.SHORT, + status: PerpetualPositionStatus.OPEN, + size: '-99.1', + maxSize: '-38.9', + entryPrice: '25.22', + exitPrice: '25.28201816347124117053', + sumOpen: '198.2', + sumClose: '99.1', + createdAt: '2024-06-23T12:29:11.973Z', + closedAt: undefined, + createdAtHeight: '18768858', + closedAtHeight: undefined, + openEventId: Buffer.from([1, 30, 99, 218, 0, 0, 0, 2, 0, 0, 0, 9]), + closeEventId: undefined, + lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), + settledFunding: '0.012883', + }, + { + id: '1da7e871-a6d1-592a-a64e-381ed67db24c', + subaccountId, + perpetualId: '24', + side: PositionSide.SHORT, + status: PerpetualPositionStatus.OPEN, + size: '-6242', + maxSize: '-500', + entryPrice: '0.80028212111502723486', + exitPrice: undefined, + sumOpen: '6242', + sumClose: '0', + createdAt: '2024-06-19T14:51:22.211Z', + closedAt: undefined, + createdAtHeight: '18488508', + closedAtHeight: undefined, + openEventId: Buffer.from([1, 26, 28, 188, 0, 0, 0, 2, 0, 0, 0, 1]), + closeEventId: undefined, + lastEventId: Buffer.from([1, 30, 99, 180, 0, 0, 0, 2, 0, 0, 0, 3]), + settledFunding: '0.006242', + }, + { + id: '75bf2367-bc6e-5147-be11-c00581e6004b', + subaccountId, + perpetualId: '27', + side: PositionSide.SHORT, + status: PerpetualPositionStatus.OPEN, + size: '-2681', + maxSize: '-130', + entryPrice: '1.85676837001118985454', + exitPrice: undefined, + sumOpen: '2681', + sumClose: '0', + createdAt: '2024-06-19T14:50:30.074Z', + closedAt: undefined, + createdAtHeight: '18488468', + closedAtHeight: undefined, + openEventId: Buffer.from([1, 26, 28, 148, 0, 0, 0, 2, 0, 0, 0, 3]), + closeEventId: undefined, + lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), + settledFunding: '0.399469', + }, + { + id: 'e1942859-ba80-5d6f-98d2-cf0122d25341', + subaccountId, + perpetualId: '43', + side: PositionSide.SHORT, + status: PerpetualPositionStatus.OPEN, + size: '-3566', + maxSize: '-3566', + entryPrice: '0.701', + exitPrice: undefined, + sumOpen: '3566', + sumClose: '0', + createdAt: '2024-06-19T14:58:28.562Z', + closedAt: undefined, + createdAtHeight: '18488827', + closedAtHeight: undefined, + openEventId: Buffer.from([1, 26, 29, 251, 0, 0, 0, 2, 0, 0, 0, 4]), + closeEventId: undefined, + lastEventId: Buffer.from([1, 30, 99, 180, 0, 0, 0, 2, 0, 0, 0, 3]), + settledFunding: '-0.203262', + }, + { + id: '88109da6-dd31-5acd-afaa-15b424c03cf8', + subaccountId, + perpetualId: '52', + side: PositionSide.SHORT, + status: PerpetualPositionStatus.OPEN, + size: '-1610', + maxSize: '-1287', + entryPrice: '1.55259875776397515528', + exitPrice: undefined, + sumOpen: '1610', + sumClose: '0', + createdAt: '2024-06-19T14:59:45.514Z', + closedAt: undefined, + createdAtHeight: '18488887', + closedAtHeight: undefined, + openEventId: Buffer.from([1, 26, 30, 55, 0, 0, 0, 2, 0, 0, 0, 0]), + closeEventId: undefined, + lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), + settledFunding: '-0.03381', + }, + { + id: '06c89d02-639b-5664-8f7b-da457a45e886', + subaccountId, + perpetualId: '60', + side: PositionSide.SHORT, + status: PerpetualPositionStatus.OPEN, + size: '-22060', + maxSize: '-22060', + entryPrice: '0.068', + exitPrice: undefined, + sumOpen: '22060', + sumClose: '0', + createdAt: '2024-06-19T14:57:17.579Z', + closedAt: undefined, + createdAtHeight: '18488771', + closedAtHeight: undefined, + openEventId: Buffer.from([1, 26, 29, 195, 0, 0, 0, 2, 0, 0, 0, 0]), + closeEventId: undefined, + lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), + settledFunding: '-0.75004', + }, + { + id: '9edc5040-4f5d-593a-a316-1ea26e4ac9f4', + subaccountId, + perpetualId: '63', + side: PositionSide.SHORT, + status: PerpetualPositionStatus.OPEN, + size: '-1858', + maxSize: '-27', + entryPrice: '1.345', + exitPrice: undefined, + sumOpen: '1858', + sumClose: '0', + createdAt: '2024-06-24T16:57:05.863Z', + closedAt: undefined, + createdAtHeight: '18854975', + closedAtHeight: undefined, + openEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), + closeEventId: undefined, + lastEventId: Buffer.from([1, 31, 180, 66, 0, 0, 0, 2, 0, 0, 0, 4]), + settledFunding: '0', + }, + { + id: '5d019914-cc08-5a56-8da9-f6b02dea439d', + subaccountId, + perpetualId: '64', + side: PositionSide.SHORT, + status: PerpetualPositionStatus.OPEN, + size: '-31570', + maxSize: '-31570', + entryPrice: '0.0792', + exitPrice: undefined, + sumOpen: '31570', + sumClose: '0', + createdAt: '2024-06-19T15:01:04.024Z', + closedAt: undefined, + createdAtHeight: '18488943', + closedAtHeight: undefined, + openEventId: Buffer.from([1, 26, 30, 111, 0, 0, 0, 2, 0, 0, 0, 6]), + closeEventId: undefined, + lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), + settledFunding: '-0.078925', + }, ]; - -const marketPrices = { - '0': '60909.76231', - '1': '3361.77635', - '2': '13.9680125', - '3': '0.552134848', - // ... (other prices) -}; - -const lastUpdatedFundingIndexMap = { - '0': '10061.34', - '1': '543.858', - '2': '2.985764', - '3': '0.1285959', - // ... (other indices) +const marketPrices: PriceMap = { + 0: '60909.76231', + 1: '3361.77635', + 2: '13.9680125', + 3: '0.552134848', + 4: '0.29958214', + 5: '135.82356527', + 6: '0.3838904009', + 7: '26.025678', + 8: '4.380006792', + 9: '70.966605', + 10: '0.12259020958', + 11: '6.790035274', + 12: '5.811713508', + 13: '9.228308676', + 14: '376.6165974', + 15: '0.12295570059', + 16: '5.188984481', + 17: '2344.259947', + 18: '0.0904728581', + 19: '23.58820589', + 20: '48.66063962', + 21: '2.75245776', + 22: '0.953859256', + 23: '6.759246301', + 24: '0.805277888', + 25: '0.2094170931', + 26: '2.346921497', + 27: '1.733306677', + 28: '0.0000124738113795', + 29: '0.353442416', + 30: '0.000017181405', + 31: '0.8274', + 32: '0.4678471199', + 33: '6.35593868', + 35: '0.7761083583', + 36: '92.91933226', + 37: '570.5617752', + 38: '2.336065573', + 39: '37.08116753', + 40: '0.5697963733', + 41: '8.13525606', + 42: '1.514545636', + 43: '0.689332761', + 44: '1.7490698853', + 45: '0.2162842939', + 46: '0.3131747301', + 47: '0.00002224525837', + 48: '0.7204272756', + 49: '7.4964', + 50: '1.698820471', + 51: '23.11401524', + 52: '1.521372268', + 53: '0.0749255184', + 54: '0.1394521111', + 55: '0.2168133064', + 56: '0.3350659736', + 57: '4.080367852', + 58: '0.5729', + 59: '0.491005049', + 60: '0.0670109795', + 61: '1.96948784', + 62: '1.980929719', + 64: '0.0766789439', + 65: '1.976194857', + 66: '3.169575537', + 67: '7.499050379', + 68: '0.3345798701', + 69: '0.0766393442', + 70: '0.0000014237804878', + 72: '0.8466670082', + 73: '0.1547834777', + 1000000: '0.999083812', + 1000001: '1.353323338', }; -const currentFundingIndexMap = { - '0': '10055.39', - '1': '543.467', - '2': '2.982482', - '3': '0.1286097', - // ... (other indices) +const lastUpdatedFundingIndexMap: FundingIndexMap = { + 0: Big('10061.34'), + 1: Big('543.858'), + 2: Big('2.985764'), + 3: Big('0.1285959'), + 4: Big('0.0242804'), + 5: Big('28.99982'), + 6: Big('0.0708093'), + 7: Big('6.69042'), + 8: Big('1.2721'), + 9: Big('4.13646'), + 10: Big('0.02794695'), + 11: Big('1.217797'), + 12: Big('0.689199'), + 13: Big('0.441051'), + 14: Big('1.2766'), + 15: Big('0.00377638'), + 16: Big('0.78862'), + 17: Big('165.689'), + 18: Big('0.0012887'), + 19: Big('1.06049'), + 20: Big('1.69429'), + 21: Big('0.490159'), + 22: Big('0.094816'), + 23: Big('0.343845'), + 24: Big('0.124828'), + 25: Big('0.0270862'), + 26: Big('0.175539'), + 27: Big('0.375521'), + 28: Big('0.0000009835177'), + 29: Big('0.0876866'), + 30: Big('0.000000319023'), + 31: Big('0.1205733'), + 32: Big('0.0524673'), + 33: Big('0.77201'), + 35: Big('0.0219666'), + 36: Big('1.77613'), + 37: Big('-7.2966'), + 38: Big('0.028685'), + 39: Big('1.37204'), + 40: Big('0.0042681'), + 41: Big('0.0733'), + 42: Big('0.058138'), + 43: Big('0.057493'), + 44: Big('0.1512811'), + 45: Big('-0.0045601'), + 46: Big('0.0012934'), + 47: Big('0.000000393'), + 48: Big('0.0321677'), + 49: Big('0.634502'), + 50: Big('0.109547'), + 51: Big('2.15939'), + 52: Big('0.22969'), + 53: Big('0.0067454'), + 54: Big('0.0061943'), + 55: Big('-0.0004333'), + 56: Big('0.002139'), + 57: Big('0.111904'), + 58: Big('0.306223'), + 59: Big('0.110517'), + 60: Big('0.0139074'), + 61: Big('0.116049'), + 62: Big('0.119478'), + 63: Big('0.016529'), + 64: Big('-0.0002865'), + 65: Big('0.04877'), + 66: Big('-0.008642'), + 67: Big('0.166207'), + 68: Big('-0.0003235'), + 69: Big('0.0000029'), + 70: Big('0'), + 72: Big('0'), + 73: Big('0'), }; -const subaccountTotalTransfersMap: { [key: string]: { [key: string]: Big } } = { - '070180e5-7da1-58b2-b7ae-15d690160a0c': { - 'USDC': new Big('63947.007662'), - }, +const currentFundingIndexMap: FundingIndexMap = { + 0: Big('10055.39'), + 1: Big('543.467'), + 2: Big('2.982482'), + 3: Big('0.1286097'), + 4: Big('0.0242792'), + 5: Big('28.90172'), + 6: Big('0.0708094'), + 7: Big('6.69019'), + 8: Big('1.272176'), + 9: Big('4.13605'), + 10: Big('0.02794737'), + 11: Big('1.217986'), + 12: Big('0.689189'), + 13: Big('0.44105'), + 14: Big('1.1969'), + 15: Big('0.00377973'), + 16: Big('0.789157'), + 17: Big('165.527'), + 18: Big('0.0012886'), + 19: Big('1.0604'), + 20: Big('1.67323'), + 21: Big('0.490159'), + 22: Big('0.094765'), + 23: Big('0.343823'), + 24: Big('0.12483'), + 25: Big('0.0262726'), + 26: Big('0.175646'), + 27: Big('0.375548'), + 28: Big('0.000000985444'), + 29: Big('0.0876434'), + 30: Big('0.000000319019'), + 31: Big('0.1205725'), + 32: Big('0.0524705'), + 33: Big('0.77198'), + 35: Big('0.0219655'), + 36: Big('1.78032'), + 37: Big('-7.3177'), + 38: Big('0.0286'), + 39: Big('1.37204'), + 40: Big('0.0042682'), + 41: Big('0.07325'), + 42: Big('0.058041'), + 43: Big('0.057493'), + 44: Big('0.1512811'), + 45: Big('-0.0044936'), + 46: Big('0.0012932'), + 47: Big('0.00000039291'), + 48: Big('0.0321677'), + 49: Big('0.635787'), + 50: Big('0.109865'), + 51: Big('2.15939'), + 52: Big('0.229677'), + 53: Big('0.0067454'), + 54: Big('0.0062494'), + 55: Big('-0.0004809'), + 56: Big('0.0020542'), + 57: Big('0.113332'), + 58: Big('0.306306'), + 59: Big('0.110517'), + 60: Big('0.0139032'), + 61: Big('0.118044'), + 62: Big('0.119483'), + 63: Big('0.017067'), + 64: Big('-0.0002876'), + 65: Big('0.048771'), + 66: Big('-0.009382'), + 67: Big('0.170857'), + 68: Big('-0.0003235'), + 69: Big('0.0000157'), + 70: Big('0'), + 72: Big('0'), + 73: Big('0'), }; -const USDC_ASSET_ID = 'USDC'; - -// Dummy implementations of calculateEquity and calculateTotalPnl -function calculateEquity( - usdcPositionSize: Big, - openPerpetualPositionsForSubaccount: PerpetualPosition[], - marketPrices: { [key: string]: string }, - lastUpdatedFundingIndexMap: { [key: string]: string }, - currentFundingIndexMap: { [key: string]: string } -): Big { - // Implement your equity calculation logic here - return new Big(0); -} - -function calculateTotalPnl( - currentEquity: Big, - subaccountTotalTransfers: Big -): Big { - // Implement your PnL calculation logic here - return new Big(0); -} +// const subaccountTotalTransfersMap: { [key: string]: { [key: string]: Big } } = { +// '070180e5-7da1-58b2-b7ae-15d690160a0c': { +// USDC: new Big('63947.007662'), +// }, +// }; +// +// const USDC_ASSET_ID = 'USDC'; // Call the functions with the provided data const currentEquity: Big = calculateEquity( @@ -90,17 +467,13 @@ const currentEquity: Big = calculateEquity( openPerpetualPositionsForSubaccount, marketPrices, lastUpdatedFundingIndexMap, - currentFundingIndexMap + currentFundingIndexMap, ); +console.log('Calculated equity:', currentEquity.toFixed()); const totalPnl: Big = calculateTotalPnl( currentEquity, - subaccountTotalTransfersMap[subaccountId][USDC_ASSET_ID] + '100000', ); -// Log the results -console.log('Calculated equity and total PnL:', { - subaccountId, - currentEquity: currentEquity.toFixed(), - totalPnl: totalPnl.toFixed(), -}); +console.log('Calculated total PnL:', totalPnl.toFixed()); From 18f06e15653f798138d360979e7b636684d59f32 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 15:49:07 -0400 Subject: [PATCH 07/15] update wf --- .github/workflows/indexer-build-and-push-dev-staging.yml | 1 - .github/workflows/indexer-build-and-push-mainnet.yml | 1 - 2 files changed, 2 deletions(-) diff --git a/.github/workflows/indexer-build-and-push-dev-staging.yml b/.github/workflows/indexer-build-and-push-dev-staging.yml index 649e88ca30..5a98b72552 100644 --- a/.github/workflows/indexer-build-and-push-dev-staging.yml +++ b/.github/workflows/indexer-build-and-push-dev-staging.yml @@ -3,7 +3,6 @@ name: Indexer Build & Push Images to AWS ECR for Dev / Staging branches on: # yamllint disable-line rule:truthy push: branches: - - 'wl/pnldbg' - main - 'release/indexer/v[0-9]+.[0-9]+.x' # e.g. release/indexer/v0.1.x - 'release/indexer/v[0-9]+.x' # e.g. release/indexer/v1.x diff --git a/.github/workflows/indexer-build-and-push-mainnet.yml b/.github/workflows/indexer-build-and-push-mainnet.yml index d94a0456f2..78d54d2f68 100644 --- a/.github/workflows/indexer-build-and-push-mainnet.yml +++ b/.github/workflows/indexer-build-and-push-mainnet.yml @@ -3,7 +3,6 @@ name: Indexer Build & Push Images to AWS ECR for Mainnet on: # yamllint disable-line rule:truthy push: branches: - - 'wl/pnldbg' - main - 'release/indexer/v[0-9]+.[0-9]+.x' # e.g. release/indexer/v0.1.x - 'release/indexer/v[0-9]+.x' # e.g. release/indexer/v1.x From 012bd529353295626b95a4ae0c77d8f45f374594 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 15:50:46 -0400 Subject: [PATCH 08/15] rm unneeded files --- indexer/services/roundtable/package.json | 1 - .../services/roundtable/src/helpers/script.ts | 479 ------------------ 2 files changed, 480 deletions(-) delete mode 100644 indexer/services/roundtable/src/helpers/script.ts diff --git a/indexer/services/roundtable/package.json b/indexer/services/roundtable/package.json index c354af144c..ad23413ca1 100644 --- a/indexer/services/roundtable/package.json +++ b/indexer/services/roundtable/package.json @@ -7,7 +7,6 @@ "start": "node --heapsnapshot-signal=SIGUSR2 -r dd-trace/init -r dotenv-flow/config build/src/index.js", "build": "rm -rf build/ && tsc", "build:prod": "pnpm run build", - "test-script": "ts-node src/helpers/script.ts", "build:watch": "pnpm run build -- --watch", "coverage": "pnpm test -- --coverage", "lint": "eslint --ext .ts,.js .", diff --git a/indexer/services/roundtable/src/helpers/script.ts b/indexer/services/roundtable/src/helpers/script.ts deleted file mode 100644 index d41d479082..0000000000 --- a/indexer/services/roundtable/src/helpers/script.ts +++ /dev/null @@ -1,479 +0,0 @@ -// Sample data from the logs -import { - FundingIndexMap, - PerpetualPositionFromDatabase, - PerpetualPositionStatus, - PositionSide, - PriceMap, -} from '@dydxprotocol-indexer/postgres'; -import Big from 'big.js'; - -import { calculateEquity, calculateTotalPnl } from './pnl-ticks-helper'; - -const subaccountId = '070180e5-7da1-58b2-b7ae-15d690160a0c'; -// const latestBlockHeight = 19010585; -// const latestBlockTime = new Date('2024-06-26T18:56:28.918Z'); -const usdcPositionSize = new Big('63947.007662'); - -const openPerpetualPositionsForSubaccount: PerpetualPositionFromDatabase[] = [ - { - id: '623d0608-240b-5cd0-a5b3-6dd1d5f22c5c', - subaccountId, - perpetualId: '3', - side: PositionSide.SHORT, - status: PerpetualPositionStatus.OPEN, - size: '-8770', - maxSize: '-3500', - entryPrice: '0.57064218928164196123', - exitPrice: undefined, - sumOpen: '8770', - sumClose: '0', - createdAt: '2024-06-19T14:55:32.510Z', - closedAt: undefined, - createdAtHeight: '18488696', - closedAtHeight: undefined, - openEventId: Buffer.from([1, 26, 29, 120, 0, 0, 0, 2, 0, 0, 0, 0]), - closeEventId: undefined, - lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), - settledFunding: '0.047358', - }, - { - id: '5c7a2e82-6cfc-5115-8a7f-25224d134780', - subaccountId, - perpetualId: '5', - side: PositionSide.SHORT, - status: PerpetualPositionStatus.OPEN, - size: '-37.3', - maxSize: '-15', - entryPrice: '133.98402144772117962466', - exitPrice: undefined, - sumOpen: '37.3', - sumClose: '0', - createdAt: '2024-06-23T12:28:28.384Z', - closedAt: undefined, - createdAtHeight: '18768820', - closedAtHeight: undefined, - openEventId: Buffer.from([1, 30, 99, 180, 0, 0, 0, 2, 0, 0, 0, 3]), - closeEventId: undefined, - lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), - settledFunding: '-0.930262', - }, - { - id: 'b7383524-a615-5bad-b708-9b8e093b973e', - subaccountId, - perpetualId: '7', - side: PositionSide.SHORT, - status: PerpetualPositionStatus.OPEN, - size: '-99.1', - maxSize: '-38.9', - entryPrice: '25.22', - exitPrice: '25.28201816347124117053', - sumOpen: '198.2', - sumClose: '99.1', - createdAt: '2024-06-23T12:29:11.973Z', - closedAt: undefined, - createdAtHeight: '18768858', - closedAtHeight: undefined, - openEventId: Buffer.from([1, 30, 99, 218, 0, 0, 0, 2, 0, 0, 0, 9]), - closeEventId: undefined, - lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), - settledFunding: '0.012883', - }, - { - id: '1da7e871-a6d1-592a-a64e-381ed67db24c', - subaccountId, - perpetualId: '24', - side: PositionSide.SHORT, - status: PerpetualPositionStatus.OPEN, - size: '-6242', - maxSize: '-500', - entryPrice: '0.80028212111502723486', - exitPrice: undefined, - sumOpen: '6242', - sumClose: '0', - createdAt: '2024-06-19T14:51:22.211Z', - closedAt: undefined, - createdAtHeight: '18488508', - closedAtHeight: undefined, - openEventId: Buffer.from([1, 26, 28, 188, 0, 0, 0, 2, 0, 0, 0, 1]), - closeEventId: undefined, - lastEventId: Buffer.from([1, 30, 99, 180, 0, 0, 0, 2, 0, 0, 0, 3]), - settledFunding: '0.006242', - }, - { - id: '75bf2367-bc6e-5147-be11-c00581e6004b', - subaccountId, - perpetualId: '27', - side: PositionSide.SHORT, - status: PerpetualPositionStatus.OPEN, - size: '-2681', - maxSize: '-130', - entryPrice: '1.85676837001118985454', - exitPrice: undefined, - sumOpen: '2681', - sumClose: '0', - createdAt: '2024-06-19T14:50:30.074Z', - closedAt: undefined, - createdAtHeight: '18488468', - closedAtHeight: undefined, - openEventId: Buffer.from([1, 26, 28, 148, 0, 0, 0, 2, 0, 0, 0, 3]), - closeEventId: undefined, - lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), - settledFunding: '0.399469', - }, - { - id: 'e1942859-ba80-5d6f-98d2-cf0122d25341', - subaccountId, - perpetualId: '43', - side: PositionSide.SHORT, - status: PerpetualPositionStatus.OPEN, - size: '-3566', - maxSize: '-3566', - entryPrice: '0.701', - exitPrice: undefined, - sumOpen: '3566', - sumClose: '0', - createdAt: '2024-06-19T14:58:28.562Z', - closedAt: undefined, - createdAtHeight: '18488827', - closedAtHeight: undefined, - openEventId: Buffer.from([1, 26, 29, 251, 0, 0, 0, 2, 0, 0, 0, 4]), - closeEventId: undefined, - lastEventId: Buffer.from([1, 30, 99, 180, 0, 0, 0, 2, 0, 0, 0, 3]), - settledFunding: '-0.203262', - }, - { - id: '88109da6-dd31-5acd-afaa-15b424c03cf8', - subaccountId, - perpetualId: '52', - side: PositionSide.SHORT, - status: PerpetualPositionStatus.OPEN, - size: '-1610', - maxSize: '-1287', - entryPrice: '1.55259875776397515528', - exitPrice: undefined, - sumOpen: '1610', - sumClose: '0', - createdAt: '2024-06-19T14:59:45.514Z', - closedAt: undefined, - createdAtHeight: '18488887', - closedAtHeight: undefined, - openEventId: Buffer.from([1, 26, 30, 55, 0, 0, 0, 2, 0, 0, 0, 0]), - closeEventId: undefined, - lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), - settledFunding: '-0.03381', - }, - { - id: '06c89d02-639b-5664-8f7b-da457a45e886', - subaccountId, - perpetualId: '60', - side: PositionSide.SHORT, - status: PerpetualPositionStatus.OPEN, - size: '-22060', - maxSize: '-22060', - entryPrice: '0.068', - exitPrice: undefined, - sumOpen: '22060', - sumClose: '0', - createdAt: '2024-06-19T14:57:17.579Z', - closedAt: undefined, - createdAtHeight: '18488771', - closedAtHeight: undefined, - openEventId: Buffer.from([1, 26, 29, 195, 0, 0, 0, 2, 0, 0, 0, 0]), - closeEventId: undefined, - lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), - settledFunding: '-0.75004', - }, - { - id: '9edc5040-4f5d-593a-a316-1ea26e4ac9f4', - subaccountId, - perpetualId: '63', - side: PositionSide.SHORT, - status: PerpetualPositionStatus.OPEN, - size: '-1858', - maxSize: '-27', - entryPrice: '1.345', - exitPrice: undefined, - sumOpen: '1858', - sumClose: '0', - createdAt: '2024-06-24T16:57:05.863Z', - closedAt: undefined, - createdAtHeight: '18854975', - closedAtHeight: undefined, - openEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), - closeEventId: undefined, - lastEventId: Buffer.from([1, 31, 180, 66, 0, 0, 0, 2, 0, 0, 0, 4]), - settledFunding: '0', - }, - { - id: '5d019914-cc08-5a56-8da9-f6b02dea439d', - subaccountId, - perpetualId: '64', - side: PositionSide.SHORT, - status: PerpetualPositionStatus.OPEN, - size: '-31570', - maxSize: '-31570', - entryPrice: '0.0792', - exitPrice: undefined, - sumOpen: '31570', - sumClose: '0', - createdAt: '2024-06-19T15:01:04.024Z', - closedAt: undefined, - createdAtHeight: '18488943', - closedAtHeight: undefined, - openEventId: Buffer.from([1, 26, 30, 111, 0, 0, 0, 2, 0, 0, 0, 6]), - closeEventId: undefined, - lastEventId: Buffer.from([1, 31, 180, 63, 0, 0, 0, 2, 0, 0, 0, 31]), - settledFunding: '-0.078925', - }, -]; -const marketPrices: PriceMap = { - 0: '60909.76231', - 1: '3361.77635', - 2: '13.9680125', - 3: '0.552134848', - 4: '0.29958214', - 5: '135.82356527', - 6: '0.3838904009', - 7: '26.025678', - 8: '4.380006792', - 9: '70.966605', - 10: '0.12259020958', - 11: '6.790035274', - 12: '5.811713508', - 13: '9.228308676', - 14: '376.6165974', - 15: '0.12295570059', - 16: '5.188984481', - 17: '2344.259947', - 18: '0.0904728581', - 19: '23.58820589', - 20: '48.66063962', - 21: '2.75245776', - 22: '0.953859256', - 23: '6.759246301', - 24: '0.805277888', - 25: '0.2094170931', - 26: '2.346921497', - 27: '1.733306677', - 28: '0.0000124738113795', - 29: '0.353442416', - 30: '0.000017181405', - 31: '0.8274', - 32: '0.4678471199', - 33: '6.35593868', - 35: '0.7761083583', - 36: '92.91933226', - 37: '570.5617752', - 38: '2.336065573', - 39: '37.08116753', - 40: '0.5697963733', - 41: '8.13525606', - 42: '1.514545636', - 43: '0.689332761', - 44: '1.7490698853', - 45: '0.2162842939', - 46: '0.3131747301', - 47: '0.00002224525837', - 48: '0.7204272756', - 49: '7.4964', - 50: '1.698820471', - 51: '23.11401524', - 52: '1.521372268', - 53: '0.0749255184', - 54: '0.1394521111', - 55: '0.2168133064', - 56: '0.3350659736', - 57: '4.080367852', - 58: '0.5729', - 59: '0.491005049', - 60: '0.0670109795', - 61: '1.96948784', - 62: '1.980929719', - 64: '0.0766789439', - 65: '1.976194857', - 66: '3.169575537', - 67: '7.499050379', - 68: '0.3345798701', - 69: '0.0766393442', - 70: '0.0000014237804878', - 72: '0.8466670082', - 73: '0.1547834777', - 1000000: '0.999083812', - 1000001: '1.353323338', -}; - -const lastUpdatedFundingIndexMap: FundingIndexMap = { - 0: Big('10061.34'), - 1: Big('543.858'), - 2: Big('2.985764'), - 3: Big('0.1285959'), - 4: Big('0.0242804'), - 5: Big('28.99982'), - 6: Big('0.0708093'), - 7: Big('6.69042'), - 8: Big('1.2721'), - 9: Big('4.13646'), - 10: Big('0.02794695'), - 11: Big('1.217797'), - 12: Big('0.689199'), - 13: Big('0.441051'), - 14: Big('1.2766'), - 15: Big('0.00377638'), - 16: Big('0.78862'), - 17: Big('165.689'), - 18: Big('0.0012887'), - 19: Big('1.06049'), - 20: Big('1.69429'), - 21: Big('0.490159'), - 22: Big('0.094816'), - 23: Big('0.343845'), - 24: Big('0.124828'), - 25: Big('0.0270862'), - 26: Big('0.175539'), - 27: Big('0.375521'), - 28: Big('0.0000009835177'), - 29: Big('0.0876866'), - 30: Big('0.000000319023'), - 31: Big('0.1205733'), - 32: Big('0.0524673'), - 33: Big('0.77201'), - 35: Big('0.0219666'), - 36: Big('1.77613'), - 37: Big('-7.2966'), - 38: Big('0.028685'), - 39: Big('1.37204'), - 40: Big('0.0042681'), - 41: Big('0.0733'), - 42: Big('0.058138'), - 43: Big('0.057493'), - 44: Big('0.1512811'), - 45: Big('-0.0045601'), - 46: Big('0.0012934'), - 47: Big('0.000000393'), - 48: Big('0.0321677'), - 49: Big('0.634502'), - 50: Big('0.109547'), - 51: Big('2.15939'), - 52: Big('0.22969'), - 53: Big('0.0067454'), - 54: Big('0.0061943'), - 55: Big('-0.0004333'), - 56: Big('0.002139'), - 57: Big('0.111904'), - 58: Big('0.306223'), - 59: Big('0.110517'), - 60: Big('0.0139074'), - 61: Big('0.116049'), - 62: Big('0.119478'), - 63: Big('0.016529'), - 64: Big('-0.0002865'), - 65: Big('0.04877'), - 66: Big('-0.008642'), - 67: Big('0.166207'), - 68: Big('-0.0003235'), - 69: Big('0.0000029'), - 70: Big('0'), - 72: Big('0'), - 73: Big('0'), -}; - -const currentFundingIndexMap: FundingIndexMap = { - 0: Big('10055.39'), - 1: Big('543.467'), - 2: Big('2.982482'), - 3: Big('0.1286097'), - 4: Big('0.0242792'), - 5: Big('28.90172'), - 6: Big('0.0708094'), - 7: Big('6.69019'), - 8: Big('1.272176'), - 9: Big('4.13605'), - 10: Big('0.02794737'), - 11: Big('1.217986'), - 12: Big('0.689189'), - 13: Big('0.44105'), - 14: Big('1.1969'), - 15: Big('0.00377973'), - 16: Big('0.789157'), - 17: Big('165.527'), - 18: Big('0.0012886'), - 19: Big('1.0604'), - 20: Big('1.67323'), - 21: Big('0.490159'), - 22: Big('0.094765'), - 23: Big('0.343823'), - 24: Big('0.12483'), - 25: Big('0.0262726'), - 26: Big('0.175646'), - 27: Big('0.375548'), - 28: Big('0.000000985444'), - 29: Big('0.0876434'), - 30: Big('0.000000319019'), - 31: Big('0.1205725'), - 32: Big('0.0524705'), - 33: Big('0.77198'), - 35: Big('0.0219655'), - 36: Big('1.78032'), - 37: Big('-7.3177'), - 38: Big('0.0286'), - 39: Big('1.37204'), - 40: Big('0.0042682'), - 41: Big('0.07325'), - 42: Big('0.058041'), - 43: Big('0.057493'), - 44: Big('0.1512811'), - 45: Big('-0.0044936'), - 46: Big('0.0012932'), - 47: Big('0.00000039291'), - 48: Big('0.0321677'), - 49: Big('0.635787'), - 50: Big('0.109865'), - 51: Big('2.15939'), - 52: Big('0.229677'), - 53: Big('0.0067454'), - 54: Big('0.0062494'), - 55: Big('-0.0004809'), - 56: Big('0.0020542'), - 57: Big('0.113332'), - 58: Big('0.306306'), - 59: Big('0.110517'), - 60: Big('0.0139032'), - 61: Big('0.118044'), - 62: Big('0.119483'), - 63: Big('0.017067'), - 64: Big('-0.0002876'), - 65: Big('0.048771'), - 66: Big('-0.009382'), - 67: Big('0.170857'), - 68: Big('-0.0003235'), - 69: Big('0.0000157'), - 70: Big('0'), - 72: Big('0'), - 73: Big('0'), -}; - -// const subaccountTotalTransfersMap: { [key: string]: { [key: string]: Big } } = { -// '070180e5-7da1-58b2-b7ae-15d690160a0c': { -// USDC: new Big('63947.007662'), -// }, -// }; -// -// const USDC_ASSET_ID = 'USDC'; - -// Call the functions with the provided data -const currentEquity: Big = calculateEquity( - usdcPositionSize, - openPerpetualPositionsForSubaccount, - marketPrices, - lastUpdatedFundingIndexMap, - currentFundingIndexMap, -); -console.log('Calculated equity:', currentEquity.toFixed()); - -const totalPnl: Big = calculateTotalPnl( - currentEquity, - '100000', -); - -console.log('Calculated total PnL:', totalPnl.toFixed()); From 03f2f709202aa97acaa711286bc2637079b8c235 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 15:58:22 -0400 Subject: [PATCH 09/15] fix --- .../indexer-build-and-push-mainnet.yml | 1 + .../helpers/pnl-ticks-helper.test.ts | 32 +++++++++++++++++++ .../src/helpers/pnl-ticks-helper.ts | 2 -- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/.github/workflows/indexer-build-and-push-mainnet.yml b/.github/workflows/indexer-build-and-push-mainnet.yml index 78d54d2f68..21c038c84a 100644 --- a/.github/workflows/indexer-build-and-push-mainnet.yml +++ b/.github/workflows/indexer-build-and-push-mainnet.yml @@ -3,6 +3,7 @@ name: Indexer Build & Push Images to AWS ECR for Mainnet on: # yamllint disable-line rule:truthy push: branches: + - 'wl/pnldbg2' - main - 'release/indexer/v[0-9]+.[0-9]+.x' # e.g. release/indexer/v0.1.x - 'release/indexer/v[0-9]+.x' # e.g. release/indexer/v1.x diff --git a/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts b/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts index ef7487db4c..1e59d99f4a 100644 --- a/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts +++ b/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts @@ -348,6 +348,38 @@ describe('pnl-ticks-helper', () => { expect(equity).toEqual(new Big('190530')); }); + it('calculateEquity with missing price', () => { + const positions2: PerpetualPositionFromDatabase[] = [ + ...positions, + { + ...testConstants.defaultPerpetualPosition, + side: PositionSide.SHORT, + perpetualId: testConstants.defaultPerpetualMarket2.id, + entryPrice: '20000', + sumOpen: '10', + size: '-10', + sumClose: '0', + openEventId: testConstants.defaultTendermintEventId2, + id: PerpetualPositionTable.uuid( + testConstants.defaultPerpetualPosition.subaccountId, + testConstants.defaultTendermintEventId2, + ), + }, + ]; + const marketPricesMissingPrice: PriceMap = { + [testConstants.defaultPerpetualMarket.id]: '20000', + }; + const usdcPosition: Big = new Big('10000'); + const equity: Big = calculateEquity( + usdcPosition, + positions2, + marketPricesMissingPrice, + lastUpdatedFundingIndexMap, + currentFundingIndexMap, + ); + expect(equity).toEqual(new Big('200530')); + }); + it('calculateTotalPnl', () => { const equity: Big = new Big('200100'); const transfers: string = '-20.5'; diff --git a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts index 2d62968ddc..f2245025f0 100644 --- a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts +++ b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts @@ -454,11 +454,9 @@ export function calculateEquity( const signedPositionNotional: Big = positions.reduce( (acc: Big, position: PerpetualPositionFromDatabase) => { - console.log(`position.size: ${position.size}`); const price: Big = Number(position.perpetualId) in marketPrices ? Big(marketPrices[Number(position.perpetualId)]) : Big(0); - console.log(`price: ${marketPrices[Number(position.perpetualId)]}`); const positionNotional: Big = Big(position.size).times(price); // Add positionNotional to the accumulator return acc.plus(positionNotional); From 4e2461988c0d949ed1f8e115c3b2bb89b988fedb Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 16:39:09 -0400 Subject: [PATCH 10/15] update gh wf --- .github/workflows/indexer-build-and-push-mainnet.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/indexer-build-and-push-mainnet.yml b/.github/workflows/indexer-build-and-push-mainnet.yml index 21c038c84a..78d54d2f68 100644 --- a/.github/workflows/indexer-build-and-push-mainnet.yml +++ b/.github/workflows/indexer-build-and-push-mainnet.yml @@ -3,7 +3,6 @@ name: Indexer Build & Push Images to AWS ECR for Mainnet on: # yamllint disable-line rule:truthy push: branches: - - 'wl/pnldbg2' - main - 'release/indexer/v[0-9]+.[0-9]+.x' # e.g. release/indexer/v0.1.x - 'release/indexer/v[0-9]+.x' # e.g. release/indexer/v1.x From 268c89feee9862936a8b8f3bd01e43d2f7dbdd91 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 17:15:17 -0400 Subject: [PATCH 11/15] fix --- .../helpers/pnl-ticks-helper.test.ts | 42 +++++++++++++++++-- .../src/helpers/pnl-ticks-helper.ts | 8 ++-- .../roundtable/src/tasks/create-pnl-ticks.ts | 2 + 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts b/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts index 1e59d99f4a..c60cca50bc 100644 --- a/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts +++ b/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts @@ -17,6 +17,11 @@ import { Transaction, TransferTable, PositionSide, + PerpetualMarketStatus, + PerpetualMarketType, + MarketTable, + PerpetualMarketTable, + perpetualMarketRefresher, } from '@dydxprotocol-indexer/postgres'; import { calculateEquity, @@ -52,10 +57,12 @@ describe('pnl-ticks-helper', () => { const lastUpdatedFundingIndexMap: FundingIndexMap = { [testConstants.defaultPerpetualMarket.id]: Big('10050'), [testConstants.defaultPerpetualMarket2.id]: Big('5'), + 5: Big('5'), }; const currentFundingIndexMap: FundingIndexMap = { [testConstants.defaultPerpetualMarket.id]: Big('11000'), [testConstants.defaultPerpetualMarket2.id]: Big('8'), + 5: Big('8'), }; const marketPrices: PriceMap = { [testConstants.defaultPerpetualMarket.id]: '20000', @@ -73,6 +80,7 @@ describe('pnl-ticks-helper', () => { beforeEach(async () => { await testMocks.seedData(); + await perpetualMarketRefresher.updatePerpetualMarkets(); }); afterAll(async () => { @@ -348,13 +356,40 @@ describe('pnl-ticks-helper', () => { expect(equity).toEqual(new Big('190530')); }); - it('calculateEquity with missing price', () => { + it('calculateEquity with perpetualId/marketId mismatch', async () => { + await MarketTable.create({ + id: 1000, + pair: 'TEST-USD', + exponent: -12, + minPriceChangePpm: 50, + oraclePrice: '1.00', + }); + await PerpetualMarketTable.create({ + id: '5', + clobPairId: '5', + ticker: 'TEST-USD', + marketId: 1000, + status: PerpetualMarketStatus.ACTIVE, + priceChange24H: '0.000000001', + volume24H: '10000000', + trades24H: 200, + nextFundingRate: '1.2', + openInterest: '40000', + quantumConversionExponent: -16, + atomicResolution: -2, + subticksPerTick: 10, + stepBaseQuantums: 1, + liquidityTierId: 0, + marketType: PerpetualMarketType.ISOLATED, + baseOpenInterest: '100000', + }); + await perpetualMarketRefresher.updatePerpetualMarkets(); const positions2: PerpetualPositionFromDatabase[] = [ ...positions, { ...testConstants.defaultPerpetualPosition, side: PositionSide.SHORT, - perpetualId: testConstants.defaultPerpetualMarket2.id, + perpetualId: '5', entryPrice: '20000', sumOpen: '10', size: '-10', @@ -368,6 +403,7 @@ describe('pnl-ticks-helper', () => { ]; const marketPricesMissingPrice: PriceMap = { [testConstants.defaultPerpetualMarket.id]: '20000', + 1000: '1000', }; const usdcPosition: Big = new Big('10000'); const equity: Big = calculateEquity( @@ -377,7 +413,7 @@ describe('pnl-ticks-helper', () => { lastUpdatedFundingIndexMap, currentFundingIndexMap, ); - expect(equity).toEqual(new Big('200530')); + expect(equity).toEqual(new Big('190530')); }); it('calculateTotalPnl', () => { diff --git a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts index f2245025f0..4aa5b4978a 100644 --- a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts +++ b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts @@ -6,6 +6,7 @@ import { helpers, IsoString, OraclePriceTable, + perpetualMarketRefresher, PerpetualPositionFromDatabase, PerpetualPositionTable, PnlTicksCreateObject, @@ -454,10 +455,9 @@ export function calculateEquity( const signedPositionNotional: Big = positions.reduce( (acc: Big, position: PerpetualPositionFromDatabase) => { - const price: Big = Number(position.perpetualId) in marketPrices - ? Big(marketPrices[Number(position.perpetualId)]) - : Big(0); - const positionNotional: Big = Big(position.size).times(price); + const marketId: + number = perpetualMarketRefresher.getPerpetualMarketFromId(position.perpetualId)!.marketId; + const positionNotional: Big = Big(position.size).times(Big(marketPrices[marketId])); // Add positionNotional to the accumulator return acc.plus(positionNotional); }, diff --git a/indexer/services/roundtable/src/tasks/create-pnl-ticks.ts b/indexer/services/roundtable/src/tasks/create-pnl-ticks.ts index ab0e10baaf..36d7f40698 100644 --- a/indexer/services/roundtable/src/tasks/create-pnl-ticks.ts +++ b/indexer/services/roundtable/src/tasks/create-pnl-ticks.ts @@ -5,6 +5,7 @@ import { PnlTicksCreateObject, PnlTicksTable, Transaction, + perpetualMarketRefresher, } from '@dydxprotocol-indexer/postgres'; import { LatestAccountPnlTicksCache } from '@dydxprotocol-indexer/redis'; import _ from 'lodash'; @@ -53,6 +54,7 @@ export default async function runTask(): Promise { const txId: number = await Transaction.start(); let newTicksToCreate: PnlTicksCreateObject[] = []; try { + await perpetualMarketRefresher.updatePerpetualMarkets(); newTicksToCreate = await getPnlTicksCreateObjects(latestBlockHeight, latestBlockTime, txId); } catch (error) { logger.error({ From 8b51353b0215e3dc8bcd208fdd0df9e8ccb4f169 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 17:16:08 -0400 Subject: [PATCH 12/15] test --- .github/workflows/indexer-build-and-push-mainnet.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/indexer-build-and-push-mainnet.yml b/.github/workflows/indexer-build-and-push-mainnet.yml index 78d54d2f68..21c038c84a 100644 --- a/.github/workflows/indexer-build-and-push-mainnet.yml +++ b/.github/workflows/indexer-build-and-push-mainnet.yml @@ -3,6 +3,7 @@ name: Indexer Build & Push Images to AWS ECR for Mainnet on: # yamllint disable-line rule:truthy push: branches: + - 'wl/pnldbg2' - main - 'release/indexer/v[0-9]+.[0-9]+.x' # e.g. release/indexer/v0.1.x - 'release/indexer/v[0-9]+.x' # e.g. release/indexer/v1.x From ecfe97ffd93edd7b56c98fbeba6a13507e701509 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Wed, 26 Jun 2024 17:23:49 -0400 Subject: [PATCH 13/15] revert change --- .github/workflows/indexer-build-and-push-mainnet.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/indexer-build-and-push-mainnet.yml b/.github/workflows/indexer-build-and-push-mainnet.yml index 21c038c84a..78d54d2f68 100644 --- a/.github/workflows/indexer-build-and-push-mainnet.yml +++ b/.github/workflows/indexer-build-and-push-mainnet.yml @@ -3,7 +3,6 @@ name: Indexer Build & Push Images to AWS ECR for Mainnet on: # yamllint disable-line rule:truthy push: branches: - - 'wl/pnldbg2' - main - 'release/indexer/v[0-9]+.[0-9]+.x' # e.g. release/indexer/v0.1.x - 'release/indexer/v[0-9]+.x' # e.g. release/indexer/v1.x From 02749a176825e9232eff8ee6ab893889fd8e5223 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Thu, 27 Jun 2024 10:57:23 -0400 Subject: [PATCH 14/15] test --- .../indexer-build-and-push-mainnet.yml | 1 + .../src/helpers/pnl-ticks-helper.ts | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.github/workflows/indexer-build-and-push-mainnet.yml b/.github/workflows/indexer-build-and-push-mainnet.yml index 78d54d2f68..21c038c84a 100644 --- a/.github/workflows/indexer-build-and-push-mainnet.yml +++ b/.github/workflows/indexer-build-and-push-mainnet.yml @@ -3,6 +3,7 @@ name: Indexer Build & Push Images to AWS ECR for Mainnet on: # yamllint disable-line rule:truthy push: branches: + - 'wl/pnldbg2' - main - 'release/indexer/v[0-9]+.[0-9]+.x' # e.g. release/indexer/v0.1.x - 'release/indexer/v[0-9]+.x' # e.g. release/indexer/v1.x diff --git a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts index 4aa5b4978a..bcd440272c 100644 --- a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts +++ b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts @@ -320,12 +320,12 @@ export function getNewPnlTick( subaccountId, latestBlockHeight, latestBlockTime, - usdcPositionSize, - openPerpetualPositionsForSubaccount: JSON.stringify(openPerpetualPositionsForSubaccount), - marketPrices: JSON.stringify(marketPrices), - lastUpdatedFundingIndexMap: JSON.stringify(lastUpdatedFundingIndexMap), - currentFundingIndexMap: JSON.stringify(currentFundingIndexMap), - subaccountTotalTransfersMap: JSON.stringify(subaccountTotalTransfersMap), + // usdcPositionSize, + // openPerpetualPositionsForSubaccount: JSON.stringify(openPerpetualPositionsForSubaccount), + // marketPrices: JSON.stringify(marketPrices), + // lastUpdatedFundingIndexMap: JSON.stringify(lastUpdatedFundingIndexMap), + // currentFundingIndexMap: JSON.stringify(currentFundingIndexMap), + // subaccountTotalTransfersMap: JSON.stringify(subaccountTotalTransfersMap), }); const currentEquity: Big = calculateEquity( usdcPositionSize, @@ -334,6 +334,12 @@ export function getNewPnlTick( lastUpdatedFundingIndexMap, currentFundingIndexMap, ); + logger.info({ + at: 'createPnlTicks#getNewPnlTick', + message: 'Calculated equity and total pnl', + subaccountId, + currentEquity: currentEquity.toFixed(), + }); const totalPnl: Big = calculateTotalPnl( currentEquity, @@ -343,7 +349,6 @@ export function getNewPnlTick( at: 'createPnlTicks#getNewPnlTick', message: 'Calculated equity and total pnl', subaccountId, - currentEquity: currentEquity.toFixed(), totalPnl: totalPnl.toFixed(), }); From 3a81e2b49aa576185c2e5b7d0672190fe0fe5b77 Mon Sep 17 00:00:00 2001 From: Will Liu Date: Thu, 27 Jun 2024 13:10:00 -0400 Subject: [PATCH 15/15] logs --- .../indexer-build-and-push-mainnet.yml | 1 - .../roundtable/src/helpers/pnl-ticks-helper.ts | 18 +----------------- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/.github/workflows/indexer-build-and-push-mainnet.yml b/.github/workflows/indexer-build-and-push-mainnet.yml index 21c038c84a..78d54d2f68 100644 --- a/.github/workflows/indexer-build-and-push-mainnet.yml +++ b/.github/workflows/indexer-build-and-push-mainnet.yml @@ -3,7 +3,6 @@ name: Indexer Build & Push Images to AWS ECR for Mainnet on: # yamllint disable-line rule:truthy push: branches: - - 'wl/pnldbg2' - main - 'release/indexer/v[0-9]+.[0-9]+.x' # e.g. release/indexer/v0.1.x - 'release/indexer/v[0-9]+.x' # e.g. release/indexer/v1.x diff --git a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts index bcd440272c..ee0f6d5970 100644 --- a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts +++ b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts @@ -83,11 +83,6 @@ export async function getPnlTicksCreateObjects( const newSubaccountIds: string[] = _.difference( subaccountIdsWithTranfers, _.keys(accountToLastUpdatedBlockTime), ); - logger.info({ - at: 'pnl-ticks-helper#getPnlTicksCreateObjects', - message: 'New subaccounts with transfers', - newSubaccountIds, - }); // get accounts to update based on last updated block height const accountsToUpdate: string[] = [ ...getAccountsToUpdate(accountToLastUpdatedBlockTime, blockTime), @@ -320,12 +315,6 @@ export function getNewPnlTick( subaccountId, latestBlockHeight, latestBlockTime, - // usdcPositionSize, - // openPerpetualPositionsForSubaccount: JSON.stringify(openPerpetualPositionsForSubaccount), - // marketPrices: JSON.stringify(marketPrices), - // lastUpdatedFundingIndexMap: JSON.stringify(lastUpdatedFundingIndexMap), - // currentFundingIndexMap: JSON.stringify(currentFundingIndexMap), - // subaccountTotalTransfersMap: JSON.stringify(subaccountTotalTransfersMap), }); const currentEquity: Big = calculateEquity( usdcPositionSize, @@ -334,12 +323,6 @@ export function getNewPnlTick( lastUpdatedFundingIndexMap, currentFundingIndexMap, ); - logger.info({ - at: 'createPnlTicks#getNewPnlTick', - message: 'Calculated equity and total pnl', - subaccountId, - currentEquity: currentEquity.toFixed(), - }); const totalPnl: Big = calculateTotalPnl( currentEquity, @@ -349,6 +332,7 @@ export function getNewPnlTick( at: 'createPnlTicks#getNewPnlTick', message: 'Calculated equity and total pnl', subaccountId, + currentEquity: currentEquity.toFixed(), totalPnl: totalPnl.toFixed(), });