-
Notifications
You must be signed in to change notification settings - Fork 115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CT-970] Add debug logs to roundtable task, fix case where marketId!=perpetualId #1786
Changes from 14 commits
da22485
418b451
52e3a2e
ab15341
87c1707
db7e0a7
18f06e1
012bd52
03f2f70
4e24619
268c89f
8b51353
ecfe97f
02749a1
3a81e2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ import { | |
helpers, | ||
IsoString, | ||
OraclePriceTable, | ||
perpetualMarketRefresher, | ||
PerpetualPositionFromDatabase, | ||
PerpetualPositionTable, | ||
PnlTicksCreateObject, | ||
|
@@ -73,15 +74,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, | ||
|
@@ -176,13 +194,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'), | ||
}); | ||
Comment on lines
+192
to
+204
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ensure error handling includes necessary details for debugging. The error logging within - error,
- account,
- pnlTicksToBeCreatedAt,
- blockHeight,
- blockTime,
+ error: error.message,
+ account: account.id, # Assuming `account` is an object
+ pnlTicksToBeCreatedAt: pnlTicksToBeCreatedAt.toISO(),
+ blockHeight: blockHeight.toString(),
+ blockTime: blockTime.toString(),
|
||
stats.timing( | ||
`${config.SERVICE_NAME}_get_ticks_compute_pnl`, | ||
new Date().getTime() - computePnlStart, | ||
|
@@ -290,24 +314,49 @@ export function getNewPnlTick( | |
lastUpdatedFundingIndexMap: FundingIndexMap, | ||
currentFundingIndexMap: FundingIndexMap, | ||
): PnlTicksCreateObject { | ||
logger.info({ | ||
at: 'createPnlTicks#getNewPnlTick', | ||
message: 'Creating new PNL tick', | ||
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, | ||
openPerpetualPositionsForSubaccount, | ||
marketPrices, | ||
lastUpdatedFundingIndexMap, | ||
currentFundingIndexMap, | ||
); | ||
logger.info({ | ||
at: 'createPnlTicks#getNewPnlTick', | ||
message: 'Calculated equity and total pnl', | ||
subaccountId, | ||
currentEquity: currentEquity.toFixed(), | ||
}); | ||
|
||
const totalPnl: Big = calculateTotalPnl( | ||
currentEquity, | ||
subaccountTotalTransfersMap[subaccountId][USDC_ASSET_ID], | ||
); | ||
logger.info({ | ||
at: 'createPnlTicks#getNewPnlTick', | ||
message: 'Calculated equity and total pnl', | ||
subaccountId, | ||
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) && | ||
|
@@ -328,7 +377,7 @@ export function getNewPnlTick( | |
}); | ||
} | ||
|
||
return { | ||
const pnlTick: PnlTicksCreateObject = { | ||
totalPnl: totalPnl.toFixed(6), | ||
netTransfers: usdcNetTransfersSinceLastPnlTick.toFixed(6), | ||
subaccountId, | ||
|
@@ -337,6 +386,13 @@ export function getNewPnlTick( | |
blockHeight: latestBlockHeight, | ||
blockTime: latestBlockTime, | ||
}; | ||
logger.info({ | ||
at: 'createPnlTicks#getNewPnlTick', | ||
message: 'New PNL tick', | ||
subaccountId, | ||
pnlTick, | ||
}); | ||
return pnlTick; | ||
} | ||
|
||
/** | ||
|
@@ -404,9 +460,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); | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<void> { | |
const txId: number = await Transaction.start(); | ||
let newTicksToCreate: PnlTicksCreateObject[] = []; | ||
try { | ||
await perpetualMarketRefresher.updatePerpetualMarkets(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tip Codebase Verification Ensure proper error handling and transaction management when updating perpetual markets. The function
Analysis chainEnsure proper error handling and transaction management when updating perpetual markets. The addition of Consider wrapping the call to Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Verify that `updatePerpetualMarkets` does not perform any write operations that could affect the transaction's integrity.
ast-grep --lang typescript --pattern $'async function updatePerpetualMarkets() {
$$$
}'
Length of output: 90 Script: #!/bin/bash
# Searching for the definition of `updatePerpetualMarkets` to inspect its implementation
rg 'updatePerpetualMarkets' --glob '*.{ts,js}'
Length of output: 9773 Script: #!/bin/bash
# Extracting the implementation of `updatePerpetualMarkets` from the identified file
ast-grep --lang typescript --pattern $'export async function updatePerpetualMarkets($_): Promise<void> {
$$$
}' indexer/packages/postgres/src/loops/perpetual-market-refresher.ts
Length of output: 2064 |
||
newTicksToCreate = await getPnlTicksCreateObjects(latestBlockHeight, latestBlockTime, txId); | ||
} catch (error) { | ||
logger.error({ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tip
Codebase Verification
Ensure isolation of market updates in test setup.
The
updatePerpetualMarkets
function is called in the test files but is not explicitly mocked. To prevent actual database or network interactions during tests, ensure that this function is properly mocked.indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts
indexer/services/roundtable/__tests__/tasks/remove-expired-orders.test.ts
Analysis chain
Ensure isolation of market updates in test setup.
Calling
updatePerpetualMarkets()
in thebeforeEach
hook is crucial for setting a consistent state. However, ensure this is properly mocked to prevent actual database or network interactions during tests.Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 107
Script:
Length of output: 97
Script:
Length of output: 509
Script:
Length of output: 6610