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 4 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.

33 changes: 33 additions & 0 deletions db/migrations/1723751676020-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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" DROP COLUMN "send_gas_limit"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP COLUMN "send_gas_price"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD "recv_gas_limit" numeric NOT NULL`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD "recv_gas_price" numeric NOT NULL`)
Inkvi marked this conversation as resolved.
Show resolved Hide resolved
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" ADD "send_gas_limit" numeric NOT NULL`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD "send_gas_price" numeric NOT NULL`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP COLUMN "recv_gas_limit"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP COLUMN "recv_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"`)
}
}
6 changes: 4 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type SendPacket @entity {
polymerGas: Int
polymerBlockNumber: BigInt

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

Expand Down Expand Up @@ -352,8 +354,8 @@ 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!
Expand Down
12 changes: 11 additions & 1 deletion src/handlers/backfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,17 @@ 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);
if (!sendPacket.feesDeposited) {
sendPacket.feesDeposited = [sendPacketFee];
}
// Store up to 20 fee transactions per packet
else if (sendPacket.feesDeposited.length >= 20) {
console.log(`Send packet ${sendPacket.id} already has 20 associated fee transactions`);
} else {
sendPacket.feesDeposited = sendPacket.feesDeposited.concat(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
45 changes: 42 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,44 @@ 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
if (!sendPacket.feesDeposited) {
sendPacket.feesDeposited = [sendPacketFee];
}
// Store up to 20 fee transactions per packet
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is 20 coming from?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made it up. Seemed like more fees than anyone would realistically add but also small enough to prevent a single row in the table from getting too bloated. Could probably be as high as 100ish without any problems though.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to store individual fees? What if we just store a sum?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Inkvi let me know what you think of new solution

else if (sendPacket.feesDeposited.length >= 20) {
console.log(`Send packet ${sendPacket.id} already has 20 associated fee transactions`);
} else {
sendPacket.feesDeposited = sendPacket.feesDeposited.concat(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
8 changes: 8 additions & 0 deletions src/model/generated/sendPacket.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ export class SendPacket {
@BigIntColumn_({nullable: true})
polymerBlockNumber!: bigint | undefined | null

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

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

@OneToMany_(() => SendPacketFeeDeposited, e => e.sendPacket)
feesDeposited!: SendPacketFeeDeposited[]
}
4 changes: 2 additions & 2 deletions src/model/generated/sendPacketFeeDeposited.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading