Events handler in Ethers.js not working

206
February 04, 2018, at 05:53 AM

I'm using https://github.com/ethers-io/ethers.js/ I have The following smart contract,

contract WhiteList {
   event logAddressChange(address indexed purchaser, uint256 value, uint256 amount);
   function changeContractAddress(address _purchaser, uint256 _whitelistCap)
        public
        returns (bool)
    {
        logAddressChange(_purchaser, _whitelistCap, _whitelistCap);
        return true;
    }
}

I'm trying to develop a node js listener for logAddressChange event. I want to listen to that event only.

my node server js

app.listen(port, function() {
    homeController.check_health();
});

My node js controller

var config = require('config');
var ethers = require('ethers');
var providers = require('ethers').providers;
var network = providers.networks.rinkeby;
var infuraProvider = new providers.InfuraProvider(network);
var etherscanProvider = new providers.EtherscanProvider(network);
var fallbackProvider = new providers.FallbackProvider([
    infuraProvider,
    etherscanProvider
]);
var provider = providers.getDefaultProvider(network)
var contract_provider = config.get('contract_provider');
var contract_address = config.get('contract_address');
var contract_abi = config.get('contract_abi');
var account_private_key = config.get('account_private_key');
var account_address = config.get('account_address');
var crm_url = config.get('crm_url');
var wallet = new ethers.Wallet(account_private_key, provider);
var contract = new ethers.Contract(contract_address, contract_abi, wallet);
var provider_contract = new ethers.Contract(contract_address, contract_abi, provider);
var overrideOptions = {
    gasLimit: 250000,
    nonce: 0,
    value: ethers.utils.parseEther('1.0')
};
exports.check_health = function(req, res) {
  console.log('started event');
  contract.events.onlogAddressChange = function(purchaser, value, amount) {
    console.log('started event1');
      console.log("purchaser:" + purchaser);
      console.log("value:" + value);
      console.log("amount:" + amount);
  };
}; 

I have waited for longer than an hour after triggering the event and still nothing.

I do not know what I'm doing wrong.

Can you please help?

Answer 1

So it turned out I missed something in the doc.

The event watcher has to be all lower case even if function is CamelCase:

Only change needed is instead of onlogAddressChange I should have onlogaddresschange

exports.check_health = function(req, res) {
  console.log('started event');
  contract.events.onlogaddresschange = function(purchaser, value, amount) {
    console.log('started event1');
      console.log("purchaser:" + purchaser);
      console.log("value:" + value);
      console.log("amount:" + amount);
  };
};

Thanks

Rent Charter Buses Company
READ ALSO
can't pass data in to delete request with Vue-Resource

can't pass data in to delete request with Vue-Resource

im getting problem with passing data in to delete request with Vue-resourceServer is reciving blank object without data

206
Libuv thread pool UV_THREADPOOL_SIZE in a node cluser setup

Libuv thread pool UV_THREADPOOL_SIZE in a node cluser setup

How is the environment variable UV_THREADPOOL_SIZE is used under Node Cluser setup?Support I have this variable set as 4 and there are 10 cluster worker processes, would there be 4 * 10 = 40 threads running?

385
StackDriver Custom Metric for Nodejs event loop latency

StackDriver Custom Metric for Nodejs event loop latency

I am trying to construct a custom metric for Google StackDriver that I can use to track nodejs event loop latenciesAll the apps are running in Google AppEngine so I am confined to using the monitored resource global (at least to my understanding)

223
How to return from a Node js callback [duplicate]

How to return from a Node js callback [duplicate]

This question already has an answer here:

272