import type { ColumnType } from 'kysely' import type { Address } from 'viem' import type * as Db from '../Db.js' import type * as db_Schema from '../Schema.js' /** Columns of the `reward_campaign_distributors` table. */ export type Table = Omit & { /** Bigint chain id, read as a string from pg and written as a number. */ chainId: ColumnType } /** A stored historical reward distributor. */ export type Record = db_Schema.RewardCampaignDistributor /** Retains one verified reward distributor for historical payout recognition. */ export async function add(db: Db.Db, options: add.Options): Promise { await db.kysely .insertInto('reward_campaign_distributors') .values({ chainId: options.chainId, createdAt: new Date().toISOString(), distributorAddress: normalize(options.distributorAddress), vaultAddress: normalize(options.vaultAddress), }) .onConflict((oc) => oc.doNothing()) .execute() } export declare namespace add { /** Verified distributor identity. */ type Options = { /** Chain containing the vault. */ chainId: number /** Verified reward distributor address. */ distributorAddress: Address /** EarnVault address. */ vaultAddress: Address } } /** Lists every verified reward distributor for one vault. */ export async function list(db: Db.Db, options: list.Options): Promise { const rows = await db.kysely .selectFrom('reward_campaign_distributors') .selectAll() .where('chainId', '=', String(options.chainId)) .where('vaultAddress', '=', normalize(options.vaultAddress)) .orderBy('createdAt') .orderBy('distributorAddress') .execute() return rows.map((row) => ({ ...row, chainId: Number(row.chainId) })) } export declare namespace list { /** Campaign identity. */ type Options = { /** Chain containing the vault. */ chainId: number /** EarnVault address. */ vaultAddress: Address } } /** Canonicalizes persisted addresses. */ function normalize(address: string): Address { return address.toLowerCase() as Address }