@lifi/composer-sdk
Version:
Public Composer SDK for building and submitting flows
62 lines (55 loc) • 1.8 kB
text/typescript
import type { Address } from './types.js';
// These resource types are SDK-local declarations that mirror the shape of
// `Resource` from `@lifi/compose-spec`. They exist so the SDK can be used
// without directly depending on `@lifi/compose-spec` at the call site.
// The types are structurally compatible, so they are assignable to each other.
/** A resource declaration representing the chain's native token (e.g. ETH, MATIC). */
export interface NativeResource {
readonly kind: 'native';
readonly chainId: number;
}
/** A resource declaration representing an ERC-20 token at a specific address. */
export interface Erc20Resource {
readonly kind: 'erc20';
readonly token: Address;
readonly chainId: number;
}
/** A token resource — either the chain's native token or an ERC-20. */
export type Resource = NativeResource | Erc20Resource;
/**
* Creates a native token resource declaration.
*
* @param chainId - The EVM chain ID the native token belongs to.
* @returns A {@link NativeResource} for use in flow input schemas.
*
* @example
* ```ts
* const builder = sdk.flow(1, {
* inputs: { eth: native(1) },
* });
* ```
*/
export const native = (chainId: number): NativeResource => ({
kind: 'native',
chainId,
});
/**
* Creates an ERC-20 token resource declaration.
*
* @param token - The `0x`-prefixed contract address of the ERC-20 token.
* @param chainId - The EVM chain ID the token is deployed on.
* @returns An {@link Erc20Resource} for use in flow input schemas.
*
* @example
* ```ts
* const USDC = '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48';
* const builder = sdk.flow(1, {
* inputs: { usdc: erc20(USDC, 1) },
* });
* ```
*/
export const erc20 = (token: Address, chainId: number): Erc20Resource => ({
kind: 'erc20',
token,
chainId,
});