Skip to content

Commit

Permalink
feat: application payout repository
Browse files Browse the repository at this point in the history
  • Loading branch information
0xnigir1 committed Nov 14, 2024
1 parent 0d4872b commit 8eee37b
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 4 deletions.
6 changes: 6 additions & 0 deletions apps/processing/src/services/sharedDependencies.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { IpfsProvider } from "@grants-stack-indexer/metadata";
import { PricingProviderFactory } from "@grants-stack-indexer/pricing";
import {
createKyselyDatabase,
KyselyApplicationPayoutRepository,
KyselyApplicationRepository,
KyselyDonationRepository,
KyselyProjectRepository,
Expand Down Expand Up @@ -50,6 +51,10 @@ export class SharedDependenciesService {
kyselyDatabase,
env.DATABASE_SCHEMA,
);
const applicationPayoutRepository = new KyselyApplicationPayoutRepository(
kyselyDatabase,
env.DATABASE_SCHEMA,
);
const pricingProvider = PricingProviderFactory.create(env, { logger });

const metadataProvider = new IpfsProvider(env.IPFS_GATEWAYS_URL, logger);
Expand All @@ -72,6 +77,7 @@ export class SharedDependenciesService {
pricingProvider,
donationRepository,
metadataProvider,
applicationPayoutRepository,
},
registries: {
eventsRegistry,
Expand Down
4 changes: 4 additions & 0 deletions packages/data-flow/src/data-loader/dataLoader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Changeset,
IApplicationPayoutRepository,
IApplicationRepository,
IDonationRepository,
IProjectRepository,
Expand All @@ -10,6 +11,7 @@ import { ILogger, stringify } from "@grants-stack-indexer/shared";
import { ExecutionResult, IDataLoader, InvalidChangeset } from "../internal.js";
import {
createApplicationHandlers,
createApplicationPayoutHandlers,
createDonationHandlers,
createProjectHandlers,
createRoundHandlers,
Expand Down Expand Up @@ -38,6 +40,7 @@ export class DataLoader implements IDataLoader {
round: IRoundRepository;
application: IApplicationRepository;
donation: IDonationRepository;
applicationPayout: IApplicationPayoutRepository;
},
private readonly logger: ILogger,
) {
Expand All @@ -46,6 +49,7 @@ export class DataLoader implements IDataLoader {
...createRoundHandlers(repositories.round),
...createApplicationHandlers(repositories.application),
...createDonationHandlers(repositories.donation),
...createApplicationPayoutHandlers(repositories.applicationPayout),
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
ApplicationPayoutChangeset,
IApplicationPayoutRepository,
} from "@grants-stack-indexer/repository";

import { ChangesetHandler } from "../types/index.js";

/**
* Collection of handlers for application-related operations.
* Each handler corresponds to a specific Application changeset type.
*/
export type ApplicationPayoutHandlers = {
[K in ApplicationPayoutChangeset["type"]]: ChangesetHandler<K>;
};

/**
* Creates handlers for managing application-related operations.
*
* @param repository - The application repository instance used for database operations
* @returns An object containing all application-related handlers
*/
export const createApplicationPayoutHandlers = (
repository: IApplicationPayoutRepository,
): ApplicationPayoutHandlers => ({
InsertApplicationPayout: (async (changeset): Promise<void> => {
await repository.insertApplicationPayout(changeset.args.applicationPayout);
}) satisfies ChangesetHandler<"InsertApplicationPayout">,
});
1 change: 1 addition & 0 deletions packages/data-flow/src/data-loader/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./application.handlers.js";
export * from "./project.handlers.js";
export * from "./round.handlers.js";
export * from "./donation.handlers.js";
export * from "./applicationPayout.handlers.js";
1 change: 1 addition & 0 deletions packages/data-flow/src/orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class Orchestrator {
round: this.dependencies.roundRepository,
application: this.dependencies.applicationRepository,
donation: this.dependencies.donationRepository,
applicationPayout: this.dependencies.applicationPayoutRepository,
},
this.logger,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/data-flow/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ProcessorDependencies } from "@grants-stack-indexer/processors";
import {
Changeset,
IApplicationPayoutRepository,
IApplicationRepository,
IDonationRepository,
IProjectRepository,
Expand Down Expand Up @@ -33,4 +34,5 @@ export type CoreDependencies = Pick<
projectRepository: IProjectRepository;
applicationRepository: IApplicationRepository;
donationRepository: IDonationRepository;
applicationPayoutRepository: IApplicationPayoutRepository;
};
6 changes: 6 additions & 0 deletions packages/data-flow/test/data-loader/dataLoader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";

import {
Changeset,
IApplicationPayoutRepository,
IApplicationRepository,
IDonationRepository,
IProjectRepository,
Expand Down Expand Up @@ -33,6 +34,10 @@ describe("DataLoader", () => {
insertManyDonations: vi.fn(),
} as IDonationRepository;

const mockApplicationPayoutRepository = {
insertApplicationPayout: vi.fn(),
} as IApplicationPayoutRepository;

const logger: ILogger = {
debug: vi.fn(),
error: vi.fn(),
Expand All @@ -46,6 +51,7 @@ describe("DataLoader", () => {
round: mockRoundRepository,
application: mockApplicationRepository,
donation: mockDonationRepository,
applicationPayout: mockApplicationPayoutRepository,
},
logger,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/data-flow/test/unit/orchestrator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { IIndexerClient } from "@grants-stack-indexer/indexer-client";
import { UnsupportedStrategy } from "@grants-stack-indexer/processors";
import {
Changeset,
IApplicationPayoutRepository,
IApplicationRepository,
IDonationRepository,
IProjectRepository,
Expand Down Expand Up @@ -94,6 +95,7 @@ describe("Orchestrator", { sequential: true }, () => {
roundRepository: {} as unknown as IRoundRepository,
applicationRepository: {} as unknown as IApplicationRepository,
donationRepository: {} as unknown as IDonationRepository,
applicationPayoutRepository: {} as unknown as IApplicationPayoutRepository,
pricingProvider: {
getTokenPrice: vi.fn(),
},
Expand Down
15 changes: 14 additions & 1 deletion packages/repository/src/db/connection.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { CamelCasePlugin, ColumnType, Kysely, PostgresDialect, WithSchemaPlugin } from "kysely";
import {
CamelCasePlugin,
ColumnType,
Generated,
Kysely,
PostgresDialect,
WithSchemaPlugin,
} from "kysely";
import pg from "pg";

import {
Application,
ApplicationPayout,
Donation as DonationTable,
MatchingDistribution,
PendingProjectRole as PendingProjectRoleTable,
Expand Down Expand Up @@ -37,6 +45,10 @@ type RoundTable = Omit<Round, "matchingDistribution"> & {
>;
};

type ApplicationPayoutTable = Omit<ApplicationPayout, "id"> & {
id: Generated<number>;
};

export interface Database {
rounds: RoundTable;
pendingRoundRoles: PendingRoundRoleTable;
Expand All @@ -46,6 +58,7 @@ export interface Database {
projectRoles: ProjectRoleTable;
applications: ApplicationTable;
donations: DonationTable;
applicationsPayouts: ApplicationPayoutTable;
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/repository/src/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export type {
IApplicationRepository,
IApplicationReadRepository,
IDonationRepository,
IApplicationPayoutRepository,
DatabaseConfig,
} from "./internal.js";

Expand All @@ -32,19 +33,23 @@ export type {

export type { Donation, NewDonation } from "./types/index.js";

export type { NewApplicationPayout, ApplicationPayout } from "./types/index.js";

export type {
Changeset,
ProjectChangeset,
RoundChangeset,
ApplicationChangeset,
DonationChangeset,
ApplicationPayoutChangeset,
} from "./types/index.js";

export {
KyselyRoundRepository,
KyselyProjectRepository,
KyselyApplicationRepository,
KyselyDonationRepository,
KyselyApplicationPayoutRepository,
} from "./repositories/kysely/index.js";

export {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { NewApplicationPayout } from "../types/applicationPayout.types.js";

export interface IApplicationPayoutRepository {
/**
* Inserts a new application payout into the database.
* @param applicationPayout - The new application payout to insert.
* @returns A promise that resolves when the application payout is inserted.
*/
insertApplicationPayout(applicationPayout: NewApplicationPayout): Promise<void>;
}
1 change: 1 addition & 0 deletions packages/repository/src/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./projectRepository.interface.js";
export * from "./roundRepository.interface.js";
export * from "./applicationRepository.interface.js";
export * from "./donationRepository.interface.js";
export * from "./applicationPayoutRepository.interface.js";
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn("amount", BIGINT_TYPE)
.addColumn("tokenAddress", ADDRESS_TYPE)
.addColumn("amountInUsd", CURRENCY_TYPE)
.addColumn("amountInRoundMatchToken", "text")
.addColumn("amountInRoundMatchToken", BIGINT_TYPE)
.addColumn("transactionHash", "text")
.addColumn("timestamp", "timestamptz")
.addColumn("sender", ADDRESS_TYPE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Kysely } from "kysely";

import { Database, IApplicationPayoutRepository, NewApplicationPayout } from "../../internal.js";

export class KyselyApplicationPayoutRepository implements IApplicationPayoutRepository {
constructor(
private readonly db: Kysely<Database>,
private readonly schemaName: string,
) {}

/** @inheritdoc */
async insertApplicationPayout(applicationPayout: NewApplicationPayout): Promise<void> {
await this.db
.withSchema(this.schemaName)
.insertInto("applicationsPayouts")
.values(applicationPayout)
.execute();
}
}
1 change: 1 addition & 0 deletions packages/repository/src/repositories/kysely/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./project.repository.js";
export * from "./round.repository.js";
export * from "./application.repository.js";
export * from "./donation.repository.js";
export * from "./applicationPayout.repository.js";
17 changes: 17 additions & 0 deletions packages/repository/src/types/applicationPayout.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Address, ChainId, Hex } from "@grants-stack-indexer/shared";

export type ApplicationPayout = {
id: number;
chainId: ChainId;
roundId: string;
applicationId: string;
amount: bigint;
tokenAddress: Address;
amountInUsd: string;
amountInRoundMatchToken: bigint;
transactionHash: Hex;
sender: Address;
timestamp: Date | null;
};

export type NewApplicationPayout = Omit<ApplicationPayout, "id">;
11 changes: 9 additions & 2 deletions packages/repository/src/types/changeset.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Address, ChainId } from "@grants-stack-indexer/shared";

import { NewApplicationPayout } from "../internal.js";
import { NewApplication, PartialApplication } from "./application.types.js";
import { NewDonation } from "./donation.types.js";
import {
Expand Down Expand Up @@ -159,10 +160,16 @@ export type DonationChangeset =
};
};

//TODO: add changeset for Donation and Payout tables
export type ApplicationPayoutChangeset = {
type: "InsertApplicationPayout";
args: {
applicationPayout: NewApplicationPayout;
};
};

export type Changeset =
| ProjectChangeset
| RoundChangeset
| ApplicationChangeset
| DonationChangeset;
| DonationChangeset
| ApplicationPayoutChangeset;
1 change: 1 addition & 0 deletions packages/repository/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export * from "./round.types.js";
export * from "./application.types.js";
export * from "./changeset.types.js";
export * from "./donation.types.js";
export * from "./applicationPayout.types.js";

0 comments on commit 8eee37b

Please sign in to comment.