Execute a Same Chain Swap

This guide walks you through fetching a quote, checking token allowance, generating transactions, signing, and broadcasting them using expand.network by Blockdaemon DeFi API.




Step 1: Check the Swap Quote

To begin a token swap, retrieve and compare quotes from multiple DEX protocols to ensure you get the best available rate for your token pair.

API: https://svc.blockdaemon.com/defi/v1/dex/quotes

Sample Request

The DEX Swap Quotes API fetches token swap quotes from multiple DEX protocols supported by Blockdaemon on a specified blockchain network. It enables users to compare swap rates across different DEXes for a specific token pair and input amount.

curl --request GET \
  --url 'https://svc.blockdaemon.com/defi/v1/dex/quotes?chainId=1&path=0xdac17f958d2ee523a2206206994597c13d831ec7,0x6B175474E89094C44Da98b954EedeAC495271d0F&amountIn=10000' \
  --header 'X-API-Key: YOUR_API_KEY' \
  --header 'Accept: application/json'

Parameters

NameDescription
chainIdThe unique ID used to direct queries or transactions to the correct blockchain (e.g., 1 for Ethereum, 56 for BSC).

Find a list of the supported chains here.
pathArray of token addresses (e.g., tokenIn,tokenOut)
amountInAmount of the input token, in wei (e.g., 1000000000000000000 for 1 ETH).

👍

Adjusting Slippage

You can adjust the slippage tolerance in subsequent steps during the execution of the swap by using the slippage or amountOutMin parameters.

Sample Response

The response contains an array of quotes sorted by amountsOut with the first entry represents the best quote (highest output amount for the given input).

{
  "status": 200,
  "msg": "success",
  "data": [
    {
      "dexName": "0x",
      "dexId": "1600",
      "amountIn": "1000000",
      "path": [
        "0xdac17f958d2ee523a2206206994597c13d831ec7",
        "0x6B175474E89094C44Da98b954EedeAC495271d0F"
      ],
      "amountsOut": [
        "1000000",
        "1002502103426469968"
      ]
    },
    {
      "dexName": "SushiswapV2",
      "dexId": "1100",
      "amountIn": "1000000",
      "path": [
        "0xdac17f958d2ee523a2206206994597c13d831ec7",
        "0x6B175474E89094C44Da98b954EedeAC495271d0F"
      ],
      "amountsOut": [
        "1000000",
        "1002189910250308204"
      ]
    },
    {
      "dexName": "Kyberswap",
      "dexId": "2200",
      "amountIn": "1000000",
      "path": [
        "0xdac17f958d2ee523a2206206994597c13d831ec7",
        "0x6B175474E89094C44Da98b954EedeAC495271d0F"
      ],
      "amountsOut": [
        "1000000",
        "1002189910250308204"
      ]
    },
    {
      "dexName": "CurveV2",
      "dexId": "1500",
      "amountIn": "1000000",
      "path": [
        "0xdac17f958d2ee523a2206206994597c13d831ec7",
        "0x6B175474E89094C44Da98b954EedeAC495271d0F"
      ],
      "amountsOut": [
        "1000000",
        "1000399509916717049"
      ]
    },
    {
      "dexName": "BalancerV2",
      "dexId": "1400",
      "amountIn": "1000000",
      "path": [
        "0xdac17f958d2ee523a2206206994597c13d831ec7",
        "0x6B175474E89094C44Da98b954EedeAC495271d0F"
      ],
      "amountsOut": [
        "1000000",
        "1000363941967920384"
      ]
    },
    {
      "dexName": "UniswapV3",
      "dexId": "1300",
      "amountIn": "1000000",
      "path": [
        "0xdac17f958d2ee523a2206206994597c13d831ec7",
        "0x6B175474E89094C44Da98b954EedeAC495271d0F"
      ],
      "amountsOut": [
        "1000000",
        "999896502154908225"
      ]
    },
    {
      "dexName": "UniswapV2",
      "dexId": "1000",
      "amountIn": "1000000",
      "path": [
        "0xdac17f958d2ee523a2206206994597c13d831ec7",
        "0x6B175474E89094C44Da98b954EedeAC495271d0F"
      ],
      "amountsOut": [
        "1000000",
        "996499217880095575"
      ]
    }
  ]
}

Response Fields

FieldDescription
dexId, dexNameIdentifier and name of the DEX protocol (or bridgeId and bridgeName for bridges).
amountInThe amount of the input token to swap (in wei).
pathAn array of token addresses of the swap path (e.g., input and output tokens).
amountsOutAn array with two elements: the input amount and the expected output amount.

Step 2: Check Allowance & Execute the Swap

API: https://svc.blockdaemon.com/defi/v1/dex/swap-with-approval

Sample Request

The DEX Swap With Approval API generates transaction payloads to perform a same chain swap on a supported DEX. It checks the current token allowance for the DEX router and adjusts the response accordingly.

curl --request POST \
  --url https://svc.blockdaemon.com/defi/v1/dex/swap-with-approval \
  --header 'X-API-Key: YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{
    "dexId": "1600",
    "path": [
      "0xdac17f958d2ee523a2206206994597c13d831ec7",
      "0x6B175474E89094C44Da98b954EedeAC495271d0F"
    ],
    "amountIn": "1000000",
    "amountOutMin": "0",
    "to": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
    "from": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
}'

Parameters

FieldDescription
dexIdThe identifier of the DEX protocol. Find a list of the supported DEXs here .
pathAn array of token addresses of the swap path (e.g., input and output tokens).
amountInThe amount of the input token to swap (in wei).
amountOutMinThe minimum acceptable output amount (set to 0 to accept any amount).
toThe recipient address for the output tokens.
fromThe address initiating the swap.

👍

Optimizing Amount

Set amountOutMin based on the quote from Step 1 to ensure a favorable swap rate.

Sample Response

The response includes an array of transactions:

  • If the allowance is less than amountIn, you'll get both an approval transaction and a swap transaction.
  • If the allowance is sufficient, only the swap transaction will be returned.
{
  "status": 200,
  "msg": "success",
  "data": [
    {
      "chainId": "1",
      "from": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
      "to": "0xdac17f958d2ee523a2206206994597c13d831ec7",
      "gas": "58723",
      "value": "0",
      "data": "0x095ea7b300000000000000000000000082d88875d64d60cbe9cbea47cb960ae0f04ebd4d00000000000000000000000000000000000000000000000000000000000f4240",
      "estimationCheck": true,
      "referenceId": "5969b1f73b124e819069f0e7bcdc4203"
    },
    {
      "chainId": "1",
      "from": "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
      "to": "0x82d88875d64d60cbe9cbea47cb960ae0f04ebd4d",
      "gas": "500000",
      "value": "0",
      "data": "0x1fff991f000000000000000000000000d8da6bf26964af9d7eed9e03e53415d37aa960450000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000db9f4786c17703200000000000000000000000000000000000000000000000000000000000000a0a3e0b9fc72d75e480ef4a39fa1a07800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000c4ca9e5d0f00000000000000000000000082d88875d64d60cbe9cbea47cb960ae0f04ebd4d000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000000027100000000000000000000000003058ef90929cb8180174d74c507176cca6835d7300000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000dd82b619c369a50000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000064c876d21d000000000000000000000000f5c4f3dc02c3fb9279495a8fef7b0741da9561570000000000000000000000006b175474e89094c44da98b954eedeac495271d0f0000000000000000000000000000000000000000000000000fe3339a2c36761d00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000012438c9c1470000000000000000000000006b175474e89094c44da98b954eedeac495271d0f000000000000000000000000000000000000000000000000000000000000000f0000000000000000000000006b175474e89094c44da98b954eedeac495271d0f000000000000000000000000000000000000000000000000000000000000002400000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000044a9059cbb000000000000000000000000ad01c20d5886137e056775af56915de824c8fce500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffc1fb425e00000000000000000000000082d88875d64d60cbe9cbea47cb960ae0f04ebd4d000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec700000000000000000000000000000000000000000000000000000000000f42400000000000000000000000000000000000006e898131631616b1779bad70bc0500000000000000000000000000000000000000000000000000000000685bbed100000000000000000000000000000000000000000000000000000000000000c0",
      "estimationCheck": true,
      "referenceId": "5969b1f73b124e819069f0e7bcdc4203"
    }
  ]
}

Response Fields

FieldDescription
dataArray of transaction objects:
– If two transactions are returned, the first is the approval transaction and the second is the swap transaction.
– If one transaction is returned, it is the swap transaction.
chainIdChain ID for the transaction (source chain).
fromSender’s address.
toContract address (token contract or bridge router).
valueAmount of native token to send with the transaction (e.g., for gas fees, in wei).
gasEstimated gas limit for the transaction.
dataEncoded transaction data (e.g., function call and arguments).
referenceIdA unique identifier for tracking the request.

👍

Configuring allowance limits

While the DEX Swap with Approval API is convenient, some use-cases require customization of allowance limits. For this we offer individual Allowance and DEX Swap APIs


Step 3: Sign the Transaction

Once you’ve received the approval and swap transaction payloads, you need to sign each transaction before broadcasting it to the blockchain. Signing ensures that the transaction is authorized using the sender’s private key and is valid for execution.

Follow the steps below:

  1. Retrieve the approval and swap transaction objects from the response of the DEX Swap with Approval API.
  2. Sign each transaction:
    1. Use your preferred wallet (e.g., MetaMask, WalletConnect) or the Blockdaemon SDK
    2. Make sure the from address in the transaction matches the wallet or SDK account initiating the swap.
    3. The result will be a rawTransaction (e.g., a hexadecimal-encoded string).
  3. Verify the signed transaction includes the correct nonce, gas, and to fields to prevent errors during broadcasting.

Step 4: Broadcast the Signed Transaction

Once you've signed the transaction(s), broadcast them to the blockchain to execute the same chain swap.

API: https://svc.blockdaemon.com/tx/v1/{blockchain_id}/send

Sample Request

The Submit a Signed Transaction API broadcasts a signed transaction to the specified blockchain network. This endpoint is used to submit both approval and swap transactions generated in Step 3.

curl --request POST \
  --url https://svc.blockdaemon.com/tx/v1/ethereum-mainnet/send \
  --header 'X-API-Key: YOUR_API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{
    "tx": "0100000001ca19af5fb94ced7e62b623d0039a398a42e60050405a1341efe475894629c131010000008b483045022100d77b002b3142013b3f825a730f5bc3ead2014266f07ba4449269af0cf6f086310220365bca1d616ba86fac42ad69efd5f92c5ed6cf16f27ebf5ab55010efc72c219d014104417eb0abe69db2eca63c84eb44266c29c24973dc81cde16ca86c9d923630cb5f797bae7d7fab13498e06146111356eb271da74add05ebda8f72ff2b2878fddb7ffffffff0410270000000000001976a914344a0f48ca150ec2b903817660b9b68b13a6702688ac204e0000000000001976a914344a0f48ca150ec2b903817660b9b68b13a6702688ac30750000000000001976a914344a0f48ca150ec2b903817660b9b68b13a6702688ac48710000000000001976a914d6fa8814924b480fa7ff903b5ef61100ab4d92fe88ac00000000"
}'

Parameters

ParameterDescription
blockchain_idEither a hyphen-separated protocol-network pair like ethereum-mainnet or the chain-network.
txThe signed transaction.

Sample Response

The response includes the transaction hash, which you can use to track its status via the Get a Transaction API.

{
  "id": "9c8ac345b443dd10a418ea0beaa320ef233dbae5590be2a11ac090e0e9839c1c"
}

Step 5: Verify the Transaction

After executing the same chain swap, monitor the transaction to confirm its status and ensure the successful transfer of tokens.

API: https://svc.blockdaemon.com/universal/{version}/{protocol}/{network}/tx/{hash}

Sample Request

The Get a Transaction API verifies the status and details of a swap transaction after it's been signed and broadcast.

curl --request GET \
  --url https://svc.blockdaemon.com/universal/v1/bitcoin/mainnet/tx/71d4f3412ec11128bbd9ce988d5bff2ec3bb6ea3953c8faf189d88ae49de9f7a \
  --header 'X-API-Key: YOUR_API_KEY' \
  --header 'Content-Type: application/json'

Parameters

FieldDescription
protocolThe protocol used for the transaction.
networkThe blockchain network where the transaction was executed (e.g., ethereum-mainnet, polygon-mainnet).
hashThe unique transaction hash generated after signing and broadcasting the swap transaction. You can use this to track the transaction on a block explorer. See Step 4.

Sample Response

It returns detailed information about a specific transaction on a blockchain network.

{
  "id": "71d4f3412ec11128bbd9ce988d5bff2ec3bb6ea3953c8faf189d88ae49de9f7a",
  "block_id": "000000000000000000031911bca2ecf6c19ede467d81659f0c43a420ede1fd21",
  "date": 1667882555,
  "status": "completed",
  "num_events": 4,
  "meta": {
    "vsize": 144
  },
  "block_number": 762213,
  "confirmations": 140333,
  "events": [
    {
      "id": "71d4f3412ec11128bbd9ce988d5bff2ec3bb6ea3953c8faf189d88ae49de9f7a-fee",
      "transaction_id": "71d4f3412ec11128bbd9ce988d5bff2ec3bb6ea3953c8faf189d88ae49de9f7a",
      "type": "fee",
      "denomination": "BTC",
      "date": 1667882555,
      "amount": 147,
      "decimals": 8
    },
    {
      "id": "input-55aeae5f9438ab72b52e51d0ebbe85d4509b6a853a60ebabe7bbd9dd33648c82-1",
      "transaction_id": "71d4f3412ec11128bbd9ce988d5bff2ec3bb6ea3953c8faf189d88ae49de9f7a",
      "type": "utxo_input",
      "denomination": "BTC",
      "source": "bc1qex0aqq8mxqfh4cpl62eg755836djjx20yzuuu8",
      "meta": {
        "addresses": [
          "bc1qex0aqq8mxqfh4cpl62eg755836djjx20yzuuu8"
        ],
        "index": 1,
        "script": "0014c99fd000fb30137ae03fd2b28f52878e9b29194f",
        "script_type": "witness_v0_keyhash",
        "input_index": 0
      },
      "date": 1667882555,
      "amount": 81736,
      "decimals": 8
    },
    {
      "id": "71d4f3412ec11128bbd9ce988d5bff2ec3bb6ea3953c8faf189d88ae49de9f7a-0",
      "transaction_id": "71d4f3412ec11128bbd9ce988d5bff2ec3bb6ea3953c8faf189d88ae49de9f7a",
      "type": "utxo_output",
      "denomination": "BTC",
      "destination": "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa",
      "meta": {
        "addresses": [
          "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"
        ],
        "index": 0,
        "script": "76a91462e907b15cbf27d5425399ebf6f0fb50ebb88f1888ac",
        "script_type": "pubkeyhash"
      },
      "date": 1667882555,
      "amount": 558,
      "decimals": 8
    },
    {
      "id": "71d4f3412ec11128bbd9ce988d5bff2ec3bb6ea3953c8faf189d88ae49de9f7a-1",
      "transaction_id": "71d4f3412ec11128bbd9ce988d5bff2ec3bb6ea3953c8faf189d88ae49de9f7a",
      "type": "utxo_output",
      "denomination": "BTC",
      "destination": "bc1qex0aqq8mxqfh4cpl62eg755836djjx20yzuuu8",
      "meta": {
        "addresses": [
          "bc1qex0aqq8mxqfh4cpl62eg755836djjx20yzuuu8"
        ],
        "index": 1,
        "script": "0014c99fd000fb30137ae03fd2b28f52878e9b29194f",
        "script_type": "witness_v0_keyhash"
      },
      "date": 1667882555,
      "amount": 81031,
      "decimals": 8
    }
  ]
}

Response Fields

FieldDescription
idUnique transaction ID (same as hash).
block_idIdentifier of the block where this transaction was included.
block_numberBlock height where the transaction was confirmed.
dateUnix timestamp of when the transaction occurred.
statusStatus of the transaction (e.g., "completed").
confirmationsNumber of confirmations since the transaction was included in a block.
num_eventsTotal number of events (e.g., inputs, outputs, fees) associated with the transaction.
meta.vsizeVirtual size of the transaction in vbytes (used for fee calculation in Bitcoin).
events[]Array of transaction-related events such as UTXO inputs, outputs, and fee.

👋 Need Help?

Contact us through email or our support page for any issues, bugs, or assistance you may need.