Contract-Development

無法使用 web3.js 和 infura 呼叫事件

  • April 16, 2018

我的活動電話是

event AmountChanged(address indexed purchaser,  uint256 value);

我在 Solidity 中的功能

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

當我通過 web3 呼叫它時

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

我收到錯誤—-

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)

使用 web3@0.19.1 web3.providers.HttpProvider(" https://rinkeby.infura.io/ “)

提前致謝

我們可以用

npm install web3-provider-engine

從智能合約監聽事件

謝謝。

我記得,Infura 不支持基於 HTTP 的 API 來觀看事件。您需要使用不同的提供程序或像 MetaMask 一樣自己過濾日誌消息。

引用自:https://ethereum.stackexchange.com/questions/39059