Skip to content

Commit

Permalink
Added trades into the aggregateStats (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeifuChen authored Jan 22, 2024
1 parent 308f3aa commit 90df650
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/perps-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
ETHER,
FUNDING_RATE_PERIODS,
FUNDING_RATE_PERIOD_TYPES,
ONE,
ONE_HOUR_SECONDS,
ZERO,
getTimeID,
Expand Down Expand Up @@ -143,7 +144,13 @@ export function handleOrderSettled(event: OrderSettledEvent): void {
positionEntity.pnlWithFeesPaid = ZERO;
positionEntity.totalVolume = volume;

updateAggregateStatEntities(positionEntity.marketId, positionEntity.marketSymbol, event.block.timestamp, volume);
updateAggregateStatEntities(
positionEntity.marketId,
positionEntity.marketSymbol,
event.block.timestamp,
ONE,
volume,
);

positionEntity.save();
} else {
Expand Down Expand Up @@ -181,7 +188,13 @@ export function handleOrderSettled(event: OrderSettledEvent): void {
positionEntity.netFunding = positionEntity.netFunding.plus(event.params.accruedFunding);
positionEntity.size = positionEntity.size.plus(event.params.sizeDelta);

updateAggregateStatEntities(positionEntity.marketId, positionEntity.marketSymbol, event.block.timestamp, volume);
updateAggregateStatEntities(
positionEntity.marketId,
positionEntity.marketSymbol,
event.block.timestamp,
ONE,
volume,
);

positionEntity.save();
}
Expand Down Expand Up @@ -349,6 +362,7 @@ function getOrCreateMarketAggregateStats(
aggregateEntity.timestamp = timestamp;
aggregateEntity.marketId = marketId;
aggregateEntity.marketSymbol = marketSymbol;
aggregateEntity.trades = ZERO;
aggregateEntity.volume = ZERO;
}
return aggregateEntity as PerpsV3AggregateStat;
Expand All @@ -358,6 +372,7 @@ export function updateAggregateStatEntities(
marketId: BigInt,
marketSymbol: string,
timestamp: BigInt,
trades: BigInt,
volume: BigInt,
): void {
// this function updates the aggregate stat entities for the specified account and market
Expand All @@ -370,6 +385,7 @@ export function updateAggregateStatEntities(

// update the aggregate for this market
let aggStats = getOrCreateMarketAggregateStats(marketId, marketSymbol, aggTimestamp, thisPeriod);
aggStats.trades = aggStats.trades.plus(trades);
aggStats.volume = aggStats.volume.plus(volume);
aggStats.save();
}
Expand Down
1 change: 1 addition & 0 deletions subgraphs/perps-v3.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,6 @@ type PerpsV3AggregateStat @entity {
timestamp: BigInt!
marketId: BigInt!
marketSymbol: String!
trades: BigInt!
volume: BigInt!
}

0 comments on commit 90df650

Please sign in to comment.