admin管理员组文章数量:1392076
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
2 Answers
Reset to default 7That 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)
web3.eth.getAccounts().then(function(acc){ accounts = acc })
accounts
That's is...
if you want specific account then accounts[0/1/2...9]
本文标签: javascriptweb3ethaccounts returning a functionStack Overflow
版权声明:本文标题:javascript - web3.eth.accounts returning a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744780303a2624668.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论