Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
thelostone-mc committed Sep 24, 2024
1 parent c120404 commit c6ebcb1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 30 deletions.
23 changes: 17 additions & 6 deletions src/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
LegacyProjectTable,
ApplicationPayout,
IpfsDataTable,
AttestationTable,
AttestationTxnTable,
} from "./schema.js";
import { migrate, migrateDataFetcher, migratePriceFetcher } from "./migrate.js";
import { encodeJsonWithBigInts } from "../utils/index.js";
Expand All @@ -39,6 +41,8 @@ interface Tables {
legacyProjects: LegacyProjectTable;
applicationsPayouts: ApplicationPayout;
ipfsData: IpfsDataTable;
attestations: AttestationTable;
attestationTxns: AttestationTxnTable;
}

type KyselyDb = Kysely<Tables>;
Expand Down Expand Up @@ -623,23 +627,30 @@ export class Database {
.withSchema(this.chainDataSchemaName)
.insertInto("attestations")
.values(change.attestation.attestationData)
.returning(["uid", "chainId"])
.execute();

// Insert into attestation transactions
let transactionsData = change.attestation.transactionsData as any;
const transactionsData = change.attestation.transactionsData;
const attestationTxns: AttestationTxnTable[] = [];
for (let i = 0; i < transactionsData.length; i++) {
// Link transaction to attestation
transactionsData[i].attestationId = attestation[0].id;
attestationTxns.push({
chainId: transactionsData[i].chainId,
txnHash: transactionsData[i].txnHash,
attestationUid: attestation[0].uid,
attestationChainId: attestation[0].chainId,
});
}

await this.#db
.withSchema(this.chainDataSchemaName)
.insertInto("attestationTransactions")
.values(transactionsData)
.insertInto("attestationTxns")
.values(attestationTxns)
.execute();

// Update donations with transactionsData
// Update donations to link attestationTxns
// TODO

break;
}

Expand Down
23 changes: 13 additions & 10 deletions src/database/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,15 +275,17 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {
.addColumn("amountInUSD", "real")
.addColumn("amountInRoundMatchToken", BIGINT_TYPE)

.addColumn("attestationTxnId", "integer")
// Add the foreign key columns
.addColumn("attestationTxnHash", "integer")
.addColumn("attestationTxnChainId", CHAIN_ID_TYPE)

.addPrimaryKeyConstraint("donations_pkey", ["id"])

.addForeignKeyConstraint(
"donations_attestation_txns_fkey",
["attestationTxnId"],
["attestationTxnHash", "attestationTxnChainId"],
"attestation_txns",
["id"],
["txnHash", "chainId"],
(cb) => cb.onDelete("set null")
)
.execute();
Expand All @@ -308,36 +310,37 @@ export async function migrate<T>(db: Kysely<T>, schemaName: string) {

await schema
.createTable("attestations")
.addColumn("id", "serial", (col) => col.primaryKey())
.addColumn("uid", "text")
.addColumn("chainId", CHAIN_ID_TYPE)
.addColumn("fee", BIGINT_TYPE)
.addColumn("recipient", ADDRESS_TYPE)
.addColumn("refUID", "text")
.addColumn("projectsContributed", BIGINT_TYPE)
.addColumn("roundsContributed", BIGINT_TYPE)
.addColumn("roundsContributed", BIGINT_TYPE)
.addColumn("chainIdsContributed", BIGINT_TYPE)
.addColumn("timestamp", "timestamptz")
.addColumn("metadataCid", "text")
.addColumn("metadata", "jsonb")
.addUniqueConstraint("gitcoin_attestations_pkey", ["uid", "chainId"])
.addUniqueConstraint("unique_uid_chainId", ["uid", "chainId"])
.execute();

await schema
.createTable("attestation_txns")
.addColumn("id", "serial", (col) => col.primaryKey())
.addColumn("txnHash", "text")
.addColumn("chainId", CHAIN_ID_TYPE)

.addColumn("attestationId", "integer")
// Add the foreign key columns
.addColumn("attestationUid", "text")
.addColumn("attestationChainId", CHAIN_ID_TYPE)

// Add Constraints
.addUniqueConstraint("attestation_txns_pkey", ["txnHash", "chainId"])

.addForeignKeyConstraint(
"attestation_txns_attestations_fkey",
["attestationId"],
["attestationUid", "attestationChainId"],
"attestations",
["id"],
["uid", "chainId"],
(cb) => cb.onDelete("cascade")
)
.execute();
Expand Down
38 changes: 24 additions & 14 deletions src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,20 +269,30 @@ export type NewIpfsData = {
data: unknown;
};

// Attestations
export type AttestationTable = {
uid: string;
chainId: ChainId;
fee: bigint;
recipient: Address;
refUID: string;
projectsContributed: bigint;
roundsContributed: bigint;
chainIdsContributed: bigint;
totalUSDAmount: bigint;
timestamp: bigint;
metadataCid: string;
metadata: unknown;
};

export type AttestationTxnTable = {
chainId: ChainId;
txnHash: string;
attestationUid: string;
attestationChainId: ChainId;
};

export type NewAttestationData = {
attestationData: {
uid: string;
chainId: ChainId;
fee: bigint;
recipient: Address;
refUID: string;
projectsContributed: bigint;
roundsContributed: bigint;
chainIdsContributed: bigint;
totalUSDAmount: bigint;
timestamp: bigint;
metadataCid: string;
metadata: unknown;
};
attestationData: AttestationTable;
transactionsData: AttestationTxnData[];
};

0 comments on commit c6ebcb1

Please sign in to comment.