Cross-Chain Contract Calls

Cross-Chain Contract Calls: This feature enables contract calls across chains, buy NFTs, stake or donate without needing gas on the destination chain!

Destination calls are currently supported on Polygon, Binance, Optimism, Ethereum, Fantom, Avalanche and Arbitrum.

To read more about what xChain Contract calls are and how they work, please click here.

With this feature, you can make arbitrary contract calls across blockchains. All you need to do is prepare the callData for the contract call you want to make on the destination chain (e.g. stake KLIMA in the example below). Pass that callData along with information about where you want to pay for it to the endpoint. The response contains information on how many tokens must be submitted to pay for all transfer and swap fees.

Parameters

About the contract call:

How does the user want to pay:

Additional options:

Example: stake KLIMA on Polygon

The example below demonstrates how to stake KLIMA token (KlimaDAO) into sKLIMA (staked KLIMA). The process involves

  • sending some DAI token from Binance Smart Chain (BSC) to Polygon (POL)

  • swapping DAI for KLIMA

  • performing a contract call to the KLIMA staking contract.

The callData will be executed by our executer contract, not by the users wallet, so ensure that the contract does not expect the user to be msg.sender. The staked tokens are sent to the executor which will then forward them to the user if the contractOutputsToken parameter is set.

In the end, the contract will produce the staked KLIMA token sKLIMA which will then be transferred back to the user.

import axios from 'axios';
import { ethers } from 'ethers';

const endpoint = 'https://li.quest/v1/quote/contractCall';

const DAI_ON_BSC = '0x1af3f329e8be154074d8769d1ffa4ee058b1dbc3';
const KLIMA_ON_POL = '0x4e78011ce80ee02d2c3e649fb657e45898257815';
const SKLIMA_ON_POL = '0xb0c22d8d350c67420f06f48936654f567c73e8c8';

const KLIMA_STAKING_CONTRACT = '0x4D70a031Fc76DA6a9bC0C922101A05FA95c3A227';

// Full ABI on 
// https://polygonscan.com/address/0x4D70a031Fc76DA6a9bC0C922101A05FA95c3A227#code
const KLIMA_STAKING_ABI = ['function stake(uint _amount) external'];
  
const generateKLIMATransaction = async (receivedAmount: string) => {
    const stakeKlimaTx = await new ethers.Contract(
        KLIMA_STAKING_CONTRACT,
        KLIMA_STAKING_ABI
      ).populateTransaction.stake(receivedAmount);
    return stakeKlimaTx;
};

const getQuote = async (): Promise<any> => {
    // We would like to stake this amount of KLIMA to get sKLIMA
    const stakeAmount = '300000000';

    const stakeKlimaTx = await generateKLIMATransaction(stakeAmount);

    const quoteRequest = {
        fromChain: 'BSC',
        fromToken: DAI_ON_BSC,
        fromAddress: '0x552008c0f6870c2f77e5cC1d2eb9bdff03e30Ea0',
        toChain: 'POL',
        toToken: KLIMA_ON_POL,
        toAmount: stakeAmount,
        toContractAddress: stakeKlimaTx.to,
        toContractCallData: stakeKlimaTx.data,
        toContractGasLimit: '900000',
        contractOutputsToken: SKLIMA_ON_POL,
      };
    
    const response = await axios.post(endpoint, quoteRequest);
    return response.data;
};

getQuote().then(console.log);

Once the quote is generated, you can execute it like any other quote as explained here.

Here you can find links to the sending and receiving transactions for the transfer from DAI on BNB Smart Chain to sKLIMA on Polygon via the custom contract call.

Sending: https://bscscan.com/tx/0x3cbc7e29ad89d0b04a8f59a4851fae17a65b7b5248567da74f5462e28642231f

Receiving: https://polygonscan.com/tx/0xeb399611d324467c9dbd7cf2006e3c73aa982ca19b67b987365e4098162a5a09

Last updated