API Flow
Rango Exchange Main API Flow
Last updated
Was this helpful?
Rango Exchange Main API Flow
Last updated
Was this helpful?
Here is a sample interaction scenario between a dApp and Rango Main API/SDK. This flow is designed to be as straightforward as possible, but additional steps can be taken to enhance functionality.
Sample Code:
const metaResponse = await axios.get('https://api.rango.exchange/meta', {
params: {
'apiKey': 'c6381a79-2817-4602-83bf-6a641a409e32'
}
});
Use the fetched metadata to display available blockchains and their tokens to the user. Allow the user to select the source and destination tokens.
BSC.BNB
to AVAX_CCHAIN.USDT--0x9702230a8ea53601f5cd2dc00fdbc13d4df4a8c7
. Here is the sample code:
const tokens = meta.tokens;
const BNB_ADDRESS = null
const USDT_ADDRESS = '0xc7198437980c041c805a1edcba50c1ce5db95118'
const BSC_BNB = tokens.find(t => t.address === BNB_ADDRESS && t.blockchain === 'BSC')
const AVAX_USDT = tokens.find(t => t.address === USDT_ADDRESS && t.blockchain === 'AVAX_CCHAIN')
Here is the sample code:
const routingResponse = await axios.post(
'https://api.rango.exchange/routing/bests',
{
'from': {
'blockchain': 'BSC',
'symbol': 'BNB',
'address': null,
},
'to': {
'blockchain': 'AVAX_CCHAIN',
'symbol': 'USDT.E',
'address': '0xc7198437980c041c805a1edcba50c1ce5db95118'
},
'amount': '1',
'slippage': '1.0'
},
{
params: {
'apiKey': 'c6381a79-2817-4602-83bf-6a641a409e32'
},
headers: {
'content-type': 'application/json'
}
}
);
// check if the route was okay
if (!routingResponse.results || routingResponse.error) {
// no routes found
// you could show diagnosis messages to user if it is not empty
// sample: "diagnosisMessages":["Your input amount might be too low!"]
console.log(routingResponse.diagnosisMessages)
} else {
// everything was okay
}
This is an example of selected route which includes two steps:
Step 1: Swap BSC.BNB to BSC.USDT via 1Inch.
Step 2: Swap BSC.USDT to AVAX_CCHAIN.USDT via Stargate Bridge.
Here is the sample code:
const confirmResponse = await axios.post(
'https://api.rango.exchange/routing/confirm',
{
'selectedWallets': {
'BSC': '0xeae6d42093eae057e770010ffd6f4445f7956613',
'AVAX_CCHAIN': '0xeae6d42093eae057e770010ffd6f4445f7956613'
},
'destination': '0x6f33bb1763eebead07cf8815a62fcd7b30311fa3',
'requestId': '33e0b996-da5e-4922-9fdc-f7206247fc34'
},
{
params: {
'apiKey': 'c6381a79-2817-4602-83bf-6a641a409e32'
},
headers: {
'content-type': 'application/json'
}
}
);
// check if the route was okay
if (!confirmResponse.result || confirmResponse.error) {
// there was a problem in confirming the route
} else {
// everything was okay
// you could compare confirmed route with the route output amount
// and warn the user if there is noticable difference
// e.g. check if confirmed route output is 2% less than previous one
const confirmedOutput = new BigNumber(routingResponse.results[selected]?.outputAmount)
const finalOutput = new BigNumber(confirmResponse.result?.outputAmount)
if (finalOutput.lt(confirmedOutput.multipliedBy(new BigNumber(0.98))) {
// get double confirmation from the user
} else {
// proceed to executing the route
}
}
For every steps of the selected route, we need to repeat the next steps:
Is it possible to generate approve transaction on client side without using Rango API?
It is important to use approve transaction data generated by Rango API and not hard-coding something on your client side for creating approve transaction, because for some protocols (some bridges), the contract that should be approved is dynamically generated via their API based on the route.
Sample Code for EVM transactions:
// sample type guard
export const isEvmTransaction = (tx: {
type: TransactionType
}): transaction is EvmTransaction => tx.type === TransactionType.EVM
// how to build and sign EVM approve transaction
const tx = createTransactionResponse.transaction
if (isEvmTransacation(tx)) {
if (tx.isApproval) {
// user doesn't have enough approval and needs to sign approve tx
let approveTx = {
from: tx.from,
to: tx.to,
data: tx.data,
value: tx.value,
gasLimit: tx.gasLimit
}
if (tx.gasPrice) {
swapTx = { gasPrice: tx.gasPrice, ...swapTx }
} else if (tx.maxPriorityFeePerGas && tx.maxFeePerGas) {
swapTx = {
maxFeePerGas: tx.maxFeePerGas,
maxPriorityFeePerGas: tx.maxPriorityFeePerGas,
...swapTx
}
}
const approveTxHash = (await signer.sendTransaction(approveTx)).hash
} else {
// user already has enough approve amoaunt
// we could proceed to 8th step (main transaction)
}
}
The response of create transaction API could be an approve transaction (isApproval=true
) or the main transaction. (isApproval=false
)
const createTransactionResponse = await axios.post(
'https://api.rango.exchange/tx/create',
{
'requestId': '1978d8fa-335d-4915-a039-77f1a17315f5',
'step': 1,
'userSettings': {
'slippage': 3,
'infiniteApprove': false
},
'validations': {
'balance': true,
'fee': true,
'approve': true
}
},
{
params: {
'apiKey': 'c6381a79-2817-4602-83bf-6a641a409e32'
},
headers: {
'content-type': 'application/json'
}
}
);
if (createTransactionResponse.transaction) {
// everything was okay
} else {
// there was a problem in creating transaction
console.log(createTransactionResponse.error)
}
How to set validation parameters?
If you are verifying the balance and fee amount on your client side or you've already checked them in route confirmation step, it is advisable to set the balance and fee parameters to false to prevent duplicate checks or potential errors during validation.
For checking approval transaction status, you could also check it directly from the RPC endpoint if you prefer and skip calling Rango API for this purpose.
Here is the sample code:
const response = await axios.get('https://api.rango.exchange/tx/b3a12c6d-86b8-4c21-97e4-809151dd4036/check-approval', {
params: {
'txId': '0x7f17aaba51d1f24204cd8b02251001d3704add46d84840a5826b95ef49b8b74f',
'apiKey': 'c6381a79-2817-4602-83bf-6a641a409e32'
}
});
if (response.isApproved) {
// user has enough approve amount now
// we could proceed to the next step
} else {
if (response.txStatus === 'failed') {
// approve transaction with given hash fails on blockchain
// it could happen for different reasons, e.g. low gas price
// action => ask user to signs the approve transaction again.
} else {
// scenario: response.txStatus === 'success'
// approve transaction succeeds but user doesn't have still enough approval
// it could happen in rare cases for example when user changes dapp
// suggested approve amount in Metamask and override it with a lower one
}
}
Sample code for EVM transactions:
// sample type guard
export const isEvmTransaction = (tx: {
type: TransactionType
}): transaction is EvmTransaction => tx.type === TransactionType.EVM
// how to build and sign EVM main transaction
const tx = createTransactionResponse.transaction
if (isEvmTransacation(tx)) {
let swapTx = {
from: tx.from,
to: tx.to,
data: tx.data,
value: tx.value,
gasLimit: tx.gasLimit
}
if (tx.gasPrice) {
swapTx = { gasPrice: tx.gasPrice, ...swapTx }
} else if (tx.maxPriorityFeePerGas && tx.maxFeePerGas) {
swapTx = {
maxFeePerGas: tx.maxFeePerGas,
maxPriorityFeePerGas: tx.maxPriorityFeePerGas,
...swapTx
}
}
const swpTxHash = (await signer.sendTransaction(swapTx)).hash
}
By calling this method, dApp could get the outbound transaction hash and make sure if the outbound transaction on the destination blockchain succeeds or transaction failed and user was refunded.
const response = await axios.post(
'https://api.rango.exchange/tx/check-status',
{
'requestId': 'b3a12c6d-86b8-4c21-97e4-809151dd4036',
'txId': '0xfa88b705a5b4049adac7caff50c887d9600ef023ef1a937f8f8b6f44e90042b5',
'step': 1
},
{
params: {
'apiKey': 'c6381a79-2817-4602-83bf-6a641a409e32'
},
headers: {
'content-type': 'application/json'
}
}
);
if (response.status) {
// show latest status of the swap to the user
if (response.status === TransactionStatus.SUCCESS) {
// swap suceeded
} else if (response.status === TransactionStatus.FAILED) {
// swap failed
} else {
// swap is still running
// we need to call check-status method again after a timeout (10s)
}
}
Sample code:
await axios.post(
'https://api.rango.exchange/tx/report-tx',
{
'requestId': '688b308e-a06b-4a4e-a837-220d458b8642',
'step': 1,
'eventType': 'SEND_TX_FAILED',
'reason': 'RPC Error'
},
{
params: {
'apiKey': 'c6381a79-2817-4602-83bf-6a641a409e32'
},
headers: {
'content-type': 'application/json'
}
}
);