跳转到主要内容
通过 Composer 实现复杂 DeFi 工作流程的复制粘贴配方:兑换 + 跨链 + 存款,以及跨协议组合。
所有配方都使用 GET /quote 端点。相同的 toToken 地址适用于 POST /advanced/routes 和 LI.FI SDK。完整集成指南请参见 API 集成SDK 集成

跨链 + 兑换 + 存款

这些配方展示了 Composer 如何在单个流程中处理多个步骤,无需用户干预。

Ethereum 上的 ETH → Base 上的 Morpho 金库

这是一个经典的多步骤流程:ETH → USDC 跨链 → 存入 Morpho 金库。
curl -X GET 'https://li.quest/v1/quote?\
fromChain=1&\
toChain=8453&\
fromToken=0x0000000000000000000000000000000000000000&\
toToken=0x7BfA7C4f149E7415b73bdeDfe609237e29CBF34A&\
fromAddress=0xYOUR_WALLET_ADDRESS&\
toAddress=0xYOUR_WALLET_ADDRESS&\
fromAmount=100000000000000000'
可能发生的步骤:
  1. 在 Ethereum 上将 ETH 兑换为 USDC(如果需要)
  2. 将 USDC 从 Ethereum 跨链到 Base
  3. 将 USDC 存入 Base 上的 Morpho 金库
  4. 用户收到 Morpho 金库代币

Arbitrum 上的 ARB → Optimism 上的 Aave V3

跨链 + 存入 Aave V3 借贷协议。
curl -X GET 'https://li.quest/v1/quote?\
fromChain=42161&\
toChain=10&\
fromToken=ARB_TOKEN_ADDRESS_ON_ARBITRUM&\
toToken=AAAVE_AUSDC_TOKEN_ADDRESS_ON_OPTIMISM&\
fromAddress=0xYOUR_WALLET_ADDRESS&\
toAddress=0xYOUR_WALLET_ADDRESS&\
fromAmount=AMOUNT_IN_SMALLEST_UNIT'

协议间组合

这些配方展示了如何组合不同的协议类型。

质押 + 金库存款

将质押的收益代币存入金库以获得复合收益。
curl -X GET 'https://li.quest/v1/quote?\
fromChain=1&\
toChain=1&\
fromToken=0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0&\
toToken=MORPHO_VAULT_TOKEN_ADDRESS&\
fromAddress=0xYOUR_WALLET_ADDRESS&\
toAddress=0xYOUR_WALLET_ADDRESS&\
fromAmount=AMOUNT_IN_SMALLEST_UNIT'
流程:
  1. wstETH → ETH(如果需要)
  2. ETH → USDC(如果需要)
  3. USDC → 存入 Morpho 金库
  4. 用户收到 Morpho 金库代币

复杂跨链工作流程

多链收益聚合

从多个链收集资产并集中到单个协议中。
# 示例:Polygon 上的 MATIC → Ethereum 上的 Lido wstETH
curl -X GET 'https://li.quest/v1/quote?\
fromChain=137&\
toChain=1&\
fromToken=0x0000000000000000000000000000000000001010&\
toToken=0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0&\
fromAddress=0xYOUR_WALLET_ADDRESS&\
toAddress=0xYOUR_WALLET_ADDRESS&\
fromAmount=1000000000000000000'

执行复杂工作流程

使用 SDK 处理复杂性

import { getQuote, convertQuoteToRoute, executeRoute } from '@lifi/sdk';

const executeComplexWorkflow = async () => {
  // 1. 获取复杂工作流程的报价
  const quote = await getQuote({
    fromChain: 1,                                                 // Ethereum
    toChain: 8453,                                                // Base
    fromToken: '0x0000000000000000000000000000000000000000',       // ETH
    toToken: '0x7BfA7C4f149E7415b73bdeDfe609237e29CBF34A',       // Morpho 金库
    fromAmount: '100000000000000000',                             // 0.1 ETH
    fromAddress: '0xYOUR_WALLET_ADDRESS',
  });

  // 2. 转换并执行 - SDK 处理所有步骤
  const route = convertQuoteToRoute(quote);
  const executedRoute = await executeRoute(route, {
    updateRouteHook(updatedRoute) {
      console.log('工作流程进度:', updatedRoute);
      
      // 跟踪每个步骤
      updatedRoute.steps.forEach((step, index) => {
        console.log(`步骤 ${index + 1}: ${step.tool} - ${step.execution?.status}`);
      });
    },
    onChainSwitch(chainId) {
      console.log('切换到链:', chainId);
    },
  });

  console.log('复杂工作流程完成!', executedRoute);
};

手动 API 执行

const executeComplexWorkflowManually = async () => {
  // 1. 获取报价
  const quote = await axios.get('https://li.quest/v1/quote', {
    params: {
      fromChain: 1,
      toChain: 8453,
      fromToken: '0x0000000000000000000000000000000000000000',
      toToken: '0x7BfA7C4f149E7415b73bdeDfe609237e29CBF34A',
      fromAmount: '100000000000000000',
      fromAddress: '0xYOUR_WALLET_ADDRESS',
      toAddress: '0xYOUR_WALLET_ADDRESS',
    },
  });

  const quoteData = quote.data;

  // 2. 设置授权(如果需要)
  if (quoteData.estimate.approvalAddress) {
    // 授权逻辑...
  }

  // 3. 执行源链交易
  const tx = await signer.sendTransaction(quoteData.transactionRequest);
  console.log('源链交易:', tx.hash);

  // 4. 跟踪跨链状态
  const trackStatus = async () => {
    const status = await axios.get('https://li.quest/v1/status', {
      params: {
        txHash: tx.hash,
        fromChain: 1,
        toChain: 8453,
      },
    });

    const statusData = status.data;
    console.log('状态:', statusData.status);

    if (statusData.status !== 'DONE' && statusData.status !== 'FAILED') {
      setTimeout(trackStatus, 5000); // 5 秒后重试
    } else {
      console.log('复杂工作流程完成!');
    }
  };

  trackStatus();
};

最佳实践

1. 错误处理

try {
  const executedRoute = await executeRoute(route);
} catch (error) {
  if (error.code === 'TRANSACTION_FAILED') {
    console.error('工作流程失败,请检查协议状态');
  } else if (error.code === 'INSUFFICIENT_BALANCE') {
    console.error('余额不足');
  }
}

2. 进度跟踪

const executeWithProgress = async (route) => {
  return await executeRoute(route, {
    updateRouteHook(updatedRoute) {
      const currentStep = updatedRoute.steps.find(step => 
        step.execution?.status === 'PENDING'
      );
      
      if (currentStep) {
        updateUI(`正在执行:${currentStep.tool}`);
      }
    },
  });
};

3. 成本优化

const quote = await getQuote({
  fromChain: 1,
  toChain: 8453,
  fromToken: '0x0000000000000000000000000000000000000000',
  toToken: '0x7BfA7C4f149E7415b73bdeDfe609237e29CBF34A',
  fromAmount: '100000000000000000',
  fromAddress: '0xYOUR_WALLET_ADDRESS',
  options: {
    slippage: 0.005, // 0.5% slippage
    bridges: {
      allow: ['layerzero', 'stargate'], // 偏好快速跨链桥
    },
  },
});

通用模式

所有多步骤配方都遵循相同的模式:
GET /quote
  fromChain  = 源链 ID
  toChain    = 目标链 ID(相同或不同)
  fromToken  = 您起始的代币
  toToken    = 最终目标协议代币地址
  fromAmount = 最小单位的金额
  fromAddress = 您的钱包
  toAddress   = 您的钱包(接收最终代币)
Composer 自动处理中间步骤:兑换、跨链、授权、存款等。

下一步