[Ethereum] Error from web3.eth.getPastLogs() hex string without 0x prefix

errorweb3js

I want to retrieve all token transfers to a given address by scanning the logs but receive an error. Am I doing something wrong? Could it be a web3 beta issue?

Code:

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
const transfer = web3.utils.keccak256( "Transfer(address,address,uint256)" );

// find all tokens received by this address ...
let sca = web3.utils.padLeft('0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8', 64);

console.log( 'transfer: ', transfer );
console.log( '     sca: ', sca );

web3.eth.getPastLogs( {
    fromBlock: 1,
    toBlock: 'latest',
    topics: [transfer, null, sca, null]
  }
).then( (events) => { console.log( events ); } );  

Result:

transfer:  0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef
     sca:  0x000000000000000000000000EA674fdDe714fd979de3EdF0F56AA9716B898ec8

Unhandled rejection Error: Returned error: invalid argument 0: hex string without 0x prefix
    at Object.ErrorResponse (/home/acct/node_modules/web3-core-helpers/src/errors.js:29:16)
    at /home/acct/node_modules/web3-core-requestmanager/src/index.js:137:36
    at XMLHttpRequest.request.onreadystatechange (/home/acct/node_modules/web3-providers-http/src/index.js:64:13)
    at XMLHttpRequestEventTarget.dispatchEvent (/home/acct/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/home/acct/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpResponseEnd (/home/acct/node_modules/xhr2/lib/xhr2.js:509:12)
    at IncomingMessage.<anonymous> (/home/acct/node_modules/xhr2/lib/xhr2.js:469:24)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:188:7)
    at endReadableNT (_stream_readable.js:975:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)

Environment:

  • node.js 7.8.0
  • web3 1.0.0-beta.22
  • geth 1.7.3-stable on go1.9
  • Ubuntu x86_64

Already tried:

  • use WebsocketProvider instead of HttpProvider
  • specify address: "0x..." in the filter with a real address
  • replace fromBlock: 1 with `fromBlock: "0x1", and same using 0
  • replace null topics with valid values
  • forcing transfer and sca to be strings using for example sca = '' + web3…

Yet to try:

  • revert to web3.js version 0.19

Best Answer

I have figured it out at my end while facing the same problem. When you are using fromBlock and toBlock parameters in the function call web3.eth.getPastLogs you will have to cast the values to their hex version/equivalent. Which can be done using :

web3.utils.toHex(<fromBlock/toBlock_Value>)

This worked for me.