Web3.js: Can't transfer tokens

Created on 1 May 2019  路  19Comments  路  Source: ChainSafe/web3.js

Description

I'm trying to transfer tokens like this

const Web3 = require('web3');
const abi = require('human-standard-token-abi');

async function testSend() {
  let url = 'http://127.0.0.1:8545';
  let contractAddress = '0x6031ab93c5e436dfc6f4db819bf50f67e7b1289d';
  let from = '0x76b4514ceb23475be392e1305877a834401bdde8';
  let secret = 'secret';
  let to = '0xe63fb07be1bbc1d84c7e41554faf5457a1c4327e';
  let amount = 5 * 10 ** 18;
  let gas = 100000;

  let web3 = new Web3(new Web3.providers.HttpProvider(url));
  let contract = new web3.eth.Contract(abi, contractAddress);
  let gasPrice = await web3.eth.getGasPrice();
  await web3.eth.personal.unlockAccount(from, secret, null);
  let tx = await contract.methods.transfer(to, `${amount}`).send({from, gas, gasPrice});
  console.log(tx);
}

and I'm receiving an error "Invalid params: invalid type: null, expected a block number or 'latest', 'earliest' or 'pending'."

RPC calls traces

  1. eth_gasPrice
{ jsonrpc: '2.0', id: 0, method: 'eth_gasPrice', params: [] }
{ jsonrpc: '2.0', result: '0x3b9aca00', id: 0 }



md5-d0c8bf143f59fd4721c0088b89ccff66



{ jsonrpc: '2.0',
  id: 1,
  method: 'personal_unlockAccount',
  params:
   [ '0x76b4514ceb23475be392e1305877a834401bdde8',
     '734873kjn873',
     null ] }



md5-6a518bb51b86448b875c61ee3034b77b



{ jsonrpc: '2.0', result: true, id: 1 }



md5-f06863f3355b71b7d66b7a21e5263385



{ jsonrpc: '2.0',
  id: 2,
  method: 'eth_sendTransaction',
  params:
   [ { from: '0x76b4514ceb23475be392e1305877a834401bdde8',
       gas: '0x186a0',
       gasPrice: '0x3b9aca00',
       data:
        '0xa9059cbb000000000000000000000000e63fb07be1bbc1d84c7e41554faf5457a1c4327e0000000000000000000000000000000000000000000000004563918244f40000',
       to: '0x6031ab93c5e436dfc6f4db819bf50f67e7b1289d' } ] }



md5-6a518bb51b86448b875c61ee3034b77b



{ jsonrpc: '2.0',
  result:
   '0xd1e0cf24bb077418b1836470b2d225c37568b373490b1b2087987c2ec8c06545',
  id: 2 }



md5-f61d2f0153e73a98e036ac4c706d1d26



{ jsonrpc: '2.0',
  id: 3,                                                                                                                                                                                                      15:40
  method: 'eth_getTransactionReceipt',
  params:
   [ '0xd1e0cf24bb077418b1836470b2d225c37568b373490b1b2087987c2ec8c06545' ] }



md5-6a518bb51b86448b875c61ee3034b77b



{ jsonrpc: '2.0',
  result:
   { blockHash: null,
     blockNumber: null,
     contractAddress: null,
     cumulativeGasUsed: '0x96b9',
     from: null,
     gasUsed: '0x96b9',
     logs: [ [Object] ],
     logsBloom:
      '0x0000000000100000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000800000000000000000000000000000000010000000000000000
0000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000002000000000000000000100000000000000000000000000000000000000000010000000000000000000000000000002000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000002',
     root: null,
     status: '0x1',
     to: null,
     transactionHash:
      '0xd1e0cf24bb077418b1836470b2d225c37568b373490b1b2087987c2ec8c06545',
     transactionIndex: '0x0' },
  id: 3 }



md5-0c860c0cbd034894d0473ee570d9a032



{ jsonrpc: '2.0',
  id: 4,
  method: 'eth_getBlockByNumber',
  params: [ null, false ] }



md5-6a518bb51b86448b875c61ee3034b77b



{ jsonrpc: '2.0',
  error:
   { code: -32602,
     message:
      'Invalid params: invalid type: null, expected a block number or \'latest\', \'earliest\' or \'pending\'.' },
  id: 4 }

Versions

  • web3.js: 1.0.0-beta.53
  • nodejs: v10.12.0
  • parity: 2.4.5 (ropsten network)
more information needed

Most helpful comment

I agree, I'm still getting this issue. The issue shouldn't be closed until there's a satisfactory resolution for it. Please re-open.

All 19 comments

Thanks for opening this issue! As it looks like is partiy not returning a block number in the transaction receipt:

{ jsonrpc: '2.0',
  result:
   { blockHash: null,
     blockNumber: null,
     contractAddress: null,
     cumulativeGasUsed: '0x96b9',
     from: null,
     gasUsed: '0x96b9',
     logs: [ [Object] ],
     logsBloom:
      '0x0000000000100000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000800000000000000000000000000000000010000000000000000
0000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000002000000000000000000100000000000000000000000000000000000000000010000000000000000000000000000002000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000002',
     root: null,
     status: '0x1',
     to: null,
     transactionHash:
      '0xd1e0cf24bb077418b1836470b2d225c37568b373490b1b2087987c2ec8c06545',
     transactionIndex: '0x0' },
  id: 3 }

Could you open an issue in the parity repository?

BTW with 1.0.0-beta.36 the source piece of code works well.

Yes, because there isn't a check if a new block got mined if you use the HttpProvider. The fix in the latest version would be to use the WebsocketProvider.

Edit: TransactionObserver

Will the issue be fixed for the HttpProvider?

I've changed the source code with the WebsocketProvider. Now it hangs on the line:

let tx = await contract.methods.transfer(to, `${amount}`).send({from, gas, gasPrice});

@vanzay The HttpProvider issue got already fixed and will be released today.
Did you configure the transaction confirmation workflow as documented in the documentation?

Btw.: Could you ask further question in our gitter channel, on StackOverflow or the community-driven discord server?

In 1.0.0-beta.54 the HttpProvider still doesn't work.

Could you add a GitHub repository or all required code snippets to reproduce it?

Did you also test it with a configured transaction confirmation workflow as described in the documentation?

Also tried this way:

  let web3 = new Web3(new Web3.providers.HttpProvider(url), null, {
    transactionBlockTimeout: 50,
    transactionConfirmationBlocks: 24,
    transactionPollingTimeout: 480
  });

the same result.

You have to wait for 24 blocks and then the transaction will resolve.

But why do I receive an exception? How to avoid it?

@nivida Why has the issue been closed? It's not resolved yet (see the comment above).

I agree, I'm still getting this issue. The issue shouldn't be closed until there's a satisfactory resolution for it. Please re-open.

I am currently not happy:

  • to use httpprovider as expected, beta.51+ needs to be used (https://github.com/ethereum/web3.js/issues/1986#event-2279111028)
  • when using beta.51+, ganache is broken (https://github.com/ethereum/web3.js/issues/2610)

and now, when i finally figured all that out:

  • apparently, to transfer tokens, beta.36 needs to be used?? otherwise there is no clean way to support parity or am i wrong?

what is going on @nivida?

Could you reference a GitHub repository? @imbenwolf

hi @nivida please see referenced issues in my comment. here a short summary:

  • httpprovider returns "Uncaught TypeError: Cannot read property 'node' of undefined." when under beta.51 as per your comment
  • ganache-cli does not support eth_chainid rpc endpoint thus not working with web3 (bad!). It is unfortunate that the truffle team does not fix such issues quickly but why not calculate for a fallback? this way nobody can use ganache-cli to develop with web3!
  • after some experimenting, i realized that what you were suggesting "You have to wait for 24 blocks and then the transaction will resolve." can be achieved with setting "web3.transactionConfirmationBlocks = 24" in the future i would suggest constructively not closing an issue without also providing a working resolution as it will make it very hard for developers to keep up with all these issues while working for a company. I do appreciate the work & help you are giving though
Was this page helpful?
0 / 5 - 0 ratings

Related issues

xpepermint picture xpepermint  路  3Comments

dzarezenko picture dzarezenko  路  3Comments

sundbry picture sundbry  路  3Comments

mishell-trickster picture mishell-trickster  路  3Comments

gabmontes picture gabmontes  路  3Comments