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

Enable fee indexing and fix some issues #15

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions db/migrations/1721827264108-Data.js
Original file line number Diff line number Diff line change
@@ -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"`)
}
}
6 changes: 4 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
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": 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 = {
Expand Down
3 changes: 2 additions & 1 deletion src/chains/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion src/chains/optimism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 11 additions & 2 deletions src/handlers/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +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,
Expand All @@ -23,6 +31,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,
Expand Down
81 changes: 46 additions & 35 deletions src/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ type Entities = {
const portPrefixCache = new Map<string, string>();

export async function handler(ctx: Context) {
let chainIdPromise = ctx._chain.client.call("eth_chainId")
const entities: Entities = {
openInitIbcChannels: [],
openTryIbcChannels: [],
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions src/model/generated/sendPacketFeeDeposited.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading