Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storing more fee vault info and linking to send packets #25

Merged
merged 7 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
414 changes: 206 additions & 208 deletions abi/fee.json

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions db/migrations/1723751676020-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
module.exports = class Data1723751676020 {
name = 'Data1723751676020'

async up(db) {
await db.query(`DROP INDEX "public"."IDX_36524a30939bb522f1eda95267"`)
await db.query(`DROP INDEX "public"."IDX_b05770c7b9f6fba9a0b12814e9"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" RENAME COLUMN "send_gas_limit" TO "recv_gas_limit"`);
await db.query(`ALTER TABLE "send_packet_fee_deposited" RENAME COLUMN "send_gas_price" TO "recv_gas_price"`);
await db.query(`ALTER TABLE "send_packet" ADD "total_recv_fees_deposited" numeric NOT NULL`)
await db.query(`ALTER TABLE "send_packet" ADD "total_ack_fees_deposited" numeric NOT NULL`)
await db.query(`CREATE INDEX "IDX_2c83d97c2235020f49a5b6b03d" ON "send_packet_fee_deposited" ("recv_gas_limit") `)
await db.query(`CREATE INDEX "IDX_8091819331faf8b95f5e8425ae" ON "send_packet_fee_deposited" ("recv_gas_price") `)
await db.query(`CREATE INDEX "IDX_145dc81dfcbadcef6bc0e7165c" ON "send_packet" ("total_recv_fees_deposited") `)
await db.query(`CREATE INDEX "IDX_fa9429f1c64a25e7c5b64ee8b0" ON "send_packet" ("total_ack_fees_deposited") `)
}

async down(db) {
await db.query(`CREATE INDEX "IDX_36524a30939bb522f1eda95267" ON "send_packet_fee_deposited" ("send_gas_limit") `)
await db.query(`CREATE INDEX "IDX_b05770c7b9f6fba9a0b12814e9" ON "send_packet_fee_deposited" ("send_gas_price") `)
await db.query(`ALTER TABLE "send_packet_fee_deposited" RENAME COLUMN "recv_gas_limit" TO "send_gas_limit"`);
await db.query(`ALTER TABLE "send_packet_fee_deposited" RENAME COLUMN "recv_gas_price" TO "send_gas_price"`);
await db.query(`ALTER TABLE "send_packet" DROP COLUMN "total_recv_fees_deposited"`)
await db.query(`ALTER TABLE "send_packet" DROP COLUMN "total_ack_fees_deposited"`)
await db.query(`DROP INDEX "public"."IDX_2c83d97c2235020f49a5b6b03d"`)
await db.query(`DROP INDEX "public"."IDX_8091819331faf8b95f5e8425ae"`)
await db.query(`DROP INDEX "public"."IDX_145dc81dfcbadcef6bc0e7165c"`)
await db.query(`DROP INDEX "public"."IDX_fa9429f1c64a25e7c5b64ee8b0"`)
}
}
10 changes: 6 additions & 4 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ type SendPacket @entity {
polymerGas: Int
polymerBlockNumber: BigInt

feesDeposited: [SendPacketFeeDeposited!] @derivedFrom(field: "sendPacket")
Inkvi marked this conversation as resolved.
Show resolved Hide resolved
totalRecvFeesDeposited: BigInt! @index
totalAckFeesDeposited: BigInt! @index
lastFeeDeposited: SendPacketFeeDeposited @derivedFrom(field: "sendPacket")
}

type RecvPacket @entity {
Expand Down Expand Up @@ -352,16 +354,16 @@ type SendPacketFeeDeposited @entity {
id: ID!
channelId: String! @index
sequence: BigInt! @index
sendGasLimit: BigInt! @index
sendGasPrice: BigInt! @index
recvGasLimit: BigInt! @index
recvGasPrice: BigInt! @index
ackGasLimit: BigInt! @index
ackGasPrice: BigInt! @index
blockNumber: BigInt!
blockTimestamp: BigInt! @index
transactionHash: String! @index
chainId: Int! @index
from: String! @index
sendPacket: SendPacket @index
sendPacket: SendPacket @index @unique
}

type OpenChannelFeeDeposited @entity {
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/backfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ async function updateMissingSendPacketFees(ctx: Context) {
}
});
if (sendPacket) {
sendPacket.feesDeposited = [sendPacketFee];
sendPacket.totalRecvFeesDeposited = sendPacket.totalRecvFeesDeposited + BigInt(sendPacketFee.recvGasLimit * sendPacketFee.recvGasPrice);
sendPacket.totalAckFeesDeposited = sendPacket.totalAckFeesDeposited + BigInt(sendPacketFee.ackGasLimit * sendPacketFee.ackGasPrice);
sendPacket.lastFeeDeposited = sendPacketFee;
sendPacketFee.sendPacket = sendPacket;

updatedSendPackets.push(sendPacket);
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export function handleSendPacketFee(block: Block, log: Log) {
id: log.id,
channelId: channelId,
sequence: event.sequence,
sendGasLimit: BigInt(event.gasLimits[0]),
sendGasPrice: BigInt(event.gasPrices[0]),
recvGasLimit: BigInt(event.gasLimits[0]),
recvGasPrice: BigInt(event.gasPrices[0]),
ackGasLimit: BigInt(event.gasLimits[1]),
ackGasPrice: BigInt(event.gasPrices[1]),
blockNumber: BigInt(block.height),
Expand Down
37 changes: 34 additions & 3 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ import {
WriteAckPacket,
WriteTimeoutPacket
} from "../model";
import { Entity } from "@subsquid/typeorm-store/lib/store";
import { handleOpenChannelFee, handleSendPacketFee } from "./fees";
import { Entity } from "@subsquid/typeorm-store/lib/store";

export enum StatName {
SendPacket = 'SendPacket',
Expand Down Expand Up @@ -174,8 +174,9 @@ export async function handler(ctx: Context) {
}

await upsertNewEntities(ctx, entities);
await postBlockChannelHook(ctx, entities)
await postBlockPacketHook(ctx, entities)
await postBlockChannelHook(ctx, entities);
await postBlockPacketHook(ctx, entities);
await postBlockFeeHook(ctx, entities);
}

export async function postBlockChannelHook(ctx: Context, entities: Entities) {
Expand Down Expand Up @@ -269,6 +270,36 @@ export async function postBlockPacketHook(ctx: Context, entities: Entities) {
}
}

async function postBlockFeeHook(ctx: Context, entities: Entities) {
let updatedSendPackets: SendPacket[] = [];
let updatedSendPacketFees: SendPacketFeeDeposited[] = [];

for (let sendPacketFee of entities.sendPacketFees) {
let sendPacket = await ctx.store.findOne(SendPacket,
{
where: {
chainId: sendPacketFee.chainId,
srcChannelId: sendPacketFee.channelId,
sequence: sendPacketFee.sequence
}
});
if (sendPacket) {
sendPacket.totalRecvFeesDeposited = sendPacket.totalRecvFeesDeposited + BigInt(sendPacketFee.recvGasLimit * sendPacketFee.recvGasPrice);
sendPacket.totalAckFeesDeposited = sendPacket.totalAckFeesDeposited + BigInt(sendPacketFee.ackGasLimit * sendPacketFee.ackGasPrice);
mvpoyatt marked this conversation as resolved.
Show resolved Hide resolved
sendPacket.lastFeeDeposited = sendPacketFee;
sendPacketFee.sendPacket = sendPacket;

updatedSendPackets.push(sendPacket);
updatedSendPacketFees.push(sendPacketFee);
} else {
console.log(`Could not find send packet for send packet fee ${sendPacketFee.id}`);
}
}

await ctx.store.upsert(updatedSendPackets);
await ctx.store.upsert(updatedSendPacketFees);
}

async function upsertNewEntities(ctx: Context, entities: Entities) {
await ctx.store.upsert(entities.openInitIbcChannels);
await ctx.store.upsert(entities.openTryIbcChannels);
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/packets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export function handleSendPacket(block: Block, log: Log, portPrefix: string, uch
maxFeePerGas,
maxPriorityFeePerGas,
from: log.transaction?.from || '',
totalRecvFeesDeposited: BigInt(0),
totalAckFeesDeposited: BigInt(0)
})
}

Expand Down
14 changes: 11 additions & 3 deletions src/model/generated/sendPacket.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, ManyToOne as ManyToOne_, BigIntColumn as BigIntColumn_, IntColumn as IntColumn_, OneToOne as OneToOne_, OneToMany as OneToMany_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, ManyToOne as ManyToOne_, BigIntColumn as BigIntColumn_, IntColumn as IntColumn_, OneToOne as OneToOne_} from "@subsquid/typeorm-store"
import {Channel} from "./channel.model"
import {Packet} from "./packet.model"
import {SendPacketFeeDeposited} from "./sendPacketFeeDeposited.model"
Expand Down Expand Up @@ -95,6 +95,14 @@ export class SendPacket {
@BigIntColumn_({nullable: true})
polymerBlockNumber!: bigint | undefined | null

@OneToMany_(() => SendPacketFeeDeposited, e => e.sendPacket)
feesDeposited!: SendPacketFeeDeposited[]
@Index_()
@BigIntColumn_({nullable: false})
totalRecvFeesDeposited!: bigint

@Index_()
@BigIntColumn_({nullable: false})
totalAckFeesDeposited!: bigint

@OneToOne_(() => SendPacketFeeDeposited, e => e.sendPacket)
lastFeeDeposited!: SendPacketFeeDeposited | undefined | null
}
11 changes: 6 additions & 5 deletions src/model/generated/sendPacketFeeDeposited.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, BigIntColumn as BigIntColumn_, IntColumn as IntColumn_, ManyToOne as ManyToOne_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, BigIntColumn as BigIntColumn_, IntColumn as IntColumn_, OneToOne as OneToOne_, JoinColumn as JoinColumn_} from "@subsquid/typeorm-store"
import {SendPacket} from "./sendPacket.model"

@Entity_()
Expand All @@ -20,11 +20,11 @@ export class SendPacketFeeDeposited {

@Index_()
@BigIntColumn_({nullable: false})
sendGasLimit!: bigint
recvGasLimit!: bigint

@Index_()
@BigIntColumn_({nullable: false})
sendGasPrice!: bigint
recvGasPrice!: bigint

@Index_()
@BigIntColumn_({nullable: false})
Expand Down Expand Up @@ -53,7 +53,8 @@ export class SendPacketFeeDeposited {
@StringColumn_({nullable: false})
from!: string

@Index_()
@ManyToOne_(() => SendPacket, {nullable: true})
@Index_({unique: true})
@OneToOne_(() => SendPacket, {nullable: true})
@JoinColumn_()
sendPacket!: SendPacket | undefined | null
}
Loading