javascript - web3.eth.accounts returning a function - Stack Overflow

I'm following a tutorial here that uses testrpc with web3.js. After installing the packages ethere

I'm following a tutorial here that uses testrpc with web3.js. After installing the packages ethereumjs-testrpc and web3js, testrpc is started which gives 10 available accounts and their private keys.

web3 is at 1.0.0-beta.18 and ethereumjs-testrpc at 4.1.1.

When the following code is run

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

I get the following output instead of the 10 accounts as shown in the tutorial. What went wrong?

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' } }

Later in the tutorial, web3.eth.accounts is needed when deploying the contract

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

I'm following a tutorial here that uses testrpc with web3.js. After installing the packages ethereumjs-testrpc and web3js, testrpc is started which gives 10 available accounts and their private keys.

web3 is at 1.0.0-beta.18 and ethereumjs-testrpc at 4.1.1.

When the following code is run

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

I get the following output instead of the 10 accounts as shown in the tutorial. What went wrong?

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' } }

Later in the tutorial, web3.eth.accounts is needed when deploying the contract

deployedContract = VotingContract.new(['Rama','Nick','Jose'],
    {data: byteCode, from: web3.eth.accounts[0], gas: 4700000})
Share Improve this question edited Jul 6, 2022 at 14:27 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Sep 10, 2017 at 19:30 NyxynyxNyxynyx 63.9k163 gold badges507 silver badges856 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

That tutorial was written before web3.js v1 came out. The API has changed significantly in v1, including eth.accounts. You can either pin to an older version of web3.js, like 0.19.0, or find the equivalent method in the new v1 docs.

Retrieving the accounts is now done asynchronously, like many other calls in the new API. So you can call it with a callback or using promises. Printing the list of accounts to your console would look like:

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

From the web3.eth.getAccounts v1 documentation

So specifically rewriting the section you referenced at the end:

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

If web3.eth.accounts is not showing the expected result then, follow this two simple step (Inside the truffle console)

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

  2. accounts

That's is... if you want specific account then accounts[0/1/2...9]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744780304a4593296.html

相关推荐

  • javascript - web3.eth.accounts returning a function - Stack Overflow

    I'm following a tutorial here that uses testrpc with web3.js. After installing the packages ethere

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信