首页 文章

使用web3传输自定义ERC20令牌(无效地址错误)

提问于
浏览
1

我正在使用web3库版本0.20.2 . 我想用智能合约做的只是传输自定义的erc20令牌 .

下面的智能合约代码(sloidity)

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

我想使用此函数将我的自定义标记转移到_to(地址) .

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);});

这是我的控制台 .

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)

我不知道有什么问题,我是否会错过什么?请告诉我如何修复此“无效地址”错误 . 谢谢!

1 回答

  • 0

    我猜你没有 from 地址,因为web3只会验证 transactiontofrom 地址 . transfer 函数中的 to 地址是事务有效负载的一部分,并不是独立的事务,因此 var isAddress = web3.isAddress(to); 检查不是特别有用

    from 来自您当前节点的coinbase帐户 . 将 console.log(web3.eth.coinbase) 添加到您的代码中,看看它是什么 .

相关问题