Swaps VIA deposit
This page explains how to execute a transaction via deposit using Rubic API.
Retrieving Token Quotes
Now that the wallet is connected, we can request token quotes from Rubic API.
Endpoint: POST https://api-v2.rubic.exchange/api/routes/quoteBest
async function quoteBest() {
const response = await fetch("https://api-v2.rubic.exchange/api/routes/quoteBest", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: {
"srcTokenAddress": "0x0000000000000000000000000000000000000000",
"srcTokenAmount": "3",
"srcTokenBlockchain": "COSMOS",
"dstTokenAddress": "0x0000000000000000000000000000000000000000",
"dstTokenBlockchain": "TRON",
"referrer": "rubic.exchange"
}
});
const data = await response.json();
const { estimate, transaction, id } = data;
console.log(estimate);
// {
// This is an estimated amount you will get after the swap.
// "destinationTokenAmount": "56.797373",
// "destinationTokenMinAmount": "55.093452",
//
// "destinationUsdAmount": 13.26,
// "destinationUsdMinAmount": 12.86,
//
// "destinationWeiAmount": "56797373",
// "destinationWeiMinAmount": "55093452",
//
// "durationInMinutes": 5,
// "priceImpact": 3.29,
// "slippage": 0.03
// }
console.log(id);
// This is the swap ID. It will be needed later for swap request.
return data;
}You get more information about quote endpoint here:
Request QuoteRetrieving deposit quotes
We can request deposit quotes even without wallet connection.
Endpoint: POST https://api-v2.rubic.exchange/api/routes/quoteDepositTrades
async function quoteDepositTrades() {
const response = await fetch("https://api-v2.rubic.exchange/api/routes/quoteDepositTrades", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: {
"srcTokenAddress": "0x0000000000000000000000000000000000000000",
"srcTokenAmount": "3",
"srcTokenBlockchain": "ETH",
"dstTokenAddress": "0x0000000000000000000000000000000000000000",
"dstTokenBlockchain": "TRON",
"referrer": "rubic.exchange"
}
});
const data = await response.json();
const { routes } = data;
const bestRoute = routes[0]
console.log(bestRoute.estimate);
// {
// "destinationTokenAmount": "40403.458219",
// "destinationTokenMinAmount": "39191.354472",
// "destinationUsdAmount": 11993.69,
// "destinationUsdMinAmount": 11633.88,
// "destinationWeiAmount": "40403458219",
// "destinationWeiMinAmount": "39191354472",
// "durationInMinutes": 5,
// "priceImpact": 0.36,
// "slippage": 0.03
// }
console.log(id);
// This is the swap ID. It will be needed later for swap request.
return data;
}You get more information about deposit quote endpoint here:
Request QuoteRetrieving Data to Execute a Transaction
To perform a token swap through Rubic API, we need to get the necessary data for the transaction.
Endpoint: POST https://api-v2.rubic.exchange/api/routes/swap
async function getSwapData() {
const response = await fetch("https://api-v2.rubic.exchange/api/routes/swap", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: {
"srcTokenAddress": "0x0000000000000000000000000000000000000000",
"srcTokenAmount": "3",
"srcTokenBlockchain": "COSMOS"
"dstTokenAddress": "0x0000000000000000000000000000000000000000",
"dstTokenBlockchain": "TRON",
"referrer": "rubic.exchange",
"fromAddress": "USER WALLET ADDRESS",
"id": "ID FROM QUOTE STEP",
"receiver": "RECEIVER ADDRESS"
}
});
const result = await response.json();
const { transaction } = result;
console.log(transaction);
// {
// "depositAddress": "cosmos1hgp84me0lze8t4jfrwsr05aep2kr57zrk4gecx",
// "amountToSend": "3"
// "exchangeId": "jz4flw7ub37tr78b",
//
// "extraFields": { // can be null
// "name": "Memo",
// "value": "7799954959159693"
// }
// },
return result;
}You get more information about swap endpoint here:
Request DataRetrieving Deposit Data to Make Transfer
To perform a token swap through Rubic API, we need to get the necessary data for the transaction.
Endpoint: POST https://api-v2.rubic.exchange/api/routes/swapDepositTrade
async function getSwapDepositData() {
const response = await fetch("https://api-v2.rubic.exchange/api/routes/swapDepositTrade", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: {
"srcTokenAddress": "0x0000000000000000000000000000000000000000",
"srcTokenAmount": "3",
"srcTokenBlockchain": "ETH"
"dstTokenAddress": "0x0000000000000000000000000000000000000000",
"dstTokenBlockchain": "TRON",
"referrer": "rubic.exchange",
"id": "ID FROM QUOTE DEPOSIT STEP",
"receiver": "RECEIVER ADDRESS" // required
}
});
const result = await response.json();
const { transaction } = result;
console.log(transaction);
// {
// "depositAddress": "0x4ae29365f677b3fA1ada610afC24f2a82eDEC73F",
// "amountToSend": "3",
// "exchangeId": "aba38e0ed78780"
// },
return result;
}
export async function makeTransfer() {
const depositData = await getSwapDepositData()
const provider = new ethers.providers.Web3Provider(window.ethereum)
// the receiver
const recipient = '0xDbe59d4941bdaE9A867D95c4Fc10c7c9fa0018f4'
// transaction data, recipient and value in wei
const txData = {
to: recipient,
value: ethers.parseEther(depositData.amountToSend)// eth to wei
}
const signer = provider.getSigner()
const tx = signer.sendTransaction({
to: depositData.destAddress,
value: ethers.utils.parseEther(depositData.amountToSend)
});
const tx = await wallet.sendTransaction(txData)
console.log(`Waiting tx... ${tx.hash}`)
}
makeTransfer().then(txHash => console.log('hash of transfer -', txHash))Executing a Transaction with the API Response Data
You should display the depositAddress, amountToSend, exchangeId and extraFields fields from the transaction object in your UI. The user should send amount to the received depositAddress from his wallet by himself.
The screenshot shows how a user should make a deposit:

Last updated
Was this helpful?