Skip to content

spring boot application, which uses the web3j library to communicate with the ethereum (testnet) blockchain. The README also includes some challenges, where you can play around with web3j. Feel free to fork or clone the code.

Notifications You must be signed in to change notification settings

Julius278/eth-web3j-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

59 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

On the bottom of the README there are some challenges to get into the topic. Have fun!

General Web3J information

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>

Usage

keyfile

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.

api development / testing software

tools like Postman or Insomnia are great for testing REST endpoints. Pros use cURL ;)

Postman

example for postman or other testing tools:

postman_test

cURL

example for the blockNumber endpoint:

curl --location 'http://localhost:8080/api/eth/blockNumber'

different ETH node

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.

Web3j challenges

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

Challenge 1

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

intellij_maven_plugin

  • it should show a BUILD SUCCESS message

maven_build_success

  • 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

Challenge 2

  • 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

Challenge 3

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

Challenge 99

Write some units tests for your new code ;)

About

spring boot application, which uses the web3j library to communicate with the ethereum (testnet) blockchain. The README also includes some challenges, where you can play around with web3j. Feel free to fork or clone the code.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published