Skip to content

Commit

Permalink
Merge pull request #585 from gitcoinco/feature/latestMaciContract
Browse files Browse the repository at this point in the history
VoteOptionIndex event handling
  • Loading branch information
codenamejason committed Jun 20, 2024
2 parents 90ef948 + f0aa5a4 commit 0e35834
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/database/changeset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
NewApplicationPayout,
NewContribution,
NewMessage,
NewVoteOptionIndex,
} from "./schema.js";

export type DataChange =
Expand Down Expand Up @@ -144,4 +145,8 @@ export type DataChange =
| {
type: "InsertMessage";
message: NewMessage;
}
| {
type: "InsertVoteOptionIndex";
voteOptionIndex: NewVoteOptionIndex;
};
10 changes: 10 additions & 0 deletions src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ApplicationPayout,
ContributionTable,
MessageTable,
VoteOptionIndexTable,
} from "./schema.js";
import { migrate } from "./migrate.js";
import { encodeJsonWithBigInts } from "../utils/index.js";
Expand All @@ -41,6 +42,7 @@ interface Tables {
applicationsPayouts: ApplicationPayout;
contributions: ContributionTable;
messages: MessageTable;
votingIndexOptions: VoteOptionIndexTable;
}

type KyselyDb = Kysely<Tables>;
Expand Down Expand Up @@ -521,6 +523,14 @@ export class Database {
break;
}

case "InsertVoteOptionIndex": {
await this.#db
.insertInto("votingIndexOptions")
.values(change.voteOptionIndex)
.execute();
break;
}

default:
throw new Error(`Unknown changeset type`);
}
Expand Down
16 changes: 16 additions & 0 deletions src/database/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,22 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
)
.execute();

await schema
.createTable("votingIndexOptions")
.addColumn("id", "text") // Unique identifier for each vote option index
.addColumn("chainId", CHAIN_ID_TYPE) // Chain ID
.addColumn("roundId", ADDRESS_TYPE) // Foreign key to the rounds table
.addColumn("optionIndex", "integer") // Option index number
.addColumn("recipientId", ADDRESS_TYPE) // Recipient ID
.addPrimaryKeyConstraint("vote_option_index_pkey", ["id"])
.addForeignKeyConstraint(
"vote_option_index_round_fkey",
["chainId", "roundId"],
"rounds",
["chainId", "id"]
)
.execute();

// https://www.graphile.org/postgraphile/smart-tags/
// https://www.graphile.org/postgraphile/computed-columns/
await sql`
Expand Down
11 changes: 11 additions & 0 deletions src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,14 @@ export type MACIMessage = {
msgType: string;
data: string[];
};

export type VoteOptionIndexTable = {
id: string;
chainId: ChainId;
roundId: Address | string;
optionIndex: number;
recipientId: Hex;
};

export type VoteOptionIndex = Selectable<VoteOptionIndexTable>;
export type NewVoteOptionIndex = Insertable<VoteOptionIndexTable>;
44 changes: 44 additions & 0 deletions src/indexer/allo/v2/handleEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,50 @@ export async function handleEvent(
}
}

case "RecipientVotingOptionAdded": {
const strategyAddress = parseAddress(event.address);
const round = await db.getRoundByStrategyAddress(
chainId,
strategyAddress
);

if (round === null) {
return [];
}
const recipientId = parseAddress(event.params.recipientId);
const optionIndex = Number(event.params.recipientIndex);
const id = ethers.utils.solidityKeccak256(
["string"],
[`${event.blockNumber}-${round.id}-${chainId}-${recipientId}`]
);

switch (round.strategyName) {
case "allov2.MACIQF": {
return [
{
type: "InsertVoteOptionIndex",
voteOptionIndex: {
chainId,
id,
roundId: round.id,
recipientId,
optionIndex,
},
},
];
}

default: {
logger.warn({
msg: `Unsupported strategy ${round.strategyName}`,
event,
});

return [];
}
}
}

case "SignUp": {
const maciAddress = parseAddress(event.address);

Expand Down
2 changes: 1 addition & 1 deletion src/indexer/allo/v2/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function extractStrategyFromId(_id: string): Strategy | null {
const id = _id.toLowerCase();
/* eslint-disable no-fallthrough */
switch (id) {
case "0x02ce039501668fadbe8b9ef4030e619cf4eefbc3d70415b61ebdfd3c5d467ad2":
case "0x9b167da08fc4d96c2bda5fa376d9c734f55bc41f7b5de98280322597dfb097ed":
return {
id: id,
name: "allov2.MACIQF",
Expand Down

0 comments on commit 0e35834

Please sign in to comment.