diff --git a/src/database/index.ts b/src/database/index.ts index 6b454629..e2e5ebbb 100644 --- a/src/database/index.ts +++ b/src/database/index.ts @@ -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"; @@ -39,6 +41,8 @@ interface Tables { legacyProjects: LegacyProjectTable; applicationsPayouts: ApplicationPayout; ipfsData: IpfsDataTable; + attestations: AttestationTable; + attestationTxns: AttestationTxnTable; } type KyselyDb = Kysely; @@ -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; } diff --git a/src/database/migrate.ts b/src/database/migrate.ts index 764cea47..af6a0ebd 100644 --- a/src/database/migrate.ts +++ b/src/database/migrate.ts @@ -275,15 +275,17 @@ export async function migrate(db: Kysely, 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(); @@ -308,7 +310,6 @@ export async function migrate(db: Kysely, schemaName: string) { await schema .createTable("attestations") - .addColumn("id", "serial", (col) => col.primaryKey()) .addColumn("uid", "text") .addColumn("chainId", CHAIN_ID_TYPE) .addColumn("fee", BIGINT_TYPE) @@ -316,28 +317,30 @@ export async function migrate(db: Kysely, schemaName: string) { .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(); diff --git a/src/database/schema.ts b/src/database/schema.ts index 435ea1fa..6abbc3cd 100644 --- a/src/database/schema.ts +++ b/src/database/schema.ts @@ -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[]; };