An Introduction to Ethereum Blockchain Java Library Web3j

Posted By : Ishank

Aug 26, 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.

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

April 4, 2024 at 07:59 am

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text