Skip to content

Transactions and Smart Contracts

When working with smart contract wrappers as outlined below, you will have to perform all of the conversions from Solidity to native Java types manually. It is far more effective to use Web3j's Solidity smart contract wrappers which take care of all code generation and this conversion for you.

Creation of a smart contract

To deploy a new smart contract, the following attributes will need to be provided


the amount of Ether you wish to deposit in the smart contract (assumes zero if not provided)


the hex formatted, compiled smart contract creation code

// using a raw transaction
RawTransaction rawTransaction = RawTransaction.createContractTransaction(
        "0x <compiled smart contract code>");
// send...

// get contract address
EthGetTransactionReceipt transactionReceipt =

if (transactionReceipt.getTransactionReceipt.isPresent()) {
    String contractAddress = transactionReceipt.get().getContractAddress();
} else {
    // try again

If the smart contract contains a constructor, the associated constructor field values must be encoded and appended to the compiled smart contract code:

String encodedConstructor =
             FunctionEncoder.encodeConstructor(Arrays.asList(new Type(value), ...));

// using a regular transaction
Transaction transaction = Transaction.createContractTransaction(
        "0x <compiled smart contract code>" + encodedConstructor);

// send...

Transacting with a smart contract

To transact with an existing smart contract, the following attributes will need to be provided:


the smart contract address


the amount of Ether you wish to deposit in the smart contract (if the smart contract accepts ether)


the encoded function selector and parameter arguments

Web3j takes care of the function encoding for you, for further details on the implementation refer to the Application Binary Interface section.

Function function = new Function<>(
             "functionName",  // function we're calling
             Arrays.asList(new Type(value), ...),  // Parameters to pass as Solidity Types
             Arrays.asList(new TypeReference<Type>() {}, ...));

String encodedFunction = FunctionEncoder.encode(function)
Transaction transaction = Transaction.createFunctionCallTransaction(
             <from>, <gasPrice>, <gasLimit>, contractAddress, <funds>, encodedFunction);

org.web3j.protocol.core.methods.response.EthSendTransaction transactionResponse =

String transactionHash = transactionResponse.getTransactionHash();

// wait for response using EthGetTransactionReceipt...

It is not possible to return values from transactional functional calls, regardless of the return type of the message signature. However, it is possible to capture values returned by functions using filters. Please refer to the Filters and Events section for details.

Querying the state of a smart contract

This functionality is facilitated by the eth_call JSON-RPC call.

eth_call allows you to call a method on a smart contract to query a value. There is no transaction cost associated with this function, this is because it does not change the state of any smart contract method's called, it simply returns the value from them:

Function function = new Function<>(
             Arrays.asList(new Type(value)),  // Solidity Types in smart contract functions
             Arrays.asList(new TypeReference<Type>() {}, ...));

String encodedFunction = FunctionEncoder.encode(function)
org.web3j.protocol.core.methods.response.EthCall response = web3j.ethCall(
             Transaction.createEthCallTransaction(<from>, contractAddress, encodedFunction),

List<Type> someTypes = FunctionReturnDecoder.decode(
             response.getValue(), function.getOutputParameters());

Note: If an invalid function call is made, or a null result is obtained, the return value will be an instance of Collections.emptyList()