🔄Step-by-step Swap Example

Routing API usage by example

Swap Avalanche (AAVE) for Avalanche (USDT)

This particular route (swap) consists of two transactions. However, the number of transactions required for swapping different coins and across different chains may vary and depend on several factors such as the source coin, the destination coin, and the number of available addresses for signing transactions.

Step 1

In this step, we prepare the data for the chains and assets that our routing system supports. This data enables us to display the source and destination tokens to users and query and display swap route in the next step.

1.1 Get list of supported chains

Explorer query ChainsV2

query ChainsV2 {
  routingV2 {
    chainsV2 {
      name
    }
  }
}

1.2 Get list of assets for supported chain

Explorer query ChainV2

query ChainV2($name: String!) {
  routingV2 {
    chainV2(name: $name) {
      name
      tokens {
        asset {
          contract
          symbol
        }
      }
    }
  }
}

Step 2

Query swap route. This query returns a tradesRoute collection, along with other properties. Based on this data, we can show the user which providers we will use, the amount of fees in dollars and assets for each transaction, and thus calculate the total fees and approximate transaction time in seconds.

Explorer query RouteV2

query RouteV2($srcToken: String!, $destToken: String!, $slippage: String!, $addresses: [AddressRouteInputTypeV2!]!, $destAddress: String!, $amountSource: String, $infiniteApproval: Boolean) {
  routingV2 {
    routeV2(srcToken: $srcToken, destToken: $destToken, slippage: $slippage, addresses: $addresses, destAddress: $destAddress, amountSource: $amountSource, infiniteApproval: $infiniteApproval) {
      addresses {
        chain
        address
      }
      destAddress
      priceRate
      priceRateText
      slippage
      priceImpact
      amountIn
      tradesRoute {
        provider {
          id
        }
        amountIn
        amountOut
        minAmountReceived
        assetIn {
          id
        }
        assetOut {
          id
        }
        fee {
          networkFeeDollar
          networkFeeAsset
          inboundFeeDollar
          inboundFeeAsset
          swapFee
          feeRateTransaction
          xdefiSwapFee
          xdefiSwapFeeDollar
        }
        priceRateUsdAssetOut
        priceRateUsdAssetIn
        tradeType
      }
      gasPrices
      approvalInfiniteFlag
      errorBuildingRoute
    }
  }
}srcToken

Step 3

The mutation creates a new route and generates a unique routeId, which is essential for the subsequent steps to retrieve trades (transactions) related to the current route (swap). The input for this mutation is of type RouteInputTypeV2, and all the necessary properties are obtained from the response of the previous query.

Explorer mutation TransactionsV2

mutation TransactionsV2($routeData: RouteInputTypeV2!) {
  transactionsV2(routeData: $routeData) {
    routeId
  }
}

Step 4

To retrieve trades associated with a particular routeId, which is obtained as a response from the previous step, execute the relevant query. Note that the response to this query will change in subsequent steps, and therefore, it's necessary to periodically poll this query to keep track of the transaction statuses.

Explorer query TradesV2

query TradesV2($routeId: String!) {
  routingV2 {
    tradesV2(routeId: $routeId) {
      transaction {
        chain
        amount
        recipient
        feeRate
        txType
        tradeId
        routeId
        gasPrice
        data
        unsignedStdTx
        gasLimit
        memo
        actions
        signerId
        receiverId
      }
      routeId
      status {
        status
        txHash
      }
    }
  }
}

Step 5

The process of signing, broadcasting, and updating the trade is repeated for each transaction in the trades obtained from the previous step.

5.1 Sign and broadcast transaction to the network.

On this stage, we have a transaction prepared as a response to the previous step's query (TradesV2). We need to sign this transaction and broadcast it in order to obtain its hash.

Transaction hash

5.2 Update route trade by mutation that takes routeId, tradeId, transactionHash.

When we receive the broadcasted transaction hash, we update the trade by performing a mutation that allows us to track the transaction status and obtain the next transaction to sign.

Explorer mutation TransactionHashV2

mutation TransactionHashV2($routeId: String!, $tradeId: String!, $transactionHash: String!) {
  transactionHashV2(routeId: $routeId, tradeId: $tradeId, transactionHash: $transactionHash)
}

Step 6

Re query of trades (step 4) in this step returns different response.

The first trade now has a status of SUCCESS, indicating that the transaction has been confirmed, and the txHash has been assigned the value of the first signed transaction in step 5.2. Right after previous step the status of the transaction could be PENDING and transaction property gets null value. As for the second trade, it now has transaction data that needs to be signed, which we will do in the next step.

{
    "data": {
        "routingV2": {
            "tradesV2": [
                {
                "routeId": "3923c390-ff4a-44b7-b9be-5dd84d538ab9",
                "status": {
                    "status": "SUCCESS",
                    "txHash": "0x7c1c1a08a9607768b8acde0ebd2cb4a3fa0126cb5020e3efef3e5e43307163f8"
                },
                "transaction": null
            },
            {
                "routeId": "3923c390-ff4a-44b7-b9be-5dd84d538ab9",
                "status": {
                    "status": null,
                    "txHash": null
                },
                "transaction": {
                    "chain": "AVAX",
                    "amount": "0",
                    "recipient": "0x1111111254fb6c44bac0bed2854e76f90643097d",
                    "feeRate": "25000000001",
                    "txType": "SWAP",
                    "tradeId": "12902",
                    "gasPrice": "25000000001",
                    "data": "0x7c025200000000000000000000000000f01ef4051130cc8871fa0c17024a6d62e379e8560000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000018000000000000000000000000063a72806098bd3d9520cc43356dd78afe5d386d9000000000000000000000000c7198437980c041c805a1edcba50c1ce5db95118000000000000000000000000c3e6d9f7a3a5e3e223356383c7c055ee3f26a34f0000000000000000000000005329ebc5903be1ca544762191343f60edb5c9ca300000000000000000000000000000000000000000000000003311fc80a57000000000000000000000000000000000000000000000000000000000000010d0fca00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001300000000000000000000000000000000000000000000000000000000000f200a007e5c0d20000000000000000000000000000000000000000000000000000ce00006700206ae40711b8002dc6c0c3e6d9f7a3a5e3e223356383c7c055ee3f26a34f1f0bc5c91518d903c0c097bde9741746b44230080000000000000000000000000000000000000000000000000df489a8970d864b63a72806098bd3d9520cc43356dd78afe5d386d900206ae40711b8001e84801f0bc5c91518d903c0c097bde9741746b44230081111111254fb6c44bac0bed2854e76f90643097d00000000000000000000000000000000000000000000000000000000010d0fcab31f66aa3c1e785363f0875a1b74e27b85fd66c700000000000000000000000000000000000000000000000003311fc80a5700000000000000000000000000000000000014e67b34",
                    "unsignedStdTx": null,
                    "gasLimit": "265913",
                    "routeId": null,
                    "memo": null
                }
            }
            ]
        }
    }
}

Step 7

7.1 Sign and broadcast transaction to the network.

Transaction hash

7.2 Update route trade by mutation that takes routeId, tradeId, transactionHash.

The mutation is the same as in step 5.2 but with variables related two the second transaction.

Explorer mutation TransactionHashV2

mutation TransactionHashV2($routeId: String!, $tradeId: String!, $transactionHash: String!) {
  transactionHashV2(routeId: $routeId, tradeId: $tradeId, transactionHash: $transactionHash)
}

Step 8

Continuously query (step 4) until all trade statuses show as SUCCESS. Once all trades have a SUCCESS status, the current swap is considered complete and the amount of Avalanche (USDT) will be transferred to the destination address.

{
    "data": {
        "routingV2": {
            "tradesV2": [
                {
                    "routeId": "3923c390-ff4a-44b7-b9be-5dd84d538ab9",
                    "status": {
                        "status": "SUCCESS",
                        "txHash": "0x7c1c1a08a9607768b8acde0ebd2cb4a3fa0126cb5020e3efef3e5e43307163f8"
                    },
                    "transaction": null
                },
                {
                    "routeId": "3923c390-ff4a-44b7-b9be-5dd84d538ab9",
                    "status": {
                        "status": "SUCCESS",
                        "txHash": "0xf305ff5b3031797a4404ac535ff90d4e974917f72449711ab9403d78a792b37d"
                    },
                    "transaction": null
                }
            ]
        }
    }
}

Last updated

XDEFI Technologies 2024