Solana

The transactions returned by the Brian API are compatible with the most common development tools, such as Solana-Web3.js.

The transaction contained in the API response is a serialized transaction. The steps to be taken are:

  1. deserialize and sign the transaction

  2. execute the transaction

The transaction returned in the API response follows the same format as the response from the Jupiter API. The deserialization, signing, and execution process are the same. 👉 Check here for more info.

Deserialize, sign and execute the transaction(s)

Access the transaction object from the Brian API response (using the Brian ts SDK):


const brian = new BrianSDK(options);

const request = await brian.transact({
  prompt: "swap 1 sol for USDC on solana",
  address: "53unSgGWqEWANcPYRF35B2Bgf8BkszUtcccKiXwGGLyr",
});
console.log("transaction result:", request);

// Create an empty array to store transactions
const txArray = [];

// Loop through the requests and their steps
for (const req of request) {
  for (const step of req.data.steps) {
    // Push the step's data into the txArray
    txArray.push(step.data);
  }
}

console.log("Transaction Array:", txArray);

Now, you can deserialize, sign, and execute the transaction(s)

// Get the latest blockhash before processing transactions
  const latestBlockHash = await connection.getLatestBlockhash();

  // Loop through each transaction data in txArray
  for (const txData of txArray) {
    try {
      // Deserialize the transaction
      const swapTransactionBuf = Buffer.from(txData, "base64");
      const transaction = VersionedTransaction.deserialize(swapTransactionBuf);
      console.log("Deserialized transaction:", transaction);

      // Sign the transaction
      transaction.sign([wallet.payer]); // Ensure wallet.payer is a valid Keypair or signer
      console.log("Signed transaction:", transaction);

      // Execute the transaction
      const rawTransaction = transaction.serialize();
      const txid = await connection.sendRawTransaction(rawTransaction, {
        skipPreflight: true,
        maxRetries: 2,
      });
      await connection.confirmTransaction({
        blockhash: latestBlockHash.blockhash,
        lastValidBlockHeight: latestBlockHash.lastValidBlockHeight,
        signature: txid,
      });

      console.log(`Transaction successful: https://solscan.io/tx/${txid}`);
    } catch (error) {
      console.error("Error processing transaction:", error);
    }
  }
} catch (error) {
  console.error("Error during request or transaction:", error);
}

Last updated