Giter VIP home page Giter VIP logo

eth-web3j's Introduction

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 ;)

eth-web3j's People

Contributors

julius278 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.