Mkurowski03 / indexer

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

Task Tile #2

Open Mkurowski03 opened 3 months ago

Mkurowski03 commented 3 months ago

What - description of what you me to do Example: Each chain query can be historical as far as I remember. In GraphQL, you only have current data. It might be faster to add this to the indexer. Maybe add to the indexer to save each positionCreated event to the database additionally, and never delete them. No, save each positionCreated event to the database and that's it. Never delete them

Why - explain why this is important Example: I want to allow users to signup and login using their Google account

codeautopilot[bot] commented 3 months ago

Potential solution

To address the task of ensuring that each positionCreated event is saved to the database and retained without deletion, we need to make sure that the PositionCreatedEventProcessor correctly processes and stores these events. Additionally, we need to verify that the schema in src/model/generated/position.model.ts is sufficient for storing all necessary details of the positionCreated events.

How to implement

Step 1: Modify PositionCreatedEventProcessor

  1. Verify the Save Operation: Ensure that the positionCreated event is correctly parsed and saved.
  2. Add Logging for Debugging: Add logging to confirm that the event is being processed and saved.

Updated Code for src/eventprocessor/market/postionCreatedEventProcessor.ts

import { EventProcessor } from "../eventProcessor";
import { Store } from "@subsquid/typeorm-store";
import { Market, Position, PositionStatus } from "../../model";
import * as events from "../../types/events";
import {
  DataHandlerContext,
  Event,
  Block,
} from "@subsquid/substrate-processor";
import { encodeUserValue } from "../../utils/encodersUtils";
import { BigDecimal } from "@subsquid/big-decimal";
import { USDC_DECIMALS } from "../../utils";

export class PositionCreatedEventProcessor implements EventProcessor {
  getHandledEventName(): string {
    return "Market.PositionCreated";
  }

  async process(
    ctx: DataHandlerContext<Store, any>,
    block: Block<any>,
    event: Event,
  ) {
    console.log("Position created event");
    const positionCreatedEvent = events.market.positionCreated.v1;
    if (positionCreatedEvent.is(event)) {
      let parsedEvent = positionCreatedEvent.decode(event);
      let market = await ctx.store.get(Market, parsedEvent.market.toString());
      if (market) {
        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),
          createPrice: BigDecimal(parsedEvent.price, USDC_DECIMALS),
          price: BigDecimal(parsedEvent.price, USDC_DECIMALS), // temporary - set in the next event
        });
        await ctx.store.save(position);
        console.log(`Position ${position.id} saved successfully.`);
      } else {
        console.warn("Market undefined");
      }
    } else {
      console.error("Unsupported spec");
    }
  }
}

Step 2: Review and Modify Schema in position.model.ts

  1. Review the Schema: Ensure that the schema has all necessary fields to store positionCreated events.
  2. Add Any Missing Fields: If there are any additional fields required to store the positionCreated events, add them to the schema.
  3. Ensure Data Retention: Verify that the schema and the database operations do not delete any positionCreated events.

Example Code Changes for src/model/generated/position.model.ts

If no additional fields are required, the current schema is sufficient. However, if we need to add a new field, for example, eventType, we can do it as follows:

import {BigDecimal} from "@subsquid/big-decimal"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, ManyToOne as ManyToOne_, Index as Index_, BigDecimalColumn as BigDecimalColumn_, BigIntColumn as BigIntColumn_, StringColumn as StringColumn_, DateTimeColumn as DateTimeColumn_} from "@subsquid/typeorm-store"
import {Market} from "./market.model"
import {PositionStatus} from "./_positionStatus"

@Entity_()
export class Position {
    constructor(props?: Partial<Position>) {
        Object.assign(this, props)
    }

    @PrimaryColumn_()
    id!: string

    @Index_()
    @ManyToOne_(() => Market, {nullable: true})
    market!: Market

    @BigDecimalColumn_({nullable: false})
    createPrice!: BigDecimal

    @BigDecimalColumn_({nullable: false})
    price!: BigDecimal

    @BigIntColumn_({nullable: false})
    quantity!: bigint

    @BigIntColumn_({nullable: false})
    quantityLeft!: bigint

    @StringColumn_({nullable: false})
    long!: string

    @StringColumn_({nullable: false})
    short!: string

    @BigIntColumn_({nullable: false})
    blockHeight!: bigint

    @DateTimeColumn_({nullable: false})
    timestamp!: Date

    @Column_("varchar", {length: 6, nullable: true})
    status!: PositionStatus | undefined | null

    // New field to store event type
    @StringColumn_({nullable: false})
    eventType!: string
}

Conclusion

The current schema in src/model/generated/position.model.ts appears to be sufficient for storing positionCreated events. Ensure that the event processor logic does not delete any records from this table to retain all positionCreated events. If any additional fields are required, they can be added to the schema as demonstrated.

Click here to create a Pull Request with the proposed solution

Files used for this task:

Changes on src/eventprocessor/market/postionCreatedEventProcessor.ts To address the task of ensuring that each `positionCreated` event is saved to the database and retained without deletion, we need to make sure that the `PositionCreatedEventProcessor` correctly processes and stores these events. The current implementation already saves the `positionCreated` event to the database. However, we should ensure that no logic inadvertently deletes these records. Here's a concrete plan to achieve this: 1. **Review the Current Implementation**: Ensure that the current implementation correctly saves the `positionCreated` event to the database. 2. **Ensure Data Retention**: Verify that there are no mechanisms in place that delete these records. This might involve checking other parts of the codebase, but for this file, we will ensure that the save operation is correctly implemented. ### Steps to Modify `PositionCreatedEventProcessor` 1. **Verify the Save Operation**: Ensure that the `positionCreated` event is correctly parsed and saved. 2. **Add Logging for Debugging**: Add logging to confirm that the event is being processed and saved. ### Updated Code ```typescript import { EventProcessor } from "../eventProcessor"; import { Store } from "@subsquid/typeorm-store"; import { Market, Position, PositionStatus } from "../../model"; import * as events from "../../types/events"; import { DataHandlerContext, Event, Block, } from "@subsquid/substrate-processor"; import { encodeUserValue } from "../../utils/encodersUtils"; import { BigDecimal } from "@subsquid/big-decimal"; import { USDC_DECIMALS } from "../../utils"; export class PositionCreatedEventProcessor implements EventProcessor { getHandledEventName(): string { return "Market.PositionCreated"; } async process( ctx: DataHandlerContext, block: Block, event: Event, ) { console.log("Position created event"); const positionCreatedEvent = events.market.positionCreated.v1; if (positionCreatedEvent.is(event)) { let parsedEvent = positionCreatedEvent.decode(event); let market = await ctx.store.get(Market, parsedEvent.market.toString()); if (market) { 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), // @ts-ignore timestamp: new Date(block.header.timestamp), status: PositionStatus.OPEN, quantityLeft: BigInt(parsedEvent.quantity), createPrice: BigDecimal(parsedEvent.price, USDC_DECIMALS), price: BigDecimal(parsedEvent.price, USDC_DECIMALS), // temporary - set in the next event }); await ctx.store.save(position); console.log(`Position ${position.id} saved successfully.`); } else { console.warn("Market undefined"); } } else { console.error("Unsupported spec"); } } } ``` ### Explanation 1. **Logging**: Added a log statement after saving the position to confirm that the position is saved successfully. 2. **Data Retention**: The current implementation does not include any deletion logic, so no changes are needed to ensure data retention in this file. This should ensure that each `positionCreated` event is saved to the database and retained. If there are any issues with data retention, they would likely be in other parts of the codebase, which another agent will review.
Changes on src/model/generated/position.model.ts To ensure that all `positionCreated` events are saved and retained in the database, we need to review the schema in `src/model/generated/position.model.ts`. The current schema appears to be well-structured for storing position data, but we need to ensure that it aligns with the requirements of retaining all `positionCreated` events. ### Plan for `src/model/generated/position.model.ts` 1. **Review the Schema**: Ensure that the schema has all necessary fields to store `positionCreated` events. 2. **Add Any Missing Fields**: If there are any additional fields required to store the `positionCreated` events, add them to the schema. 3. **Ensure Data Retention**: Verify that the schema and the database operations do not delete any `positionCreated` events. ### Steps to Implement 1. **Review the Current Schema**: - The current schema includes fields such as `id`, `market`, `createPrice`, `price`, `quantity`, `quantityLeft`, `long`, `short`, `blockHeight`, `timestamp`, and `status`. - These fields seem sufficient to store the details of a `positionCreated` event. 2. **Add Any Missing Fields**: - If there are any additional fields required for the `positionCreated` event that are not currently in the schema, add them. For example, if we need to store additional metadata, we can add those fields. 3. **Ensure Data Retention**: - Ensure that the database operations related to this schema do not delete any `positionCreated` events. This can be managed by reviewing the event processor logic and ensuring that no delete operations are performed on this table. ### Example Code Changes If no additional fields are required, the current schema is sufficient. However, if we need to add a new field, for example, `eventType`, we can do it as follows: ```typescript import {BigDecimal} from "@subsquid/big-decimal" import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, ManyToOne as ManyToOne_, Index as Index_, BigDecimalColumn as BigDecimalColumn_, BigIntColumn as BigIntColumn_, StringColumn as StringColumn_, DateTimeColumn as DateTimeColumn_} from "@subsquid/typeorm-store" import {Market} from "./market.model" import {PositionStatus} from "./_positionStatus" @Entity_() export class Position { constructor(props?: Partial) { Object.assign(this, props) } @PrimaryColumn_() id!: string @Index_() @ManyToOne_(() => Market, {nullable: true}) market!: Market @BigDecimalColumn_({nullable: false}) createPrice!: BigDecimal @BigDecimalColumn_({nullable: false}) price!: BigDecimal @BigIntColumn_({nullable: false}) quantity!: bigint @BigIntColumn_({nullable: false}) quantityLeft!: bigint @StringColumn_({nullable: false}) long!: string @StringColumn_({nullable: false}) short!: string @BigIntColumn_({nullable: false}) blockHeight!: bigint @DateTimeColumn_({nullable: false}) timestamp!: Date @Column_("varchar", {length: 6, nullable: true}) status!: PositionStatus | undefined | null // New field to store event type @StringColumn_({nullable: false}) eventType!: string } ``` In this example, a new field `eventType` is added to store the type of event, which can help in distinguishing between different types of events if needed. ### Conclusion The current schema in `src/model/generated/position.model.ts` appears to be sufficient for storing `positionCreated` events. Ensure that the event processor logic does not delete any records from this table to retain all `positionCreated` events. If any additional fields are required, they can be added to the schema as demonstrated.