I am trying to write a web3js code. I have already written it using web3j (java) and now i want to do exactly same thing but using web3js. I have seen documentation and stuff, but i am not able to subscribe to events in web3js.
Below is my code written using web3j :-
web3j = Web3j.build(webSocketService);
Web3ClientVersion web3ClientVersion = web3j.web3ClientVersion().send();
ClientTransactionManager transactionManager = new ClientTransactionManager(web3j, RTKContractAddress);
EthFilter RTKContractFilter = new EthFilter(DefaultBlockParameterName.LATEST, DefaultBlockParameterName.LATEST, RTKContractAddress);
disposable = web3j.ethLogFlowable(RTKContractFilter).subscribe(log -> {
String hash = log.getTransactionHash(); // Here we obtain transaction hash of transaction from the log that we get from subscribe
String prevHash = flowableTransactionLog.get(numberOfTransactionsFetched.get() - 1).getTransactionHash();
if(!hash.equals(prevHash)) {
flowableTransactionLog.add(log);
System.out.println("Chat ID : " + chat_id + " - Trx : " + log.getTransactionHash());
Optional<Transaction> trx = web3j.ethGetTransactionByHash(hash).send().getTransaction();
trx.ifPresent(transaction -> allRTKTransactions.add(transaction));
numberOfTransactionsFetched.getAndIncrement();
}
}, throwable -> {
throwable.printStackTrace();
webSocketService.close();
webSocketService.connect();
});
In the above code, whenever there is a new trx with the smart contract, my code gets executed, and in that code, i add the received LOG in a ArrayList. Later, i read them when it becomes necessary.
So, in java subscribing to a smart contract was super easy. All i needed was the contract address. Using the contract address, i made a filter and using the filter, i subscribe to all the logs that will be made after the Latest block. Using the logs, I also fetched the transaction details.
But i am not able to do the same in web3js. One things i saw is that, to make a filter in web3js, i need to have the ABI of the contract, but the contract which i want to subscribe to is not open source and the ABI of the contract is not available online.
This is that token :- CRTS TOMO Token
What is the equivalent web3js code? Three problems/questions that i am facing are :-
- I don't have contact ABI
- Without ABI, how to make a filter
- How to subscribe to the smart contract.