Skip to content

Commit

Permalink
Add Order Committed Entity (#174)
Browse files Browse the repository at this point in the history
* entity and logic added

* use txnHash to be inline with other types

* fix
  • Loading branch information
0xlinus authored Jan 31, 2024
1 parent 02f2085 commit 4478125
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
43 changes: 42 additions & 1 deletion src/perps-v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
FundingRatePeriod,
FundingRateUpdate,
OpenPerpsV3Position,
OrderCommitted,
OrderSettled,
PendingOrder,
PerpsV3AggregateStat,
PerpsV3Market,
PerpsV3Position,
Expand All @@ -20,6 +22,7 @@ import {
PermissionGranted as PermissionGrantedEvent,
PermissionRevoked as PermissionRevokedEvent,
CollateralModified as CollateralModifiedEvent,
OrderCommitted as OrderCommittedEvent,
} from '../generated/subgraphs/perps-v3/PerpsV3/PerpsV3MarketProxy';
import { BigInt, log, store } from '@graphprotocol/graph-ts';
import {
Expand Down Expand Up @@ -101,6 +104,16 @@ export function handlePositionLiquidated(event: PositionLiquidatedEvent): void {
export function handleOrderSettled(event: OrderSettledEvent): void {
const orderId = event.params.accountId.toString() + '-' + event.block.timestamp.toString();
const order = new OrderSettled(orderId);

const pendingOrderId = event.params.accountId.toString() + '-' + event.params.marketId.toString();
const pendingOrder = PendingOrder.load(pendingOrderId);

if (pendingOrder !== null) {
order.orderCommitted = pendingOrder.orderCommittedId;
store.remove('PendingOrder', pendingOrderId);
}

order.txnHash = event.transaction.hash.toHex();
order.accountId = event.params.accountId;
order.account = event.params.accountId.toString();
order.accruedFunding = event.params.accruedFunding;
Expand All @@ -115,7 +128,7 @@ export function handleOrderSettled(event: OrderSettledEvent): void {
order.newSize = event.params.newSize;
order.referralFees = event.params.referralFees;
order.settler = event.params.settler;
order.txHash = event.transaction.hash.toHex();
order.txnHash = event.transaction.hash.toHex();
order.pnl = ZERO;

let positionId = event.params.marketId.toString() + '-' + event.params.accountId.toString();
Expand Down Expand Up @@ -401,6 +414,34 @@ export function handleCollateralModified(event: CollateralModifiedEvent): void {
}
}

export function handleOrderCommitted(event: OrderCommittedEvent): void {
const orderCommittedId = event.params.accountId.toString() + '-' + event.block.timestamp.toString();
const pendingOrderId = event.params.accountId.toString() + '-' + event.params.marketId.toString();

const pendingOrder = new PendingOrder(pendingOrderId);
const orderCommitted = new OrderCommitted(orderCommittedId);

pendingOrder.orderCommittedId = orderCommitted.id;
pendingOrder.save();

orderCommitted.marketId = event.params.marketId;
orderCommitted.accountId = event.params.accountId;
orderCommitted.account = event.params.accountId.toString();
orderCommitted.orderType = event.params.orderType;
orderCommitted.sizeDelta = event.params.sizeDelta;
orderCommitted.acceptablePrice = event.params.acceptablePrice;
orderCommitted.commitmentTime = event.params.commitmentTime;
orderCommitted.expectedPriceTime = event.params.expectedPriceTime;
orderCommitted.settlementTime = event.params.settlementTime;
orderCommitted.expirationTime = event.params.expirationTime;
orderCommitted.trackingCode = event.params.trackingCode;
orderCommitted.sender = event.params.sender;
orderCommitted.txnHash = event.transaction.hash.toHex();
orderCommitted.timestamp = event.block.timestamp;

orderCommitted.save();
}

function getOrCreateMarketAggregateStats(
marketId: BigInt,
marketSymbol: string,
Expand Down
26 changes: 25 additions & 1 deletion subgraphs/perps-v3.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type OrderSettled @entity {
marketId: BigInt!
accountId: BigInt!
account: Account!
orderCommitted: OrderCommitted
fillPrice: BigInt!
accruedFunding: BigInt!
sizeDelta: BigInt!
Expand All @@ -38,7 +39,30 @@ type OrderSettled @entity {
trackingCode: Bytes!
settler: Bytes!
pnl: BigInt!
txHash: String!
txnHash: String!
}

type OrderCommitted @entity {
id: ID!
timestamp: BigInt!
marketId: BigInt!
accountId: BigInt!
account: Account!
orderType: Int!
sizeDelta: BigInt!
acceptablePrice: BigInt!
commitmentTime: BigInt!
expectedPriceTime: BigInt!
settlementTime: BigInt!
expirationTime: BigInt!
trackingCode: Bytes!
sender: Bytes!
txnHash: String!
}

type PendingOrder @entity {
id: ID!
orderCommittedId: String!
}

type PerpsV3Position @entity {
Expand Down
5 changes: 5 additions & 0 deletions subgraphs/perps-v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ manifest.push({
event: 'CollateralModified(indexed uint128,indexed uint128,int256,indexed address)',
handler: 'handleCollateralModified',
},
{
event:
'OrderCommitted(indexed uint128,indexed uint128,uint8,int128,uint256,uint256,uint256,uint256,uint256,indexed bytes32,address)',
handler: 'handleOrderCommitted',
},
],
},
});
Expand Down

0 comments on commit 4478125

Please sign in to comment.