Posted By : Ishank
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.
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.
November 23, 2024 at 01:21 am
Your comment is awaiting moderation.