Skip to main content
The GenericSwapFacet enables users to perform same-chain token swaps through any DEX or swap aggregator. This facet is useful when you need to swap tokens without bridging to another chain.

Functions

swapTokensGeneric

Executes one or more token swaps and transfers the result to a receiver.
function swapTokensGeneric(
  _transactionId: BytesLike,
  _integrator: string,
  _referrer: string,
  _receiver: string,
  _minAmount: BigNumberish,
  _swapData: LibSwap.SwapDataStruct[]
): Promise<ContractTransaction>
_transactionId
bytes32
required
Unique identifier for this transaction
_integrator
string
required
Name or identifier of the integrator
_referrer
string
required
Referrer address or identifier
_receiver
address
required
Address that will receive the swapped tokens
_minAmount
uint256
required
Minimum amount of tokens expected after all swaps (slippage protection)
_swapData
LibSwap.SwapData[]
required
Array of swap data structures defining the swap operations to execute

Data Structures

LibSwap.SwapData

Defines a single swap operation.
callTo
address
required
The DEX or swap contract to call
approveTo
address
required
The address to approve for spending tokens (usually the same as callTo)
sendingAssetId
address
required
Address of the token being sold (use zero address for native token)
receivingAssetId
address
required
Address of the token being bought (use zero address for native token)
fromAmount
uint256
required
Amount of sendingAssetId to swap
callData
bytes
required
Encoded function call data for the swap
requiresDeposit
bool
required
Whether the contract should hold the tokens before swapping

Events

LiFiSwappedGeneric

Emitted when a generic swap is executed.
transactionId
bytes32
The transaction identifier
integrator
string
The integrator name
referrer
string
The referrer identifier
fromAssetId
address
Token being sold
toAssetId
address
Token being bought
fromAmount
uint256
Amount sold
toAmount
uint256
Amount received

LiFiGenericSwapCompleted

Emitted when all swaps are completed and tokens are transferred to receiver.
transactionId
bytes32
The transaction identifier
integrator
string
The integrator name
referrer
string
The referrer identifier
receiver
address
Address receiving the final tokens
fromAssetId
address
Initial token
toAssetId
address
Final token
fromAmount
uint256
Initial amount
toAmount
uint256
Final amount received

AssetSwapped

Emitted for each individual swap in the swap array.
transactionId
bytes32
The transaction identifier
dex
address
The DEX contract used
fromAssetId
address
Token being sold
toAssetId
address
Token being bought
fromAmount
uint256
Amount sold
toAmount
uint256
Amount received
timestamp
uint256
Block timestamp

Usage Example

import { GenericSwapFacet__factory } from '@lifi/contract-types';
import { ethers } from 'ethers';

const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
const signer = provider.getSigner();
const facet = GenericSwapFacet__factory.connect(DIAMOND_ADDRESS, signer);

const swapData = [
  {
    callTo: '0xDEX_ADDRESS',
    approveTo: '0xDEX_ADDRESS',
    sendingAssetId: '0xUSDC_ADDRESS',
    receivingAssetId: '0xUSDT_ADDRESS',
    fromAmount: ethers.utils.parseUnits('100', 6),
    callData: '0x...', // Encoded swap call
    requiresDeposit: true
  }
];

const tx = await facet.swapTokensGeneric(
  ethers.utils.randomBytes(32),
  'MyDApp',
  '0xREFERRER_ADDRESS',
  '0xRECEIVER_ADDRESS',
  ethers.utils.parseUnits('99', 6), // Min 99 USDT
  swapData
);

await tx.wait();

Build docs developers (and LLMs) love