Skip to main content
The WalletClient is used for writing to the blockchain. It manages accounts, signs transactions, and handles gas estimation.

Usage

import io.brane.rpc.DefaultWalletClient;
import io.brane.rpc.BranePublicClient;
import io.brane.core.chain.ChainProfiles;
import io.brane.core.crypto.PrivateKeySigner;

// 1. Create Public Client
var publicClient = BranePublicClient.forChain(ChainProfiles.ETH_SEPOLIA).build();

// 2. Create Signer
var signer = new PrivateKeySigner(System.getenv("SEPOLIA_PRIVATE_KEY"));

// 3. Create Wallet
WalletClient wallet = DefaultWalletClient.create(
    publicClient.getProvider(),
    publicClient,
    signer,
    ChainProfiles.ETH_SEPOLIA
);

Methods

sendTransactionAndWait

Sends a transaction and blocks until it is mined. This is the recommended method for scripts and backend services.
TransactionReceipt receipt = wallet.sendTransactionAndWait(
    new TransactionRequest(
        wallet.getAddress(),           // from
        new Address("0x..."),          // to
        Wei.fromEther("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 io.brane.core.builder.TxBuilder;

TransactionRequest request = TxBuilder.eip1559()
    .to(new Address("0x..."))
    .value(Wei.fromEther("0.1"))
    .build();

// Returns the transaction hash (e.g., "0x...")
String txHash = wallet.sendTransaction(request);

signTransaction

Signs a transaction offline without sending it.
TransactionRequest request = TxBuilder.eip1559()
    .to(new Address("0x..."))
    .value(Wei.fromEther("0.1"))
    .build();

// Returns the signed RLP-encoded transaction hex
String signedTx = wallet.signTransaction(request);

getAddress

Returns the address of the account managed by this wallet.
Address myAddress = wallet.getAddress();

Gas Management

The WalletClient 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.