-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Inkvi
committed
Apr 11, 2024
1 parent
4fca302
commit b51e0dd
Showing
4 changed files
with
187 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { Virtual } from "@ponder/core"; | ||
import { config, schema } from "@/generated"; | ||
import { Prettify } from "viem/types/utils"; | ||
import { Infer } from "@ponder/core/src/schema/types"; | ||
import logger from "./logger"; | ||
|
||
async function updateInitToTryTime<name extends Virtual.EventNames<config>>(context: Virtual.Context<config, schema, name>, channel: Prettify<Infer<schema>["Channel"]>) { | ||
if (channel.state == "INIT" && !channel.initToTryTime) { | ||
const openInitChannel = await context.db.OpenIbcChannel.findUnique({id: channel.openInitChannelId}); | ||
if (!openInitChannel) { | ||
throw new Error(`No openInitChannel found for channel with state INIT and id: ${channel.id}`); | ||
} | ||
|
||
const openTryChannel = await context.db.OpenIbcChannel.findMany({ | ||
where: { | ||
portId: openInitChannel.counterpartyPortId, | ||
blockTimestamp: {gt: openInitChannel.blockTimestamp} | ||
}, | ||
orderBy: {blockTimestamp: "asc"}, | ||
limit: 1 | ||
}); | ||
|
||
if (openTryChannel.items.length > 1) { | ||
throw new Error(`Multiple openTryChannels found for openInitChannelId: ${channel.openInitChannelId}`); | ||
} | ||
|
||
if (openTryChannel.items.length == 1) { | ||
let initToTryTime = openTryChannel.items[0]!.blockTimestamp - openInitChannel.blockTimestamp; | ||
await context.db.Channel.update({ | ||
id: channel.id, | ||
data: { | ||
initToTryTime: Number(initToTryTime), | ||
openTryChannelId: openTryChannel.items[0]!.id, | ||
}, | ||
}); | ||
const cpChannel = await context.db.Channel.findMany({ | ||
where: { | ||
openTryChannelId: openTryChannel.items[0]!.id, | ||
state: "TRY" | ||
} | ||
}); | ||
|
||
if (cpChannel.items.length == 0) { | ||
throw new Error(`No counterparty channel found for openTryChannelId: ${openTryChannel.items[0]!.id}`); | ||
} | ||
|
||
if (cpChannel.items.length > 1) { | ||
throw new Error(`Multiple counterparty channels found for openTryChannelId: ${openTryChannel.items[0]!.id}`); | ||
} | ||
|
||
await context.db.Channel.update({ | ||
id: cpChannel.items[0]!.id, | ||
data: { | ||
initToTryTime: Number(initToTryTime), | ||
openInitChannelId: channel.id, | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
if (channel.state == "TRY" && !channel.initToTryTime) { | ||
const openTryChannel = await context.db.OpenIbcChannel.findUnique({id: channel.openTryChannelId}); | ||
const openInitChannel = await context.db.OpenIbcChannel.findMany({ | ||
where: { | ||
portId: openTryChannel?.counterpartyPortId, | ||
blockTimestamp: {lt: openTryChannel!.blockTimestamp} | ||
}, | ||
orderBy: {blockTimestamp: "desc"}, | ||
limit: 1 | ||
}); | ||
|
||
if (openInitChannel.items.length > 1) { | ||
throw new Error(`Multiple openInitChannels found for openTryChannelId: ${channel.openTryChannelId}`); | ||
} | ||
|
||
if (openInitChannel.items.length == 1) { | ||
let initToTryTime = openTryChannel!.blockTimestamp - openInitChannel.items[0]!.blockTimestamp; | ||
await context.db.Channel.update({ | ||
id: channel.id, | ||
data: { | ||
initToTryTime: Number(initToTryTime), | ||
openInitChannelId: openInitChannel.items[0]!.id | ||
}, | ||
}); | ||
const cpChannel = await context.db.Channel.findMany({ | ||
where: { | ||
openInitChannelId: openInitChannel.items[0]!.id, | ||
state: "INIT" | ||
} | ||
}); | ||
|
||
if (cpChannel.items.length == 0) { | ||
logger.error(`No counterparty channel found for openInitChannelId: ${openInitChannel.items[0]!.id}`); | ||
return; | ||
} | ||
|
||
if (cpChannel.items.length > 1) { | ||
logger.error(`Multiple counterparty channels found for openInitChannelId: ${openInitChannel.items[0]!.id}`); | ||
return; | ||
} | ||
|
||
await context.db.Channel.update({ | ||
id: cpChannel.items[0]!.id, | ||
data: { | ||
initToTryTime: Number(initToTryTime), | ||
openTryChannelId: channel.id | ||
}, | ||
}); | ||
} | ||
} | ||
|
||
} | ||
|
||
export async function updateChannel<name extends Virtual.EventNames<config>>(context: Virtual.Context<config, schema, name>, id: string) { | ||
let channel = await context.db.Channel.findUnique({id}) | ||
if (!channel) { | ||
console.warn('No channel found with id', id) | ||
return; | ||
} | ||
|
||
await updateInitToTryTime(context, channel) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters