Brian - Documentation
  • đŸ–ī¸Welcome
  • 👀Prompt-to-earn
  • 🤖Why an Intent Recognition Engine?
  • 🌐Brian AI models
  • 📜Prompt Guide
  • 🧠Use Cases
  • đŸ’ģPowered by Brian
  • â‰ī¸FAQ
  • Brian API
    • âš™ī¸APIs
      • đŸ’ģAPI Swagger
      • 💸/transaction
        • â›“ī¸Actions, Networks, and Tokens supported
          • Deposit and Withdraw to/from DeFi protocols
        • Transactions flow
          • EVM
          • Solana
          • Starknet
      • 📚/knowledge
        • 📑Knowledge Boxes
        • 🐝Public Embeddings
      • đŸ§™â€â™‚ī¸/agent
      • 🧑‍đŸ”Ŧ/parameters-extraction
      • 👨‍đŸ’ģ/smart-contract (alpha)
      • 📰/networks
      • â„šī¸/actions
    • đŸĨŗWhat's New?
    • 🔑API key
  • Brian SDK
    • 📚Brian Typescript SDK
  • AI AGENTS
    • 🤖LangChain<>Brian SDK
  • Brian App
    • 🍀Overview
    • 📤Send Transactions
    • 🙏Ask Brian
    • 🚚Deploy Smart Contracts
  • Misc.
    • Telegram Dev community
    • Website
    • Github
    • Medium
    • Twitter
    • HuggingFace
Powered by GitBook
On this page
  1. Brian API
  2. APIs
  3. /transaction
  4. Transactions flow

Solana

PreviousEVMNextStarknet

Last updated 4 months ago

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 .

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);
}

âš™ī¸
💸
here for more info