0

I'm trying to subscribe to BAT(Basic Attention Token) ERC-20 token "Transfer" event to detect any transaction made to my ethereum address using web3 in nodejs. I'm using parity (Parity/v1.10.6-stable-bc0d134-20180605/x86_64-linux-gnu/rustc1.26.1) node running on same local machine -

parity --jsonrpc-hosts all --jsonrpc-apis all --ws-apis all --ws-hosts all --ws-origins all --ws-interface all

Here is the code that I've used to subscribe -

const Web3 = require('web3');
const abi = require('human-standard-token-abi');

var contract = '0x0D8775F648430679A709E98d2b0Cb6250d2887EF'; //BAT contract address

var web3Socket = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8546'));

function tokenEventListener = (address) => {
        let token = new web3Socket.eth.Contract(abi, address);

        console.log(address);
        let subscription = token.events.Transfer({
            fromBlock: 0
        })
            .on('data', (event) => {
                 // do something
             })
            .on('error', console.error);
}

tokenEventListener(contract);

I get an error when I run the code - Error: CONNECTION ERROR: Couldn't connect to node on WS

  • have you already a running provider ws://localhost:8546? – Badr Bellaj Jul 25 '18 at 12:13
  • no sir there are no other providers running from my side. Is there any way of checking from parity whether or not a provider is runnig? – dkpaul91 Jul 25 '18 at 13:25
0

Check the --ws-origins and --ws-hosts flags for parity. Try setting both to all.

  • I have made the changes that you mentioned, the issue still persists. – dkpaul91 Jul 26 '18 at 6:50
  • Try adding --ws-interface all. You should provide more information regardless. Where are you trying to run the code (node, in browser?)? Is the parity node running on the same machine? – Xilis Jul 26 '18 at 14:34
  • Parity node is running on my local machine and my code is written in Nodejs. adding --ws-interface all didn't solve it. – dkpaul91 Jul 26 '18 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.