Skip to main content

sendRawTransaction

Sends a raw, serialized, signed, and Base64-encoded transaction to a Solana RPC node. This is a low-level function you would use when constructing complex transactions (e.g., with multiple instructions) or when you build and sign the transaction manually.

Parameters

ParameterTypeDescription
txBase64const String&The Base64 encoded raw transaction to be sent.
outSignatureString&An output parameter that will be populated with the Base58 transaction signature if successfully sent.

Return Value

  • bool: Returns true if the transaction was successfully accepted by the RPC node, false otherwise. A true return does not mean the transaction is confirmed, only that it was successfully submitted. Use confirmTransaction to check for finality.

Example Code (main.cpp)

This example shows how to manually build, sign, and send a SOL transfer transaction using sendRawTransaction.

#include <Arduino.h>
#include <WiFi.h>
#include "Infratic-lib.h"

// --- WiFi Settings ---
const char* WIFI_SSID = "YOUR_WIFI_SSID";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";

// --- Solana Settings ---
const String SOLANA_RPC_URL = "[https://api.devnet.solana.com](https://api.devnet.solana.com)";
const String MY_PRIVATE_KEY_BASE58 = "YOUR_PRIVATE_KEY_BASE58";
const String MY_PUBLIC_KEY_BASE58 = "YOUR_PUBLIC_KEY_BASE58";
const String RECIPIENT_PUBKEY_BASE58 = "RECIPIENT_PUBKEY_BASE58"; // Recipient address

Infratic solana(SOLANA_RPC_URL);

void setup() {
Serial.begin(115200);
delay(1000);

// Connect to WiFi
Serial.print("Connecting to WiFi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\n✅ WiFi connected!");

Serial.println("\n=== Send Raw Transaction Example ===");

// 1. Get a recent blockhash
String blockhash = solana.getLatestBlockhash();
if (blockhash.isEmpty()) {
Serial.println("❌ Failed to get blockhash.");
return;
}
Serial.println("Got blockhash: " + blockhash);

// 2. Create the transaction object
Transaction tx;
tx.recent_blockhash = blockhash;
tx.fee_payer = Pubkey::fromBase58(MY_PUBLIC_KEY_BASE58);

// 3. Create the instruction (SOL transfer)
Pubkey from = Pubkey::fromBase58(MY_PUBLIC_KEY_BASE58);
Pubkey to = Pubkey::fromBase58(RECIPIENT_PUBKEY_BASE58);
uint64_t lamports = 100000; // 0.0001 SOL
Pubkey systemProgramId = Pubkey::fromBase58("11111111111111111111111111111111");

std::vector<uint8_t> data(12);
// Instruction index (2 for transfer)
data[0] = 2; data[1] = 0; data[2] = 0; data[3] = 0;
// Lamports
memcpy(&data[4], &lamports, 8);

Instruction ix(
systemProgramId,
{
AccountMeta::writable(from, true),
AccountMeta::writable(to, false)
},
data
);
tx.add(ix);

// 4. Sign the transaction
uint8_t rawPrivateKey[64];
size_t privLen = 64;
base58Decode(MY_PRIVATE_KEY_BASE58, rawPrivateKey, privLen);
Keypair signer = Keypair::fromPrivateKey(rawPrivateKey);
tx.sign({signer});

// 5. Serialize and Base64 encode the transaction
String txBase64 = tx.serializeBase64();
Serial.println("Transaction encoded to Base64.");

// 6. Send the raw transaction
String signature;
if (solana.sendRawTransaction(txBase64, signature)) {
Serial.println("✅ Transaction sent successfully! Signature: " + signature);

// 7. (Optional) Confirm it
Serial.println("Waiting for confirmation...");
if (solana.confirmTransaction(signature, 15000)) {
Serial.println("✅ Transaction confirmed!");
} else {
Serial.println("❌ Transaction confirmation failed.");
}
} else {
Serial.println("❌ Failed to send raw transaction.");
}
}

void loop() {}