【问题标题】:web3.eth.accounts returning a functionweb3.eth.accounts 返回一个函数
【发布时间】:2018-02-19 01:09:16
【问题描述】:

我正在关注tutorial here that uses testrpc with web3.js。安装包ethereumjs-testrpcweb3 后,testrpc 启动,提供 10 个可用帐户及其私钥。

web3 位于 1.0.0-beta.18,ethereumjs-testrpc 位于 4.1.1。

当下面的代码运行时

Web3 = require('web3');
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
web3.eth.accounts

我得到以下输出,而不是教程中显示的 10 个帐户。出了什么问题?

Accounts {
  currentProvider: [Getter/Setter],
  _requestManager:
   RequestManager {
     provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
     providers:
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     subscriptions: {} },
  givenProvider: null,
  providers:
   { WebsocketProvider: [Function: WebsocketProvider],
     HttpProvider: [Function: HttpProvider],
     IpcProvider: [Function: IpcProvider] },
  _provider: HttpProvider { host: 'http://localhost:8545', timeout: 0, connected: false },
  setProvider: [Function],
  _ethereumCall:
   { getId:
      { [Function: send]
        method: [Object],
        request: [Function: bound ],
        call: 'net_version' },
     getGasPrice:
      { [Function: send]
        method: [Object],
        request: [Function: bound ],
        call: 'eth_gasPrice' },
     getTransactionCount:
      { [Function: send]
        method: [Object],
        request: [Function: bound ],
        call: 'eth_getTransactionCount' } },
  wallet:
   Wallet {
     length: 0,
     _accounts: [Circular],
     defaultKeyName: 'web3js_wallet' } }

在教程后面,部署合约时需要web3.eth.accounts

deployedContract = VotingContract.new(['Rama','Nick','Jose'],
    {data: byteCode, from: web3.eth.accounts[0], gas: 4700000})

【问题讨论】:

    标签: ethereum solidity web3


    【解决方案1】:

    如果web3.eth.accounts 未显示预期结果 然后,按照这两个简单的步骤(在松露控制台内)

    1. web3.eth.getAccounts().then(function(acc){ accounts = acc })

    2. accounts

    那是... 如果您想要特定帐户,那么accounts[0/1/2...9]

    【讨论】:

      【解决方案2】:

      该教程是在 web3.js v1 发布之前编写的。 API 在 v1 中发生了显着变化,包括eth.accounts。您可以固定到旧版本的 web3.js,例如 0.19.0,或者在新的 v1 docs 中找到等效方法。

      现在检索帐户是异步完成的,就像新 API 中的许多其他调用一样。因此,您可以使用回调或使用 Promise 来调用它。将帐户列表打印到控制台如下所示:

      web3.eth.getAccounts(console.log);
      // or
      web3.eth.getAccounts().then(console.log);
      

      来自web3.eth.getAccounts v1 documentation

      所以专门重写你最后引用的部分:

      web3.eth.getAccounts()
      .then(function (accounts) {
        return VotingContract.new(['Rama','Nick','Jose'],
          {data: byteCode, from: accounts[0], gas: 4700000});
      })
      .then(function (deployedContract) {
        // whatever you want to do with deployedContract...
      })
      

      【讨论】:

      • 谢谢,由于web3.eth.getAccounts() 使用 Promises,我也很难找到如何更新此代码 deployedContract = VotingContract.new(['Rama','Nick','Jose'], {data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
      • 好的,在答案中添加了一个部分
      猜你喜欢
      • 2018-11-04
      • 2020-08-30
      • 2018-07-06
      • 2019-05-17
      • 1970-01-01
      • 2019-08-15
      • 2020-10-25
      • 2021-11-10
      • 1970-01-01
      相关资源
      最近更新 更多