import {z} from 'zod' export type CreateApiIntentOperationsArgs = {'x-api-version': '2026-01.alps', 'x-api-key': string, body: {signedIntentOp: {sponsor: string, nonce: unknown, targetExecutionNonce?: unknown | undefined, expires: number, elements: Array<{arbiter: string, chainId: unknown, idsAndAmounts: Array, spendTokens: Array, mandate: {recipient: string, tokenOut: Array, destinationChainId: unknown, fillDeadline: number, preClaimOps: {vt: string, ops: Array<{to: string, value: unknown, data: string}>}, destinationOps: {vt: string, ops: Array<{to: string, value: unknown, data: string}>}, qualifier: {settlementContext: {settlementLayer: 'INTENT_EXECUTOR', using7579: boolean, fundingMethod: 'NO_FUNDING', gasRefund: {overhead: unknown, exchangeRate: unknown, token: string}, prefundAmount?: unknown | undefined, erc20Prefund?: Array<{token: string, amount: unknown}> | undefined, bridgeFill?: ({destinationChainId: number, fillExpirationPeriod?: number | undefined, type: 'OFT'} | {destinationChainId: number, fillExpirationPeriod?: number | undefined, type: 'RELAY', requestId: string} | {destinationChainId: number, fillExpirationPeriod?: number | undefined, type: 'NEAR', depositAddress: string} | {destinationChainId: number, fillExpirationPeriod?: number | undefined, type: 'RHINO', commitmentId: string} | {destinationChainId: number, fillExpirationPeriod?: number | undefined, type: 'CCTP', sourceDomainId: number, destinationDomainId: number}) | undefined, bridgeDestinationOps?: true | undefined} | {settlementLayer: 'SAME_CHAIN', using7579: boolean, fundingMethod: 'PERMIT2' | 'NO_FUNDING', bridgeFill?: ({destinationChainId: number, fillExpirationPeriod?: number | undefined, type: 'OFT'} | {destinationChainId: number, fillExpirationPeriod?: number | undefined, type: 'RELAY', requestId: string} | {destinationChainId: number, fillExpirationPeriod?: number | undefined, type: 'NEAR', depositAddress: string} | {destinationChainId: number, fillExpirationPeriod?: number | undefined, type: 'RHINO', commitmentId: string} | {destinationChainId: number, fillExpirationPeriod?: number | undefined, type: 'CCTP', sourceDomainId: number, destinationDomainId: number}) | undefined} | {settlementLayer: 'ACROSS', using7579: boolean, fundingMethod: 'PERMIT2' | 'NO_FUNDING'} | {settlementLayer: 'ECO', using7579: boolean, fundingMethod: 'PERMIT2' | 'NO_FUNDING', provingFee: unknown} | {settlementLayer: 'RELAY', using7579: boolean, fundingMethod: 'PERMIT2' | 'NO_FUNDING', requestId: string, orderId: string, subsidizedAmount?: string | undefined, multicallData?: string | undefined, executionValidationGas?: unknown | undefined} | {settlementLayer: 'NEAR', using7579: boolean, fundingMethod: 'PERMIT2' | 'NO_FUNDING', depositAddress: string, correlationId: string} | {settlementLayer: 'RHINO', using7579: boolean, fundingMethod: 'PERMIT2' | 'NO_FUNDING', quoteId: string} | {settlementLayer: 'CCTP', using7579: boolean, fundingMethod: 'PERMIT2' | 'NO_FUNDING', sourceDomainId: number, destinationDomainId: number}, encodedVal: string}, minGas: unknown, swapDestinations?: (Array<{tokenIn: string, amountIn: unknown, amountOut: unknown, slippage: number, quoter: string, executions: Array<{to: string, value: unknown, data: string, senderAddressPlaceholder?: string | undefined, recipientAddressPlaceholder?: string | undefined}>, outputDecimals: number, outputSymbol: string} | null>) | undefined}, swapOrigins?: ({[key: string]: {tokenIn: string, tokenOut: string, amountIn: unknown, amountOut: unknown, slippage: number, quoter: string, executions: Array<{to: string, value: unknown, data: string, senderAddressPlaceholder?: string | undefined, recipientAddressPlaceholder?: string | undefined}>, inputDecimals: number, inputSymbol: string, price: number, gasEstimate?: number | undefined}}) | undefined, bundleId?: unknown | undefined}>, serverSignature: string, signedMetadata: {strategy?: ('ACROSS' | 'ECO' | 'SAME_CHAIN' | 'TOKENLESS' | 'RELAY' | 'OFT' | 'NEAR' | 'RHINO' | 'CCTP_FAST' | 'CCTP_CHEAP') | undefined, schemeId?: string | undefined, tokenPrices: {[key: string]: number}, gasPrices: {[key: string]: unknown}, opGasParams: {estimatedCalldataSize?: number | undefined} | {[key: string]: {l1BaseFee: unknown, l1BlobBaseFee: unknown, baseFeeScalar: unknown, blobFeeScalar: unknown}}, account: {address: string, accountType?: ('smartAccount' | 'GENERIC' | 'EOA' | 'ERC7579') | undefined, setupOps?: Array<{to: string, data: string}> | undefined, accountContext: {[key: string]: {accountType: 'EOA'} | {accountType: 'smartAccount', isDeployed: boolean, isERC7579: boolean, erc7579AccountType?: ('Safe' | 'Kernel' | 'Nexus') | undefined, erc7579AccountVersion?: string | undefined}}, requiredDelegations?: {[key: string]: {contract: string}} | undefined}, recipient?: ({address: string, accountType?: ('smartAccount' | 'GENERIC' | 'EOA' | 'ERC7579') | undefined, setupOps?: Array<{to: string, data: string}> | undefined, accountContext: {[key: string]: {accountType: 'EOA'} | {accountType: 'smartAccount', isDeployed: boolean, isERC7579: boolean, erc7579AccountType?: ('Safe' | 'Kernel' | 'Nexus') | undefined, erc7579AccountVersion?: string | undefined}}, requiredDelegations?: {[key: string]: {contract: string}} | undefined}) | undefined, fees?: ({sponsorFee?: {relayer: number, protocol: number} | undefined, protocolFees?: {[key: string]: unknown} | undefined, appFee?: Array<{feeBps: number, baseAmount: unknown, amount: unknown, chainId: number, tokenAddress: string}> | undefined}) | undefined, sponsorSettings?: ({gas?: boolean | undefined, bridgeFees?: boolean | undefined, swapFees?: boolean | undefined}) | undefined, executionTokensReceived?: Array | undefined, preClaimExecutions?: {[key: string]: Array<{to: string, value: unknown, data: string}>} | undefined, accounting?: ({bridgeFill?: {tokensOut: Array<{address: string, chainId: number, amount: unknown}>} | undefined}) | undefined}, signedAuthorizations?: Array<{chainId: number, address: string, nonce: number, yParity: number, r: string, s: string}> | undefined, recipientSignedAuthorizations?: Array<{chainId: number, address: string, nonce: number, yParity: number, r: string, s: string}> | undefined, destinationSignature: string, targetExecutionSignature?: string | undefined, originSignatures: Array}}}; export type CreateApiIntentOperationsOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const createApiIntentOperationsResponse = z.object({result: z.union([z.object({id: z.string(), status: z.literal("PENDING")}), z.object({id: z.string(), status: z.literal("FAILED")})])}); 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 Intent * * POST /intent-operations */ export const createApiIntentOperations = async (args: CreateApiIntentOperationsArgs, options: CreateApiIntentOperationsOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-api-version'] != null) headers.set("x-api-version", String(args['x-api-version'])) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/intent-operations${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(createApiIntentOperationsResponse, await res.json()) };