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

Feat/futures #122

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Cumulative futures stats
  • Loading branch information
avclarke committed Oct 22, 2021
commit eda9b2e5bf18b19e909d1bcfe5478aad6785b0e1
50 changes: 49 additions & 1 deletion src/futures-mapping.ts
Original file line number Diff line number Diff line change
@@ -11,10 +11,19 @@ import {
FuturesMarket as FuturesMarketContract,
} from '../generated/templates/FuturesMarket/FuturesMarket';

import { FuturesMarket as FuturesMarketEntity, FuturesPosition, FuturesTrade, FuturesStat } from '../generated/schema';
import {
FuturesMarket as FuturesMarketEntity,
FuturesPosition,
FuturesTrade,
FuturesStat,
FuturesCumulativeStat,
FuturesOneMinStat,
} from '../generated/schema';
import { ZERO } from './common';

let ETHER = BigInt.fromI32(10).pow(18);
let ONE_MINUTE_SECONDS = BigInt.fromI32(60);
let SINGLE_INDEX = '0';

export function handleMarketAdded(event: MarketAddedEvent): void {
let futuresMarketContract = FuturesMarketContract.bind(event.params.market);
@@ -39,6 +48,7 @@ export function handlePositionModified(event: PositionModifiedEvent): void {
let marketEntity = FuturesMarketEntity.load(proxyAddress.toHex());
let positionEntity = FuturesPosition.load(positionId);
let statEntity = FuturesStat.load(statId);
let cumulativeEntity = getCumulativeEntity();
if (statEntity == null) {
statEntity = new FuturesStat(statId);
statEntity.account = event.params.account;
@@ -57,6 +67,23 @@ export function handlePositionModified(event: PositionModifiedEvent): void {
tradeEntity.asset = marketEntity.asset;
statEntity.totalTrades = statEntity.totalTrades.plus(BigInt.fromI32(1));
tradeEntity.save();

let volume = tradeEntity.size.times(tradeEntity.price);
cumulativeEntity.totalTrades = cumulativeEntity.totalTrades.plus(BigInt.fromI32(1));
cumulativeEntity.totalVolume = cumulativeEntity.totalVolume.plus(volume);
cumulativeEntity.averageVolume = cumulativeEntity.totalVolume.div(cumulativeEntity.totalTrades);

let timestamp = getTimeID(event.block.timestamp, ONE_MINUTE_SECONDS);
let oneMinStat = FuturesOneMinStat.load(timestamp.toString());
if (oneMinStat == null) {
oneMinStat = new FuturesOneMinStat(timestamp.toString());
oneMinStat.trades = BigInt.fromI32(1);
oneMinStat.volume = volume;
} else {
oneMinStat.trades = oneMinStat.trades.plus(BigInt.fromI32(1));
oneMinStat.volume = oneMinStat.volume.plus(volume);
}
oneMinStat.save();
}
if (positionEntity == null) {
positionEntity = new FuturesPosition(positionId);
@@ -89,6 +116,7 @@ export function handlePositionModified(event: PositionModifiedEvent): void {
positionEntity.timestamp = event.block.timestamp;
positionEntity.save();
statEntity.save();
cumulativeEntity.save();
}

export function handlePositionLiquidated(event: PositionLiquidatedEvent): void {
@@ -102,4 +130,24 @@ export function handlePositionLiquidated(event: PositionLiquidatedEvent): void {
statEntity.save();
positionEntity.isLiquidated = true;
positionEntity.save();
let cumulativeEntity = getCumulativeEntity();
cumulativeEntity.totalLiquidations = cumulativeEntity.totalLiquidations.plus(BigInt.fromI32(1));
cumulativeEntity.save();
}

function getCumulativeEntity(): FuturesCumulativeStat {
let cumulativeEntity = FuturesCumulativeStat.load(SINGLE_INDEX);
if (cumulativeEntity == null) {
cumulativeEntity = new FuturesCumulativeStat(SINGLE_INDEX);
cumulativeEntity.totalLiquidations = ZERO;
cumulativeEntity.totalTrades = ZERO;
cumulativeEntity.totalVolume = ZERO;
cumulativeEntity.averageVolume = ZERO;
}
return cumulativeEntity as FuturesCumulativeStat;
}

function getTimeID(timestamp: BigInt, num: BigInt): BigInt {
let remainder = timestamp.mod(num);
return timestamp.minus(remainder);
}
16 changes: 15 additions & 1 deletion subgraphs/synthetix-futures.graphql
Original file line number Diff line number Diff line change
@@ -35,4 +35,18 @@ type FuturesStat @entity {
pnlWithFeesPaid: BigInt!
liquidations: BigInt!
totalTrades: BigInt!
}
}

type FuturesCumulativeStat @entity {
id: ID!
totalLiquidations: BigInt!
totalTrades: BigInt!
totalVolume: BigInt!
averageVolume: BigInt!
}

type FuturesOneMinStat @entity {
id: ID!
trades: BigInt!
volume: BigInt!
}