From b6c6eef1d33abd7e60e83b145078d79ccf3c5faa Mon Sep 17 00:00:00 2001 From: "linuz.eth" <99757679+0xlinus@users.noreply.github.com> Date: Mon, 13 May 2024 15:17:34 +0200 Subject: [PATCH] Bug[V2]: Markets orders not showing trigger conditions/executed info (#187) * added LastMarketTrade entity and link it to FuturesOrder * added fillPrice to FuturesOrders entity * add fillPrice when cond order filled --- src/perps.ts | 12 +++++++ src/smartmargin.ts | 70 +++++++++++++++-------------------------- subgraphs/perps.graphql | 7 +++++ 3 files changed, 45 insertions(+), 44 deletions(-) diff --git a/src/perps.ts b/src/perps.ts index baa588da..7985e938 100644 --- a/src/perps.ts +++ b/src/perps.ts @@ -14,6 +14,7 @@ import { FuturesOrder, SmartMarginOrder, FundingRatePeriod, + LastMarketTrade, } from '../generated/subgraphs/perps/schema'; import { MarketAdded as MarketAddedEvent, @@ -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(); @@ -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(); } } diff --git a/src/smartmargin.ts b/src/smartmargin.ts index 2e2c67de..a036ab61 100644 --- a/src/smartmargin.ts +++ b/src/smartmargin.ts @@ -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(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(); + } } } diff --git a/subgraphs/perps.graphql b/subgraphs/perps.graphql index 89c15877..0e9bd6b3 100644 --- a/subgraphs/perps.graphql +++ b/subgraphs/perps.graphql @@ -196,6 +196,7 @@ type FuturesOrder @entity { keeper: Bytes! reduceOnly: Boolean priceOracle: FuturesPriceOracleUsed + fillPrice: BigInt } type SmartMarginAccount @entity { @@ -249,3 +250,9 @@ type CrossMarginAccount @entity { id: ID! owner: Bytes! } + +type LastMarketTrade @entity { + id: ID! + account: Bytes! + price: BigInt! +}