On the bottom of the README there are some challenges to get into the topic. Have fun!
The application takes advantage of the Web3J library to communicate with the ethereum blockchain. To be correct, you connect to an ethereum (main or test net) node via JSON-RPC connection and retrieve the information this specific node/chain provides.
<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>4.10.3</version>
</dependency>
To also work with Solidity SmartContracts and interact with them within your code, you will have to add two build plugins.
The web3j-maven-plugin is needed
to generate the Java wrapper classes for your Solidity contracts.
The build-helper-maven-plugin
is needed to include the generated classes into your /target/ folder.
More details in the pom.xml
<build>
<plugins>
<plugin>
<groupId>org.web3j</groupId>
<artifactId>web3j-maven-plugin</artifactId>
<version>${web3j-maven-plugin.version}</version>
<executions>
<execution>
...
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
...
</execution>
</executions>
</plugin>
</plugins>
</build>
the given keyfile.json (/src/main/resources/) is a newly generated wallet with some SEPOLIA or Holesky testnet eth. If you test with this application, you can test with this wallet or create / include your own.
tools like Postman or Insomnia are great for testing REST endpoints. Pros use cURL ;)
example for postman or other testing tools:
example for the blockNumber endpoint:
curl --location 'http://localhost:8080/api/eth/blockNumber'
It is also possible to use every other node by simply replace the eth.node.address variable in application.properties.
You can also override the node by defining it via environment variables (ETH_NODE_ADDRESS=http....
)
As this is just a showcase / fun project / poc / whatever, there are no special profiles for main or test net.
Hey, glad you're ready to learn about Web3j.
Feel free to fork (or just clone) and play around.
If you want to track your transactions, you can observe them on the testnet block explorer:
https://holesky.etherscan.io/address/0x9f162b41fa8e44f885bedae410418145a4e8ed06
- Git clone project (https://github.com/Julius278/eth-web3j-java)
git clone https://github.com/Julius278/eth-web3j-java.git
- (if you have a GitHub account and are familiar with the platform, you can also fork and clone your own repository)
- you're on the "main" branch now
- Build the Java project on the project root directory
mvn clean install
- alternatively you can use the maven plugin of your IDE, here is an example for Intellij
- it should show a BUILD SUCCESS message
- To play around with the existing functionality you can start the spring boot service
mvn spring-boot:run
or use the PlayAroundDemo
- Everything is prepared
- you can just start by choosing your PropertyName and ID (uniqueness is checked on the PropertySafe)
- you don't have to worry about setting up a node, wallet, funding of your account, transaction fees or the connection
- Adjust a given solidity contract (Property.sol, e.g. a new getter function)
function getTest() external view returns (string memory){....}
- Build the Java maven project again
mvn clean install
- Use the new getter function in your Java code
- A possible solution is shown in branch: challenge-3-solution, in short you can see it here
Let's do a calculation on-chain
- Add a new solidity function which takes two numbers (int) and returns an int (like sum or multiply it)
- one hint to Integers
- Solidity supports 64 different integer types
- int results in Solidity to int256
- int in Solidity is way bigger than the normal int in Java, so in Java you will have to use BigInteger
- Build the Java maven project again
- Use the new function in your Java code
- A possible solution is shown in branch: challenge-3-solution, in short you can see it here
Write some units tests for your new code ;)