An Introduction to Ethereum Blockchain Java Library Web3j

An Introduction to Ethereum Blockchain Java Library Web3j

Posted By : Ishank Gupta | 26-Aug-2020

What is Web3j

 

Web3j is a lightweight, type-safe Java and Android library for working with Smart Contracts and integrating with nodes on the Ethereum network. It allows us to work with the Ethereum blockchain, without the additional overhead of having to write our integration code for the platform.

 

Prerequisites:

 

It should have java8+, Spring boot, and any preferred IDE. We are using the Spring Tool Suite for our application..

 

Dependency:

 

Maven:

<dependency>
  <groupId>org.web3j</groupId>
  <artifactId>core</artifactId>
  <version>4.6.3</version>
</dependency>

Gradle:

compile ('org.web3j:core:4.6.3')

 

Now, we have added required dependency in our spring boot application, we are ready to use it's readymade methods one by one, firstly we will create a component class to use its instance in our application and then we will know about different classes with there returning objects.
 

@Component
public class Web3jServiceUtil {
    @Value("${ethereum.service.url}")//Etherium service provider url
    private String serverurl;

    private Web3j instance = null;

    @PostConstruct
    void init() {
        HttpService httpService = new HttpService(serverurl);
        instance = Web3j.build(httpService);
    }

    public Web3jServiceUtil() {
      //constructor
    }

    /**
     * method for getting instance of ethereum web3j
     */
    public  Web3j getWeb3jInstance() {
        return instance;
    }
}

 

Also, Read | Deploying a Multisignature Wallet Smart Contract on Ethereum

 

1.TransactionReceipt

 

TransactionReceipt transactionReceipt = web3j.ethGetTransactionReceipt("add transaction hash to get receipt")
                        .send().getTransactionReceipt().get();

 

Returns:-

     String transactionHash;
     String transactionIndex;
     String blockHash;
     String blockNumber;
     String cumulativeGasUsed;
     String gasUsed;
     String contractAddress;  // this is present in the spec
     String root;
     String from; //wallet address of origin account
     String to; //wallet address of destination account
     List<Log> logs;
     String logsBloom;

 

2.Transaction

 

To get transaction details by transaction hash:-

 

Transaction transaction=web3j.ethGetTransactionByHash(transactionHash);

Returns:-

private String hash;
     String nonce;
     String blockHash;
     String blockNumber;
     String transactionIndex;
     String from; //wallet address of origin account
     String to; //wallet address of destination account
     String value;
     String gasPrice;
     String gas;
     String input;
     String creates;
     String publicKey;
     String raw;
     String r;
     String s;
     int v;  // see https://github.com/web3j/web3j/issues/44

 

To Start listener for listening all incoming transactions:-

 

public void startListener() {
        web3j.transactionObservable().subscribe(tx -> {
           //to do code
        }, error -> {// to print any error while starting listener
            logger.error("error in transaction listen: {}", error);
        });

 

Returns Transaction object as shown above

 

Command Line Tools:

 

A web3j jar is distributed with each release providing command-line tools that allow us to use some of the functionality of web3j from the command line:

 

  • Wallet creation
  • Wallet password management
  • Transfer of funds from one wallet to another
  • Generate Solidity smart contract function wrappers

 

Please refer to the documentation, managedTransaction for further information.

 

Thanks for reading, if you found this helpful please do share it with your friends and colleagues. Also, drop your comment for any queries.

About Author

loading
Ishank Gupta

He is quick learner, logical thinker, team player and self learner.Always exited to learn new things :)

Leave a Comment

Name is required

Comment is required

Recaptcha is required.

No Comments Yet.

    Request For Proposal

    Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.

    We would love to hear from you!

    Oodles | Blockchain Development Company

    Please enter a valid Phone Number

    Please remove URL from text

    Recaptcha is required.