1

My event call is

event AmountChanged(address indexed purchaser,  uint256 value);

My function in solidity

function () external payable {   
    uint256  amountinwei = msg.value; 
    AmountChanged(msg.sender, msg.value);
}

When i am calling it through web3

contractinstance.AmountChanged({}, {fromBlock: 0, toBlock: 'latest'}).watch(function(err, result){
    console.log(result);
    console.log(err);
});

I am getting error ----

Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/Volumes/Macintosh HD 2 2/MorpheusApiGit/WebServices/Node-API-Morpho/node_modules/web3/lib/web3/errors.js:38:16)
    at XMLHttpRequest.request.onreadystatechange (/Volumes/Macintosh HD 2 2/MorpheusApiGit/WebServices/Node-API-Morpho/node_modules/web3/lib/web3/httpprovider.js:115:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/Volumes/Macintosh HD 2 2/MorpheusApiGit/WebServices/Node-API-Morpho/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/Volumes/Macintosh HD 2 2/MorpheusApiGit/WebServices/Node-API-Morpho/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpResponseEnd (/Volumes/Macintosh HD 2 2/MorpheusApiGit/WebServices/Node-API-Morpho/node_modules/xhr2/lib/xhr2.js:509:12)
    at IncomingMessage.<anonymous> (/Volumes/Macintosh HD2 2/MorpheusApiGit/WebServices/Node-API-Morpho/node_modules/xhr2/lib/xhr2.js:469:24)
    at IncomingMessage.emit (events.js:164:20)
    at endReadableNT (_stream_readable.js:1054:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickCallback (internal/process/next_tick.js:180:9)

using web3@0.19.1 web3.providers.HttpProvider("https://rinkeby.infura.io/")

Thanks in advanced

Matt Swezey
  • 1,246
  • 6
  • 13
Muddassar Shaikh
  • 145
  • 1
  • 3
  • 13

2 Answers2

1

We can use

npm install web3-provider-engine

to listen event from smart contract

Thanks.

Muddassar Shaikh
  • 145
  • 1
  • 3
  • 13
0

As I recall, Infura doesn't support the HTTP-based API for watching events. You'll need to use a different provider or filter through log messages yourself like MetaMask does.

user19510
  • 27,999
  • 2
  • 30
  • 48
  • Can you please provide some sample code and different provider or filter.

    Also, when we go live does main net of infura support http-based API ???

    – Muddassar Shaikh Feb 09 '18 at 09:05
  • Mainnet has the same restriction. Can you explain your scenario? Your users won't be using Mist or MetaMask? Is your app read-only? – user19510 Feb 09 '18 at 09:37
  • We are creating a crowd sale contract, we have tested this properly using truffle, we now wish to check this on rinkeby.

    However when we go live on the client side when we use web3, we would like to connect to mainnet without downloading it, thats why we choose infura.

    Can you please guide us, how to test this on rinkeby and which provider to use when we go live.....

    Thanks for your support

    – Muddassar Shaikh Feb 09 '18 at 09:44
  • Users won't be able to buy tokens unless they have something like MetaMask (or they trust you with their private keys). – user19510 Feb 09 '18 at 09:50
  • User would send ether to our contract and we would listen to event using web3.

    So, which web3 provider should we use on mainnet as event dont work with infura??

    Also, how contract are deployed on main net

    – Muddassar Shaikh Feb 09 '18 at 09:51
  • How will they send ether to your contract? – user19510 Feb 09 '18 at 09:52
  • They will directly send ethers to our contract address – Muddassar Shaikh Feb 09 '18 at 09:54
  • I don't think this is a productive conversation. – user19510 Feb 09 '18 at 09:55
  • @smarx Does infura still doesn't support Http methods, it is working for few though, I am following a video course where the calls were successful using the same code mentioned in the thread https://ethereum.stackexchange.com/questions/68145/unhandledpromiserejectionwarning-error-subscriptions-are-not-supported-with-th – Darey Mar 10 '19 at 09:59