5

Here's a scenario:

  1. Dapp backend connects to a remote Ethereum node using external HTTP RPC:

const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/janNFDJkMgDfan"));
  1. Dapp user sends transaction to a contract and then submits this transaction's hash to dapp backend.
  2. Dapp backend waits for the pending block to get confirmed and checks if the transaction is included in it.

Here's the problem:

As it turns out when web3 object is provided by an external node backend can't use events. For example, backend can't watch for new blocks. The following doesn't work:

var filter = web3.eth.filter('latest');

filter.watch(function(error, result){
  var block = web3.eth.getBlock(result, true);
  console.log('current block #' + block.number);
});

TypeError: filterCreationErrorCallback is not a function

So, the problem is that backend doesn't know when a new block is mined.

One way to handle this:

Backend can query the blockchain every ~20 seconds to check if a new block has been mined.

check(){
    web3.eth.getBlock("pending", (error, result) => {
      console.log('error:', error);
      console.log('results', result);
    });
}
setTimeout(check, 20000);

and then make sure that the user's transaction was included.

Another way of handling this is:

?????

1
  • 2
    Does "run your own node" count? Jan 10 '17 at 18:08
2

Still haven't got enoguh reputation to comment so I'm just dropping a line as an answer to say you should remove your access code from the example you provided! ;)

const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/***********"));

2
  • 2
    I doubt that this is important as this JS code can be view in the source code of the dapp, so this is probably not a secret. Jan 26 '17 at 15:22
  • You can edit answers/questions with 1 rep.
    – Balthazar
    Sep 1 '17 at 16:03
2

This is how I've managed to listen to events on the backend. The problem is that the events don't fire consistently.

const Web3 = require('web3');
const ProviderEngine = require('web3-provider-engine/index.js');
const ZeroClientProvider = require('web3-provider-engine/zero.js');

const engine = ZeroClientProvider({
  getAccounts: function(){},
  rpcUrl: 'https://ropsten.infura.io/WEQuhowqe78qdqweqw', //RANDOM ID, TO GET A REAL ONE, REGISTER AT INFURA.IO
})
const web3 = new Web3(engine);

const contractAddress = '0xc740ab099da657612143c34c5f7593a3af5ee6e8';
const contractAbi = [{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"emit","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"x","type":"uint256"}],"name":"myEvent","type":"event"}];
const contract = web3.eth.contract(contractAbi).at(contractAddress);

const event = contract.myEvent({x: null})
event.watch(function(error, result){
  console.log(error, result.args.x.toNumber());
});

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.