Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grants Stack Indexer MACIQF migration #569

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/database/changeset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
NewPrice,
NewLegacyProject,
NewApplicationPayout,
NewContribution,
NewMessage,
} from "./schema.js";

export type DataChange =
Expand Down Expand Up @@ -134,4 +136,12 @@ export type DataChange =
| {
type: "InsertApplicationPayout";
payout: NewApplicationPayout;
}
| {
type: "InsertContribution";
contribution: NewContribution;
}
| {
type: "InsertMessage";
message: NewMessage;
};
31 changes: 31 additions & 0 deletions src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import {
NewDonation,
LegacyProjectTable,
ApplicationPayout,
ContributionTable,
MessageTable,
} from "./schema.js";
import { migrate } from "./migrate.js";
import { encodeJsonWithBigInts } from "../utils/index.js";
Expand All @@ -37,6 +39,8 @@ interface Tables {
prices: PriceTable;
legacyProjects: LegacyProjectTable;
applicationsPayouts: ApplicationPayout;
contributions: ContributionTable;
messages: MessageTable;
}

type KyselyDb = Kysely<Tables>;
Expand Down Expand Up @@ -504,6 +508,19 @@ export class Database {
break;
}

case "InsertContribution": {
await this.#db
.insertInto("contributions")
.values(change.contribution)
.execute();
break;
}

case "InsertMessage": {
await this.#db.insertInto("messages").values(change.message).execute();
break;
}

default:
throw new Error(`Unknown changeset type`);
}
Expand Down Expand Up @@ -627,6 +644,20 @@ export class Database {
return rounds;
}

async getRoundsByStrategyNameAndChainId(
chainId: ChainId,
strategyName: string
) {
const rounds = await this.#db
.selectFrom("rounds")
.where("chainId", "=", chainId)
.where("strategyName", "=", strategyName)
.selectAll()
.execute();

return rounds;
}

async getAllRoundApplications(chainId: ChainId, roundId: string) {
return await this.#db
.selectFrom("applications")
Expand Down
32 changes: 32 additions & 0 deletions src/database/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,38 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
.addUniqueConstraint("unique_v2ProjectId", ["v2ProjectId"])
.execute();

await schema
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we reuse donation and add a data field ?

.createTable("contributions")
.addColumn("id", "text")
.addColumn("roundId", "text")
.addColumn("chainId", CHAIN_ID_TYPE)
.addColumn("maciId", ADDRESS_TYPE)
.addColumn("stateIndex", BIGINT_TYPE)
.addColumn("contributorAddress", ADDRESS_TYPE)
.addColumn("voiceCreditBalance", BIGINT_TYPE)
.addColumn("transactionHash", "text")
.addColumn("timestamp", "timestamptz")
.addPrimaryKeyConstraint("contributions_pkey", ["id"])
.execute();

await schema
.createTable("messages")
.addColumn("messageId", ADDRESS_TYPE) // Unique identifier for each message
.addColumn("contributionId", "text") // Foreign key to contributions table
.addColumn("chainId", CHAIN_ID_TYPE)
.addColumn("pollId", ADDRESS_TYPE)
.addColumn("maciId", ADDRESS_TYPE)
.addColumn("message", "jsonb")
.addColumn("createdByAddress", ADDRESS_TYPE)
.addPrimaryKeyConstraint("messages_pkey", ["messageId"])
.addForeignKeyConstraint(
"messages_contribution_fkey",
["contributionId"],
"contributions",
["id"]
)
.execute();

// https://www.graphile.org/postgraphile/smart-tags/
// https://www.graphile.org/postgraphile/computed-columns/
await sql`
Expand Down
33 changes: 33 additions & 0 deletions src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,3 +252,36 @@ export type ApplicationPayout = {
};

export type NewApplicationPayout = Insertable<ApplicationPayout>;

export type Contribution = Selectable<ContributionTable>;
export type NewContribution = Insertable<ContributionTable>;

export type ContributionTable = {
id: string;
roundId: string;
chainId: ChainId;
maciId: Address | string;
stateIndex: bigint;
contributorAddress: Address;
voiceCreditBalance: bigint;
transactionHash: Hex;
timestamp: Date;
};

export type Message = Selectable<ContributionTable>;
export type NewMessage = Insertable<MessageTable>;

export type MessageTable = {
messageId: string;
contributionId: string;
chainId: ChainId;
pollId: Address | string;
maciId: Address | string;
message: string;
createdByAddress: Address;
};

export type MACIMessage = {
msgType: string;
data: string[];
};
Loading
Loading