Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit

Permalink
fix: factory and pool creation event
Browse files Browse the repository at this point in the history
  • Loading branch information
yvesfracari committed Feb 24, 2025
1 parent a61212a commit 32fa989
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 15 deletions.
2 changes: 1 addition & 1 deletion ponder.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const factoryNewPoolEvent = parseAbiItem(
);
const factoryPoolAddress = "0x669c864169Cf8ba8e7E1cf9a4D4A2C979E456Dc8";

const factoryStartBlock = 22131714;
const factoryStartBlock = 22131713;

export default createConfig({
networks: {
Expand Down
2 changes: 2 additions & 0 deletions ponder.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ export const factory = onchainTable("factory", (t) => ({
mintFee: t.bigint().notNull(),
redeemFee: t.bigint().notNull(),
offPegFeeMultiplier: t.bigint().notNull(),
owner: t.hex().notNull(),
A: t.bigint().notNull(),
creationTransactionId: t.text().notNull(),
}));

export const pool = onchainTable("pool", (t) => ({
id: t.text().primaryKey(),
chainId: t.integer().notNull(),
address: t.hex().notNull(),
factoryId: t.text().notNull(),
lpTokenId: t.text().notNull(),
wlpTokenId: t.text().notNull(),
Expand Down
70 changes: 58 additions & 12 deletions src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,68 @@ import {
handleFactoryUpdateParameters,
} from "./utils/databaseWriteUtils";
import { readPoolInfo } from "./utils/multicalls";
import { pool } from "ponder:schema";
import { factory, pool } from "ponder:schema";
import { getAddressId, getTransactionId } from "./utils/ids";
import { Address } from "viem";

ponder.on("Factory:AModified", handleFactoryUpdateParameters);
ponder.on("Factory:GovernorModified", handleFactoryUpdateParameters);
ponder.on("Factory:SwapFeeModified", handleFactoryUpdateParameters);
ponder.on("Factory:MintFeeModified", handleFactoryUpdateParameters);
ponder.on("Factory:OffPegFeeMultiplierModified", handleFactoryUpdateParameters);
ponder.on("Factory:RedeemFeeModified", handleFactoryUpdateParameters);
ponder.on("Factory:AModified", async ({ context, event }) => {
await handleFactoryUpdateParameters({
context,
event,
data: event.args,
});
});
ponder.on("Factory:GovernorModified", async ({ context, event }) => {
await handleFactoryUpdateParameters({
context,
event,
data: event.args,
});
});
ponder.on("Factory:SwapFeeModified", async ({ context, event }) => {
await handleFactoryUpdateParameters({
context,
event,
data: event.args,
});
});
ponder.on("Factory:MintFeeModified", async ({ context, event }) => {
await handleFactoryUpdateParameters({
context,
event,
data: event.args,
});
});
ponder.on("Factory:OffPegFeeMultiplierModified", async ({ context, event }) => {
await handleFactoryUpdateParameters({
context,
event,
data: event.args,
});
});
ponder.on("Factory:RedeemFeeModified", async ({ context, event }) => {
await handleFactoryUpdateParameters({
context,
event,
data: event.args,
});
});
ponder.on("Factory:OwnershipTransferred", async ({ context, event }) => {
await handleFactoryUpdateParameters({
context,
event,
data: { owner: event.args.newOwner },
});
});

ponder.on("Factory:PoolCreated", async ({ context, event }) => {
const chainId = context.network.chainId;
const factoryId = getAddressId(chainId, event.log.address);

const [poolInfo, factoryData] = await Promise.all([
readPoolInfo(context, event.args.selfPeggingAsset),
context.db.find(pool, {
id: getAddressId(context.network.chainId, event.log.address),
context.db.find(factory, {
id: factoryId,
}),
]);

Expand All @@ -32,7 +78,6 @@ ponder.on("Factory:PoolCreated", async ({ context, event }) => {
context.network.chainId,
event.args.selfPeggingAsset
);
const chainId = context.network.chainId;

const tokenAId = getAddressId(chainId, poolInfo.tokens[0] as Address);

Expand All @@ -44,10 +89,11 @@ ponder.on("Factory:PoolCreated", async ({ context, event }) => {
createIfNotExistsToken(event.args.poolToken, context),
createIfNotExistsToken(event.args.wrappedPoolToken, context),
context.db.insert(pool).values({
id: getAddressId(chainId, event.args.selfPeggingAsset),
id: poolId,
chainId: chainId,
address: event.args.selfPeggingAsset,
creationTransactionId: getTransactionId(event, context),
factoryId: getAddressId(chainId, event.log.address),
factoryId: factoryId,
lpTokenId: getAddressId(chainId, event.args.poolToken),
wlpTokenId: getAddressId(chainId, event.args.wrappedPoolToken),
mintFee: factoryData.mintFee,
Expand Down
7 changes: 5 additions & 2 deletions src/utils/databaseWriteUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { readErc20Information, readFactoryInfo } from "./multicalls";
export async function handleFactoryUpdateParameters({
context,
event,
data,
}: {
context: Context;
event: Event<
Expand All @@ -16,14 +17,16 @@ export async function handleFactoryUpdateParameters({
| "Factory:OffPegFeeMultiplierModified"
| "Factory:RedeemFeeModified"
| "Factory:GovernorModified"
| "Factory:OwnershipTransferred"
>;
data: Partial<typeof factory.$inferInsert>;
}) {
const factoryId = getAddressId(context.network.chainId, event.log.address);

const factoryDb = await context.db.find(factory, { id: factoryId });

if (factoryDb) {
await context.db.update(factory, { id: factoryId }).set(event.args);
await context.db.update(factory, { id: factoryId }).set(data);
return;
}

Expand All @@ -43,7 +46,7 @@ export async function handleFactoryUpdateParameters({
address: event.log.address,
creationTransactionId,
})
.onConflictDoUpdate(event.args),
.onConflictDoUpdate(data),
createIfNotExistsTransaction(event, context),
]);
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/multicalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ export async function readFactoryInfo(context: Context, factory: Address) {
...factoryContract,
functionName: "A",
},
{
...factoryContract,
functionName: "owner",
},
],
});

Expand All @@ -49,6 +53,7 @@ export async function readFactoryInfo(context: Context, factory: Address) {
redeemFee: res[3].result as bigint,
offPegFeeMultiplier: res[4].result as bigint,
A: res[5].result as bigint,
owner: res[6].result as Address,
};
}

Expand Down

0 comments on commit 32fa989

Please sign in to comment.