以太坊智能合约开发教程

2021年5月26日

https://hackernoon.com/ethereum-development-walkthrough-part-2-truffle-ganache-geth-and-mist-8d6320e12269

web3 API文档:https://web3js.readthedocs.io/en/v1.2.0/index.html

可以看到有web3.eth.getAccounts但没有web3.eth.accounts。

truffle(development)> web3.eth.getAccounts()
[
  '0x4cf53f61FE5777A3f6Dc7231377063234fBAdF41',
  '0x427B2aD000C1f8a27267779282a856814DB50101',
  '0xD12b198844F890d876cD72426825010ad8a1E863',
  '0xCb792ba73312419f3Fa49E01BEA63CDF200b038a',
  '0x2029528aB01167Cc29628Ed95ee56A33d031B374',
  '0xe8e61Ff8fCc9A521aC807ccFC133A0a78cd56128',
  '0xEb607217D20Cc7D34CEE2041A0827Ae1EeB5c374',
  '0xcc5F73bFbe905FC038166e59cc88c642E176d895',
  '0xAeC4b817a6B3187A6aB6F17164d122B616a4379D',
  '0x7715E3dF3B38eE7AD6F534dB840C4e2C84c472F9'
]

在命令行运行getAccounts()返回的看起来像数组,但API文档表示它其实返回的是返回Array的Promise。难怪web3.eth.getAccounts().length返回的是undefined。要求length,就要用web3.eth.getAccounts().then(a=>console.log(a.length))

WrestlingInstance.wrestle({from: account0, value: web3.utils.toWei(“2”, “ether”)})