Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill committed Jun 26, 2024
1 parent 4e24619 commit 268c89f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 7 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,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',
Expand All @@ -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(
Expand All @@ -377,7 +413,7 @@ describe('pnl-ticks-helper', () => {
lastUpdatedFundingIndexMap,
currentFundingIndexMap,
);
expect(equity).toEqual(new Big('200530'));
expect(equity).toEqual(new Big('190530'));
});

it('calculateTotalPnl', () => {
Expand Down
8 changes: 4 additions & 4 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 @@ -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);
},
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 268c89f

Please sign in to comment.