Skip to content

Commit

Permalink
Connect open channel fees to channel open init
Browse files Browse the repository at this point in the history
  • Loading branch information
Inkvi committed Jul 25, 2024
1 parent dda8e6a commit 4b3731e
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 15 deletions.
2 changes: 0 additions & 2 deletions db/migrations/1721926496341-Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = class Data1721926496341 {
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP CONSTRAINT "FK_9415c52ccc157e1a37c9f3f6235"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP CONSTRAINT "UQ_9415c52ccc157e1a37c9f3f6235"`)
await db.query(`CREATE INDEX "IDX_9415c52ccc157e1a37c9f3f623" ON "send_packet_fee_deposited" ("send_packet_id") `)
await db.query(`CREATE INDEX "IDX_f702d8ab77e40da621977ffe11" ON "send_packet" ("sequence") `)
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD CONSTRAINT "FK_9415c52ccc157e1a37c9f3f6235" FOREIGN KEY ("send_packet_id") REFERENCES "send_packet"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
}

Expand All @@ -15,7 +14,6 @@ module.exports = class Data1721926496341 {
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD CONSTRAINT "FK_9415c52ccc157e1a37c9f3f6235" FOREIGN KEY ("send_packet_id") REFERENCES "send_packet"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" ADD CONSTRAINT "UQ_9415c52ccc157e1a37c9f3f6235" UNIQUE ("send_packet_id")`)
await db.query(`DROP INDEX "public"."IDX_9415c52ccc157e1a37c9f3f623"`)
await db.query(`DROP INDEX "public"."IDX_f702d8ab77e40da621977ffe11"`)
await db.query(`ALTER TABLE "send_packet_fee_deposited" DROP CONSTRAINT "FK_9415c52ccc157e1a37c9f3f6235"`)
}
}
15 changes: 15 additions & 0 deletions db/migrations/1721926766448-Data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = class Data1721926766448 {
name = 'Data1721926766448'

async up(db) {
await db.query(`ALTER TABLE "open_channel_fee_deposited" ADD "open_channel_id" character varying`)
await db.query(`CREATE INDEX "IDX_a3c03e6c9e7053f39ef10aaab7" ON "open_channel_fee_deposited" ("open_channel_id") `)
await db.query(`ALTER TABLE "open_channel_fee_deposited" ADD CONSTRAINT "FK_a3c03e6c9e7053f39ef10aaab73" FOREIGN KEY ("open_channel_id") REFERENCES "channel_open_init"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`)
}

async down(db) {
await db.query(`ALTER TABLE "open_channel_fee_deposited" DROP COLUMN "open_channel_id"`)
await db.query(`DROP INDEX "public"."IDX_a3c03e6c9e7053f39ef10aaab7"`)
await db.query(`ALTER TABLE "open_channel_fee_deposited" DROP CONSTRAINT "FK_a3c03e6c9e7053f39ef10aaab73"`)
}
}
3 changes: 3 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ type ChannelOpenInit @entity {
polymerTxHash: String @index
polymerGas: Int
polymerBlockNumber: BigInt

feesDeposited: [OpenChannelFeeDeposited!] @derivedFrom(field: "openChannel")
}

type ChannelOpenTry @entity {
Expand Down Expand Up @@ -375,6 +377,7 @@ type OpenChannelFeeDeposited @entity {
transactionHash: String! @index
chainId: Int! @index
from: String! @index
openChannel: ChannelOpenInit @index
}

type Transaction @entity {
Expand Down
58 changes: 47 additions & 11 deletions src/handlers/backfill.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Context } from "../utils/types";
import { BACKFILL_CONCURRENCY, CATCHUP_BATCH_SIZE, CATCHUP_ERROR_LIMIT, ENABLE_CATCHUP } from "../chains/constants";
import { Channel, Packet, PacketCatchUpError, SendPacket, SendPacketFeeDeposited } from "../model";
import {
Channel, ChannelOpenInit,
OpenChannelFeeDeposited,
Packet,
PacketCatchUpError,
SendPacket,
SendPacketFeeDeposited
} from "../model";
import { And, IsNull, LessThan, MoreThan, Not } from "typeorm";
import { channelMetrics } from "./channels";
import { packetMetrics } from "./packets";
Expand All @@ -12,7 +19,8 @@ export async function handler(ctx: Context) {
await updateMissingChannelMetrics(ctx);
}

await updateMissingFees(ctx)
await updateMissingSendPacketFees(ctx)
await updateMissingOpenChannelFeeDeposited(ctx)
}

export function getMissingChannelMetricsClauses() {
Expand Down Expand Up @@ -134,15 +142,7 @@ async function updateMissingPacketMetrics(ctx: Context) {
await packetMetrics(Array.from(uniquePacketIds), ctx, BACKFILL_CONCURRENCY);
}

async function updateMissingFees(ctx: Context) {
const missingFeesCount = await ctx.store.count(SendPacketFeeDeposited, {
where: {
sendPacket: IsNull()
}
});

console.log(`Missing ${missingFeesCount} send packet fees`);

async function updateMissingSendPacketFees(ctx: Context) {
let sendPacketFees = await ctx.store.find(SendPacketFeeDeposited, {
take: CATCHUP_BATCH_SIZE,
where: {
Expand Down Expand Up @@ -175,4 +175,40 @@ async function updateMissingFees(ctx: Context) {

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


async function updateMissingOpenChannelFeeDeposited(ctx: Context) {
let openChannelFees = await ctx.store.find(OpenChannelFeeDeposited, {
take: CATCHUP_BATCH_SIZE,
where: {
openChannel: IsNull()
}
})

let updatedOpenChannels: ChannelOpenInit[] = [];
let updatedOpenChannelFees: OpenChannelFeeDeposited[] = [];

for (let openChannelFee of openChannelFees) {
let openChannel = await ctx.store.findOne(ChannelOpenInit,
{
where: {
chainId: openChannelFee.chainId,
counterpartyPortId: openChannelFee.counterpartyPortId,
portAddress: openChannelFee.sourceAddress,
}
});
if (openChannel) {
openChannel.feesDeposited = [openChannelFee];
openChannelFee.openChannel = openChannel;

updatedOpenChannels.push(openChannel);
updatedOpenChannelFees.push(openChannelFee);
} else {
console.log(`Could not find open channel for open channel fee ${openChannelFee.id}`);
}
}

await ctx.store.upsert(updatedOpenChannels);
await ctx.store.upsert(updatedOpenChannelFees);
}
6 changes: 5 additions & 1 deletion src/model/generated/channelOpenInit.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, IntColumn as IntColumn_, BooleanColumn as BooleanColumn_, BigIntColumn as BigIntColumn_, Index as Index_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, IntColumn as IntColumn_, BooleanColumn as BooleanColumn_, BigIntColumn as BigIntColumn_, Index as Index_, OneToMany as OneToMany_} from "@subsquid/typeorm-store"
import {OpenChannelFeeDeposited} from "./openChannelFeeDeposited.model"

@Entity_()
export class ChannelOpenInit {
Expand Down Expand Up @@ -84,4 +85,7 @@ export class ChannelOpenInit {

@BigIntColumn_({nullable: true})
polymerBlockNumber!: bigint | undefined | null

@OneToMany_(() => OpenChannelFeeDeposited, e => e.openChannel)
feesDeposited!: OpenChannelFeeDeposited[]
}
7 changes: 6 additions & 1 deletion src/model/generated/openChannelFeeDeposited.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, IntColumn as IntColumn_, BigIntColumn as BigIntColumn_} from "@subsquid/typeorm-store"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, StringColumn as StringColumn_, Index as Index_, IntColumn as IntColumn_, BigIntColumn as BigIntColumn_, ManyToOne as ManyToOne_} from "@subsquid/typeorm-store"
import {ChannelOpenInit} from "./channelOpenInit.model"

@Entity_()
export class OpenChannelFeeDeposited {
Expand Down Expand Up @@ -46,4 +47,8 @@ export class OpenChannelFeeDeposited {
@Index_()
@StringColumn_({nullable: false})
from!: string

@Index_()
@ManyToOne_(() => ChannelOpenInit, {nullable: true})
openChannel!: ChannelOpenInit | undefined | null
}

0 comments on commit 4b3731e

Please sign in to comment.