solidity - 如何与solidity函数交互并从不同的地址进行交易?
问题描述
所以我有一个我已经定义然后编译的solidity合约:
voting_contract_compiled = compile_contract('Voting')
voting_deployment_tx_receipt, Voting = deploy_contract(w3, voting_contract_compiled, 10)
当我这样做时,Voting.all_functions()
我得到:
[<Function getNumVoters()>,
<Function getStatus()>,
<Function getWinner()>,
<Function isVotingOpen()>,
<Function totalVotesFor(int256)>,
<Function validateAndCacheVote()>,
<Function voteForCandidate(int256)>,
<Function votesReceived(int256)>]
这是我定义的功能。我现在要做的是与来自默认帐户以外的发件人的这些功能进行交互。我不知道该怎么做。我是否需要编译另一份合同(这似乎不是正确的选择),但似乎每当我这样做时Voting.something
,它指的是那里的默认帐户,所以制作新合同是我唯一能想到的,但这似乎也是错误的然后我将实例化一个新合同。
我想做类似的事情:
account1 = {'from': w3.eth.accounts[1], 'value': w3.toWei(1, 'ether')}
Voting.functions.voteForCandidate(1).transact(account1)
但我明白了TransactionFailed: execution reverted: b''
。
解决方案
事实证明,这样做的方法如下:
transaction = {'from': w3.eth.accounts[6], 'value': w3.toWei(1, 'ether')}
tx_hash = Voting.functions.voteForCandidate(1).transact(transaction)
tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)
wherew3.eth.accounts
只是不同帐户名称的列表。
推荐阅读
- gstreamer - 在 gstreamer 中删除缓冲区
- android - 如何在 ubuntu 的 android studio 中添加新的实时模板
- asp.net - ASP.NET Webforms - 将 SSL 证书绑定到站点后,文件下载和 SSRS 报告显示功能失败
- python - Python - 图例与多个饼图重叠
- python - 自动即插即用 Raspberry PI WebSockets 的方法
- javascript - 如何使用我的代码修复网格模板问题?
- embedded - PISO 寄存器输出不如预期
- c++ - 标记指针 CRTP 访问器类
- android - Android studio Firebase 如何显示其他用户信息?
- visual-studio-code - 向下移动光标?VS Code 键盘快捷键