Posted By : Ishank
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.
It should have java8+, Spring boot, and any preferred IDE. We are using the Spring Tool Suite for our application..
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
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;
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
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:
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.
November 21, 2024 at 11:05 am
Your comment is awaiting moderation.