> ## Documentation Index
> Fetch the complete documentation index at: https://docs.li.fi/llms.txt
> Use this file to discover all available pages before exploring further.

# 多步骤配方

> 通过 LI.FI Composer 实现复杂 DeFi 工作流程的复制粘贴配方：兑换 + 跨链 + 存款，以及跨协议组合。

通过 Composer 实现复杂 DeFi 工作流程的复制粘贴配方：兑换 + 跨链 + 存款，以及跨协议组合。

<Note>
  所有配方都使用 `GET /quote` 端点。相同的 `toToken` 地址适用于 `POST /advanced/routes` 和 LI.FI SDK。完整集成指南请参见 [API 集成](/composer/guides/api-integration) 或 [SDK 集成](/composer/guides/sdk-integration)。
</Note>

***

## 跨链 + 兑换 + 存款

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

### Ethereum 上的 ETH → Base 上的 Morpho 金库

这是一个经典的多步骤流程：ETH → USDC 跨链 → 存入 Morpho 金库。

<CodeGroup>
  ```bash curl theme={"system"}
  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'
  ```

  ```ts TypeScript theme={"system"}
  const quote = await axios.get('https://li.quest/v1/quote', {
    params: {
      fromChain: 1,                                                 // Ethereum
      toChain: 8453,                                                // Base
      fromToken: '0x0000000000000000000000000000000000000000',       // ETH (原生)
      toToken: '0x7BfA7C4f149E7415b73bdeDfe609237e29CBF34A',       // Morpho 金库代币
      fromAddress: '0xYOUR_WALLET_ADDRESS',
      toAddress: '0xYOUR_WALLET_ADDRESS',
      fromAmount: '100000000000000000',                             // 0.1 ETH
    },
  });
  ```
</CodeGroup>

**可能发生的步骤：**

1. 在 Ethereum 上将 ETH 兑换为 USDC（如果需要）
2. 将 USDC 从 Ethereum 跨链到 Base
3. 将 USDC 存入 Base 上的 Morpho 金库
4. 用户收到 Morpho 金库代币

### Arbitrum 上的 ARB → Optimism 上的 Aave V3

跨链 + 存入 Aave V3 借贷协议。

<CodeGroup>
  ```bash curl theme={"system"}
  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'
  ```

  ```ts TypeScript theme={"system"}
  const quote = await axios.get('https://li.quest/v1/quote', {
    params: {
      fromChain: 42161,                                             // Arbitrum
      toChain: 10,                                                  // Optimism
      fromToken: 'ARB_TOKEN_ADDRESS_ON_ARBITRUM',                   // Arbitrum 上的 ARB
      toToken: 'AAAVE_AUSDC_TOKEN_ADDRESS_ON_OPTIMISM',             // Optimism 上的 Aave aUSDC
      fromAddress: '0xYOUR_WALLET_ADDRESS',
      toAddress: '0xYOUR_WALLET_ADDRESS',
      fromAmount: 'AMOUNT_IN_SMALLEST_UNIT',
    },
  });
  ```
</CodeGroup>

***

## 协议间组合

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

### 质押 + 金库存款

将质押的收益代币存入金库以获得复合收益。

<CodeGroup>
  ```bash curl theme={"system"}
  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'
  ```

  ```ts TypeScript theme={"system"}
  const quote = await axios.get('https://li.quest/v1/quote', {
    params: {
      fromChain: 1,                                                 // Ethereum
      toChain: 1,                                                   // Ethereum (同链)
      fromToken: '0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0',     // wstETH
      toToken: 'MORPHO_VAULT_TOKEN_ADDRESS',                        // Morpho 金库代币
      fromAddress: '0xYOUR_WALLET_ADDRESS',
      toAddress: '0xYOUR_WALLET_ADDRESS',
      fromAmount: 'AMOUNT_IN_SMALLEST_UNIT',
    },
  });
  ```
</CodeGroup>

**流程：**

1. wstETH → ETH（如果需要）
2. ETH → USDC（如果需要）
3. USDC → 存入 Morpho 金库
4. 用户收到 Morpho 金库代币

***

## 复杂跨链工作流程

### 多链收益聚合

从多个链收集资产并集中到单个协议中。

<CodeGroup>
  ```bash curl theme={"system"}
  # 示例：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'
  ```

  ```ts TypeScript theme={"system"}
  const quote = await axios.get('https://li.quest/v1/quote', {
    params: {
      fromChain: 137,                                                // Polygon
      toChain: 1,                                                   // Ethereum
      fromToken: '0x0000000000000000000000000000000000001010',      // MATIC
      toToken: '0x7f39C581F595B53c5cb19bD0b3f8dA6c935E2Ca0',       // wstETH
      fromAddress: '0xYOUR_WALLET_ADDRESS',
      toAddress: '0xYOUR_WALLET_ADDRESS',
      fromAmount: '1000000000000000000',                            // 1 MATIC
    },
  });
  ```
</CodeGroup>

***

## 执行复杂工作流程

### 使用 SDK 处理复杂性

```ts theme={"system"}
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 执行

```ts theme={"system"}
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. 错误处理

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

### 2. 进度跟踪

```ts theme={"system"}
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. 成本优化

```ts theme={"system"}
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: ['stargateV2', 'across'], // 偏好快速跨链桥
    },
  },
});
```

***

## 通用模式

所有多步骤配方都遵循相同的模式：

```
GET /quote
  fromChain  = 源链 ID
  toChain    = 目标链 ID（相同或不同）
  fromToken  = 您起始的代币
  toToken    = 最终目标协议代币地址
  fromAmount = 最小单位的金额
  fromAddress = 您的钱包
  toAddress   = 您的钱包（接收最终代币）
```

Composer 自动处理中间步骤：兑换、跨链、授权、存款等。

***

## 下一步

<CardGroup cols={2}>
  <Card title="金库存款配方" icon="vault" href="/composer/recipes/vault-deposits">
    Morpho、Euler、Felix 金库配方
  </Card>

  <Card title="借贷配方" icon="building-columns" href="/composer/recipes/lending">
    Aave V3、HyperLend、Seamless 借贷配方
  </Card>

  <Card title="支持的协议" icon="list" href="/composer/reference/supported-protocols">
    支持的协议和功能的完整列表
  </Card>

  <Card title="跨链模式" icon="bridge" href="/composer/guides/cross-chain-compose">
    深入了解跨链 Composer 流程
  </Card>
</CardGroup>
