Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Sending Transactions

Usage

import sh.brane.rpc.Brane;
import sh.brane.core.crypto.PrivateKeySigner;
 
// Create a signer
var signer = new PrivateKeySigner(System.getenv("SEPOLIA_PRIVATE_KEY"));
 
// Create a Brane.Signer client
Brane.Signer client = Brane.connect("https://rpc.sepolia.org", signer);

Methods

sendTransactionAndWait

Sends a transaction and blocks until it is mined. This is the recommended method for scripts and backend services.

TransactionReceipt receipt = client.sendTransactionAndWait(
    new TransactionRequest(
        client.getAddress(),           // from
        new Address("0x..."),          // to
        Wei.fromEther(new java.math.BigDecimal("0.01")), // value
        null,                          // gasLimit (auto-filled)
        null,                          // gasPrice (not used for EIP-1559)
        null,                          // maxPriorityFeePerGas (auto-filled)
        null,                          // maxFeePerGas (auto-filled)
        null,                          // nonce (auto-fetched)
        null,                          // data
        true,                          // isEip1559
        null                           // accessList
    ),
    60_000, // timeout (ms)
    1_000   // poll interval (ms)
);
 
System.out.println("Mined in block: " + receipt.blockNumber());

sendTransaction

Sends a transaction immediately and returns the transaction hash. Does not wait for mining.

import sh.brane.core.builder.TxBuilder;
 
TransactionRequest request = TxBuilder.eip1559()
    .to(new Address("0x..."))
    .value(Wei.fromEther(new java.math.BigDecimal("0.1")))
    .build();
 
// Returns the transaction hash (e.g., "0x...")
Hash txHash = client.sendTransaction(request);

signTransaction

Signs a transaction offline without sending it.

TransactionRequest request = TxBuilder.eip1559()
    .to(new Address("0x..."))
    .value(Wei.fromEther(new java.math.BigDecimal("0.1")))
    .build();
 
// Returns the signed RLP-encoded transaction hex
HexData signedTx = client.signTransaction(request);

getAddress

Returns the address of the account managed by this client.

Address myAddress = client.getAddress();

Gas Management

Brane.Signer automatically handles gas estimation and fee calculation.

  • Gas Limit: Estimated via eth_estimateGas if not provided.
  • Fees (EIP-1559): maxFeePerGas and maxPriorityFeePerGas are fetched from the network.
  • Buffering: A SmartGasStrategy can be configured to add safety buffers to estimates.

Using Access Lists for Gas Optimization

Generate an EIP-2930 access list and include it in your transaction for 10-20% gas savings on complex contract interactions:

import sh.brane.core.model.AccessListWithGas;
import sh.brane.core.builder.TxBuilder;
import sh.brane.core.types.Address;
import sh.brane.core.types.HexData;
 
Address contractAddress = new Address("0x...");
HexData encodedCall = new HexData("0x..."); // ABI-encoded function call
 
// 1. Build initial request (without access list)
TransactionRequest initial = TxBuilder.eip1559()
    .from(client.getAddress())
    .to(contractAddress)
    .data(encodedCall)
    .build();
 
// 2. Generate access list via eth_createAccessList
AccessListWithGas result = client.createAccessList(initial);
System.out.println("Estimated gas: " + result.gasUsed());
 
// 3. Rebuild transaction with access list included
TransactionRequest optimized = TxBuilder.eip1559()
    .from(client.getAddress())
    .to(contractAddress)
    .data(encodedCall)
    .accessList(result.accessList())
    .build();
 
// 4. Send optimized transaction
TransactionReceipt receipt = client.sendTransactionAndWait(optimized, 60_000, 1_000);