How to Implement Ethereum Listener for Listening Transactions

Posted By : Ishank

Oct 28, 2020

What is a Transaction Listener?

 

A listener here is a method that we call when any transaction occurs on the Ethereum blockchain and returns transaction details that can be saved or used for tracking transactions on our application.

 

Different blockchains like Bitcoin have another way of implementing listeners, here we will only talk about Ethereum listener implementation.

 

Prerequisites

 

  • Java 8+
  • Basic understanding of blockchain
  • The application should be fully synced and connected to Ethereum main or test node according to the requirements
  • In this tutorial, we are using eth main network,
  • Spring boot understanding,
  • IDE
  • SpringToolSuit,
  • Active internet service
  • And your Ethereum service URL, maybe localhost or your own server

 

Dependency

 

In Pom.xml add the following web3j dependency. You can choose a different/latest version if you face issues with this version:

 

Maven:

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

 

Gradle:

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

 

After adding this dependency, wait for its jar to download automatically.

 

Now, we can create services and methods for the listener.

 

You may also like to read | Important Tips for Solidity Gas Optimization

 

import javax.annotation.PostConstruct;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;

@Service
public class EthereumListenerService {

    @Value("${ethereum.service.url}")//added in application.properties file/or can be hardcoded here
    private String serverurl;

    private Web3j web3j;

    @PostConstruct
    void init() {
        web3j = Web3j.build(new HttpService(serverurl));  //web3j  object after connecting with server for transaction
    }

    public void startListener() {
       //Ethereum listener started here tx is web3j transaction object described below
        web3j.transactionObservable().subscribe(tx -> {
            try {
                isActive = true;
                logger.info("Listened the transaction for trx hash = {} and address = {} ", tx.getHash(), tx.getTo());
                if (tx.getTo() == null || tx.getTo().trim().isEmpty()) {
                    //Empty block
                } else {
                    //can add checks for your application wallets because it will listen all transactions happening on ethereum
                   //To Do
                    }
                }
            } catch (Exception e) {
               //To do
            }
        }, error -> {
           //error in transaction listen
           //To do

        });

 

 

In the above code, the listener returns the web3j response Transaction object, which is as follows:-

 

package org.web3j.protocol.core.methods.response;

import java.math.BigInteger;

import org.web3j.utils.Numeric;


public class Transaction {
    private String hash;
    private String nonce;
    private String blockHash;
    private String blockNumber;
    private String transactionIndex;
    private String from;
    private String to;
    private String value;
    private String gasPrice;
    private String gas;
    private String input;
    private String creates;
    private String publicKey;
    private String raw;
    private String r;
    private String s;
    private int v;  // see https://github.com/web3j/web3j/issues/44
    }

 

Note: Object params can come in use according to requirements. Ethereum lister will notify about all transactions happening on the Ethereum blockchain, and not all transactions will be of use. We have to take care of that.

 

For more information related to Ethereum or smart contract development, connect with our skilled blockchain developers.

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

November 23, 2024 at 01:21 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
Telegram Button
Youtube Button

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