【问题标题】:Transfer custom ERC20 token using web3 (invalid address err)使用 web3 传输自定义 ERC20 令牌(无效地址错误)
【发布时间】:2018-06-17 05:43:04
【问题描述】:

我正在使用 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)

我不知道这是怎么回事,我错过了什么吗? 请让我知道如何解决此“无效地址”错误。 谢谢!

【问题讨论】:

  • 参考post进行ERC20代币转账
  • 嘿,你找到解决办法了吗?

标签: javascript solidity smartcontracts web3 erc20


【解决方案1】:

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

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

【讨论】:

  • 我添加了console.log(web3.eth.getCoinbase);,它给了我{ [Function: get] request: [Function: bound ] } 这样的对象。我设置了 defaultAccount,所以没有(无效地址)错误,但是当我调用 sendTransaction 时,我收到一个错误,即 'Inavlid JSON RPC response: ""` 。我无法理解错误。是由于抛出错误的参数引起的还是你知道它是什么?
  • getCoinbase 是一个函数,因此您可以添加 console.log(web3.eth.getCoinbase());console.log(web3.eth.coinbase); 也应该可以工作
【解决方案2】:

配置默认账户为合约部署者的账户,然后进行转账。

eth.defaultAccount = eth.coinbase

【讨论】:

    猜你喜欢
    • 2018-06-19
    • 2018-10-01
    • 2021-12-16
    • 2022-06-14
    • 2019-12-01
    • 2021-09-15
    • 2018-11-18
    • 2023-03-31
    • 2021-12-20
    相关资源
    最近更新 更多