Testing your integration

Testnets

We recommend using mainnets like Gnosis and Polygon for the following reasons:

  • Limited User Engagement: There are few users actively utilizing testnets

  • Maintenance Issues: Testnets often experience interruptions, especially during significant updates

  • Insufficient Liquidity: Tokens on testnets generally lack the liquidity needed for thorough testing

  • Cost-Effectiveness: Testing on mainnets like Gnosis and Polygon is more reliable and cost-effective

Simulating Interactions with Tenderly

You can simulate your interactions with LI.FI using Tenderly. Below are two methods to perform simulations

Using the Tenderly Interface

  1. Create a Tenderly Account:

    • Sign up for a Tenderly account

    • Create a new project in the Tenderly dashboard

  2. Access the Simulation Feature:

    • Navigate to the Simulation Feature tab

    • Fill in the required fields using data extracted from the quote response. Map the necessary fields from the returned transactionRequest object

  3. Run the Simulation:

    • Click Run Simulation to execute the transaction simulation

Code Implementation

This option involves using the Tenderly API for simulations

Prerequisites

  • Tenderly Account: Ensure you have an account and project set up on Tenderly

  • Tenderly API Key: Obtain your API key from the Tenderly dashboard

  • Optional: Install the Tenderly CLI:

    npm install -g @tenderly/cli

Example Code

The following example demonstrates how to implement a transaction simulation using the Tenderly API with Node.js

Network ID: Ensure you're using the correct network ID

Tenderly Headers: Include your X-Access-Key in the API request headers

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

// Replace with your Tenderly project details
const TENDERLY_PROJECT = "your-tenderly-project";
const TENDERLY_ACCESS_KEY = "your-tenderly-api-key";
const TENDERLY_USER = "your-tenderly-username";

// LI.FI API URL
const API_URL = 'https://li.quest/v1';

// Tenderly API URL for simulation
const TENDERLY_API_URL = `https://api.tenderly.co/api/v1/account/${TENDERLY_USER}/project/${TENDERLY_PROJECT}/simulate`;

// Get a quote for your desired transfer
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;
}

// Simulate the transaction using Tenderly
const simulateTransaction = async (transactionRequest) => {
    const payload = {
        "network_id": "100", // Use the appropriate network ID 
        "from": transactionRequest.from,
        "to": transactionRequest.to,
        "input": transactionRequest.data,
        "gas": transactionRequest.gasLimit.toString(),
        "gas_price": transactionRequest.gasPrice.toString(),
        "value": transactionRequest.value.toString()
    };

    const response = await axios.post(TENDERLY_API_URL, payload, {
        headers: {
            'X-Access-Key': TENDERLY_ACCESS_KEY
        }
    });

    return response.data;
}

// Example usage
const run = async () => {
    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 quote = await getQuote(fromChain, toChain, fromToken, toToken, fromAmount, fromAddress);
    
    // Simulate transaction using Tenderly
    const simulationResult = await simulateTransaction(quote.transactionRequest);
    
    console.log('Simulation Result:', simulationResult);
}

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

Last updated

Was this helpful?