1

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
  • Your code works fine for me as-is against ganache and works with fromBlock: "0x1" when running against Infura (https://mainnet.infura.io/<my-api-key>). I'm using the current web3.js beta (1.0.0-beta.29). – smarx Feb 10 '18 at 21:49
  • To add: I upgraded my web3 to 1.0.0-beta.29 but I get the same error – bitsanity Feb 10 '18 at 22:22
  • So you're finding that it works with testrpc but not with geth? – smarx Feb 10 '18 at 22:37
  • The code works for me (with fromBlock: "0x1") with geth. I'm on geth 1.7.3-stable with go1.9.4 on OS X. – smarx Feb 10 '18 at 22:40
  • I changed to fromBlock: "0x1" and it works now. So it was web3.js upgrade to beta 29 and this syntax change that did the trick ! – bitsanity Feb 10 '18 at 23:06
2

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.

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.