Cross-chain Contract Calls - API Guide

Swing API provides a cross-chain contract feature that allows developers to execute contract calls on different blockchains, enabling interoperability between blockchain ecosystems.

Learn More about Cross-Chain Contract Calls

The full API reference for our /quote, /approve, /allowance and /send endpoints can be found in the API reference section.

In this section, we will discuss the process of executing a cross-chain contract call transaction which involves exchanging assets and sending them across one or multiple blockchain networks. Accomplishing a successful transaction requires making calls to our endpoints to move the transaction through its various stages.

Stages:

  1. Retrieving a valid quote from a contract-call-supported bridge
  2. Getting approval for native token transfers/swaps
  3. Initiating transaction given the prior quote on a specific bridge
  4. Waiting for the transaction to complete by polling for transaction status
  5. Transaction Complete!

Step 1: Set up your project with Swing Platform

Using a Swing Platform allows you to:

  • Set custom configurations (supported chains and tokens)
  • Access transaction metrics and dashboard
  • Set up Partner Fees (Coming soon)

Create a project in Swing Platform and use your unique ProjectID when calling the following APIs /quote, /send, /config, /config/token_pairs.

Learn how to set up a project, Learn about Swing Platform

Step 2: Get a quote

Retrieving a valid quote from a contract call supported bridge

The full API reference for our /quote endpoint can be found in the API reference section.

This is the first prerequisite step for any transaction on the Swing API. We want to determine which bridge provides us with the best quote based on our needs (whether that’s price, speed or low fees).

Our quoting system requires all the following required information:

  • Source chain
  • Destination chain
  • Source Token on the Source chain
  • Destination Token on the Destination chain
  • The amount you want to send
  • Contract Call flag
  • Source Wallet Address to pull funds from

We can query our /quote endpoint in the following manner given all the necessary information:

const getQuote = async (params) => {
  const result = await axios.get(
    'https://swap.prod.swing.xyz/v0/transfer/quote',
    {
      fromChain: params.fromChain.slug,
      fromChainId: params.fromChain.chainId,
      tokenSymbol: params.fromToken.symbol,
      fromTokenAddress: params.fromToken.address,

      toChain: params.toChain.slug,
      toChainId: params.toChain.chainId,
      toTokenSymbol: params.toToken.symbol,
      toTokenAddress: params.toToken.address,

      fromUserAddress: params.userAddress,
      tokenAmount: params.tokenAmount,
      contractCall: true,
      projectId: params.projectid, // create your project here: https://platform.swing.xyz/
    }
  )
  return result.data
}

The results for our quote will include the “routes” property which will have all the possible bridges that can support the requested transaction. Each route also includes the fees associated with that route (learn more about fees).

Step 3: Get token approval

This section is only applicable to transfers of non-native tokens. Non-native tokens include governance tokens, wrapped tokens, stablecoins, and oracle tokens.

The full API reference for our /approve endpoint can be found in the API reference section.

If you are performing a transfer with a non-native token, our Swing Contract requires the right allowance limit to spend/withdraw funds (denominated in non-native tokens) from the designated wallet address. Depending on the non-native token amount you are looking to transfer, you want to call our /approve endpoint with a limit greater than or equal to the amount you are transferring.

You can use the /allowance endpoint to determine if you have already approved a sufficient limit to allow your non-native token transfer. Importantly, the allowance limit is specific to the token, chain and bridge. So you must approve an allowance limit every time for every unique transfer combination of token, chain or bridge.

Allowance Endpoint

We can query our /allowance endpoint in the following manner to get the allowance limit:

const getAllowance = async (params) => {
  const result = await axios.get(
    'https://swap.prod.swing.xyz/v0/transfer/allowance',
    {
      fromChain: params.fromChain.slug,
      fromChainId: params.fromChain.chainId,
      tokenSymbol: params.fromToken.symbol,
      tokenAddress: params.fromToken.address,
      bridge: params.bridge,
      fromAddress: params.userAddress,
      contractCall: true,
    }
  )
  return result.data.allowance
}

This will always only return the allowance limit for that specific token, on the specific chain for that specific bridge:

{
  "allowance": "1000000"
}

Approve Endpoint

If you need to approve a higher allowance limit, you can query our /approve endpoint in the following manner:

const getApprovalCallData = async (params) => {
  const result = await axios.get(
    'https://swap.prod.swing.xyz/v0/transfer/approve',
    {
      fromChain: params.fromChain.slug,
      fromChainId: params.fromChain.chainId,
      tokenSymbol: params.fromToken.symbol,
      tokenAddress: params.fromToken.address,
      toChain: params.toChain.slug,
      toChainId: params.toChain.chainId,
      bridge: params.bridge,
      fromAddress: params.userAddress,
      tokenAmount: params.amount,
      contractCall: true,
    }
  )
  return result.data
}

Since approve is a contract-level function, Swing only returns the necessary call-data that will need to be signed and executed by the local wallet (ie. Metamask, Coinbase Wallet, WalletConnect, etc).

{
  "data": "0x095ea7b30000000000000000000000008ea232420493c05b5dfcb329b33d0c2a334d3eb40000000000000000000000000000000000000000000000004563918244f40000",
  "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
  "from": "0x184AbEfBdCa24Ce0Dd964a74f6d5E69CE44D9579",
  "fromChain": {
    "chainId": 1,
    "slug": "ethereum",
    "name": "Ethereum",
    "protocolType": "evm"
  }
}

Step 4: Initiate transfer transaction

Initiate transaction given the prior quote on a specific bridge.

The full API reference for our /send endpoint can be found in the API reference section.

Similar to our quoting system, to initiate a transaction it requires all the following required information:

  • Bridge
  • Source chain
  • Destination chain
  • Source Token on the Source chain
  • Destination Token on the Destination chain
  • The amount you want to send
  • Source Wallet Address to pull funds from
  • Contract Call Information

Since initiating a transaction requires invoking a contract-level function on your supplied wallet address, Swing only returns the necessary call-data that will need to be signed and executed by the local wallet (i.e. Metamask, WalletConnect, Coinbase Wallet).

const sendTransaction = async (params) => {
  const result = await axios.post(
    'https://swap.prod.swing.xyz/v0/transfer/send',
    {
      fromChain: params.fromChain.slug,
      fromChainId: params.fromChain.chainId,
      tokenSymbol: params.fromToken.symbol,
      fromTokenAddress: params.fromToken.address,

      toChain: params.toChain.slug,
      toChainId: params.toChain.chainId,
      toTokenSymbol: params.toToken.symbol,
      toTokenAddress: params.toToken.address,

      fromUserAddress: params.userAddress,
      tokenAmount: params.tokenAmount,
      projectId: params.projectid, // create your project here: https://platform.swing.xyz/

      route: [
        {
          bridge: params.route.bridge,
          bridgeTokenAddress: params.route.tokenAddress,
          name: params.route.tokenName,
          part: 100,
        },
      ],

      contractCallInfo: [
        {
          toContractAddress: params.toContractAddress,
          toContractCallData: params.toContractCallData,
          outputTokenAddress: params.outputTokenAddress,
        },
      ],
    }
  )
  return result.data
}

Step 5: Watch a transaction status

Waiting for a transaction to complete by polling for transaction status.

The full API reference for our /status endpoint can be found in the API reference section.

Once you have a transaction submitted and pending, you will need to monitor the status of the transaction as you wait. This involves polling our /status endpoint to actively watch the status updates. For more information on fetching transaction status, refer to our guide on "How to Check Status of a Transfer".