Skip to main content
Brane allows you to bind smart contracts to Java interfaces, providing a type-safe and IDE-friendly way to interact with them.

1. Define Interface

Create a Java interface that mirrors your Solidity contract.
import io.brane.core.types.Address;
import io.brane.core.model.TransactionReceipt;
import java.math.BigInteger;

public interface Erc20 {
    // View function
    BigInteger balanceOf(Address account);

    // State-changing function
    TransactionReceipt transfer(Address to, BigInteger amount);
}

2. Bind Contract

Use BraneContract.bind to create an instance of your interface.
import io.brane.contract.BraneContract;

Erc20 token = BraneContract.bind(
    new Address("0x..."), 
    abiJson, 
    publicClient, 
    walletClient, 
    Erc20.class
);

3. Interact

Call methods directly on the interface.
// Read
BigInteger balance = token.balanceOf(myAddress);

// Write
TransactionReceipt receipt = token.transfer(recipient, amount);

Validation

When you call BraneContract.bind, the SDK performs strict validation to ensure your Java interface matches the contract ABI. This “fail-fast” behavior prevents runtime errors. The validation checks:
  1. Method Existence: Every method in your interface must exist in the ABI.
  2. Parameter Count: The number of arguments must match.
  3. Type Compatibility: Java types must be compatible with Solidity types (e.g., uint256 -> BigInteger).
  4. Mutability: You cannot bind a void return type to a view function (it must return a value).
If any check fails, bind throws an IllegalArgumentException with a detailed error message explaining the mismatch.

Supported Types

The binding system automatically maps Solidity types to Java types:
SolidityJava
uint256, int256java.math.BigInteger
addressio.brane.core.types.Address
booljava.lang.Boolean
stringjava.lang.String
bytesio.brane.core.types.HexData
uint8uint64java.math.BigInteger