2

I'm using web3 library version 0.20.2. What I want to do with my smart contract is just transfer the custom erc20 token.

smart contract code below (sloidity)

 function transfer(address _to, uint256 _value) returns (bool success) {}

I want to use this function to transfer my custom token to _to (address).

var myContract = web3.eth.contract(abi)
var myContractInstance = myContract.at(address);
var total = myContractInstance.totalSupply();
var balance = myContractInstance.balanceOf(owner);
onsole.log('total',total);
console.log('balance',balance);

var to ="0x20....";
var value = 10;
var isAddress = web3.isAddress(to);
console.log(isAddress);

myContractInstance.transfer(to, value, (err,res)=> {console.log(err,res);});

Here is my console.

Rachals-MacBook-Pro:test rachel$ node app.js
total BigNumber { s: 1, e: 10, c: [ 10000000000 ] }
balance BigNumber { s: 1, e: 9, c: [ 9999000000 ] }

true

/Users/rachel/dev/test/node_modules/web3/lib/web3/formatters.js:274
    throw new Error('invalid address');
    ^



at inputAddressFormatter (/Users/rachel/dev/test/node_modules/web3/lib/web3/formatters.js:274:11)
at inputTransactionFormatter (/Users/rachel/dev/test/node_modules/web3/lib/web3/formatters.js:100:20)
at /Users/rachel/dev/test/node_modules/web3/lib/web3/method.js:89:28
at Array.map (<anonymous>)
at Method.formatInput (/Users/rachel/dev/test/node_modules/web3/lib/web3/method.js:88:32)
at Method.toPayload (/Users/rachel/dev/test/node_modules/web3/lib/web3/method.js:114:23)
at Eth.send [as sendTransaction] (/Users/rachel/dev/test/node_modules/web3/lib/web3/method.js:139:30)
at SolidityFunction.sendTransaction (/Users/rachel/dev/test/node_modules/web3/lib/web3/function.js:173:15)
at SolidityFunction.execute (/Users/rachel/dev/test/node_modules/web3/lib/web3/function.js:256:37)
at Object.<anonymous> (/Users/rachel/dev/test/app.js:35:20)

I don't know what is the matter with it. Do I miss something?

Please let me know how to fix this 'Invalid Address' err.

TylerH
  • 20,799
  • 66
  • 75
  • 101
rachel_hong
  • 471
  • 2
  • 6
  • 15
  • Refer to this [post](https://stackoverflow.com/a/50019666/6521116) to make the ERC20 token transfer – LF00 Apr 25 '18 at 12:01

2 Answers2

1

I would guess that you don't have a from address, since web3 will only validate to and from addresses of the transaction. The to address inside transfer function is part of the transaction payload and is not a standalone the transaction, so the var isAddress = web3.isAddress(to); check is not particularly useful

The from comes from your current node's coinbase account. Add console.log(web3.eth.coinbase) to your code and see what it is.

gaiazov
  • 1,908
  • 14
  • 26
  • I added the `console.log(web3.eth.getCoinbase);` and it gave me `{ [Function: get] request: [Function: bound ] }` like this object. I set the defaultAccount so there is no (invalid address)error but when I call sendTransaction I got an Error which is 'Inavlid JSON RPC response: ""` . I can't understand the error. Is is caused for throwing wrong parameter or do you know what is it? – rachel_hong Jan 10 '18 at 05:11
  • `getCoinbase` is a function, so you would add `console.log(web3.eth.getCoinbase());`. `console.log(web3.eth.coinbase);` should also work – gaiazov Jan 10 '18 at 16:06
1

Configure the default account to be that of the deployer of the contract, then make the transfer.

eth.defaultAccount = eth.coinbase
TylerH
  • 20,799
  • 66
  • 75
  • 101