Skip to content

Commit

Permalink
add update events for allo v2 registry (#377)
Browse files Browse the repository at this point in the history
* add update events for allo v2 registry

* fix tests

* add project roles on Allo V2

* update owner roles on Allo V2

* manage project roles in Allo V1

* lint fixes
  • Loading branch information
gravityblast authored Jan 11, 2024
1 parent dd90d9e commit 08cfe10
Show file tree
Hide file tree
Showing 9 changed files with 513 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ export type Config = {
estimatesLinearQfWorkerPoolSize: number | null;
};

const CHAIN_DATA_VERSION = "8";
const CHAIN_DATA_VERSION = "9";

export function getConfig(): Config {
const buildTag = z
Expand Down
26 changes: 22 additions & 4 deletions src/database/changeset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { ChainId, Address } from "../types.js";
import {
NewProject,
PartialProject,
NewProjectRole,
ProjectRole,
NewRound,
PartialRound,
NewApplication,
Expand All @@ -17,29 +19,45 @@ export type DataChange =
}
| {
type: "UpdateProject";
chainId: ChainId;
projectId: string;
project: PartialProject;
}
| {
type: "InsertProjectRole";
projectRole: NewProjectRole;
}
| {
type: "DeleteAllProjectRolesByRole";
projectRole: Pick<ProjectRole, "chainId" | "projectId" | "role">;
}
| {
type: "DeleteAllProjectRolesByRoleAndAddress";
projectRole: Pick<
ProjectRole,
"chainId" | "projectId" | "role" | "address"
>;
}
| {
type: "InsertRound";
round: NewRound;
}
| {
type: "UpdateRound";
roundId: Address;
chainId: ChainId;
roundId: Address;
round: PartialRound;
}
| {
type: "IncrementRoundDonationStats";
roundId: Address;
chainId: ChainId;
roundId: Address;
amountInUsd: number;
}
| {
type: "IncrementApplicationDonationStats";
roundId: Address;
chainId: ChainId;
roundId: Address;
applicationId: string;
amountInUsd: number;
}
Expand All @@ -49,8 +67,8 @@ export type DataChange =
}
| {
type: "UpdateApplication";
roundId: Address;
chainId: ChainId;
roundId: Address;
applicationId: string;
application: PartialApplication;
}
Expand Down
33 changes: 32 additions & 1 deletion src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {

import {
ProjectTable,
ProjectRoleTable,
RoundTable,
ApplicationTable,
DonationTable,
Expand All @@ -26,6 +27,7 @@ export type { DataChange as Changeset };

interface Tables {
projects: ProjectTable;
projectRoles: ProjectRoleTable;
rounds: RoundTable;
applications: ApplicationTable;
donations: DonationTable;
Expand Down Expand Up @@ -160,7 +162,7 @@ export class Database {
async createSchemaIfNotExists(logger: Logger) {
const exists = await sql<{ exists: boolean }>`
SELECT EXISTS (
SELECT 1 FROM information_schema.schemata
SELECT 1 FROM information_schema.schemata
WHERE schema_name = ${this.databaseSchemaName}
)`.execute(this.#db);

Expand Down Expand Up @@ -202,6 +204,35 @@ export class Database {
break;
}

case "InsertProjectRole": {
await this.#db
.insertInto("projectRoles")
.values(change.projectRole)
.execute();
break;
}

case "DeleteAllProjectRolesByRole": {
await this.#db
.deleteFrom("projectRoles")
.where("chainId", "=", change.projectRole.chainId)
.where("projectId", "=", change.projectRole.projectId)
.where("role", "=", change.projectRole.role)
.execute();
break;
}

case "DeleteAllProjectRolesByRoleAndAddress": {
await this.#db
.deleteFrom("projectRoles")
.where("chainId", "=", change.projectRole.chainId)
.where("projectId", "=", change.projectRole.projectId)
.where("role", "=", change.projectRole.role)
.where("address", "=", change.projectRole.address)
.execute();
break;
}

case "InsertRound": {
await this.#db.insertInto("rounds").values(change.round).execute();
break;
Expand Down
28 changes: 28 additions & 0 deletions src/database/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
await schema
.createTable("projects")
.addColumn("id", "text")
.addColumn("name", "text")
.addColumn("chainId", CHAIN_ID_TYPE)
.addColumn("projectNumber", "integer")
.addColumn("registryAddress", ADDRESS_TYPE)
Expand All @@ -59,6 +60,33 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
.addPrimaryKeyConstraint("projects_pkey", ["id", "chainId"])
.execute();

await schema
.createType("project_role_name")
.asEnum(["owner", "member"])
.execute();

await schema
.createTable("project_roles")
.addColumn("chainId", CHAIN_ID_TYPE)
.addColumn("project_id", "text")
.addColumn("address", ADDRESS_TYPE)
.addColumn("role", ref("project_role_name"))
.addColumn("createdAtBlock", BIGINT_TYPE)
.addPrimaryKeyConstraint("project_roles_pkey", [
"chainId",
"project_id",
"address",
"role",
])

.execute();

await schema
.createIndex("project_roles_unique_index")
.on("project_roles")
.columns(["chain_id", "project_id", "address", "role"])
.execute();

await schema
.createType("application_status")
.asEnum(["PENDING", "APPROVED", "REJECTED", "CANCELLED", "IN_REVIEW"])
Expand Down
15 changes: 15 additions & 0 deletions src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export type PartialRound = Updateable<RoundTable>;

export type ProjectTable = {
id: string;
name: string;
chainId: ChainId;
projectNumber: number;
registryAddress: Address;
Expand All @@ -50,6 +51,20 @@ export type Project = Selectable<ProjectTable>;
export type NewProject = Insertable<ProjectTable>;
export type PartialProject = Updateable<ProjectTable>;

export type ProjectRoleNames = "owner" | "member";

export type ProjectRoleTable = {
chainId: ChainId;
projectId: string;
address: Address;
role: ProjectRoleNames;
createdAtBlock: bigint;
};

export type ProjectRole = Selectable<ProjectRoleTable>;
export type NewProjectRole = Insertable<ProjectRoleTable>;
export type PartialProjectRole = Updateable<ProjectRoleTable>;

export type ApplicationStatus = "PENDING" | "REJECTED" | "APPROVED";

export type StatusSnapshot = {
Expand Down
Loading

0 comments on commit 08cfe10

Please sign in to comment.