Open Mkurowski03 opened 3 months ago
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.
PositionCreatedEventProcessor
positionCreated
event is correctly parsed and saved.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");
}
}
}
position.model.ts
positionCreated
events.positionCreated
events, add them to the schema.positionCreated
events.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
}
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:
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