-
Notifications
You must be signed in to change notification settings - Fork 115
/
pnl-ticks-helper.ts
448 lines (426 loc) · 14.1 KB
/
pnl-ticks-helper.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import { logger, stats } from '@dydxprotocol-indexer/base';
import {
AssetPositionTable,
FundingIndexMap,
FundingIndexUpdatesTable,
helpers,
IsoString,
OraclePriceTable,
PerpetualPositionFromDatabase,
PerpetualPositionTable,
PnlTicksCreateObject,
PnlTicksTable,
PriceMap,
SubaccountAssetNetTransferMap,
SubaccountFromDatabase,
SubaccountTable,
SubaccountToPerpetualPositionsMap,
TransferTable,
} from '@dydxprotocol-indexer/postgres';
import { LatestAccountPnlTicksCache, PnlTickForSubaccounts } from '@dydxprotocol-indexer/redis';
import Big from 'big.js';
import _ from 'lodash';
import { DateTime } from 'luxon';
import config from '../config';
import { USDC_ASSET_ID, ZERO } from '../lib/constants';
import { redisClient } from './redis';
import { SubaccountUsdcTransferMap } from './types';
/**
* Normalizes a time to the nearest PNL_TICK_UPDATE_INTERVAL_MS.
* If PNL_TICK_UPDATE_INTERVAL_MS is set to 1 hour, then 12:01:00 -> 12:00:00.
*
* @param time
*/
export function normalizeStartTime(
time: Date,
): Date {
const epochMs: number = time.getTime();
const normalizedTimeMs: number = epochMs - (
epochMs % config.PNL_TICK_UPDATE_INTERVAL_MS
);
return new Date(normalizedTimeMs);
}
/**
* Gets a batch of new pnl ticks to write to the database and set in the cache.
* @param blockHeight: consider transfers up until this block height.
*/
export async function getPnlTicksCreateObjects(
blockHeight: string,
blockTime: IsoString,
txId: number,
): Promise<PnlTicksCreateObject[]> {
const startGetPnlTicksCreateObjects: number = Date.now();
const pnlTicksToBeCreatedAt: DateTime = DateTime.utc();
const [
mostRecentPnlTicks,
subaccountsWithTransfers,
]: [
PnlTickForSubaccounts,
SubaccountFromDatabase[],
] = await Promise.all([
getMostRecentPnlTicksForEachAccount(),
SubaccountTable.getSubaccountsWithTransfers(blockHeight, { readReplica: true, txId }),
]);
stats.timing(
`${config.SERVICE_NAME}_get_ticks_relevant_accounts`,
new Date().getTime() - startGetPnlTicksCreateObjects,
);
const accountToLastUpdatedBlockTime: _.Dictionary<IsoString> = _.mapValues(
mostRecentPnlTicks,
(pnlTick: PnlTicksCreateObject) => pnlTick.blockTime,
);
const subaccountIdsWithTranfers: string[] = _.map(subaccountsWithTransfers, 'id');
const newSubaccountIds: string[] = _.difference(
subaccountIdsWithTranfers, _.keys(accountToLastUpdatedBlockTime),
);
// 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);
stats.gauge(
`${config.SERVICE_NAME}_get_ticks_accounts_to_update`,
accountsToUpdate.length,
);
const idToSubaccount: _.Dictionary<SubaccountFromDatabase> = _.keyBy(
subaccountsWithTransfers,
'id',
);
const getFundingIndexStart: number = Date.now();
const blockHeightToFundingIndexMap: _.Dictionary<FundingIndexMap> = await
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,
txId,
),
OraclePriceTable.findLatestPrices(blockHeight),
FundingIndexUpdatesTable.findFundingIndexMap(blockHeight),
]);
stats.timing(
`${config.SERVICE_NAME}_get_ticks_account_info`,
new Date().getTime() - getAccountInfoStart,
);
const computePnlStart: number = Date.now();
const newTicksToCreate: PnlTicksCreateObject[] = [];
accountsToUpdate.forEach((account: string) => {
try {
const newTick: PnlTicksCreateObject = getNewPnlTick(
account,
subaccountTotalTransfersMap,
markets,
Object.values(openPerpetualPositions[account] || {}),
usdcAssetPositions[account] || ZERO,
netUsdcTransfers[account] || ZERO,
pnlTicksToBeCreatedAt,
blockHeight,
blockTime,
mostRecentPnlTicks,
blockHeightToFundingIndexMap[idToSubaccount[account].updatedAtHeight],
currentFundingIndexMap,
);
newTicksToCreate.push(newTick);
} catch (error) {
logger.error({
at: 'pnl-ticks-helper#getPnlTicksCreateObjects',
message: 'Error when getting new pnl tick',
account,
pnlTicksToBeCreatedAt,
blockHeight,
blockTime,
});
}
});
stats.timing(
`${config.SERVICE_NAME}_get_ticks_compute_pnl`,
new Date().getTime() - computePnlStart,
);
return newTicksToCreate;
}
/**
* Gets a list of subaccounts that have not been updated this hour.
*
* @param mostRecentPnlTicks
* @param blockTime
*/
export function getAccountsToUpdate(
accountToLastUpdatedBlockTime: _.Dictionary<IsoString>,
blockTime: IsoString,
): string[] {
// get accounts to update based on last updated block time
const accountsToUpdate: string[] = [
..._.keys(accountToLastUpdatedBlockTime).filter(
(accountId) => {
const normalizedBlockTime: Date = normalizeStartTime(
new Date(blockTime),
); // 12:00:01 -> 12:00:00
const lastUpdatedBlockTime = accountToLastUpdatedBlockTime[accountId];
const normalizedLastUpdatedBlockTime = normalizeStartTime(
new Date(lastUpdatedBlockTime),
); // 12:00:01 -> 12:00:00
return normalizedBlockTime.getTime() !== normalizedLastUpdatedBlockTime.getTime();
},
),
];
return accountsToUpdate;
}
/**
* Get a map of block height to funding index state.
* Funding index state represents the most recent funding index value for every perpetual market.
*
* @param subaccountsWithTransfers
* @param accountsToUpdate
*/
export async function getBlockHeightToFundingIndexMap(
subaccountsWithTransfers: SubaccountFromDatabase[],
accountsToUpdate: string[],
txId: number | undefined = undefined,
): Promise<_.Dictionary<FundingIndexMap>> {
const idToSubaccount: _.Dictionary<SubaccountFromDatabase> = _.keyBy(
subaccountsWithTransfers,
'id',
);
// get the subaccount id to last updated block height
const blockHeights: Set<string> = _.reduce(
accountsToUpdate,
(acc: Set<string>, accountId: string) => {
acc.add(idToSubaccount[accountId].updatedAtHeight);
return acc;
},
new Set<string>(),
);
const fundingIndexMaps: FundingIndexMap[] = await Promise.all(
[...blockHeights].map(
(blockHeight: string) => FundingIndexUpdatesTable.findFundingIndexMap(
blockHeight,
{
readReplica: true,
txId,
},
),
),
);
const blockHeightToFundingIndexMap: _.Dictionary<FundingIndexMap> = _.zipObject(
Object.values([...blockHeights]), fundingIndexMaps,
);
return blockHeightToFundingIndexMap;
}
/**
* Get the most recent pnl tick for a given subaccount
* @param subaccountId: subaccountId to compute pnl tick for
* @param subaccountTotalTransfersMap: total historical transfers across all subaccounts
* @param markets: latest market prices effectiveBeforeOrAt latestBlockHeight
* @param openPerpetualPositionsForSubaccount: list of open perpetual positions for given subaccount
* @param usdcPositionSize: USDC asset position of subaccount
* @param usdcNetTransfersSinceLastPnlTick: net USDC transfers since last pnl tick
* @param pnlTicksToBeCreatedAt: time at which new pnl tick will be created
* @param latestBlockHeight: block height at which new pnl tick will be created
* @param latestBlockTime: block time for above block height
* @param mostRecentPnlTicks: most recent pnl ticks for all subaccounts
*/
// TODO(IND-126): Add support for multiple assets.
export function getNewPnlTick(
subaccountId: string,
subaccountTotalTransfersMap: SubaccountAssetNetTransferMap,
marketPrices: PriceMap,
openPerpetualPositionsForSubaccount: PerpetualPositionFromDatabase[],
usdcPositionSize: Big,
usdcNetTransfersSinceLastPnlTick: Big,
pnlTicksToBeCreatedAt: DateTime,
latestBlockHeight: string,
latestBlockTime: IsoString,
mostRecentPnlTicks: PnlTickForSubaccounts,
lastUpdatedFundingIndexMap: FundingIndexMap,
currentFundingIndexMap: FundingIndexMap,
): PnlTicksCreateObject {
const currentEquity: Big = calculateEquity(
usdcPositionSize,
openPerpetualPositionsForSubaccount,
marketPrices,
lastUpdatedFundingIndexMap,
currentFundingIndexMap,
);
const totalPnl: Big = calculateTotalPnl(
currentEquity,
subaccountTotalTransfersMap[subaccountId][USDC_ASSET_ID],
);
const mostRecentPnlTick: PnlTicksCreateObject | undefined = mostRecentPnlTicks[subaccountId];
// if there has been a significant chagne in equity or totalPnl, log it for debugging purposes.
if (
mostRecentPnlTick &&
Big(mostRecentPnlTick.equity).gt(0) &&
currentEquity.div(mostRecentPnlTick.equity).gt(2) &&
totalPnl.gte(10000) &&
Big(mostRecentPnlTick.totalPnl).lt(-1000) &&
usdcNetTransfersSinceLastPnlTick === ZERO
) {
logger.info({
at: 'createPnlTicks#getNewPnlTick',
message: 'large change of equity and totalPnl',
subaccountId,
previousEquity: mostRecentPnlTick.equity,
previousTotalPnl: mostRecentPnlTick.totalPnl,
currentEquity: currentEquity.toFixed(),
currentTotalPnl: totalPnl.toFixed(),
usdcPositionSize,
openPerpetualPositionsForSubaccount: JSON.stringify(openPerpetualPositionsForSubaccount),
currentMarkets: marketPrices,
});
}
return {
totalPnl: totalPnl.toFixed(6),
netTransfers: usdcNetTransfersSinceLastPnlTick.toFixed(6),
subaccountId,
createdAt: pnlTicksToBeCreatedAt.toISO(),
equity: currentEquity.toFixed(6),
blockHeight: latestBlockHeight,
blockTime: latestBlockTime,
};
}
/**
* Gets a map of subaccount id to net USDC transfers between lastUpdatedHeight and blockHeight
* @param subaccountIds: list of subaccount ids to get net USDC transfers for.
* @param mostRecentPnlTicks: most recent pnl tick for each subaccount.
* @param blockHeight: block height to get net USDC transfers up to.
* @param txId: optional transaction id to use for query.
*/
export async function getUsdcTransfersSinceLastPnlTick(
subaccountIds: string[],
mostRecentPnlTicks: PnlTickForSubaccounts,
blockHeight: string,
txId?: number,
): Promise<SubaccountUsdcTransferMap> {
const netTransfers: SubaccountUsdcTransferMap = {};
const promises = [];
for (const subaccountId of subaccountIds) {
const mostRecentPnlTick: PnlTicksCreateObject | undefined = mostRecentPnlTicks[subaccountId];
const lastUpdatedHeight: string = mostRecentPnlTick === undefined ? '0'
: mostRecentPnlTick!.blockHeight;
const transferPromise = TransferTable.getNetTransfersBetweenBlockHeightsForSubaccount(
subaccountId,
lastUpdatedHeight,
blockHeight,
{ readReplica: true, txId },
).then((transfers: TransferTable.AssetTransferMap) => {
if (USDC_ASSET_ID in transfers) {
netTransfers[subaccountId] = transfers[USDC_ASSET_ID];
}
});
promises.push(transferPromise);
}
await Promise.all(promises);
return netTransfers;
}
/**
* Calculate the current equity of a subaccount based on USDC position size and open
* perpetual positions and any unsettled funding payments.
* @param usdcPositionSize
* @param positions
* @param marketPrices
*/
// TODO(IND-226): De-duplicate this with the same function in `comlink`
export function calculateEquity(
usdcPositionSize: Big,
positions: PerpetualPositionFromDatabase[],
marketPrices: PriceMap,
lastUpdatedFundingIndexMap: FundingIndexMap,
currentFundingIndexMap: FundingIndexMap,
): Big {
const totalUnsettledFundingPayment: Big = positions.reduce(
(acc: Big, position: PerpetualPositionFromDatabase) => {
return acc.plus(
helpers.getUnsettledFunding(
position,
currentFundingIndexMap,
lastUpdatedFundingIndexMap,
),
);
},
ZERO,
);
const signedPositionNotional: Big = positions.reduce(
(acc: Big, position: PerpetualPositionFromDatabase) => {
const positionNotional: Big = Big(position.size).times(
marketPrices[Number(position.perpetualId)],
);
// Add positionNotional to the accumulator
return acc.plus(positionNotional);
},
ZERO,
);
return signedPositionNotional.plus(usdcPositionSize).plus(totalUnsettledFundingPayment);
}
/**
* Calculate the total pnl of a subaccount based on current equity and total historical
* transfers.
* @param currentEquity
* @param totalTransfers
*/
export function calculateTotalPnl(
currentEquity: Big,
totalTransfers: string,
): Big {
return currentEquity.minus(totalTransfers);
}
/**
* Fetches the most recent pnl tick for each account from Redis. If Redis is empty,
* fetches from the db. Redis will be empty if the service has just been started.
*/
export async function getMostRecentPnlTicksForEachAccount():
Promise<PnlTickForSubaccounts> {
const mostRecentCachedPnlTicks: PnlTickForSubaccounts = await
LatestAccountPnlTicksCache.getAll(redisClient);
return !_.isEmpty(mostRecentCachedPnlTicks)
? mostRecentCachedPnlTicks
// If Redis is empty, fetch the most recent pnl ticks from the db created on or after
// block height '1'.
: PnlTicksTable.findMostRecentPnlTickForEachAccount(
'1',
);
}