Skip to content

Commit

Permalink
Perps subgraph update (#1747)
Browse files Browse the repository at this point in the history
* fix: perps graph issue

* feat: update core subgraph core proxy

* feat: update perps subgraph

* Update markets/perps-market/subgraph/package.json

Co-authored-by: troy (troyb.eth) <[email protected]>

---------

Co-authored-by: Sunny Vempati <[email protected]>
Co-authored-by: troy (troyb.eth) <[email protected]>
  • Loading branch information
3 people authored Aug 4, 2023
1 parent 9321173 commit 45bb2bd
Show file tree
Hide file tree
Showing 11 changed files with 1,444 additions and 433 deletions.

Large diffs are not rendered by default.

600 changes: 531 additions & 69 deletions markets/perps-market/subgraph/generated/schema.ts

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions markets/perps-market/subgraph/networks.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"optimism-goerli": {
"PerpsMarketProxy": {
"address": "0xd78D47739Ed468a602beb11C34a2A20759bcEf4F",
"startBlock": 11669270
"address": "0xf272382cB3BE898A8CdB1A23BE056fA2Fcf4513b",
"startBlock": 12708889
}
}
}
2 changes: 1 addition & 1 deletion markets/perps-market/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"build:optimism-mainnet": "echo SKIP || node ./codegen optimism-mainnet && yarn codegen && graph build --output-dir ./build/optimism-mainnet --network optimism",
"deploy:optimism-mainnet": "echo SKIP || graph deploy --output-dir ./build/optimism-mainnet --network optimism --product hosted-service snx-v3/perps-market-optimism-mainnet",
"build:optimism-goerli": "node ./codegen optimism-goerli && yarn codegen && graph build --output-dir ./build/optimism-goerli --network optimism-goerli",
"deploy:optimism-goerli": "graph deploy --output-dir ./build/optimism-goerli --network optimism-goerli --product hosted-service snx-v3/perps-market-optimism-goerli",
"deploy:optimism-goerli": "graph deploy --output-dir ./build/optimism-goerli --network optimism-goerli --product hosted-service snx-v3/v3-perps-opt-goerli",
"test": "echo 'SKIP: No tests' || graph test",
"coverage": "yarn deployments:optimism-goerli && yarn codegen && git diff --exit-code && yarn test",
"create-local": "graph create --node http://localhost:8020/ snx-v3/goerli",
Expand Down
53 changes: 44 additions & 9 deletions markets/perps-market/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,48 @@ type Account @entity {
owner: String!
}

type LiquidatedAccount @entity {
id: ID!
accountId: BigInt!
keeperLiquidationReward: BigInt
accountFullyLiquidated: Boolean
}

type LiquidatedPosition @entity {
id: ID!
accountId: BigInt!
marketId: BigInt
amountLiquidated: BigInt
currentPositionSize: BigInt
}

type Market @entity {
id: ID!
perpsMarketId: BigInt!
marketOwner: String
marketName: String
marketSymbol: String
price: BigInt
skew: BigInt
size: BigInt
sizeDelta: BigInt
currentFundingRate: BigInt
currentFundingVelocity: BigInt
feedId: Bytes
owner: String
maxFundingVelocity: BigInt
skewScale: BigInt
initialMarginFraction: BigInt
maintenanceMarginFraction: BigInt
lockedOiPercent: BigInt
marketOwner: String
owner: String
initialMarginRatioD18: BigInt
maintenanceMarginRatioD18: BigInt
liquidationRewardRatioD18: BigInt
maxSecondsInLiquidationWindow: BigInt
minimumPositionMargin: BigInt
maxLiquidationLimitAccumulationMultiplier: BigInt
lockedOiPercent: BigInt
makerFee: BigInt
takerFee: BigInt

factoryInitialized: Boolean
}

type Order @entity {
Expand All @@ -29,7 +54,7 @@ type Order @entity {
accountId: BigInt
amountProvided: BigInt
orderType: Int
size: BigInt!
size: BigInt
acceptablePrice: BigInt
settlementTime: BigInt
expirationTime: BigInt
Expand All @@ -38,7 +63,7 @@ type Order @entity {

fillPrice: BigInt
accountPnlRealized: BigInt
newSize: BigInt!
newSize: BigInt
collectedFees: BigInt
settelementReward: BigInt
settler: String
Expand All @@ -51,8 +76,7 @@ type SettlementStrategy @entity {
id: ID!
strategyId: BigInt!
marketId: BigInt!
settlementStrategyId: BigInt!
enabled: Boolean!
enabled: Boolean
strategyType: Int
settlementDelay: BigInt
settlementWindowDuration: BigInt
Expand All @@ -64,3 +88,14 @@ type SettlementStrategy @entity {
minimumUsdExchangeAmount: BigInt
maxRoundingLoss: BigInt
}

type ReferrerShare @entity {
id: ID!
referrer: String
shareRatioD18: BigInt
}

type GlobalConfiguration @entity {
id: ID!
feeCollector: String
}
32 changes: 30 additions & 2 deletions markets/perps-market/subgraph/src/account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { AccountCreated } from '../generated/PerpsMarketProxy/PerpsMarketProxy';
import {
AccountCreated,
AccountLiquidated,
PositionLiquidated,
} from '../generated/PerpsMarketProxy/PerpsMarketProxy';

import { Account } from '../generated/schema';
import { Account, LiquidatedAccount, LiquidatedPosition } from '../generated/schema';

export function handleAccountCreated(event: AccountCreated): void {
const id = event.params.accountId.toString();
Expand All @@ -11,3 +15,27 @@ export function handleAccountCreated(event: AccountCreated): void {
account.owner = event.params.owner.toHexString();
account.save();
}

export function handleAccountLiquidated(event: AccountLiquidated): void {
const id = event.transaction.hash.toHexString();

const account = new LiquidatedAccount(id);

account.accountId = event.params.accountId;
account.accountFullyLiquidated = event.params.fullLiquidation;
account.keeperLiquidationReward = event.params.reward;
account.save();
}

export function handlePositionLiquidated(event: PositionLiquidated): void {
const id = event.transaction.hash.toHexString();

const account = new LiquidatedPosition(id);

account.accountId = event.params.accountId;
account.marketId = event.params.marketId;
account.amountLiquidated = event.params.amountLiquidated;
account.currentPositionSize = event.params.currentPositionSize;

account.save();
}
17 changes: 17 additions & 0 deletions markets/perps-market/subgraph/src/globalConfiguration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ReferrerShareUpdated } from '../generated/PerpsMarketProxy/PerpsMarketProxy';

import { ReferrerShare } from '../generated/schema';

export function handleReferrerShareUpdated(event: ReferrerShareUpdated): void {
const id = event.params.referrer.toHexString();

let referrerShare = ReferrerShare.load(id);

if (!referrerShare) {
referrerShare = new ReferrerShare(id);
}

referrerShare.referrer = event.params.referrer.toHexString();
referrerShare.shareRatioD18 = event.params.shareRatioD18;
referrerShare.save();
}
41 changes: 23 additions & 18 deletions markets/perps-market/subgraph/src/market.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
import {
MarketPriceDataUpdated,
MarketOwnerChanged,
MarketRegistered,
MarketCreated,
FundingParametersSet,
LiquidationParametersSet,
LockedOiRatioD18Set,
LockedOiRatioSet,
OrderFeesSet,
MarketUpdated,
FactoryInitialized,
} from '../generated/PerpsMarketProxy/PerpsMarketProxy';

import { Market } from '../generated/schema';

export function handleMarketRegistered(event: MarketRegistered): void {
export function handleMarketCreated(event: MarketCreated): void {
const id = event.params.perpsMarketId.toString();
const market = new Market(id);

market.perpsMarketId = event.params.perpsMarketId;
market.marketOwner = event.params.marketOwner.toHexString();
market.marketName = event.params.marketName;
market.marketSymbol = event.params.marketSymbol;
market.save();
}

export function handleMarketPriceDataUpdated(event: MarketPriceDataUpdated): void {
const id = event.params.perpsMarketId.toString();
const market = Market.load(id);
export function handleMarketUpdated(event: MarketUpdated): void {
const id = event.params.marketId.toString();
const market = new Market(id);

if (market) {
market.feedId = event.params.feedId;
market.save();
}
market.price = event.params.price;
market.skew = event.params.skew;
market.size = event.params.size;
market.sizeDelta = event.params.sizeDelta;
market.currentFundingRate = event.params.currentFundingRate;
market.currentFundingVelocity = event.params.currentFundingVelocity;
market.save();
}

export function handleMarketOwnerChanged(event: MarketOwnerChanged): void {
const id = event.params.perpsMarketId.toString();
export function handleMarketPriceDataUpdated(event: MarketPriceDataUpdated): void {
const id = event.params.marketId.toString();
const market = Market.load(id);

if (market) {
market.owner = event.params.newOwner.toHexString();
market.feedId = event.params.feedId;
market.save();
}
}
Expand All @@ -52,7 +55,7 @@ export function handleFundingParametersSet(event: FundingParametersSet): void {
}
}

export function handleLockedOiRatioD18Set(event: LockedOiRatioD18Set): void {
export function handleLockedOiRatioSet(event: LockedOiRatioSet): void {
const id = event.params.marketId.toString();
const market = Market.load(id);

Expand All @@ -67,9 +70,11 @@ export function handleLiquidationParametersSet(event: LiquidationParametersSet):
const market = Market.load(id);

if (market) {
market.initialMarginFraction = event.params.initialMarginRatioD18;
market.initialMarginRatioD18 = event.params.initialMarginRatioD18;
market.liquidationRewardRatioD18 = event.params.liquidationRewardRatioD18;
market.maintenanceMarginFraction = event.params.maintenanceMarginRatioD18;
market.maintenanceMarginRatioD18 = event.params.maintenanceMarginRatioD18;
market.maxSecondsInLiquidationWindow = event.params.maxSecondsInLiquidationWindow;
market.minimumPositionMargin = event.params.minimumPositionMargin;
market.maxLiquidationLimitAccumulationMultiplier =
event.params.maxLiquidationLimitAccumulationMultiplier;
market.save();
Expand Down
8 changes: 4 additions & 4 deletions markets/perps-market/subgraph/src/orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export function handleOrderCommitted(event: OrderCommitted): void {
'-' +
event.params.marketId.toString() +
'-' +
event.params.accountId.toString();
event.params.accountId.toString() +
'-' +
event.block.number.toString();

let order = Order.load(id);

Expand All @@ -26,9 +28,7 @@ export function handleOrderCommitted(event: OrderCommitted): void {
order.expirationTime = event.params.expirationTime;
order.trackingCode = event.params.trackingCode;
order.owner = event.params.sender.toHexString();

const sizeDelta = BigInt.fromString(event.params.sizeDelta.toString());
order.size = order.size.plus(sizeDelta);
order.size = event.params.sizeDelta;

order.block = event.block.number;
order.timestamp = event.block.timestamp;
Expand Down
16 changes: 11 additions & 5 deletions markets/perps-market/subgraph/subgraph.base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,25 @@ dataSources:
file: ./deployments/optimism-goerli/PerpsMarketProxy.json
entities:
- Account
- LiquidatedAccount
- Market
- Order
- SettlementStrategy
eventHandlers:
- event: AccountCreated(indexed uint128,indexed address)
handler: handleAccountCreated
- event: MarketRegistered(indexed uint128,indexed address,string,string)
handler: handleMarketRegistered
- event: MarketCreated(indexed uint128,string,string)
handler: handleMarketCreated
- event: MarketUpdated(uint128,uint256,int256,uint256,int256,int256,int256)
handler: handleMarketUpdated
- event: MarketPriceDataUpdated(indexed uint128,bytes32)
handler: handleMarketPriceDataUpdated
- event: FundingParametersSet(indexed uint128,uint256,uint256)
handler: handleFundingParametersSet
- event: LiquidationParametersSet(indexed uint128,uint256,uint256,uint256,uint256,uint256,uint256)
handler: handleLiquidationParametersSet
- event: LockedOiRatioD18Set(indexed uint128,uint256)
handler: handleLockedOiRatioD18Set
- event: LockedOiRatioSet(indexed uint128,uint256)
handler: handleLockedOiRatioSet
- event: OrderFeesSet(indexed uint128,uint256,uint256)
handler: handleOrderFeesSet
- event: SettlementStrategyAdded(indexed uint128,(uint8,uint256,uint256,uint256,address,bytes32,string,uint256,uint256,bool),indexed uint256)
Expand All @@ -39,5 +42,8 @@ dataSources:
handler: handleSettlementStrategyEnabled
- event: OrderCommitted(indexed uint128,indexed uint128,uint8,int128,uint256,uint256,uint256,indexed bytes32,address)
handler: handleOrderCommitted

- event: PositionLiquidated(indexed uint128,indexed uint128,uint256,int128)
handler: handlePositionLiquidated
- event: AccountLiquidated(indexed uint128,uint256,bool)
handler: handleAccountLiquidated
file: ./src/core.ts
Loading

0 comments on commit 45bb2bd

Please sign in to comment.