Step 2: Get a contract-call quote

Retrieving a valid quote from contract-call-supported routes.

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 route provides us with the best quote based on our needs.

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 routes that can support the requested transaction. Each route also includes the fees associated with that route learn more about fees.

The results for our quote will look like this for a cross-chain transaction:

{
    "routes": [
        {
            "duration": 20,
            "gas": "61288711204646256",
            "quote": {
                "bridgeFee": "15",
                "bridgeFeeInNativeToken": "121432003613139325912",
                "amount": "34403295594201661",
                "decimals": 18,
                "amountUSD": "63.577",
                "bridgeFeeUSD": "0.0000000000000277197",
                "bridgeFeeInNativeTokenUSD": "83.358",
                "fees": [
                    {
                        "type": "bridge",
                        "amount": "15",
                        "amountUSD": "0.0000000000000277197",
                        "chainSlug": "ethereum",
                        "tokenSymbol": "stETH",
                        "tokenAddress": "0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84",
                        "decimals": 18,
                        "deductedFromSourceToken": true
                    },
                    {
                        "type": "bridge",
                        "amount": "121432003613139325912",
                        "amountUSD": "83.358",
                        "chainSlug": "polygon",
                        "tokenSymbol": "MATIC",
                        "tokenAddress": "0x0000000000000000000000000000000000000000",
                        "decimals": 18,
                        "deductedFromSourceToken": false
                    },
                    {
                        "type": "gas",
                        "amount": "61288711204646256",
                        "amountUSD": "0.04207",
                        "chainSlug": "polygon",
                        "tokenSymbol": "MATIC",
                        "tokenAddress": "0x0000000000000000000000000000000000000000",
                        "decimals": 18,
                        "deductedFromSourceToken": false
                    },
                    {
                        "type": "partner",
                        "amount": "1000000000000000000",
                        "amountUSD": "0.686",
                        "chainSlug": "polygon",
                        "tokenSymbol": "MATIC",
                        "tokenAddress": "0x0000000000000000000000000000000000000000",
                        "decimals": 18,
                        "deductedFromSourceToken": true
                    }
                ]
            },
            "route": [
                {
                    "bridge": "stargate",
                    "bridgeTokenAddress": "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
                    "steps": [
                        "allowance",
                        "approve",
                        "send"
                    ],
                    "name": "USDC",
                    "part": 100
                }
            ],
            "distribution": {
                "stargate": 1
            },
            "gasUSD": "0.04207"
        }
    ],
    "fromToken": {
        "symbol": "MATIC",
        "address": "0x0000000000000000000000000000000000000000",
        "name": "MATIC",
        "decimals": 18
    },
    "fromChain": {
        "chainId": 137,
        "name": "Polygon",
        "slug": "polygon",
        "protocolType": "evm"
    },
    "toToken": {
        "symbol": "stETH",
        "address": "0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84",
        "name": "stETH"
    },
    "toChain": {
        "chainId": 1,
        "name": "Ethereum",
        "slug": "ethereum",
        "protocolType": "evm"
    }
}