API Flow
Rango Exchange Basic API Flow
Last updated
Was this helpful?
Rango Exchange Basic API Flow
Last updated
Was this helpful?
If you'd like to skip ahead to the code, please refer to the .
Here is a sample interaction scenario between a dApp and Rango Basic API/SDK. This flow is designed to be as straightforward as possible, but additional steps can be taken to enhance functionality.
Sample Code:
const meta = await axios.get('https://api.rango.exchange/basic/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'
// You can omit the following lines if you don't want to display token
// details like the icon or price to the user before retrieving the quote.
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 quote = await axios.get('https://api.rango.exchange/basic/quote', {
params: {
'from': 'BSC.BNB',
'to': 'AVAX_CCHAIN--0xc7198437980c041c805a1edcba50c1ce5db95118',
'amount': '100000000000000000',
'slippage': '3',
'apiKey': 'c6381a79-2817-4602-83bf-6a641a409e32'
}
});
// check if the route was okay
if (!quote.result) {
// there was no route
} else if (quote.resultType === 'HIGH_IMPACT') {
// there was a quote but price impact was too high
} else if (quote.resultType === 'INPUT_LIMIT_ISSUE') {
// there was a quote but not suitable for this input amount.
// user should increase or decrease the input amount
} else if (quote.resultType === 'OK') {
// everything was okay
}
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 quote.
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 = swap.tx
if (isEvmTransacation(tx)) {
if (tx.approveData && tx.approveTo) {
// user doesn't have enough approval and needs to sign approve tx
const approveTx = {
from: tx.from,
to: tx.approveTo,
data: tx.approveData,
value: '0x',
}
const approveTxHash = (await signer.sendTransaction(approveTx)).hash
} else {
// user already has enough approve amoaunt
// we could proceed to 8th step (main transaction)
}
}
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 = swap.tx
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
}
const swap = await axios.get('https://api.rango.exchange/basic/swap', {
params: {
'from': 'BSC.BNB',
'to': 'AVAX_CCHAIN.USDT.E--0xc7198437980c041c805a1edcba50c1ce5db95118',
'amount': '100000000000000000',
'slippage': '3',
'fromAddress': '0x6f33bb1763eebead07cf8815a62fcd7b30311fa3',
'toAddress': '0x6f33bb1763eebead07cf8815a62fcd7b30311fa3',
'disableEstimate': true,
'apiKey': 'c6381a79-2817-4602-83bf-6a641a409e32'
}
});
if (swap.tx && swap.resultType === 'OK') {
// transaction is created susccessfully
// you could compare confirmed quote with the swap output amount
// and warn the user if there is noticable difference
// e.g. check if new quote output is 2% less than previous one
const confirmedOutput = new BigNumber(quote.route?.outputAmount)
const finalOutput = new BigNumber(swap.route?.outputAmount)
if (finalOutput.lt(confirmedOutput.multipliedBy(new BigNumber(0.98))) {
// get double confirmation from the user
} else {
// proceed to sign flow
}
} else if (!swap.tx && swap.resultType === 'OK') {
// getting quote was successful but there was a problem
// in creating the transaction
console.log(swap.error)
} else {
// there was a problem getting the quote
}
How to set disableEstimate parameter in swap method?
If you are checking the balance and fee amount on your client side, it is recommended to set this parameter to true, as it will significantly reduce the response time.
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/basic/is-approved', {
params: {
'requestId': 'e4b0d1e7-ae1f-4aed-ab91-f1ea3ba9383b',
'txId': '0xd7a18c6e2f9afe5aefd1b5969f753513f01c6670a4fc57a2d1349ad539ae2f7f',
'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
}
}
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.get('https://api.rango.exchange/basic/status', {
params: {
'requestId': 'b3a12c6d-86b8-4c21-97e4-809151dd4036',
'txId': '0xfa88b705a5b4049adac7caff50c887d9600ef023ef1a937f8f8b6f44e90042b5',
'apiKey': 'c6381a79-2817-4602-83bf-6a641a409e32'
}
});
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 status method again after a timeout (10s)
}
}
Sample code:
const response = await axios.post(
'https://api.rango.exchange/basic/report-tx',
{
'requestId': '2823418f-9e18-4110-8d36-b569b0af025e',
'eventType': 'SEND_TX_FAILED',
'reason': 'Transaction is underpriced.'
},
{
params: {
'apiKey': 'c6381a79-2817-4602-83bf-6a641a409e32'
},
headers: {
'content-type': 'application/json'
}
}
);
If tx
field in is non-empty, it means that transaction is created successfully. If tx
field is null or resultType
is not OK
, dApp shows an error to the user and flow breaks. Here is the sample code.