kri*_*eer 2 javascript ethereum solidity web3js metamask
我对 Solidity 很陌生,正在通过一个简单的网络应用程序来探索它。我正在尝试制作一个候选人投票网络应用程序,它接受一些详细信息和一个按钮,按下该按钮应将详细信息部署到智能合约创建的块中。当我尝试web3.eth.accounts[0]在 truffle console 中使用帐户详细信息时,它返回 undefined 。当我尝试使用 truffle 控制台从帐户 1 向候选人 1 发送选票时app.vote(1,{ from:web3.eth.accounts[0] }),我收到如下错误:
truffle(development)> web3.eth.accounts[0]
undefined
truffle(development)> app.vote(1,{
from:web3.eth.accounts[0] })
Thrown:
Error: The send transactions "from" field
must be defined!
at evalmachine.<anonymous>:0:5
at sigintHandlersWrap (vm.js:272:15)
at Script.runInContext (vm.js:127:14)
at runScript
at bound (domain.js:426:14)
at REPLServer.runBound [as eval] (domain.js:439:12)
at REPLServer.onLine (repl.js:726:10)
at REPLServer.emit (events.js:219:5)
at REPLServer.EventEmitter.emit (domain.js:482:12)
at REPLServer.Interface._onLine (readline.js:324:10)
at REPLServer.Interface._line (readline.js:701:8)
at REPLServer.Interface._ttyWrite (readline.js:1026:14)
at REPLServer.self._ttyWrite (repl.js:803:7)
at ReadStream.onkeypress (readline.js:200:10)
at ReadStream.emit (events.js:219:5)
at ReadStream.EventEmitter.emit (domain.js:482:12)
at emitKeys (internal/readline/utils.js:438:14)
at emitKeys.next (<anonymous>) {
hijackedStack: 'Error: The send transactions "from" field must be defined!\n' +
' at Method.inputTransactionFormatter (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-helpers\\src\\formatters.js:142:1)\n' +
' at C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:144:1\n' +
' at Array.map (<anonymous>)\n' +
' at Method.formatInput (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:142:1)\n' +
' at Method.toPayload (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:177:1)\n' +
' at Eth.send [as sendTransaction] (C:\\npm\\node_modules\\truffle\\build\\webpack:\\node_modules\\web3-core-method\\src\\index.js:467:1)\n' +
' at Object.sendTransaction (C:\\npm\\node_modules\\truffle\\build\\webpack:\\packages\\contract\\lib\\execute.js:486:1)\n' +
' at C:\\npm\\node_modules\\truffle\\build\\webpack:\\packages\\contract\\lib\\execute.js:203:1\n' +
' at processTicksAndRejections (internal/process/task_queues.js:97:5)'
}
Run Code Online (Sandbox Code Playgroud)
小智 6
您收到此错误是因为当您传递它时,未定义 from 字段undefined,正如您在运行truffle(development)> web3.eth.accounts[0]它返回时所看到的那样undefined。这是因为这是一个异步方法,这意味着当您发送交易时,您需要等待帐户方法完成。
因此,你应该这样做(考虑到web3.eth.accounts已被弃用并且每次输入 wait 都很乏味):
truffle(development)> let accounts = await web3.eth.getAccounts()
truffle(development)> app.vote(1,{ from: accounts[0] })
Run Code Online (Sandbox Code Playgroud)