This page explains how to connect a Web3 wallet, retrieve token quotes and execute a transaction using Rubic API, with examples using CTRL wallet extension
Connecting a Web3 Wallet
To interact with the blockchain, we first need to connect a wallet.
Here are examples using CTRL wallet.
Copy async function connectWallet() {
if (window?.xfi?.bitcoin) {
const wallet = this.window?.xfi?.bitcoin;
const accounts = wallet.getAcccounts();
return accounts[0];
} else {
console.error("Bitcoin provider not found. Please install Ctrl wallet.");
}
}
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
Copy 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": "0.01",
"srcTokenBlockchain": "BITCOIN",
"dstTokenAddress": "0x0000000000000000000000000000000000000000",
"dstTokenBlockchain": "ETH",
"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": "0.42498305",
// "destinationTokenMinAmount": "0.4122335585",
//
// "destinationUsdAmount": 835.39,
// "destinationUsdMinAmount": 810.33,
//
// "destinationWeiAmount": "425207819398076439",
// "destinationWeiMinAmount": "412451584816134146",
//
// "durationInMinutes": 5,
// "priceImpact": 0.61,
// "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:
Retrieving 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
Rubic API can return 2 kinds of data for transaction: data for transfer and data for psbt transaction.
1. Transfer transaction:
Copy 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": "0.01",
"srcTokenBlockchain": "BITCOIN",
"dstTokenAddress": "0x0000000000000000000000000000000000000000",
"dstTokenBlockchain": "ETH",
"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);
// {
// "to": "bc1ptyhru60vv9q57pqvuet5t3fpcyf7x2t2j83gkz3r3kcrhs9srxls4hcws5",
// "value": "1000000",
// },
return result;
}
2. PSBT transaction:
Copy 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": "0.01",
"srcTokenBlockchain": "BITCOIN",
"dstTokenAddress": "0x0000000000000000000000000000000000000000",
"dstTokenBlockchain": "ETH",
"referrer": "rubic.exchange"
"fromAddress": "USER WALLET ADDRESS",
"id": "ID FROM QUOTE STEP",
"receiver": "RECEIVER ADDRESS",
"publicKey": "PUBLIC KEY OF USER WALLET ADDRESS"
}
});
const result = await response.json();
const { transaction } = result;
console.log(transaction);
// {
// "psbt": "cHNidP8BAL4CAAAAAX7WtwDo7JF8EsZ+...AFgGARv/uOHXKMXpGDtp5ijUAAAAA",
// "signInputs": [0],
// },
return result;
}
You get more information about swap endpoint here:
Executing a Transaction with the API Response Data
Using the data obtained from the Rubic API, you can now execute the transaction.
1. Transfer transaction:
Copy async function executeSwapViaTransfer(
// Transaction object, obtained on previous step from Rubic API
transaction,
// From wallet address
fromAddress,
// Recipient wallet address
toAddress
) {
let hash = null;
await window.xfi.bitcoin.request(
{
method: 'transfer',
params: [
{
feeRate: 10,
from: fromAddress,
recipient: toAddress,
amount: {
amount: transaction.value,
decimals: 8
}
}
]
},
(error, txHash) => {
if(error) {
console.error(error)
} else {
hash = txHash;
}
}
);
if(!hash) throw new Error('Failed to transfer funds');
return hash;
}
2. PSBT transaction:
Copy async function executeSwap(
// Transaction object, obtained on previous step from Rubic API
transaction,
// From wallet address
fromAddress,
) {
let hash = null;
await window.xfi.bitcoin.request(
{
method: 'sign_psbt',
params: {
psbt: transaction.psbt,
signInputs: {
[fromAddress]: transaction.signInputs
},
allowedSignHash: 1,
broadcast: true
}
}
},
(error, txData) => {
if(error) {
console.error(error)
} else {
hash = txData.result.txId;
}
}
);
if(!hash) throw new Error('Failed to sign psbt transaction');
return hash;
}
Track your transaction
Now you can track your transaction status
Endpoint: GET https://api-v2.rubic.exchange/api/routes/status
Copy async function getStatus(
// Your transaction hash, otained while executing transaction
hash
) {
const response = await fetch(`https://api-v2.rubic.exchange/api/info/status?srcTxHash=${hash}`);
const data = await response.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 completed
return status;
}
You get more information about status endpoint here: