From ea7732f90bc21e707d71801f3e09b9408c88678e Mon Sep 17 00:00:00 2001 From: Alexander Date: Tue, 23 Jul 2024 19:08:09 -0400 Subject: [PATCH 1/2] Enable fee indexing and fix some issues --- src/abi/fee.ts | 2 +- src/chains/base.ts | 3 +- src/chains/optimism.ts | 3 +- src/handlers/fees.ts | 2 ++ src/handlers/index.ts | 81 ++++++++++++++++++++++++------------------ 5 files changed, 53 insertions(+), 38 deletions(-) diff --git a/src/abi/fee.ts b/src/abi/fee.ts index 6377499..c1cec2f 100644 --- a/src/abi/fee.ts +++ b/src/abi/fee.ts @@ -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": p.bytes32, "sequence": p.uint64, "gasLimits": p.fixedSizeArray(p.uint256, 2), "gasPrices": p.fixedSizeArray(p.uint256, 2)}), + SendPacketFeeDeposited: event("0x0733dc80f277e205edf5d913fa5d91fa0c4cc2635db600b365471c688356c034", {"channelId": indexed(p.bytes32), "sequence": indexed(p.uint64), "gasLimits": p.fixedSizeArray(p.uint256, 2), "gasPrices": p.fixedSizeArray(p.uint256, 2)}), } export const functions = { diff --git a/src/chains/base.ts b/src/chains/base.ts index 9f2c11c..99f800e 100644 --- a/src/chains/base.ts +++ b/src/chains/base.ts @@ -8,7 +8,8 @@ const CONTRACTS: string[] = [ process.env.DISPATCHER_ADDRESS_BASE!, process.env.DISPATCHER_ADDRESS_BASE_SIMCLIENT!, process.env.UNIVERSAL_CHANNEL_ADDRESS_BASE!, - process.env.UNIVERSAL_CHANNEL_ADDRESS_BASE_SIMCLIENT! + process.env.UNIVERSAL_CHANNEL_ADDRESS_BASE_SIMCLIENT!, + process.env.FEE_VAULT_BASE! ] let processor = IbcProcessor() diff --git a/src/chains/optimism.ts b/src/chains/optimism.ts index 362eff8..a565c8c 100644 --- a/src/chains/optimism.ts +++ b/src/chains/optimism.ts @@ -8,7 +8,8 @@ const CONTRACTS: string[] = [ process.env.DISPATCHER_ADDRESS_OPTIMISM!, process.env.DISPATCHER_ADDRESS_OPTIMISM_SIMCLIENT!, process.env.UNIVERSAL_CHANNEL_ADDRESS_OPTIMISM!, - process.env.UNIVERSAL_CHANNEL_ADDRESS_OPTIMISM_SIMCLIENT! + process.env.UNIVERSAL_CHANNEL_ADDRESS_OPTIMISM_SIMCLIENT!, + process.env.FEE_VAULT_OPTIMISM! ] let processor = IbcProcessor() diff --git a/src/handlers/fees.ts b/src/handlers/fees.ts index 82d549e..b667f66 100644 --- a/src/handlers/fees.ts +++ b/src/handlers/fees.ts @@ -8,6 +8,7 @@ export function handleSendPacketFee(block: Block, log: Log) { let event = fee.events.SendPacketFeeDeposited.decode(log) let channelId = ethers.decodeBytes32String(event.channelId) return new SendPacketFeeDeposited({ + id: log.id, channelId: channelId, sequence: event.sequence, gasLimits: event.gasLimits.map(Number), @@ -23,6 +24,7 @@ export function handleSendPacketFee(block: Block, log: Log) { export function handleOpenChannelFee(block: Block, log: Log) { let event = fee.events.OpenChannelFeeDeposited.decode(log) return new OpenChannelFeeDeposited({ + id: log.id, sourceAddress: event.sourceAddress, version: event.version, ordering: event.ordering, diff --git a/src/handlers/index.ts b/src/handlers/index.ts index 8c0ccc2..223b428 100644 --- a/src/handlers/index.ts +++ b/src/handlers/index.ts @@ -82,7 +82,6 @@ type Entities = { const portPrefixCache = new Map(); export async function handler(ctx: Context) { - let chainIdPromise = ctx._chain.client.call("eth_chainId") const entities: Entities = { openInitIbcChannels: [], openTryIbcChannels: [], @@ -116,39 +115,53 @@ export async function handler(ctx: Context) { continue } - let portPrefix = portPrefixCache.get(log.address) - if (!portPrefix) { - // Get the port prefix from the last block in case the port prefix hasn't been properly set in the beginning - let latestHeight = Number(await ctx._chain.client.call("eth_blockNumber", ["latest"])) - const contract = new Contract(ctx, {height: latestHeight}, log.address) - portPrefix = String(await contract.portPrefix()) - portPrefixCache.set(log.address, portPrefix) - } - // Packet events - if (currTopic === dispatcher.events.SendPacket.topic) { - entities.sendPackets.push(handleSendPacket(block.header, log, portPrefix, uchPacketSends.get(log.transactionHash) || '')) - } else if (currTopic === dispatcher.events.RecvPacket.topic) { - entities.recvPackets.push(handleRecvPacket(block.header, log, portPrefix)) - } else if (currTopic === dispatcher.events.WriteAckPacket.topic) { - entities.writeAckPackets.push(handleWriteAckPacket(block.header, log, portPrefix)) - } else if (currTopic === dispatcher.events.Acknowledgement.topic) { - entities.acknowledgements.push(handleAcknowledgement(block.header, log, portPrefix)) - } else if (currTopic === dispatcher.events.Timeout.topic) { - entities.timeouts.push(handleTimeout(block.header, log, portPrefix)) - } else if (currTopic === dispatcher.events.WriteTimeoutPacket.topic) { - entities.writeTimeoutPackets.push(handleWriteTimeoutPacket(block.header, log, portPrefix)) - } - - // Channel events - else if (currTopic === dispatcher.events.ChannelOpenInit.topic) { - entities.openInitIbcChannels.push(handleChannelOpenInit(portPrefix, block.header, log)) - } else if (currTopic === dispatcher.events.ChannelOpenTry.topic) { - entities.openTryIbcChannels.push(handleChannelOpenTry(block.header, log)) - } else if (currTopic === dispatcher.events.ChannelOpenAck.topic) { - entities.openAckIbcChannels.push(handleChannelOpenAck(block.header, log)) - } else if (currTopic === dispatcher.events.ChannelOpenConfirm.topic) { - entities.openConfirmIbcChannels.push(handleChannelOpenConfirm(block.header, log)) + if ([ + dispatcher.events.SendPacket.topic, + dispatcher.events.RecvPacket.topic, + dispatcher.events.WriteAckPacket.topic, + dispatcher.events.Acknowledgement.topic, + dispatcher.events.Timeout.topic, + dispatcher.events.WriteTimeoutPacket.topic, + dispatcher.events.ChannelOpenInit.topic, + dispatcher.events.ChannelOpenTry.topic, + dispatcher.events.ChannelOpenAck.topic, + dispatcher.events.ChannelOpenConfirm.topic + ].includes(currTopic)) { + let portPrefix = portPrefixCache.get(log.address) + if (!portPrefix) { + // Get the port prefix from the last block in case the port prefix hasn't been properly set in the beginning + let latestHeight = Number(await ctx._chain.client.call("eth_blockNumber", ["latest"])) + const contract = new Contract(ctx, {height: latestHeight}, log.address) + portPrefix = String(await contract.portPrefix()) + portPrefixCache.set(log.address, portPrefix) + } + + // Packet events + if (currTopic === dispatcher.events.SendPacket.topic) { + entities.sendPackets.push(handleSendPacket(block.header, log, portPrefix, uchPacketSends.get(log.transactionHash) || '')) + } else if (currTopic === dispatcher.events.RecvPacket.topic) { + entities.recvPackets.push(handleRecvPacket(block.header, log, portPrefix)) + } else if (currTopic === dispatcher.events.WriteAckPacket.topic) { + entities.writeAckPackets.push(handleWriteAckPacket(block.header, log, portPrefix)) + } else if (currTopic === dispatcher.events.Acknowledgement.topic) { + entities.acknowledgements.push(handleAcknowledgement(block.header, log, portPrefix)) + } else if (currTopic === dispatcher.events.Timeout.topic) { + entities.timeouts.push(handleTimeout(block.header, log, portPrefix)) + } else if (currTopic === dispatcher.events.WriteTimeoutPacket.topic) { + entities.writeTimeoutPackets.push(handleWriteTimeoutPacket(block.header, log, portPrefix)) + } + + // Channel events + else if (currTopic === dispatcher.events.ChannelOpenInit.topic) { + entities.openInitIbcChannels.push(handleChannelOpenInit(portPrefix, block.header, log)) + } else if (currTopic === dispatcher.events.ChannelOpenTry.topic) { + entities.openTryIbcChannels.push(handleChannelOpenTry(block.header, log)) + } else if (currTopic === dispatcher.events.ChannelOpenAck.topic) { + entities.openAckIbcChannels.push(handleChannelOpenAck(block.header, log)) + } else if (currTopic === dispatcher.events.ChannelOpenConfirm.topic) { + entities.openConfirmIbcChannels.push(handleChannelOpenConfirm(block.header, log)) + } } // fee events @@ -160,8 +173,6 @@ export async function handler(ctx: Context) { } } - let chainId = Number(await chainIdPromise); - await upsertNewEntities(ctx, entities); await postBlockChannelHook(ctx, entities) await postBlockPacketHook(ctx, entities) From 9b9aaed74ba33c20ae6ed671e2c10a37c3020716 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 24 Jul 2024 09:22:23 -0400 Subject: [PATCH 2/2] Flatten out gasLimits and gasPrices --- db/migrations/1721827264108-Data.js | 21 +++++++++++++++++++ schema.graphql | 6 ++++-- src/handlers/fees.ts | 11 ++++++++-- .../generated/sendPacketFeeDeposited.model.ts | 14 +++++++++---- 4 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 db/migrations/1721827264108-Data.js diff --git a/db/migrations/1721827264108-Data.js b/db/migrations/1721827264108-Data.js new file mode 100644 index 0000000..2b13e0b --- /dev/null +++ b/db/migrations/1721827264108-Data.js @@ -0,0 +1,21 @@ +module.exports = class Data1721827264108 { + name = 'Data1721827264108' + + async up(db) { + await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP COLUMN "gas_limits"`) + await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP COLUMN "gas_prices"`) + 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" ADD "ack_gas_limit" numeric NOT NULL`) + await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD "ack_gas_price" numeric NOT NULL`) + } + + async down(db) { + await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD "gas_limits" integer array NOT NULL`) + await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD "gas_prices" integer array NOT NULL`) + 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" DROP COLUMN "ack_gas_limit"`) + await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP COLUMN "ack_gas_price"`) + } +} diff --git a/schema.graphql b/schema.graphql index 77dd175..ce390c8 100644 --- a/schema.graphql +++ b/schema.graphql @@ -348,8 +348,10 @@ type SendPacketFeeDeposited @entity { id: ID! channelId: String! @index sequence: BigInt! @index - gasLimits: [Int!]! - gasPrices: [Int!]! + sendGasLimit: BigInt! + sendGasPrice: BigInt! + ackGasLimit: BigInt! + ackGasPrice: BigInt! blockNumber: BigInt! blockTimestamp: BigInt! @index transactionHash: String! @index diff --git a/src/handlers/fees.ts b/src/handlers/fees.ts index b667f66..eec0557 100644 --- a/src/handlers/fees.ts +++ b/src/handlers/fees.ts @@ -7,12 +7,19 @@ import { OpenChannelFeeDeposited, SendPacketFeeDeposited } from "../model"; export function handleSendPacketFee(block: Block, log: Log) { let event = fee.events.SendPacketFeeDeposited.decode(log) let channelId = ethers.decodeBytes32String(event.channelId) + + if (event.gasLimits.length !== 2 || event.gasPrices.length !== 2) { + throw new Error('Invalid gas limits or gas prices') + } + return new SendPacketFeeDeposited({ id: log.id, channelId: channelId, sequence: event.sequence, - gasLimits: event.gasLimits.map(Number), - gasPrices: event.gasPrices.map(Number), + sendGasLimit: BigInt(event.gasLimits[0]), + sendGasPrice: BigInt(event.gasPrices[0]), + ackGasLimit: BigInt(event.gasLimits[1]), + ackGasPrice: BigInt(event.gasPrices[1]), blockNumber: BigInt(block.height), blockTimestamp: BigInt(log.block.timestamp), transactionHash: log.transactionHash, diff --git a/src/model/generated/sendPacketFeeDeposited.model.ts b/src/model/generated/sendPacketFeeDeposited.model.ts index e61e3fc..de21893 100644 --- a/src/model/generated/sendPacketFeeDeposited.model.ts +++ b/src/model/generated/sendPacketFeeDeposited.model.ts @@ -17,11 +17,17 @@ export class SendPacketFeeDeposited { @BigIntColumn_({nullable: false}) sequence!: bigint - @IntColumn_({array: true, nullable: false}) - gasLimits!: (number)[] + @BigIntColumn_({nullable: false}) + sendGasLimit!: bigint + + @BigIntColumn_({nullable: false}) + sendGasPrice!: bigint - @IntColumn_({array: true, nullable: false}) - gasPrices!: (number)[] + @BigIntColumn_({nullable: false}) + ackGasLimit!: bigint + + @BigIntColumn_({nullable: false}) + ackGasPrice!: bigint @BigIntColumn_({nullable: false}) blockNumber!: bigint