bigshortbets / indexer

Subsquid indexer aligned with P2P Market metadata
MIT License
1 stars 3 forks source link

Tracking of opening position prices #61

Closed Klapeyron closed 1 month ago

Klapeyron commented 1 month ago

Indexer is not tracking correctly opening position prices. Right now, it stores opening price as createPrice variable which is fine for brand new positions. If position is created out of existing position, then createPrice is set for new trade, which cases ambiguous value for position side that was passive in this transaction. Indexer should track separate opening price for each side of the position and update it in accordance with the information if position is just created or is offset.

Klapeyron commented 1 month ago

@coderabbitai review

coderabbitai[bot] commented 1 month ago

[!TIP] For best results, initiate chat on the files or code changes.

@Klapeyron, to track separate opening prices for each side of the position, we need to make the following changes:

  1. Update the Position class in src/model/generated/position.model.ts to include separate fields for the opening prices of the long and short sides:

    @BigDecimalColumn_({nullable: false})
    createPriceLong!: BigDecimal
    
    @BigDecimalColumn_({nullable: false})
    createPriceShort!: BigDecimal
  2. Update the GraphQL schema in schema.graphql to reflect these changes:

    type Position @entity {
      id: ID!
      market: Market!
      createPriceLong: BigDecimal!
      createPriceShort: BigDecimal!
      price: BigDecimal!
      quantity: BigInt!
      quantityLeft: BigInt!
      long: String!
      short: String!
      blockHeight: BigInt!
      timestamp: DateTime!
      status: PositionStatus
    }
  3. Modify the event processor in src/eventprocessor/market/postionCreatedEventProcessor.ts to set the new fields:

    let position = new Position({
      id: parsedEvent.positionId.toString(),
      market: market,
      quantity: BigInt(parsedEvent.quantity),
      long: encodeUserValue(parsedEvent.long),
      short: encodeUserValue(parsedEvent.short),
      blockHeight: BigInt(block.header.height),
      timestamp: new Date(block.header.timestamp),
      status: PositionStatus.OPEN,
      quantityLeft: BigInt(parsedEvent.quantity),
      createPriceLong: BigDecimal(parsedEvent.priceLong, USDC_DECIMALS),
      createPriceShort: BigDecimal(parsedEvent.priceShort, USDC_DECIMALS),
      price: BigDecimal(parsedEvent.price, USDC_DECIMALS), // temporary - set in the next event
    });

These changes will ensure that the indexer tracks separate opening prices for each side of the position accurately.

Analysis chain ---
Scripts executed The following scripts were executed for the analysis: Script: ```shell #!/bin/bash # Find all occurrences of `createPrice` in the codebase to understand where it is used and how it is updated rg 'createPrice' ``` Length of output: 252 --- Script: ```shell #!/bin/bash # Display the contents of the relevant files to understand the usage of `createPrice` # Display the contents of src/model/generated/position.model.ts cat src/model/generated/position.model.ts # Display the contents of schema.graphql cat schema.graphql # Display the contents of src/eventprocessor/market/postionCreatedEventProcessor.ts cat src/eventprocessor/market/postionCreatedEventProcessor.ts ``` Length of output: 5142