import {z} from 'zod' export type ScheduleType = 'DAILY' | 'MONTHLY' | 'WEEKLY'; export type ClaimScheduleConfig = {schedule_type?: ScheduleType | undefined, days_of_week?: Array | undefined, days_of_month?: Array | undefined, hour?: number | undefined, minute?: number | undefined}; export type CreatePayoutGroupRequest = {ledger?: string | undefined, asset_type?: string | undefined, jurisdiction?: string | undefined, name?: string | undefined, claimer_address?: string | undefined, schedule?: ClaimScheduleConfig | undefined}; export type CreateApiRewardsPayoutGroupsArgs = {body: CreatePayoutGroupRequest}; export type CreateApiRewardsPayoutGroupsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const payoutGroupOnchainStatus = z.enum(["UNREGISTERED", "PENDING_REGISTRATION", "NEEDS_REREGISTRATION", "ACTIVE", "FAILED_REGISTRATION", "PENDING_DELETION", "FAILED_DELETION", "PENDING_MULTIPLIER_UPDATE", "MULTIPLIER_UPDATE_FAILED", "UNREGISTERED_INSUFFICIENT_BALANCE"]); export const payoutGroup = z.object({id: z.string(), ledger: z.string(), asset_type: z.string(), rate_id: z.string(), jurisdiction: z.string(), name: z.string().optional(), claimer_address: z.string(), manager_address: z.string().optional(), destination_address: z.string().optional(), onchain_group_id: z.string().optional(), onchain_status: payoutGroupOnchainStatus, balance: z.string(), balance_at: z.string().optional(), last_claim_all_at: z.string().optional(), created_at: z.string(), updated_at: z.string()}); export const createPayoutGroupResponse = z.object({group: payoutGroup.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create Payout Group * * POST /rewards/payout-groups * * Also accepts auth: OAuth2 */ export const createApiRewardsPayoutGroups = async (args: CreateApiRewardsPayoutGroupsArgs, options?: CreateApiRewardsPayoutGroupsOptions) => { const {baseUrl = "https://api.paxos.com/v2", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/rewards/payout-groups${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createPayoutGroupResponse, await res.json()) };