3️⃣Swap
Generate data for creating transaction
Whenever the user decides to accept the quote and submit the swap, you should call the SDK swap method to get the latest route and transaction data needed to proceed.
// Swap 0.1 BSC BNB to AVAX_CCHAIN USDT.E
const quote = await rangoClient.swap({
from: {
"blockchain": "BSC",
"symbol": "BNB",
"address": null
},
to: {
"blockchain": "AVAX_CCHAIN",
"symbol": "USDT.E",
"address": "0xc7198437980c041c805a1edcba50c1ce5db95118"
},
amount: "100000000000000000",
fromAddress: "0xbe807dddb074639cd9fa61b47676c064fc50d62c",
toAddress: "0xbe807dddb074639cd9fa61b47676c064fc50d62c",
slippage: '1.5',
disableEstimate: false,
referrerAddress: null, // your dApp wallet address for referral
referrerFee: null, // your dApp desired referral fee percent
})
curl --request GET \
--url 'https://api.rango.exchange/basic/swap?from=BSC.BNB&to=AVAX_CCHAIN.USDT.E--0xc7198437980c041c805a1edcba50c1ce5db95118&amount=100000000000000000&fromAddress=0xbe807dddb074639cd9fa61b47676c064fc50d62c&toAddress=0xbe807dddb074639cd9fa61b47676c064fc50d62c&disableEstimate=true&slippage=1.5&apiKey=c6381a79-2817-4602-83bf-6a641a409e32' \
--header 'accept: */*'
And here is a sample swap response. The route
field is similar to what you've seen in the quote section and the tx
field section is the data of the transaction that needed to be passed to the proper wallet to be signed by the user.
Please check this doc if you want to get complete details on important fields in the swap request:
Configuring dApp Fee
Filtering Swappers
Disabling Estimate
sample swap
{
"requestId": "23ca9a78-c282-48e8-8744-32060b9fdab9",
"resultType": "OK",
"error": null,
"tx": {
"type": "EVM",
"blockChain": {
"name": "BSC",
"defaultDecimals": 18,
"addressPatterns": [
"^(0x)[0-9A-Fa-f]{40}$"
],
"feeAssets": [
{
"blockchain": "BSC",
"symbol": "BNB",
"address": null
}
],
"type": "EVM",
"chainId": "56"
},
"from": "0xbe807dddb074639cd9fa61b47676c064fc50d62c",
"txTo": "0x69460570c93f9DE5E2edbC3052bf10125f0Ca22d",
"approveTo": null,
"approveData": null,
"txData": "0x28ad014c0000000000000000000000000000000023ca9a78c28248e8874432060b9fdab9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055d398326f99059ff775485246999027b319795500000000000000000000000000000000000000000000000001633c600f176000000000000000000000000000000000000000000000000000000009184e72a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014c1f5abc8acb55e8000000000000000000000000000000000000000000000000000000000000006a00000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000be807dddb074639cd9fa61b47676c064fc50d62c000000000000000000000000000000000000000000000000000000000000a86a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055d398326f99059ff775485246999027b3197955000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001633c600f17600000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000e47ff36ab50000000000000000000000000000000000000000000000014c1f5abc8acb55e8000000000000000000000000000000000000000000000000000000000000008000000000000000000000000069460570c93f9de5e2edbc3052bf10125f0ca22d0000000000000000000000000000000000000000000000000000000064c7c4db0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c00000000000000000000000055d398326f99059ff775485246999027b319795500000000000000000000000000000000000000000000000000000000",
"value": "0x16345785d8a0000",
"gasLimit": "0x60488",
"gasPrice": "3300000000",
"priorityGasPrice": null,
"maxGasPrice": null
},
// route section is similar to the quote response
"route": {
"outputAmount": "24290719",
"outputAmountMin": "23567462",
"outputAmountUsd": 24.290507844043603,
"swapper": {
"id": "Hyphen Aggregator",
"title": "Hyphen",
"logo": "https://api.rango.exchange/swappers/hyphen.svg",
"swapperGroup": "Hyphen",
"types": [
"AGGREGATOR"
],
"enabled": true
},
"from": {
"blockchain": "BSC",
"symbol": "BNB",
"name": null,
"isPopular": false,
"chainId": "56",
"address": null,
"decimals": 18,
"image": "https://api.rango.exchange/tokens/ETH/BNB.png",
"blockchainImage": "https://api.rango.exchange/blockchains/bsc.svg",
"usdPrice": 244.07556880547276,
"supportedSwappers": []
},
"to": {
"blockchain": "AVAX_CCHAIN",
"symbol": "USDT.E",
"name": null,
"isPopular": false,
"chainId": "43114",
"address": "0xc7198437980c041c805a1edcba50c1ce5db95118",
"decimals": 6,
"image": "https://api.rango.exchange/i/niIihm",
"blockchainImage": "https://api.rango.exchange/blockchains/avax_cchain.svg",
"usdPrice": 0.999991307134367,
"supportedSwappers": []
},
"fee": [
{
"token": {
"blockchain": "BSC",
"symbol": "BNB",
"name": null,
"isPopular": true,
"chainId": "56",
"address": null,
"decimals": 18,
"image": "https://api.rango.exchange/tokens/ETH/BNB.png",
"blockchainImage": "https://api.rango.exchange/blockchains/bsc.svg",
"usdPrice": 244.07556880547276,
"supportedSwappers": [
"Wormhole",
"ParaSwap Bsc",
"OneInchBsc",
"Satellite",
"PancakeSwapBsc",
"Voyager",
"cBridge v2.0"
]
},
"expenseType": "FROM_SOURCE_WALLET",
"amount": "658198200000000",
"name": "Network Fee"
}
],
"feeUsd": 0.16065010005173833,
"amountRestriction": {
"min": "42388885336539702",
"max": "239517779280253452012",
"type": "EXCLUSIVE"
},
"estimatedTimeInSeconds": 75,
"path": [
// similar to the quote
]
}
}
You might need to confirm the final output amount and route preview if it differs from the last quote that users saw before submitting the swap.
const lastQuoteOutput = new BigNumber(quote.route?.outputAmount)
const swapOutput = new BigNumber(swap.route?.outputAmount)
if (swapOutput.lt(lastQuoteOutput.multipliedBy(new BigNumber(0.96))) {
// show updated route to the user
}
// execute the route
Last updated