import {z} from 'zod' export type AttributionLedgerType = 'PAXOS' | 'ETHEREUM' | 'SOLANA' | 'INK' | 'XLAYER' | 'ARBITRUM_ONE' | 'ROBINHOOD'; export type AttributionAddress = {customer_id?: string | undefined, ledger?: AttributionLedgerType | undefined, address?: string | undefined, name?: string | undefined, created_at?: string | undefined, effective_at?: string | undefined, id?: string | undefined, payout_group_id?: string | undefined, balance?: string | undefined, status?: string | undefined, asset_type?: string | undefined, verification_proof_id?: string | undefined, verification_status?: string | undefined, verification_method?: string | undefined, verification_expires_at?: string | undefined}; export type CreateAttributionAddressRequest = {address?: AttributionAddress | undefined}; export type CreateApiRewardsAddressesArgs = {body: CreateAttributionAddressRequest}; export type CreateApiRewardsAddressesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const attributionLedgerType = z.enum(["PAXOS", "ETHEREUM", "SOLANA", "INK", "XLAYER", "ARBITRUM_ONE", "ROBINHOOD"]); export const attributionAddress = z.object({customer_id: z.string().optional(), ledger: attributionLedgerType.optional(), address: z.string().optional(), name: z.string().optional(), created_at: z.string().optional(), effective_at: z.string().optional(), id: z.string().optional(), payout_group_id: z.string().optional(), balance: z.string().optional(), status: z.string().optional(), asset_type: z.string().optional(), verification_proof_id: z.string().optional(), verification_status: z.string().optional(), verification_method: z.string().optional(), verification_expires_at: z.string().optional()}); export const createAttributionAddressResponse = z.object({address: attributionAddress.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 a reward address * * POST /rewards/addresses */ export const createApiRewardsAddresses = async (args: CreateApiRewardsAddressesArgs, options?: CreateApiRewardsAddressesOptions) => { 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/addresses${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(createAttributionAddressResponse, await res.json()) };