2

I'm using web3 to transfer an ERC20 token (for a contract already deployed on the Ethereum blockchain) from one address to another.

The code I'm using is shared below:

// script.js
Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/APIKEY"))

// Contract details
const contractABI = require('human-standard-token-abi')
const contractAddress = "0x039b5649a59967e3e936d7471f9c3700100ee1ab"
const decimals = 6;
const value = 0.01 * 10 ** decimals;

// Destination address
const toAddress = "0x130FCE6849E094b36816975a1632062BF65642D8";
web3.eth.accounts.wallet.add("PRIVATEKEY")

// Transfer
contract = new web3.eth.Contract(contractABI, contractAddress, {
             from: web3.eth.accounts.wallet[0].address, 
             gas: '100000'})
contract.methods.transfer(toAddress, value).send({}, function(err, txHash) {
  console.log(err, txHash);
});

When I run this script node script.js, I get the below error:

Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/Users/rock/Desktop/vote/node_modules/web3-core-helpers/src/errors.js:42:16)
    at XMLHttpRequest.request.onreadystatechange (/Users/rock/Desktop/vote/node_modules/web3-providers-http/src/index.js:60:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/Users/rock/Desktop/vote/node_modules/xhr2/lib/xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (/Users/rock/Desktop/vote/node_modules/xhr2/lib/xhr2.js:354:12)
    at XMLHttpRequest._onHttpResponseEnd (/Users/rock/Desktop/vote/node_modules/xhr2/lib/xhr2.js:509:12)
    at IncomingMessage.<anonymous> (/Users/rock/Desktop/vote/node_modules/xhr2/lib/xhr2.js:469:24)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1056:12)
    at _combinedTickCallback (internal/process/next_tick.js:138:11)
    at process._tickDomainCallback (internal/process/next_tick.js:218:9) undefined

This error is from the last line, where I make the transfer() call. I've tried calling a method that doesn't change state (e.g. contract.methods.balanceOf(toAddress).call() and that works fine.

Please help.

Ismael
  • 30,570
  • 21
  • 53
  • 96
FloatingRock
  • 257
  • 1
  • 2
  • 6
  • 1
    Usually this error comes in when the node has not openned RPC port Moreover, you pass an object when creating the contract instance (gas + grom), From the doc and all the code I've written using that, this should not be here. – Itération 122442 Jan 18 '18 at 06:37

1 Answers1

1

Maybe it's because the node don't support RPC. You can try to see ropsten

like this: --rpc --rpcapi="db,eth,net,web3,personal,web3" --rpccorsdomain '*' --rpcaddr localhost --rpcport 8545

Echo
  • 111
  • 1
  • 2