UNPKG

@lifi/composer-sdk

Version:

Public Composer SDK for building and submitting flows

113 lines (110 loc) 4.43 kB
import { ComposeCompileResult, ComposeCompileRequest } from '@lifi/compose-spec'; import { FlowOptions, FlowBuilderCore, TypedFlow } from './authoring/FlowBuilderCore.cjs'; import { ComposeClient } from './client.cjs'; import { GeneratedOps } from './generated/operations.generated.cjs'; import { ComposeRunInput } from './run/inputs.cjs'; import { InputSchema } from './types.cjs'; import './authoring/handles.cjs'; import './discovery.cjs'; import './authoring/signatureArgs.cjs'; import 'abitype'; import './generated/config.generated.cjs'; /** * A flow builder augmented with generated operation methods and a `compile` method * that submits the flow to the Compose backend for compilation. * * Created via {@link ComposeSdk.flow}. * * @typeParam T - The input schema describing the flow's required inputs. */ type FlowBuilder<T extends InputSchema = InputSchema> = FlowBuilderCore<T> & GeneratedOps & { /** * Builds the flow document from the current builder state and submits it * to the Compose API for compilation. * * @param run - Runtime inputs, preconditions, and signer address. * @returns The compiled transaction calldata and metadata. * @throws {@link ComposeError} on network, validation, or server errors. * * @example * ```ts * const result = await builder.compile({ * inputs: { token: materialisers.balanceOf({}) }, * signer: '0xYourAddress...', * }); * console.log(result.calldata); * ``` */ readonly compile: (run: ComposeRunInput<T>) => Promise<ComposeCompileResult>; }; /** * Configuration for creating a Compose SDK instance. */ interface ComposeSdkOptions { /** Base URL of the Compose API (e.g. `"https://li.quest"`). */ readonly baseUrl: string; /** Optional custom `fetch` implementation. Defaults to `globalThis.fetch`. */ readonly fetch?: typeof globalThis.fetch; /** Optional LI.FI API key for authenticated access. Sent as the `x-lifi-api-key` header on every request. */ readonly apiKey?: string; } /** * The top-level Compose SDK interface. * * Provides methods to build flows, compile them into executable calldata, and * interact with the Compose API directly. * * @example * ```ts * import { createComposeSdk, resources, materialisers } from '@lifi/composer-sdk'; * * const sdk = createComposeSdk({ baseUrl: 'https://li.quest' }); * * const builder = sdk.flow(1, { * inputs: { usdc: resources.erc20('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', 1) }, * }); * * builder.lifi.swap('swap1', { * bind: { amountIn: builder.inputs.usdc }, * config: { resourceOut: resources.native(1) }, * }); * * const result = await builder.compile({ * inputs: { usdc: materialisers.balanceOf({}) }, * signer: '0xYourAddress...', * }); * ``` */ interface ComposeSdk { /** Low-level HTTP client for the Compose API. */ readonly client: ComposeClient; /** * Creates a new flow builder targeting the given chain. * @param chainId - The EVM chain ID (e.g. `1` for Ethereum mainnet). * @param options - Flow configuration including input declarations. * @returns A {@link FlowBuilder} with generated operation methods and a `compile` method. * @throws Error if any resource input's `chainId` doesn't match the flow's `chainId`. */ readonly flow: <T extends InputSchema>(chainId: number, options: FlowOptions<T>) => FlowBuilder<T>; /** * Builds a raw compile request without sending it. Useful for inspecting the * request payload or submitting it through a custom transport. * @param flow - A built flow document (from `builder.build()`). * @param run - Runtime inputs, preconditions, and signer address. * @returns A {@link ComposeCompileRequest} ready to send to the Compose API. */ readonly request: <T extends InputSchema>(flow: TypedFlow<T>, run: ComposeRunInput<T>) => ComposeCompileRequest; } /** * Creates a new Compose SDK instance. * * @param options - SDK configuration including the API base URL and optional fetch implementation. * @returns A {@link ComposeSdk} instance. * * @example * ```ts * const sdk = createComposeSdk({ baseUrl: 'https://li.quest' }); * ``` */ declare const createComposeSdk: (options: ComposeSdkOptions) => ComposeSdk; export { ComposeRunInput, type ComposeSdk, type ComposeSdkOptions, type FlowBuilder, createComposeSdk };