Skip to content

Commit

Permalink
[CT-970] Add debug logs to roundtable task, fix case where marketId!=…
Browse files Browse the repository at this point in the history
…perpetualId (#1786)
  • Loading branch information
dydxwill authored Jun 27, 2024
1 parent d919df6 commit d165d10
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import {
Transaction,
TransferTable,
PositionSide,
PerpetualMarketStatus,
PerpetualMarketType,
MarketTable,
PerpetualMarketTable,
perpetualMarketRefresher,
} from '@dydxprotocol-indexer/postgres';
import {
calculateEquity,
Expand Down Expand Up @@ -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',
Expand All @@ -73,6 +80,7 @@ describe('pnl-ticks-helper', () => {

beforeEach(async () => {
await testMocks.seedData();
await perpetualMarketRefresher.updatePerpetualMarkets();
});

afterAll(async () => {
Expand Down Expand Up @@ -348,6 +356,66 @@ describe('pnl-ticks-helper', () => {
expect(equity).toEqual(new Big('190530'));
});

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: '5',
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',
1000: '1000',
};
const usdcPosition: Big = new Big('10000');
const equity: Big = calculateEquity(
usdcPosition,
positions2,
marketPricesMissingPrice,
lastUpdatedFundingIndexMap,
currentFundingIndexMap,
);
expect(equity).toEqual(new Big('190530'));
});

it('calculateTotalPnl', () => {
const equity: Big = new Big('200100');
const transfers: string = '-20.5';
Expand Down
50 changes: 45 additions & 5 deletions indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
helpers,
IsoString,
OraclePriceTable,
perpetualMarketRefresher,
PerpetualPositionFromDatabase,
PerpetualPositionTable,
PnlTicksCreateObject,
Expand Down Expand Up @@ -73,6 +74,11 @@ 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),
Expand All @@ -82,6 +88,13 @@ export async function getPnlTicksCreateObjects(
...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,
Expand Down Expand Up @@ -176,13 +189,19 @@ export async function getPnlTicksCreateObjects(
logger.error({
at: 'pnl-ticks-helper#getPnlTicksCreateObjects',
message: 'Error when getting new pnl tick',
error,
account,
pnlTicksToBeCreatedAt,
blockHeight,
blockTime,
});
}
});
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,
Expand Down Expand Up @@ -290,6 +309,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,
Expand All @@ -302,12 +328,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) &&
Expand All @@ -328,7 +361,7 @@ export function getNewPnlTick(
});
}

return {
const pnlTick: PnlTicksCreateObject = {
totalPnl: totalPnl.toFixed(6),
netTransfers: usdcNetTransfersSinceLastPnlTick.toFixed(6),
subaccountId,
Expand All @@ -337,6 +370,13 @@ export function getNewPnlTick(
blockHeight: latestBlockHeight,
blockTime: latestBlockTime,
};
logger.info({
at: 'createPnlTicks#getNewPnlTick',
message: 'New PNL tick',
subaccountId,
pnlTick,
});
return pnlTick;
}

/**
Expand Down Expand Up @@ -404,9 +444,9 @@ export function calculateEquity(

const signedPositionNotional: Big = positions.reduce(
(acc: Big, position: PerpetualPositionFromDatabase) => {
const positionNotional: Big = Big(position.size).times(
marketPrices[Number(position.perpetualId)],
);
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);
},
Expand Down
2 changes: 2 additions & 0 deletions indexer/services/roundtable/src/tasks/create-pnl-ticks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PnlTicksCreateObject,
PnlTicksTable,
Transaction,
perpetualMarketRefresher,
} from '@dydxprotocol-indexer/postgres';
import { LatestAccountPnlTicksCache } from '@dydxprotocol-indexer/redis';
import _ from 'lodash';
Expand Down Expand Up @@ -53,6 +54,7 @@ export default async function runTask(): Promise<void> {
const txId: number = await Transaction.start();
let newTicksToCreate: PnlTicksCreateObject[] = [];
try {
await perpetualMarketRefresher.updatePerpetualMarkets();
newTicksToCreate = await getPnlTicksCreateObjects(latestBlockHeight, latestBlockTime, txId);
} catch (error) {
logger.error({
Expand Down

0 comments on commit d165d10

Please sign in to comment.