prepare_crosschain_swaps
Documentation for eth_defi.lifi.crosschain.prepare_crosschain_swaps function.
- prepare_crosschain_swaps(wallet, source_web3, target_web3s, min_gas_usd=Decimal('5'), top_up_usd=Decimal('20'), source_token_address='0x0000000000000000000000000000000000000000', chain_configs=None, slippage=0.03, api_timeout=30, progress=True)
Check gas balances and prepare cross-chain bridge swaps for chains that need topping up.
Checks the native token balance on each target chain. If any chain has less than
min_gas_usdworth of native token, prepares a LI.FI bridge quote to send tokens from the source chain.The source token can be either the native gas token (default) or an ERC-20 token such as USDC. When using an ERC-20 source token, the amount to bridge is calculated using the token’s on-chain decimals via
TokenDetails, and the USD price is fetched from the LI.FI token API. LI.FI handles the swap from the source token to the target chain’s native token.Chains are identified by numeric EVM chain IDs (e.g. 1 for Ethereum, 42161 for Arbitrum). These are our internal chain IDs from
eth_defi.chain.CHAIN_NAMESand are passed directly to the LI.FI API asfromChain/toChainparameters, which also uses numeric chain IDs. The corresponding RPC URLs are expected inJSON_RPC_{CHAIN_NAME}environment variables, resolved viaeth_defi.provider.env.read_json_rpc_url().- Parameters
wallet (eth_defi.hotwallet.HotWallet) – Hot wallet that holds funds on the source chain
source_web3 (web3.main.Web3) – Web3 connection to the source chain. The chain ID is read from
source_web3.eth.chain_id.target_web3s (dict[int, web3.main.Web3]) – Dict mapping chain_id to Web3 connection for each target chain. Keys are numeric EVM chain IDs.
min_gas_usd (decimal.Decimal) – Default minimum gas balance in USD. Chains below this trigger a top-up.
top_up_usd (decimal.Decimal) – Default amount to bridge in USD when topping up.
source_token_address (str) – Address of the token to bridge from the source chain. Use
LIFI_NATIVE_TOKEN_ADDRESS(default) for native gas token, or an ERC-20 address (e.g. USDC fromUSDC_NATIVE_TOKEN).chain_configs (dict[int, eth_defi.lifi.crosschain.CrossChainGasConfig] | None) – Optional per-chain overrides for min_gas_usd and top_up_usd. Keys are chain IDs.
slippage (float) – Maximum allowed slippage as a decimal (0.03 = 3%)
api_timeout (float) – API request timeout in seconds
progress (bool) – Show a
tqdmprogress bar while fetching quotes (defaultTrue)
- Returns
List of prepared swaps. Empty if all chains have sufficient gas.
- Return type