Skip to content

Commit

Permalink
feat: add handlers for profile events
Browse files Browse the repository at this point in the history
  • Loading branch information
tnrdd committed May 9, 2024
1 parent ceb17cd commit 0835d49
Show file tree
Hide file tree
Showing 20 changed files with 2,963 additions and 176 deletions.
1,374 changes: 1,374 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"@graphile-contrib/pg-simplify-inflector": "^6.1.0",
"@helia/verified-fetch": "^1.3.14",
"chainsauce": "github:boudra/chainsauce#main",
"cors": "^2.8.5",
"dotenv": "^16.4.5",
Expand Down
168 changes: 143 additions & 25 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,62 @@ import {
} from "kysely";
import { schemaName, migrate } from "./migrate.js";

type Database = {
export type Database = {
profiles: ProfilesTable;
pools: PoolsTable;
recipients: RecipientsTable;
pendingProfileRoles: PendingProfileRolesTable;
profileRoles: ProfileRolesTable;
pendingPoolRoles: PendingPoolRolesTable;
poolRoles: PoolRolesTable;
};

type PoolsTable = {
export type ProfilesTable = {
id: string;
chainId: number;
name: string;
anchorAddress: string;
metadataCid: string;
metadata: unknown;
createdAtBlock: bigint;
updatedAtBlock: bigint;
tags: string[];
};

export type PendingProfileRolesTable = {
id?: number;
chainId: number;
role: string;
address: string;
createdAtBlock: bigint;
};

export type ProfileRolesTable = {
chainId: number;
profileId: string;
address: string;
role: "owner" | "member";
createdAtBlock: bigint;
};

export type PoolsTable = {
id: string;
chainId: number;
token: string;
metadataCid: string;
metadata: unknown;
createdAtBlock: bigint;
updatedAtBlock: bigint;
managerRole: string;
adminRole: string;
strategyAddress: string;
strategyId: string;
strategyName: string;
projectId: string;
managers: string[];
profileId: string;
tags: string[];
};

type RecipientsTable = {
export type RecipientsTable = {
id: string;
chainId: number;
poolId: string;
Expand All @@ -44,12 +79,49 @@ type RecipientsTable = {
tags: string[];
};

type Pool = Selectable<PoolsTable>;
type PoolNew = Insertable<PoolsTable>;
type PoolUpdate = Updateable<PoolsTable>;
type Recipient = Selectable<RecipientsTable>;
type RecipientNew = Insertable<RecipientsTable>;
type RecipientUpdate = Updateable<RecipientsTable>;
export type PendingPoolRolesTable = {
id?: number;
chainId: number;
role: string;
address: string;
createdAtBlock: bigint;
};

export type PoolRolesTable = {
chainId: number;
poolId: string;
address: string;
role: "admin" | "manager";
createdAtBlock: bigint;
};

export type Profile = Selectable<ProfilesTable>;
export type ProfileNew = Insertable<ProfilesTable>;
export type ProfileUpdate = Updateable<ProfilesTable>;

export type Pool = Selectable<PoolsTable>;
export type PoolNew = Insertable<PoolsTable>;
export type PoolUpdate = Updateable<PoolsTable>;

export type Recipient = Selectable<RecipientsTable>;
export type RecipientNew = Insertable<RecipientsTable>;
export type RecipientUpdate = Updateable<RecipientsTable>;

export type ProfileRole = Selectable<ProfileRolesTable>;
export type NewProfileRole = Insertable<ProfileRolesTable>;
export type ProfileRoleUpdate = Updateable<ProfileRolesTable>;

export type PendingProfileRole = Selectable<PendingProfileRolesTable>;
export type NewPendingProfileRole = Insertable<PendingProfileRolesTable>;
export type PendingProfileRoleUpdate = Updateable<PendingProfileRolesTable>;

export type PendingPoolRole = Selectable<PendingPoolRolesTable>;
export type NewPendingPoolRole = Insertable<PendingPoolRolesTable>;
export type UpdatePendingPoolRole = Updateable<PendingPoolRolesTable>;

export type PoolRole = Selectable<PoolRolesTable>;
export type NewPoolRole = Insertable<PoolRolesTable>;
export type UpdatePoolRole = Updateable<PoolRolesTable>;

const pool = new pg.Pool({
connectionString: process.env.DATABASE_URI,
Expand All @@ -59,12 +131,12 @@ const dialect = new PostgresDialect({
pool,
});

const db = new Kysely<Database>({
export const db = new Kysely<Database>({
dialect,
plugins: [new CamelCasePlugin()],
}).withSchema(schemaName);

async function createSchemaIfNotExists() {
export async function createSchemaIfNotExists() {
const exists = await sql<{ exists: boolean }>`
SELECT EXISTS (
SELECT 1 FROM information_schema.schemata
Expand All @@ -85,15 +157,61 @@ async function createSchemaIfNotExists() {
});
}

export {
db,
createSchemaIfNotExists,
Database,
PoolsTable,
Pool,
PoolNew,
PoolUpdate,
Recipient,
RecipientNew,
RecipientUpdate,
};
export async function getProfile(chainId: number, id: string) {
const profile = await db
.selectFrom("profiles")
.where("chainId", "=", chainId)
.where("id", "=", id)
.selectAll()
.executeTakeFirst();

return profile ?? null;
}

export async function getPool(chainId: number, strategyAddress: string) {
const pool = await db
.selectFrom("pools")
.select("id")
.where("chainId", "=", chainId)
.where("strategyAddress", "=", strategyAddress)
.executeTakeFirst();

return pool ?? null;
}

export async function getPoolByRole(
chainId: number,
roleName: "admin" | "manager",
role: string,
) {
const pool = await db
.selectFrom("pools")
.where("chainId", "=", chainId)
.where(`${roleName}Role`, "=", role)
.selectAll()
.executeTakeFirst();

return pool ?? null;
}

export async function getPendingProfileRoles(chainId: number, role: string) {
const pendingProfileRoles = await db
.selectFrom("pendingProfileRoles")
.where("chainId", "=", chainId)
.where("role", "=", role)
.selectAll()
.execute();

return pendingProfileRoles ?? null;
}

export async function getPendingPoolRoles(chainId: number, role: string) {
const pendingPoolRoles = await db
.selectFrom("pendingPoolRoles")
.where("chainId", "=", chainId)
.where("role", "=", role)
.selectAll()
.execute();

return pendingPoolRoles ?? null;
}
108 changes: 106 additions & 2 deletions src/db/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,78 @@ async function migrate<T>(db: Kysely<T>) {

const schema = db.withSchema(schemaName).schema;

await schema
.createTable("profiles")
.addColumn("id", "text")
.addColumn("chainId", "integer")
.addColumn("name", "text")
.addColumn("anchorAddress", "text")
.addColumn("metadataCid", "text")
.addColumn("metadata", "jsonb")
.addColumn("createdAtBlock", "bigint")
.addColumn("updatedAtBlock", "bigint")
.addColumn("tags", sql`text[]`)
.addPrimaryKeyConstraint("profiles_pkey", ["id", "chainId"])
.execute();

await schema
.createTable("pending_profile_roles")
.addColumn("id", "serial", (col) => col.primaryKey())
.addColumn("chainId", "integer")
.addColumn("role", "text")
.addColumn("address", "text")
.addColumn("createdAtBlock", "bigint")
.execute();

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

await schema
.createTable("profile_roles")
.addColumn("chainId", "integer")
.addColumn("profileId", "text")
.addColumn("address", "text")
.addColumn("role", ref("profile_role_name"))
.addColumn("createdAtBlock", "bigint")
.addPrimaryKeyConstraint("profile_roles_pkey", [
"chainId",
"profileId",
"address",
"role",
])
.addForeignKeyConstraint(
"profile_roles_profiles_fkey",
["chainId", "profileId"],
"profiles",
["chainId", "id"],
)
.execute();

await schema
.createTable("pools")
.addColumn("id", "text")
.addColumn("chainId", "integer")
.addColumn("token", "text")
.addColumn("tags", sql`text[]`)
.addColumn("metadataCid", "text")
.addColumn("metadata", "jsonb")
.addColumn("managerRole", "text")
.addColumn("adminRole", "text")
.addColumn("createdAtBlock", "bigint")
.addColumn("updatedAtBlock", "bigint")
.addColumn("strategyAddress", "text")
.addColumn("strategyId", "text")
.addColumn("strategyName", "text")
.addColumn("projectId", "text")
.addColumn("managers", sql`text[]`)
.addColumn("profileId", "text")
.addPrimaryKeyConstraint("pools_pkey", ["id", "chainId"])
.addForeignKeyConstraint(
"pools_profiles_fkey",
["chainId", "profileId"],
"profiles",
["chainId", "id"],
)
.execute();

await schema
Expand Down Expand Up @@ -51,6 +108,53 @@ async function migrate<T>(db: Kysely<T>) {
(cb) => cb.onDelete("cascade"),
)
.execute();

await schema
.createIndex("idx_pools_manager_role")
.on("pools")
.columns(["managerRole"])
.execute();

await schema
.createIndex("idx_pools_admin_role")
.on("pools")
.columns(["adminRole"])
.execute();

await schema
.createTable("pending_pool_roles")
.addColumn("id", "serial", (col) => col.primaryKey())
.addColumn("chainId", "integer")
.addColumn("role", "text")
.addColumn("address", "text")
.addColumn("createdAtBlock", "bigint")
.execute();

await schema
.createType("pool_role_name")
.asEnum(["admin", "manager"])
.execute();

await schema
.createTable("pool_roles")
.addColumn("chainId", "integer")
.addColumn("poolId", "text")
.addColumn("address", "text")
.addColumn("role", ref("pool_role_name"))
.addColumn("createdAtBlock", "bigint")
.addPrimaryKeyConstraint("pool_roles_pkey", [
"chainId",
"poolId",
"address",
"role",
])
.addForeignKeyConstraint(
"pool_roles_pools_fkey",
["chainId", "poolId"],
"pools",
["chainId", "id"],
)
.execute();
}

export { schemaName, migrate };
Loading

0 comments on commit 0835d49

Please sign in to comment.