Skip to content
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

adding HistoricalMarketStats #154

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/perps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ export function handleMarketRemoved(event: MarketRemovedEvent): void {
store.remove('PerpsMarket', event.params.market.toHex());
}

// Update historical market stats entity
function updateHistoricalMarketStats(marketKey: string, timestamp: BigInt, tradeSize: BigInt): void {
const dayTimestamp = timestamp.toI32() / DAY_SECONDS * DAY_SECONDS; // Convert to the start of the day
const historicalStatId = marketKey + '-' + dayTimestamp.toString();
let historicalStat = HistoricalMarketStats.load(historicalStatId);

if (!historicalStat) {
// If the historical stat does not exist, create a new one
historicalStat = new HistoricalMarketStats(historicalStatId);
historicalStat.timestamp = dayTimestamp;
historicalStat.marketKey = Bytes.fromHexString(marketKey) as Bytes;
historicalStat.marketAsset = Bytes.fromHexString(marketEntity.asset) as Bytes;
historicalStat.period = PeriodEnum.DAILY;
historicalStat.marketSize = ZERO;
} else {
// If the historical stat already exists for the day, add the new trade size to the existing marketSize
historicalStat.marketSize = historicalStat.marketSize.plus(tradeSize);
}

// save the data
historicalStat.save();
}

export function handlePositionModified(event: PositionModifiedEvent): void {
// handler for the position modified function
// the PositionModified event it emitted any time a user interacts with a position
Expand Down Expand Up @@ -319,6 +342,9 @@ export function handlePositionModified(event: PositionModifiedEvent): void {
synthetixFeePaid,
ZERO,
);
// update historical market stats ---cici
updateHistoricalMarketStats(marketEntity.marketKey, event.block.timestamp, tradeEntity.size);

}
} else {
// if the tradeSize is equal to zero, it must be a margin transfer or a liquidation
Expand Down
16 changes: 16 additions & 0 deletions subgraphs/perps.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,19 @@ type CrossMarginAccount @entity {
id: ID!
owner: Bytes!
}

# Define the enum for the 'period' field
enum PeriodEnum {
DAILY
WEEKLY
MONTHLY
# Add other allowed values as needed
}

type HistoricalMarketStats @entity {
timestamp: BigInt!
marketKey: Bytes!
marketAsset: Bytes!
period: PeriodEnum
marketSize: BigInt!
}