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.
1
// Swap 0.1 BSC BNB to Fantom FUSDT
2
const swap = await rangoClient.swap({
3
from: {
4
"blockchain": "BSC",
5
"symbol": "BNB",
6
"address": null
7
},
8
to: {
9
"blockchain": "FANTOM",
10
"symbol": "FUSDT",
11
"address": "0x049d68029688eabf473097a2fc38ef61633a3c7a"
12
},
13
amount: "100000000000000000",
14
fromAddress: "0xbe807dddb074639cd9fa61b47676c064fc50d62c",
15
toAddress: "0xbe807dddb074639cd9fa61b47676c064fc50d62c",
16
slippage: '1.0',
17
disableEstimate: false,
18
referrerAddress: null, // your dApp wallet address for referral
19
referrerFee: null, // your dApp desired referral fee percent
20
})
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.
sample swap
1
{
2
"requestId": "bcbe4067-e0ea-42d2-a13f-52866c235dce",
3
"error": null,
4
"resultType": "OK",
5
"tx": {
6
"type": "EVM",
7
"blockChain": {
8
"name": "BSC",
9
"defaultDecimals": 18,
10
"addressPatterns": ["^(0x)[0-9A-Fa-f]{40}$"],
11
"feeAssets": [{
12
"blockchain": "BSC", "symbol": "BNB", "address": null
13
}],
14
"type": "EVM",
15
"chainId": "56"
16
},
17
"from": "0xbe807dddb074639cd9fa61b47676c064fc50d62c",
18
"txTo": "0x2a7813412b8da8d18Ce56FE763B9eb264D8e28a8",
19
"approveTo": null,
20
"approveData": null,
21
"txData": "0x...",
22
"value": "0x44eeaf0657431d",
23
"gasLimit": "0xdbba0",
24
"gasPrice": "5000000000"
25
}
26
// route section is similar to the quote response
27
"route": {
28
"outputAmount": "28862493",
29
"swapper": {
30
"id": "AnySwap Aggregator",
31
"title": "Multichain",
32
"logo": "https://api.rango.exchange/swappers/anyswap.png"
33
},
34
"from": {
35
"blockchain": "BSC",
36
"symbol": "BNB",
37
"address": null,
38
"decimals": 18,
39
"image": "https://api.rango.exchange/i/Y3v1KW",
40
"usdPrice": 294.127368524921
41
},
42
"to": {
43
"blockchain": "FANTOM",
44
"symbol": "FUSDT",
45
"address": "0x049d68029688eabf473097a2fc38ef61633a3c7a",
46
"decimals": 6,
47
"image": "https://api.rango.exchange/i/mqwkaZ",
48
"usdPrice": 1.0002665280371925
49
},
50
"fee": [
51
{
52
"token": {
53
"blockchain": "BSC",
54
"symbol": "BNB",
55
"address": null,
56
"decimals": 18,
57
"image": "https://api.rango.exchange/i/Y3v1KW",
58
"usdPrice": 294.127368524921
59
},
60
"expenseType": "FROM_SOURCE_WALLET",
61
"amount": "1261820000000000",
62
"name": "Network Fee"
63
}
64
],
65
"amountRestriction": {
66
"min": "40868463315269556",
67
"max": "68114105525449260000000",
68
"type": "INCLUSIVE"
69
},
70
"estimatedTimeInSeconds": 540,
71
"path": [...] // extra information
72
},
73
74
}
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