Skip to content

Commit

Permalink
Bug[V2]: Markets orders not showing trigger conditions/executed info (#…
Browse files Browse the repository at this point in the history
…187)

* added LastMarketTrade entity and link it to FuturesOrder

* added fillPrice to FuturesOrders entity

* add fillPrice when cond order filled
  • Loading branch information
0xlinus authored May 13, 2024
1 parent d4d5066 commit b6c6eef
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 44 deletions.
12 changes: 12 additions & 0 deletions src/perps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
FuturesOrder,
SmartMarginOrder,
FundingRatePeriod,
LastMarketTrade,
} from '../generated/subgraphs/perps/schema';
import {
MarketAdded as MarketAddedEvent,
Expand Down Expand Up @@ -322,6 +323,12 @@ export function handlePositionModified(event: PositionModifiedEvent): void {
ZERO,
);
}

// Create LastMarketTrade entity to reconcile with FuturesOrder
let lastMarketTrade = new LastMarketTrade(futuresMarketAddress.toHex() + '-' + account.toHex());
lastMarketTrade.account = account;
lastMarketTrade.price = tradeEntity.price;
lastMarketTrade.save();
} else {
// if the tradeSize is equal to zero, it must be a margin transfer or a liquidation
const txHash = event.transaction.hash.toHex();
Expand Down Expand Up @@ -810,6 +817,11 @@ export function handleDelayedOrderSubmitted(event: DelayedOrderSubmittedEvent):
futuresOrderEntity.status = 'Pending';
futuresOrderEntity.keeper = ZERO_ADDRESS;

let lastMarketTrade = LastMarketTrade.load(futuresMarketAddress.toHex() + '-' + account.toHex());
if (lastMarketTrade) {
futuresOrderEntity.targetPrice = lastMarketTrade.price;
}

futuresOrderEntity.save();
}
}
Expand Down
70 changes: 26 additions & 44 deletions src/smartmargin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,58 +182,40 @@ export function handleOrderV1Filled(event: ConditionalOrderFilledEvent): void {
}

export function handleOrderV2Filled(event: ConditionalOrderFilled1Event): void {
const v1Event = new ConditionalOrderFilledEvent(
event.address,
event.logIndex,
event.transactionLogIndex,
event.logType,
event.block,
event.transaction,
event.parameters,
event.receipt,
);

handleOrderFilled(v1Event, 'CHAINLINK');
handleOrderFilled(event, 'CHAINLINK');
}

export function handleOrderV2FilledWithPriceOracle(event: ConditionalOrderFilled2Event): void {
const priceOracle = event.params.priceOracle === 0 ? 'PYTH' : 'CHAINLINK';
const v1Params = event.parameters.filter((value) => {
return value.name !== 'priceOracle';
});

const v1Event = new ConditionalOrderFilledEvent(
event.address,
event.logIndex,
event.transactionLogIndex,
event.logType,
event.block,
event.transaction,
v1Params,
event.receipt,
);
handleOrderFilled(v1Event, priceOracle);
handleOrderFilled(event, priceOracle);
}

function handleOrderFilled(event: ConditionalOrderFilledEvent, priceOracle: string): void {
function handleOrderFilled<T>(event: T, priceOracle: string): void {
// handle order filled event for smart margin account
// update the order status to filled
const smAccountAddress = event.params.account as Address;

const futuresOrderEntityId = `SM-${smAccountAddress.toHexString()}-${event.params.conditionalOrderId.toString()}`;
const futuresOrderEntity = FuturesOrder.load(futuresOrderEntityId);
if (futuresOrderEntity) {
// update the order status
futuresOrderEntity.status = 'Filled';
futuresOrderEntity.timestamp = event.block.timestamp;
futuresOrderEntity.priceOracle = priceOracle;

const smartMarginOrder = getOrCreateSmartMarginOrder(smAccountAddress, futuresOrderEntity.marketKey);
smartMarginOrder.orderType = futuresOrderEntity.orderType;
smartMarginOrder.recordTrade = true;

futuresOrderEntity.save();
smartMarginOrder.save();
if (
event instanceof ConditionalOrderFilledEvent ||
event instanceof ConditionalOrderFilled1Event ||
event instanceof ConditionalOrderFilled2Event
) {
const smAccountAddress = event.params.account as Address;

const futuresOrderEntityId = `SM-${smAccountAddress.toHexString()}-${event.params.conditionalOrderId.toString()}`;
const futuresOrderEntity = FuturesOrder.load(futuresOrderEntityId);
if (futuresOrderEntity) {
// update the order status
futuresOrderEntity.status = 'Filled';
futuresOrderEntity.timestamp = event.block.timestamp;
futuresOrderEntity.priceOracle = priceOracle;
futuresOrderEntity.fillPrice = event.params.fillPrice;

const smartMarginOrder = getOrCreateSmartMarginOrder(smAccountAddress, futuresOrderEntity.marketKey);
smartMarginOrder.orderType = futuresOrderEntity.orderType;
smartMarginOrder.recordTrade = true;

futuresOrderEntity.save();
smartMarginOrder.save();
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions subgraphs/perps.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type FuturesOrder @entity {
keeper: Bytes!
reduceOnly: Boolean
priceOracle: FuturesPriceOracleUsed
fillPrice: BigInt
}

type SmartMarginAccount @entity {
Expand Down Expand Up @@ -249,3 +250,9 @@ type CrossMarginAccount @entity {
id: ID!
owner: Bytes!
}

type LastMarketTrade @entity {
id: ID!
account: Bytes!
price: BigInt!
}

0 comments on commit b6c6eef

Please sign in to comment.