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 1 commit
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
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
2 changes: 1 addition & 1 deletion src/abi/fee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type { EventParams as EParams, FunctionArguments, FunctionReturn } from '
export const events = {
OpenChannelFeeDeposited: event("0x8ab5595b5ac9231b64513ba86f6bd9fb73c51cae40c36083f7dfc2298e4429e6", {"sourceAddress": p.address, "version": p.string, "ordering": p.uint8, "connectionHops": p.array(p.string), "counterpartyPortId": p.string, "feeAmount": p.uint256}),
OwnershipTransferred: event("0x8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e0", {"previousOwner": indexed(p.address), "newOwner": indexed(p.address)}),
SendPacketFeeDeposited: event("0x0733dc80f277e205edf5d913fa5d91fa0c4cc2635db600b365471c688356c034", {"channelId": indexed(p.bytes32), "sequence": indexed(p.uint64), "gasLimits": p.fixedSizeArray(p.uint256, 2), "gasPrices": p.fixedSizeArray(p.uint256, 2)}),
SendPacketFeeDeposited: event("0x0733dc80f277e205edf5d913fa5d91fa0c4cc2635db600b365471c688356c034", {"channelId": p.bytes32, "sequence": p.uint64, "gasLimits": p.fixedSizeArray(p.uint256, 2), "gasPrices": p.fixedSizeArray(p.uint256, 2)}),
}

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

Choose a reason for hiding this comment

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

Correct the logic for updating totalRecvFeesDeposited.

The logic for updating totalRecvFeesDeposited seems incorrect. It should accumulate the recvGasLimit * recvGasPrice rather than adding it to totalAckFeesDeposited.

- sendPacket.totalRecvFeesDeposited = sendPacket.totalAckFeesDeposited + BigInt(sendPacketFee.recvGasLimit * sendPacketFee.recvGasPrice);
+ sendPacket.totalRecvFeesDeposited = sendPacket.totalRecvFeesDeposited + BigInt(sendPacketFee.recvGasLimit * sendPacketFee.recvGasPrice);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
sendPacket.totalRecvFeesDeposited = sendPacket.totalAckFeesDeposited + BigInt(sendPacketFee.recvGasLimit * sendPacketFee.recvGasPrice);
sendPacket.totalAckFeesDeposited = sendPacket.totalAckFeesDeposited + BigInt(sendPacketFee.ackGasLimit * sendPacketFee.ackGasPrice);
sendPacket.totalRecvFeesDeposited = sendPacket.totalRecvFeesDeposited + BigInt(sendPacketFee.recvGasLimit * sendPacketFee.recvGasPrice);
sendPacket.totalAckFeesDeposited = sendPacket.totalAckFeesDeposited + BigInt(sendPacketFee.ackGasLimit * sendPacketFee.ackGasPrice);

// Store up to 20 fee transactions per packet
if (sendPacket.feesDeposited.length < 20) {
sendPacket.feesDeposited = sendPacket.feesDeposited.concat(sendPacketFee);
} else {
console.log(`Send packet ${sendPacket.id} already has 20 associated fee transactions`);
Copy link

Choose a reason for hiding this comment

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

Consider making the fee transaction limit configurable.

The current logic limits fee transactions to 20 per packet. Consider making this limit configurable to allow flexibility based on different use cases.

- if (sendPacket.feesDeposited.length < 20) {
+ const MAX_FEE_TRANSACTIONS = 20; // Consider moving this to a configuration file
+ if (sendPacket.feesDeposited.length < MAX_FEE_TRANSACTIONS) {
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (sendPacket.feesDeposited.length < 20) {
sendPacket.feesDeposited = sendPacket.feesDeposited.concat(sendPacketFee);
} else {
console.log(`Send packet ${sendPacket.id} already has 20 associated fee transactions`);
const MAX_FEE_TRANSACTIONS = 20; // Consider moving this to a configuration file
if (sendPacket.feesDeposited.length < MAX_FEE_TRANSACTIONS) {
sendPacket.feesDeposited = sendPacket.feesDeposited.concat(sendPacketFee);
} else {
console.log(`Send packet ${sendPacket.id} already has 20 associated fee transactions`);

}
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
42 changes: 39 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,41 @@ 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.totalAckFeesDeposited + BigInt(sendPacketFee.recvGasLimit * sendPacketFee.recvGasPrice);
mvpoyatt marked this conversation as resolved.
Show resolved Hide resolved
sendPacket.totalAckFeesDeposited = sendPacket.totalAckFeesDeposited + BigInt(sendPacketFee.ackGasLimit * sendPacketFee.ackGasPrice);
mvpoyatt marked this conversation as resolved.
Show resolved Hide resolved
// 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

if (sendPacket.feesDeposited.length < 20) {
sendPacket.feesDeposited = sendPacket.feesDeposited.concat(sendPacketFee);
} else {
console.log(`Send packet ${sendPacket.id} already has 20 associated fee transactions`);
}
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