import {z} from 'zod' export type AddressTag = ; export type AddressType = 'externally-owned-account' | 'internal-account' | 'smart-contract' | 'payable-smart-contract'; export type CreateCryptoAddress = {acceptTokens?: boolean | undefined, address: string, addressTags?: Array | undefined, addressType?: AddressType | undefined, cryptoAddressId: string, currency: string, isWhitelist?: boolean | undefined, mainCurrency?: string | undefined, memo?: string | undefined, name: string, portfolioIds?: Array | undefined}; export type CreateApiCryptoAddressesArgs = {body: CreateCryptoAddress}; export type CreateApiCryptoAddressesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyConfigurationExtra = z.object({}); export const currencyConfiguration = z.object({createdAt: z.string(), createdBy: z.string(), cryptoAddressId: z.string(), currency: z.string(), currencyConfigurationId: z.string(), extra: currencyConfigurationExtra.optional(), isWhitelist: z.boolean(), lastUsedAt: z.string().optional(), portfolioIds: z.array(z.string()).optional(), updatedAt: z.string().optional(), updatedBy: z.string().optional()}); export const cryptoAddressEmbedded = z.object({currencyConfigurations: z.array(currencyConfiguration).optional()}); export const addressTag = z.enum([]); export const addressType = z.enum(["externally-owned-account", "internal-account", "smart-contract", "payable-smart-contract"]); export const addressCategory = z.enum(["smart-contract", "payable-smart-contract", "mining", "unknown", "self-hosted", "vasp"]); export const cryptoAddressExtra = z.object({}); export const cryptoAddress = z.object({_embedded: cryptoAddressEmbedded.optional(), acceptTokens: z.boolean().optional(), address: z.string(), addressTags: z.array(addressTag).optional(), addressType: addressType, category: addressCategory.optional(), createdAt: z.string(), createdBy: z.string(), cryptoAddressId: z.string(), currency: z.string(), extra: cryptoAddressExtra.optional(), isWhitelist: z.boolean(), lastUsedAt: z.string().optional(), mainCurrency: z.string().optional(), memo: z.string().optional(), name: z.string(), organizationId: z.string().optional(), portfolioIds: z.array(z.string()).optional(), updatedAt: z.string().optional(), updatedBy: z.string().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 Crypto Address * * POST /crypto-addresses */ export const createApiCryptoAddresses = async (args: CreateApiCryptoAddressesArgs, options?: CreateApiCryptoAddressesOptions) => { const {baseUrl = "https://api.copper.co/platform", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/crypto-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(cryptoAddress, await res.json()) };