Skip to main content
Brane provides two primary classes for interacting with smart contracts: ReadOnlyContract and ReadWriteContract.

ReadOnlyContract

Used for calling pure and view functions. It requires a PublicClient.
import io.brane.contract.ReadOnlyContract;
import io.brane.contract.Abi;

// 1. Load ABI
Abi abi = Abi.fromJson(jsonString);

// 2. Create Contract
ReadOnlyContract contract = ReadOnlyContract.from(
    new Address("0x..."), 
    abi, 
    publicClient
);

// 3. Call Function
// call(methodName, ReturnType.class, args...)
Address ownerAddress = new Address("0x...");
BigInteger balance = contract.call("balanceOf", BigInteger.class, ownerAddress);

ReadWriteContract

Used for state-changing transactions. It requires both a PublicClient and a WalletClient.
import io.brane.contract.ReadWriteContract;

ReadWriteContract contract = ReadWriteContract.from(
    new Address("0x..."), 
    abi, 
    publicClient, 
    walletClient
);

// Send Transaction and Wait
// sendAndWait(methodName, timeoutMs, pollIntervalMs, args...)
Address recipientAddress = new Address("0x...");
BigInteger amount = BigInteger.valueOf(1000);

TransactionReceipt receipt = contract.sendAndWait(
    "transfer", 
    60_000, 
    1_000, 
    recipientAddress, 
    amount
);

Deploying Contracts

You can deploy contracts using BraneContract.deployRequest.
import io.brane.contract.BraneContract;

String abiJson = "[...]";
String bytecodeHex = "0x...";
String constructorArg1 = "MyToken";
String constructorArg2 = "MTK";

TransactionRequest deployRequest = BraneContract.deployRequest(
    abiJson,
    bytecodeHex,
    constructorArg1,
    constructorArg2
);

TransactionReceipt receipt = walletClient.sendTransactionAndWait(deployRequest, 60_000, 1_000);
Address newContractAddress = new Address(receipt.contractAddress().value());