import {z} from 'zod' export type Address = unknown; export type Cursor = unknown; export type GetApiExperimentalV1AccountsAddressNftTransfersArgs = {address: Address, cursor?: Cursor | undefined, limit?: number | undefined, token_type?: string | undefined, source_address?: Address | undefined, recipient_address?: Address | undefined, expand?: Array | undefined, select?: Array | undefined}; export type GetApiExperimentalV1AccountsAddressNftTransfersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const identifier = z.string(); export const address = z.string(); export const proposalKey = z.object({address: address, key_index: z.string(), sequence_number: z.string()}); export const signature = z.string(); export const transactionSignature = z.object({address: address, key_index: z.string(), signature: signature}); export const transactionExecution = z.enum(["Pending", "Success", "Failure"]); export const transactionStatus = z.enum(["Pending", "Finalized", "Executed", "Sealed", "Expired"]); export const eventType = z.string(); export const event = z.object({type: eventType, transaction_id: identifier, transaction_index: z.string(), event_index: z.string(), payload: z.string()}); export const links = z.object({_self: z.string().optional()}); export const transactionResult = z.object({block_id: identifier, collection_id: identifier, execution: transactionExecution.optional(), status: transactionStatus, status_code: z.number().int(), error_message: z.string(), computation_used: z.string(), events: z.array(event), _links: links.optional()}); export const transaction = z.object({id: identifier, script: z.string(), arguments: z.array(z.string()), reference_block_id: identifier, gas_limit: z.string(), payer: address, proposal_key: proposalKey, authorizers: z.array(address), payload_signatures: z.array(transactionSignature), envelope_signatures: z.array(transactionSignature), result: transactionResult.optional(), _expandable: z.object({result: z.string().optional()}), _links: links.optional()}); export const nonFungibleTokenTransfer = z.object({transaction_id: identifier, block_height: z.string(), timestamp: z.string(), transaction_index: z.string(), event_indices: z.array(z.string()), token_type: z.string(), nft_id: z.string(), source_address: address, recipient_address: address, transaction: transaction.optional(), result: transactionResult.optional(), _expandable: z.object({transaction: z.string().optional(), result: z.string().optional()}), _links: links.optional()}); export const cursor = z.string(); export const accountNonFungibleTransfersResponse = z.object({transfers: z.array(nonFungibleTokenTransfer), next_cursor: cursor.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 }; /** * Get account non-fungible token transfers * * GET /experimental/v1/accounts/{address}/nft/transfers */ export const getApiExperimentalV1AccountsAddressNftTransfers = async (args: GetApiExperimentalV1AccountsAddressNftTransfersArgs, options?: GetApiExperimentalV1AccountsAddressNftTransfersOptions) => { const {baseUrl = "https://rest-testnet.onflow.org", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.token_type != null) query.append("token_type", String(args.token_type)) if (args.source_address != null) query.append("source_address", String(args.source_address)) if (args.recipient_address != null) query.append("recipient_address", String(args.recipient_address)) if (args.expand != null) query.append("expand", args.expand.join(',')) if (args.select != null) query.append("select", args.select.join(',')) const url = `${baseUrl.replace(/\/+$/, '')}/experimental/v1/accounts/${encodeURIComponent(String(args.address))}/nft/transfers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(accountNonFungibleTransfersResponse, await res.json()) };