Swaps from Bitcoin

Connecting a Web3 Wallet and Executing a Transaction through Rubic API

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.

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

You get more information about quote endpoint here:

Request Quote

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:

2. PSBT transaction:

You get more information about swap endpoint here:

Request Data

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:

2. PSBT transaction:

Track your transaction

Now you can track your transaction status

Endpoint: GET https://api-v2.rubic.exchange/api/routes/status

You get more information about status endpoint here:

Get Cross-Chain Status

Last updated

Was this helpful?