Links
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.
Typescript
cURL
// 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
1
{
2
"requestId": "23ca9a78-c282-48e8-8744-32060b9fdab9",
3
"resultType": "OK",
4
"error": null,
5
"tx": {
6
"type": "EVM",
7
"blockChain": {
8
"name": "BSC",
9
"defaultDecimals": 18,
10
"addressPatterns": [
11
"^(0x)[0-9A-Fa-f]{40}$"
12
],
13
"feeAssets": [
14
{
15
"blockchain": "BSC",
16
"symbol": "BNB",
17
"address": null
18
}
19
],
20
"type": "EVM",
21
"chainId": "56"
22
},
23
"from": "0xbe807dddb074639cd9fa61b47676c064fc50d62c",
24
"txTo": "0x69460570c93f9DE5E2edbC3052bf10125f0Ca22d",
25
"approveTo": null,
26
"approveData": null,
27
"txData": "0x28ad014c0000000000000000000000000000000023ca9a78c28248e8874432060b9fdab9000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055d398326f99059ff775485246999027b319795500000000000000000000000000000000000000000000000001633c600f176000000000000000000000000000000000000000000000000000000009184e72a0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014c1f5abc8acb55e8000000000000000000000000000000000000000000000000000000000000006a00000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000be807dddb074639cd9fa61b47676c064fc50d62c000000000000000000000000000000000000000000000000000000000000a86a0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e00000000000000000000000010ed43c718714eb63d5aa57b78b54704e256024e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055d398326f99059ff775485246999027b3197955000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001633c600f17600000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000e47ff36ab50000000000000000000000000000000000000000000000014c1f5abc8acb55e8000000000000000000000000000000000000000000000000000000000000008000000000000000000000000069460570c93f9de5e2edbc3052bf10125f0ca22d0000000000000000000000000000000000000000000000000000000064c7c4db0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c00000000000000000000000055d398326f99059ff775485246999027b319795500000000000000000000000000000000000000000000000000000000",
28
"value": "0x16345785d8a0000",
29
"gasLimit": "0x60488",
30
"gasPrice": "3300000000",
31
"priorityGasPrice": null,
32
"maxGasPrice": null
33
},
34
// route section is similar to the quote response
35
"route": {
36
"outputAmount": "24290719",
37
"outputAmountMin": "23567462",
38
"outputAmountUsd": 24.290507844043603,
39
"swapper": {
40
"id": "Hyphen Aggregator",
41
"title": "Hyphen",
42
"logo": "https://api.rango.exchange/swappers/hyphen.svg",
43
"swapperGroup": "Hyphen",
44
"types": [
45
"AGGREGATOR"
46
],
47
"enabled": true
48
},
49
"from": {
50
"blockchain": "BSC",
51
"symbol": "BNB",
52
"name": null,
53
"isPopular": false,
54
"chainId": "56",
55
"address": null,
56
"decimals": 18,
57
"image": "https://api.rango.exchange/tokens/ETH/BNB.png",
58
"blockchainImage": "https://api.rango.exchange/blockchains/bsc.svg",
59
"usdPrice": 244.07556880547276,
60
"supportedSwappers": []
61
},
62
"to": {
63
"blockchain": "AVAX_CCHAIN",
64
"symbol": "USDT.E",
65
"name": null,
66
"isPopular": false,
67
"chainId": "43114",
68
"address": "0xc7198437980c041c805a1edcba50c1ce5db95118",
69
"decimals": 6,
70
"image": "https://api.rango.exchange/i/niIihm",
71
"blockchainImage": "https://api.rango.exchange/blockchains/avax_cchain.svg",
72
"usdPrice": 0.999991307134367,
73
"supportedSwappers": []
74
},
75
"fee": [
76
{
77
"token": {
78
"blockchain": "BSC",
79
"symbol": "BNB",
80
"name": null,
81
"isPopular": true,
82
"chainId": "56",
83
"address": null,
84
"decimals": 18,
85
"image": "https://api.rango.exchange/tokens/ETH/BNB.png",
86
"blockchainImage": "https://api.rango.exchange/blockchains/bsc.svg",
87
"usdPrice": 244.07556880547276,
88
"supportedSwappers": [
89
"Wormhole",
90
"ParaSwap Bsc",
91
"OneInchBsc",
92
"Satellite",
93
"PancakeSwapBsc",
94
"Voyager",
95
"cBridge v2.0"
96
]
97
},
98
"expenseType": "FROM_SOURCE_WALLET",
99
"amount": "658198200000000",
100
"name": "Network Fee"
101
}
102
],
103
"feeUsd": 0.16065010005173833,
104
"amountRestriction": {
105
"min": "42388885336539702",
106
"max": "239517779280253452012",
107
"type": "EXCLUSIVE"
108
},
109
"estimatedTimeInSeconds": 75,
110
"path": [
111
// similar to the quote
112
]
113
}
114
}
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