Since 2009, we have been utilizing our extensive expertise in blockchain technologies to help businesses, both large and small, maximize their efficiency.
Explore More
With more than 400+ experts, Oodles comprises a fantastic resource of business knowledge that spans multiple industries. Whatever the circumstances, we keep to our obligations.
Explore More
At Oodles, we help our clients work with a human understanding but at superhuman speed something that others can't. They thus advance and maintain their lead
2nd November 2020
14 min read
Technical Project Manager
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:
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.
Vishal Yadav
Vishal is a highly skilled backend developer with extensive 3+ years experience in developing various blockchain platforms. He has a comprehensive understanding of the technologies and has hands-on expertise in Node.js, Ethereum, Layer 1 and Layer 2 solutions, smart contract development, and databases like MySQL and MongoDB. He has a proven track record of working on a range of blockchain-related projects, including token development, staking, governance, indexes, bridges, NFT, marketplace, ICO/IDO, and more. He is adept at managing trading bots, and developing centralized exchanges, and has a creative mind with excellent analytical skills.
Technical Project Manager
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.
We would love to
hear from you!
Innovate with confidence!