To undertake any of these transactions, it is necessary to have Ether (the fuel of the Ethereum
blockchain) residing in the Ethereum account which the transactions are taking place from. This is
to pay for the Gas costs, which is the transaction execution cost for the Ethereum client that
performs the transaction on your behalf, comitting the result to the Ethereum blockchain.
Instructions for obtaining Ether are described below in Obtaining Ether.

Mining it yourself in a private environment, or the public test environment (testnet) is very
straight forwards. However, in the main live environment (mainnet) it requires significant
dedicated GPU time which is not likely to be feasible unless you already have a gaming PC with
multiple dedicated GPUs. If you wish to use a private environment, there is some guidance on the
Homestead documentation.

To purchase Ether you will need to go via an exchange. As different regions have different
exchanges, you will need to research the best location for this yourself. The
Homestead documentation
contains a number of exchanges which is a good place to start.

There are a number of dedicated test networks in Ethereum, which are supported by various clients.

Rinkeby (Geth only)

Kovan (Parity only)

Ropsten (Geth and Parity)

For development, its recommended you use the Rinkeby or Kovan test networks. This is because they
use a Proof of Authority (PoA) consensus mechanism, ensuring transactions and blocks are created in
a consistent and timely manner. The Ropsten testnet, although closest to the Mainnet as it uses
Proof of Work (PoW) consensus, has been subject to attacks in the past and tends to be more
problematic for developers.

In the Ethereum test environment (testnet), the mining difficulty is set lower then the main
environment (mainnet). This means that you can mine new Ether with a regular CPU, such as your
laptop. What you’ll need to do is run an Ethereum client such as Geth or Parity to start building
up reserves. Further instructions are available on the respective sites.

When a transaction takes place in Ethereum, a transaction cost must be paid to the client that
executes the transaction on your behalf, committing the output of this transaction to the Ethereum
blockchain.

This cost is measure in gas, where gas is the number of instructions used to execute a transaction
in the Ethereum Virtual Machine. Please refer to the
Homestead documentation
for further information.

What this means for you when working with Ethereum clients is that there are two parameters which
are used to dictate how much Ether you wish to spend in order for a tranaction to complete:

Gas price

This is the amount you are prepared in Ether per unit of gas. web3j uses a default price
of 22,000,000,000 Wei
(22 x 10-8 Ether). This is defined in
ManagedTransaction.

Gas limit

This is the total amount of gas you are happy to spend on the transaction execution. There is an
upper limit of how large a single transaction can be in an Ethereum block which restricts this
value typically to less then 6,700,000. The current gas limit is visible at https://ethstats.net/.

These parameters taken together dictate the maximum amount of Ether you are willing to spend on
transaction costs. i.e. you can spend no more then gas price * gas limit. The gas price can also
affect how quickly a transaction takes place depending on what other transactions are available
with a more profitable gas price for miners.

You may need to adjust these parameters to ensure that transactions take place in a timely manner.

In order to transact via an Ethereum client, you first need to ensure that the client you’re
transacting with knows about your wallet address. You are best off running your own Ethereum client
such as Geth/Parity in order to do this. Once you have a client running, you can create a wallet
via:

The Geth Wiki contains
a good run down of the different mechanisms Geth supports such as importing private key files,
and creating a new account via it’s console

Alternatively you can use a JSON-RPC admin command for your client, such as personal_newAccount
for Parity
or Geth

With your wallet file created, you can unlock your account via web3j by first of all creating an
instance of web3j that supports Parity/Geth admin commands:

Adminweb3j=Admin.build(newHttpService());

Then you can unlock the account, and providing this was successful, send a transaction:

If you’d prefer not to manage your own Ethereum client, or do not want to provide wallet details
such as your password to an Ethereum client, then offline transaction signing is the way to go.

Offline transaction signing allows you to sign a transaction using your Ethereum Ethereum wallet
within web3j, allowing you to have complete control over your private credentials. A transaction
created offline can then be sent to any Ethereum client on the network, which will propagate the
transaction out to other nodes, provided it is a valid transaction.

You can also perform out of process transaction signing if required. This can be achieved by
overriding the sign method in
ECKeyPair.

Transactions to be used in an offline signing capacity, should use the
RawTransaction
type for this purpose. The RawTransaction is similar to the previously mentioned Transaction type,
however it does not require a from address, as this can be inferred from the signature.

In order to create and sign a raw transaction, the sequence of events is as follows:

The nonce is an increasing numeric value which is used to uniquely identify transactions. A nonce
can only be used once and until a transaction is mined, it is possible to send multiple versions of
a transaction with the same nonce, however, once mined, any subsequent submissions will be rejected.

Once you have obtained the next available nonce, the value can then be used to
create your transaction object:

The nonce is an increasing numeric value which is used to uniquely identify transactions. A nonce
can only be used once and until a transaction is mined, it is possible to send multiple versions of
a transaction with the same nonce, however, once mined, any subsequent submissions will be rejected.

The different types of transaction in web3j work with both Transaction and RawTransaction objects.
The key difference is that Transaction objects must always have a from address, so that the
Ethereum client which processes the
eth_sendTransaction
request know which wallet to use in order to sign and send the transaction on the message senders
behalf. As mentioned above, this is not necessary for raw transactions
which are signed offline.

The subsequent sections outline the key transaction attributes required for the different
transaction types. The following attributes remain constant for all:

Gas price

Gas limit

Nonce

From

Transaction and RawTransaction objects are used interchangeably in all of the subsequent examples.

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.

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.

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: