This project provides a very simple starter application for working with web3j and the test Ethereum network (testnet) known as Rinkeby.
The demo deploys a hello world smart contract which contains a greeting message. The current greeting message can be read and modified in the smart contract.
In order to run this demo, three pieces of information are required:
- Ethereum client (or node) endpoint. The simplest thing to do is request a free access token from Infura.
- A wallet file. This can be generated using the web3j command line tools by running the command:
- Some Ether. This can be requested from the Rinkeby Faucet.
- To create a wallet file, run the command:
web3j wallet create
-
You will be asked to secure your wallet with a password. You will have the option of choosing a destination directory for your wallet file. In the
Application.java
class under the packageorg.web3j.sample
, on line66-67
replace the placeholders with your wallet file password and directory path. -
With the access token retrieved from Infura, on line
58
replace the placeholder in the url with your token. You can find this under theEndpoint
section:
Once these details have been added to the main Application class, you'll be good to go.
- Make sure to be in the root directory
- First we need to create the smart contracts deriving from the solidity file
Greeter.sol
by running:
mvn web3j:generate-sources
- This will create the Greeter and Mortal contract under the package
org.web3j.model
- Clean and build the project, run the command:
mvn install
This will also generate a jar file with all the dependencies which we will run once it has been created.
- Run the
Main
method inApplication.java
by running
mvn exec:java
- Alternatively, you can run the
main
method inApplication.java
in your chosen IDE, e.g.IntelliJ
This application demonstrates a number of core features of web3j and Ethereum:
- Connecting to a node on the Ethereum network
- Loading an Ethereum wallet file
- Sending Ether from one address to another
- Deploying a smart contract to the network
- Reading a value from the deployed smart contract
- Updating a value in the deployed smart contract
- Viewing an event logged by the smart contract
This application has purposefully been kept as simple as possible with the aim of demonstrating how to deploy and work with a smart contract on the decentralised Ethereum network.
If you require further background information about the above configuration items for the project you can refer to the below.
For a more comprehensive overview, I recommend you read the Java Magazine Article, watch the YouTube talk, and read all of the documentation.
The contained smart contract is based on the Greeter contract example, with the addition that the value stored in the Greeter can be modified.
The associated Java smart contract wrapper is named Greeter.
For more background on smart contracts, refer to the smart contracts section of the docs.
If you don't want to sign up to Infura, and would like to run a node yourself, there are instructions in the getting started section of the docs.
For more information on the different Ethereum test networks (or testnets), refer to the testnet overview in the docs.
For background on Ether, refer to the transactions overview section of the docs.
For a more technical overview of working with wallet files, refer to the Offline transaction signing section of the docs.
A logback configuration is included in the project dependencies allowing the logging of protocol messages if required. To enable, simply change the following line in the logback configuration to read:
<logger name="org.web3j.protocol" level="DEBUG"/>