Skip to content

Commit

Permalink
feat: add recipient canceled event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tnrdd committed May 29, 2024
1 parent 00bcd5f commit 2ed51c7
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export type RecipientsTable = {
recipientAddress: string;
anchorAddress: string | null;
superappAddress: string | null;
status: "PENDING" | "REJECTED" | "APPROVED";
status: "PENDING" | "REJECTED" | "APPROVED" | "CANCELED";
metadataCid: string | null;
metadata: unknown;
createdAtBlock: bigint;
Expand Down
4 changes: 2 additions & 2 deletions src/db/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Kysely, sql } from "kysely";

const schemaName = "1";
const schemaName = "2";

async function migrate<T>(db: Kysely<T>) {
const ref = (name: string) => sql.table(`${schemaName}.${name}`);
Expand Down Expand Up @@ -83,7 +83,7 @@ async function migrate<T>(db: Kysely<T>) {

await schema
.createType("status")
.asEnum(["PENDING", "REJECTED", "APPROVED"])
.asEnum(["PENDING", "REJECTED", "APPROVED", "CANCELED"])
.execute();

await schema
Expand Down
39 changes: 39 additions & 0 deletions src/indexer/events/handleCanceled.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { EventHandlerArgs, Indexer } from "chainsauce";
import { IndexerContext } from "../handleEvent.js";
import { abis } from "../../lib/abi/index.js";
import { ZERO_ADDRESS } from "../../lib/constants.js";

export async function handleCanceled(
args: EventHandlerArgs<
Indexer<typeof abis, IndexerContext>,
"AlloStrategy",
"Canceled"
>,
) {
const {
event,
chainId,
context: { db },
} = args;

const {
params: { recipientId },
address,
} = event;

try {
await db
.updateTable("recipients")
.set({
status: "CANCELED",
superappAddress: ZERO_ADDRESS,
updatedAtBlock: event.blockNumber,
})
.where("chainId", "=", chainId)
.where("strategyAddress", "=", address.toLowerCase())
.where("id", "=", recipientId.toLowerCase())
.execute();
} catch (err) {
console.warn("DB write error");
}
}
27 changes: 13 additions & 14 deletions src/indexer/events/handlePoolCreated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,18 @@ import { getPendingPoolRoles } from "../../db/index.js";
import { IndexerContext } from "../handleEvent.js";
import { fetchIpfs } from "../ipfs.js";
import { abis } from "../../lib/abi/index.js";
import { ALLO_STRATEGY_ID } from "../../lib/constants.js";

const NULL_BYTES =
"0x0000000000000000000000000000000000000000000000000000000000000000";
const ZERO_ADDRESS =
"0x0000000000000000000000000000000000000000";
import {
ALLO_STRATEGY_ID,
NULL_BYTES,
ZERO_ADDRESS,
} from "../../lib/constants.js";

export async function handlePoolCreated(
args: EventHandlerArgs<
Indexer<typeof abis, IndexerContext>,
"Allo" | "PoolFactory",
"PoolCreated"
>
>,
) {
if (args.event.contractName === "Allo") {
const {
Expand Down Expand Up @@ -57,7 +56,7 @@ export async function handlePoolCreated(
const strategyName = "SQFSuperfluidv1";
const managerRole = pad(`0x${poolId.toString(16)}`);
const adminRole = keccak256(
encodePacked(["uint256", "string"], [poolId, "admin"])
encodePacked(["uint256", "string"], [poolId, "admin"]),
);
const metadata = await fetchIpfs(metadataCid);

Expand Down Expand Up @@ -96,7 +95,7 @@ export async function handlePoolCreated(
role: "admin",
createdAtBlock: event.blockNumber,
};
})
}),
)
.execute();

Expand All @@ -105,14 +104,14 @@ export async function handlePoolCreated(
.where(
"id",
"in",
pendingAdminRoles.map((role) => role.id)
pendingAdminRoles.map((role) => role.id),
)
.execute();
}

const pendingManagerRoles = await getPendingPoolRoles(
chainId,
managerRole
managerRole,
);

if (pendingManagerRoles.length > 0) {
Expand All @@ -127,7 +126,7 @@ export async function handlePoolCreated(
role: "manager",
createdAtBlock: event.blockNumber,
};
})
}),
)
.execute();

Expand All @@ -136,7 +135,7 @@ export async function handlePoolCreated(
.where(
"id",
"in",
pendingManagerRoles.map((role) => role.id)
pendingManagerRoles.map((role) => role.id),
)
.execute();
}
Expand Down Expand Up @@ -173,7 +172,7 @@ export async function handlePoolCreated(
const strategyName = "StreamingQuadraticFunding";
const managerRole = pad(`0x${poolId.toString(16)}`);
const adminRole = keccak256(
encodePacked(["uint256", "string"], [poolId, "admin"])
encodePacked(["uint256", "string"], [poolId, "admin"]),
);
const metadata = await fetchIpfs(metadataCid);

Expand Down
2 changes: 1 addition & 1 deletion src/indexer/events/handleRegistered.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function handleRegistered(
Indexer<typeof abis, IndexerContext>,
"AlloStrategy" | "StreamingQuadraticFunding",
"Registered"
>
>,
) {
if (args.event.contractName === "AlloStrategy") {
const {
Expand Down
7 changes: 2 additions & 5 deletions src/indexer/events/handleUpdatedRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function handleUpdatedRegistration(
Indexer<typeof abis, IndexerContext>,
"AlloStrategy" | "StreamingQuadraticFunding",
"UpdatedRegistration"
>
>,
) {
if (args.event.contractName === "AlloStrategy") {
const {
Expand Down Expand Up @@ -63,10 +63,7 @@ export async function handleUpdatedRegistration(
"UpdatedRegistration"
>;

const {
params,
address,
} = event;
const { params, address } = event;

const {
recipientAddress,
Expand Down
1 change: 1 addition & 0 deletions src/indexer/events/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { handlePoolCreated } from "./handlePoolCreated.js";
export { handleRegistered } from "./handleRegistered.js";
export { handleUpdatedRegistration } from "./handleUpdatedRegistration.js";
export { handleReviewed } from "./handleReviewed.js";
export { handleCanceled } from "./handleCanceled.js";
export { handleProfileMetadataUpdated } from "./handleProfileMetadataUpdated.js";
export { handleProfileNameUpdated } from "./handleProfileNameUpdated.js";
export { handleProfileOwnerUpdated } from "./handleProfileOwnerUpdated.js";
Expand Down
7 changes: 7 additions & 0 deletions src/indexer/handleEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
handleRegistered,
handleUpdatedRegistration,
handleReviewed,
handleCanceled,
handleProfileMetadataUpdated,
handleProfileNameUpdated,
handleProfileOwnerUpdated,
Expand Down Expand Up @@ -58,6 +59,12 @@ async function handleEvent(
break;
}

case "Canceled": {
await handleCanceled({ ...args, event });

break;
}

case "ProfileMetadataUpdated": {
await handleProfileMetadataUpdated({ ...args, event });

Expand Down
3 changes: 3 additions & 0 deletions src/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ export const ALLO_STRATEGY_ID =
"0xf8a14294e80ff012e54157ec9d1b2827421f1e7f6bde38c06730b1c031b3f935";
export const ALLO_OWNER_ROLE =
"0x815b5a78dc333d344c7df9da23c04dbd432015cc701876ddb9ffe850e6882747";
export const NULL_BYTES =
"0x0000000000000000000000000000000000000000000000000000000000000000";
export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
export const IPFS_GATEWAYS = [
"https://gateway.pinata.cloud",
"https://storry.tv",
Expand Down

0 comments on commit 2ed51c7

Please sign in to comment.