Links
4⃣

Execution

Executing the swap
Here is a sample code of how passing the swap response containing an EVM transaction to the wallet, sign it by the user, and track it using the rango client. You could check the full working example on Github.
Sample EVM Execution
1
import { ethers } from 'ethers'
2
import {
3
EvmTransaction,
4
RangoClient,
5
} from 'rango-sdk-basic'
6
7
8
const provider = new ethers.providers.Web3Provider(window.ethereum)
9
const signer = provider.getSigner()
10
11
const swapRequest = {
12
from: {
13
"blockchain": "BSC",
14
"symbol": "BNB",
15
"address": null
16
},
17
to: {
18
"blockchain": "FANTOM",
19
"symbol": "FUSDT",
20
"address": "0x049d68029688eabf473097a2fc38ef61633a3c7a"
21
},
22
amount: "100000000000000000",
23
fromAddress: "0xbe807dddb074639cd9fa61b47676c064fc50d62c",
24
toAddress: "0xbe807dddb074639cd9fa61b47676c064fc50d62c",
25
slippage: '1.0',
26
disableEstimate: false,
27
referrerAddress: null, // your dApp wallet address for referral
28
referrerFee: null, // your dApp desired referral fee percent
29
}
30
31
const swap = await rangoClient.swap(swapRequest)
32
33
if (!!swap.error || swap.resultType !== 'OK') {
34
const msg =
35
`Error swapping, message: ${swap.error}, status: ${swap.resultType}`
36
throw new Error(msg)
37
}
38
39
const evmTransaction = swap.tx as EvmTransaction
40
41
// needs approving the tx
42
if (swap.approveTo && swap.approveData) {
43
const approveTx = prepareEvmTransaction(evmTransaction, true)
44
const approveTxHash = (await signer.sendTransaction(approveTx)).hash
45
await checkApprovalSync(swap.requestId, approveTxHash, rangoClient)
46
}
47
48
// main transaction
49
const mainTx = prepareEvmTransaction(evmTransaction, false)
50
const mainTxHash = (await signer.sendTransaction(mainTx)).hash
51
const txStatus = await checkTransactionStatusSync(
52
swap.requestId,
53
mainTxHash,
54
rangoClient
55
)
56
And here you can see a sample implementation of utility functions including prepareEvmTransaction , checkTransactionStatusSync, and checkApprovalSync which are used in the above code:
Utility functions
1
import {TransactionRequest} from "@ethersproject/abstract-provider/src.ts/index";
2
import {
3
RangoClient,
4
EvmTransaction,
5
TransactionStatus
6
} from "rango-sdk-basic";
7
8
export function prepareEvmTransaction(
9
evmTx: EvmTransaction,
10
isApprove: boolean
11
): TransactionRequest {
12
const gasPrice = !!evmTx.gasPrice && !evmTx.gasPrice.startsWith('0x') ?
13
'0x' + parseInt(evmTx.gasPrice).toString(16) : null,
14
const manipulatedTx = {
15
...evmTx,
16
gasPrice
17
}
18
19
let tx = {}
20
if (!!manipulatedTx.from) tx = { ...tx, from: manipulatedTx.from }
21
if (isApprove) {
22
if (!!manipulatedTx.approveTo) tx = {...tx, to: manipulatedTx.approveTo}
23
if (!!manipulatedTx.approveData) tx = {...tx, data: manipulatedTx.approveData}
24
} else {
25
if (!!manipulatedTx.txTo) tx = { ...tx, to: manipulatedTx.txTo }
26
if (!!manipulatedTx.txData) tx = { ...tx, data: manipulatedTx.txData }
27
if (!!manipulatedTx.value) tx = { ...tx, value: manipulatedTx.value }
28
if (!!manipulatedTx.gasLimit) tx = { ...tx, gasLimit: manipulatedTx.gasLimit }
29
if (!!manipulatedTx.gasPrice) tx = { ...tx, gasPrice: manipulatedTx.gasPrice }
30
}
31
return tx
32
}
33
34
export async function checkApprovalSync(
35
requestId: string,
36
txId: string,
37
rangoClient: RangoClient
38
) {
39
while (true) {
40
const approvalResponse = await rangoClient.isApproved(requestId, txId)
41
if (approvalResponse.isApproved) {
42
return true
43
}
44
await sleep(3000)
45
}
46
}
47
48
const checkTransactionStatusSync = async (
49
requestId: string,
50
txId: string,
51
rangoClient: RangoClient
52
) => {
53
while (true) {
54
const txStatus = await rangoClient.status({
55
requestId,
56
txId,
57
})
58
.catch(error) => {
59
console.log({ error })
60
})
61
if (!!txStatus) {
62
// show latest status of the transaction to the user
63
console.log({ txStatus })
64
if (
65
!!txStatus.status &&
66
[TransactionStatus.FAILED, TransactionStatus.SUCCESS].includes(
67
txStatus.status
68
)
69
) {
70
return txStatus
71
}
72
}
73
await sleep(3000)
74
}
75
}