Li.Fi 文档中文版beta
  • 🏠什么是 LI.FI?
  • ⭐为什么选择 LI.FI?
  • ❓常问问题
  • 💡用例
  • ✅支持的链、桥和交易所
  • 整合
    • 👋开始使用
    • 🛠️LI.FI SDK
      • 准备转移
        • 路线选项
        • 获取有关可用服务的信息
      • 执行路由
      • 高级功能
        • 处理链式开关
        • 加密和解密呼叫数据
      • 加载钱包余额
      • LiFi SDK 接口
    • ⚙️LI.FI API
      • 转移代币
      • 获取代币信息
      • 完整的 API 参考
  • 对于特殊的整合伙伴
    • DEX 和 DEX 聚合器
    • 钱包
  • 简单的整合
    • 🔁LI.FI 链接参数化
    • 🖼️LI.FI 小工具
Powered by GitBook
On this page
  • 第 1 步:请求报价
  • 第 2 步:发送交易
  • 第 3 步:等待传输完成
  • 所有东西都塞在一起
  • 检查和设置津贴
  1. 整合
  2. LI.FI API

转移代币

通过一个或多个链从一个代币转移到另一个代币很简单。

以下部分将逐步解释如何跨一条或多条链转移代币。

第 1 步:请求报价

从 Gnosis 上的 1 USDC 转移到 Polygon 上的 USDC 的请求如下所示:

const getQuote = async (fromChain, toChain, fromToken, toToken, fromAmount, fromAddress) => {
    const result = await axios.get('https://li.quest/v1/quote', {
        params: {
            fromChain,
            toChain,
            fromToken,
            toToken,
            fromAmount,
            fromAddress,
        }
    });
    return result.data;
}

const fromChain = 'DAI';
const fromToken = 'USDC';
const toChain = 'POL';
const toToken = 'USDC';
const fromAmount = '1000000';
const fromAddress = YOUR_WALLET_ADDRESS;

const quote = await getQuote(fromChain, toChain, fromToken, toToken, fromAmount, fromAddress);

报价响应包含一个 transactionRequest 对象,可以直接传递给您的钱包/签名者。

第 2 步:发送交易

收到报价后,必须发送交易以触发转移。

首先,必须配置钱包。 以下示例将您的钱包连接到 Gnosis 链。

const provider = new ethers.providers.JsonRpcProvider('https://rpc.xdaichain.com/', 100);
const wallet = ethers.Wallet.fromMnemonic(YOUR_PERSONAL_MNEMONIC).connect(
    provider
);

之后,可以使用先前检索到的报价中的 transactionRequest 发送交易:

const tx = await wallet.sendTransaction(quote.transactionRequest);
await tx.wait();

对于单链上的简单交换,这就足够了。 对于跨链传输,处理需要更长的时间。 为了处理这个问题,API 提供了一个端点来检查传输状态。

第 3 步:等待传输完成

Checking the status of the transfer is only necessary for cross chain transfers

要检查令牌是否成功发送到接收链,可以调用 /status 端点:

const getStatus = async (bridge, fromChain, toChain, txHash) => {
    const result = await axios.get('https://li.quest/v1/status', {
        params: {
            bridge,
            fromChain,
            toChain,
            txHash,
        }
    });
    return result.data;
}

result = await getStatus(quote.tool, fromChain, toChain, tx.hash);

所有东西都塞在一起

整个过程如下所示:

const ethers = require('ethers');
const axios = require('axios');

const API_URL = 'https://li.quest/v1';

// Get a quote for your desired transfer
const getQuote = async (fromChain, toChain, fromToken, toToken, fromAmount, fromAddress) => {
    const result = await axios.get(`${API_URL}/quote`, {
        params: {
            fromChain,
            toChain,
            fromToken,
            toToken,
            fromAmount,
            fromAddress,
        }
    });
    return result.data;
}

// Check the status of your transfer
const getStatus = async (bridge, fromChain, toChain, txHash) => {
    const result = await axios.get(`${API_URL}/status`, {
        params: {
            bridge,
            fromChain,
            toChain,
            txHash,
        }
    });
    return result.data;
}

const fromChain = 'DAI';
const fromToken = 'USDC';
const toChain = 'POL';
const toToken = 'USDC';
const fromAmount = '1000000';
const fromAddress = YOUR_WALLET_ADDRESS;

// Set up your wallet
const provider = new ethers.providers.JsonRpcProvider('https://rpc.xdaichain.com/', 100);
const wallet = ethers.Wallet.fromMnemonic(YOUR_PERSONAL_MNEMONIC).connect(
    provider
);

const run = async () => {
    const quote = await getQuote(fromChain, toChain, fromToken, toToken, fromAmount, fromAddress);
    const tx = await wallet.sendTransaction(quote.transactionRequest);

    await tx.wait();

    // Only needed for cross chain transfers
    if (fromChain !== toChain) {
        let result;
        do {
            result = await getStatus(quote.tool, fromChain, toChain, tx.hash);
        } while (result.status !== 'DONE' && result.status !== 'FAILED')
    }
}

run().then(() => {
    console.log('DONE!')
});

检查和设置津贴

在发送任何交易之前,必须确保用户允许从他的钱包发送请求的金额。

这可以这样实现:

const { Contract } = require('ethers');

const ERC20_ABI = [
    {
        "name": "approve",
        "inputs": [
            {
                "internalType": "address",
                "name": "spender",
                "type": "address"
            },
            {
                "internalType": "uint256",
                "name": "amount",
                "type": "uint256"
            }
        ],
        "outputs": [
            {
                "internalType": "bool",
                "name": "",
                "type": "bool"
            }
        ],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "name": "allowance",
        "inputs": [
            {
                "internalType": "address",
                "name": "owner",
                "type": "address"
            },
            {
                "internalType": "address",
                "name": "spender",
                "type": "address"
            }
        ],
        "outputs": [
            {
                "internalType": "uint256",
                "name": "",
                "type": "uint256"
            }
        ],
        "stateMutability": "view",
        "type": "function"
    }
];

// Get the current the allowance and update if needed
const checkAndSetAllowance = async (wallet, tokenAddress, approvalAddress, amount) => {
    // Transactions with the native token don't need approval
    if (tokenAddress === ethers.constants.AddressZero) {
        return
    }

    const erc20 = new Contract(tokenAddress, ERC20_ABI, wallet);
    const allowance = await erc20.allowance(await wallet.getAddress(), approvalAddress);

    if (allowance.lt(amount)) {
        const approveTx = await erc20.approve(approvalAddress, amount);
        await approveTx.wait();
    }
}

await checkAndSetAllowance(wallet, quote.action.fromToken.address, quote.estimate.approvalAddress, fromAmount);

PreviousLI.FI APINext获取代币信息

Last updated 3 years ago

⚙️