3

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 :-

  1. I don't have contact ABI
  2. Without ABI, how to make a filter
  3. How to subscribe to the smart contract.
3
  • Is this an ERC20 token? Commented Sep 13, 2020 at 9:35
  • No, It is a TRC21 token Commented Sep 13, 2020 at 11:47
  • @goodvibration It's fine though. I just found the answer myself. Commented Sep 13, 2020 at 11:49

1 Answer 1

1

So i found the answer.

This is the equivalent code (Node.js) :-

const Web3 = require('Web3'); let web3 = new Web3(new Web3.providers.WebsocketProvider("wss://ws.tomochain.com")); var subscription = web3.eth.subscribe('logs', { address: "0x2c78958660E734701f5D710CF5e967A0C8585cE5" }, function(error, result){ if (!error) { // Here goes the equivalent code for operation to be performed // when we get the JSON of new transactions. We can get the hash from the // JSON and then get the transaction from the hash and do same thing as i did // in the java code. // If I just write the below line, then the respective output is given below the code. console.log(result); } }); 

When new transactions are made with the contract, this type of output if given :-

{ address: '0x2c78958660E734701f5D710CF5e967A0C8585cE5', topics: [ '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef', '0x00000000000000000000000050cd10c496a20e98c953e88d6ba7b3f6bc21c589', '0x00000000000000000000000050cd10c496a20e98c953e88d6ba7b3f6bc21c589' ], data: '0x0000000000000000000000000000000000000000000000000de0b6b3a7640000', blockNumber: 25679866, transactionHash: '0x3f9d89a07560e59a82d6189d6714c933130f3df2a08f09e90249bf908930c109', transactionIndex: 2, blockHash: '0x5c605a409ec4e6c7ba28cf411b7ab4e17884cea9bbfb2ad632122ce16020f00d', logIndex: 2, removed: false, id: 'log_dd9d4710' } 
2
  • Is the address in this case the contract address? Is that the second parameter when creating the ClientTransactionManager? Commented Nov 13, 2021 at 1:57
  • 1
    Yes, address is the contract address. Commented Nov 13, 2021 at 3:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.