Connecting a Web3 Wallet and Executing a Transaction through Rubic API
This page explains how to connect a Web3 wallet, retrieve token quotes, approve tokens, and execute a transaction using Rubic API, with examples using ethers.js, web3.js, and viem
Connecting a Web3 Wallet
To interact with the blockchain, we first need to connect a wallet. Here are examples using tronWeb.
asyncfunctionquoteBest() {constresponse=awaitfetch("https://api-v2.rubic.exchange/api/quoteBest", { method:"POST", headers: {"Content-Type":"application/json", }, body: {"srcTokenAddress":"0x0000000000000000000000000000000000000000","srcTokenAmount":"1000.05","srcTokenBlockchain":"TRON","dstTokenAddress":"0x0000000000000000000000000000000000000000","dstTokenBlockchain":"ETH","referrer":"rubic.exchange" } });constdata=awaitresponse.json();const { estimate,transaction,id } = data;console.log(estimate);// {// This is an estimated amount you will get after the swap.// "destinationTokenAmount": "8248.453781656313882666",// "destinationTokenMinAmount": "8001.000168206624466186",//// "destinationUsdAmount": 2637.13,// "destinationUsdMinAmount": 2558.02,//// "destinationWeiAmount": "8248453781656313882666",// "destinationWeiMinAmount": "8001000168206624466186",//// "durationInMinutes": 5,// "priceImpact": 0.14,// "slippage": 0.03// }console.log(transaction.approvalAddress);// This is the address you need to give approve to spend tokens.// See next section for details.// 0x3335733c454805df6a77f825f266e136FB4a3333console.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:
Before sending a transaction, you need to approve the token. Approve allows a user to authorize a contract or application to manage a specified amount of their tokens, which is necessary for secure interaction with decentralized applications such as exchanges or DeFi protocols. This gives users control over how many tokens can be used, providing an additional layer of security.
Below are examples for different libraries.
consttronWeb=require('tronweb');import { TRC20_CONTRACT_ABI } from"./TokenABI"; // replace with the correct ABIasyncfunctionapproveToken(// Token address tokenAddress,// Contract to give approve to.// Put here transaction.approvalAddress obtained on previous step spenderAddress,// Amount of tokens to approve.// For security reasons it's better to set approve// amount equal to from amount amount) {constcontract=awaittronWeb.contract(TRC20_CONTRACT_ABI, tokenAddress);consttx=awaittokenContract.approve(spenderAddress, amount);console.log("Approval successful:",tx.hash);}
Retrieving Data to Execute a Transaction
To perform a token swap through Rubic API, we need to get the necessary data for the transaction.
asyncfunctiongetStatus(// Your transaction hash, otained while executing transaction hash) {constresponse=awaitfetch(`https://api-v2.rubic.exchange/api/routes/status?srcTxHash=${hash}`);constdata=awaitresponse.json();const { status,destinationTxHash } = data;console.log(status);// Current TX status can be one of// 'PENDING' | 'LONG_PENDING' | 'REVERT' |// 'REVERTED' | 'FAIL' | 'READY_TO_CLAIM' |// 'SUCCESS' | 'NOT_FOUND';console.log(status);// shows the hash on the target network if the transaction// is successfully completedreturn status;}
You get more information about status endpoint here: