【问题标题】:How to interact with a solidity function and make transactions from a different address?如何与solidity函数交互并从不同的地址进行交易?
【发布时间】:2021-06-12 05:43:32
【问题描述】:

所以我有一个我已经定义然后编译的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''。

【问题讨论】:

    标签: solidity web3 ether web3py


    【解决方案1】:

    事实证明,这样做的方法如下:

    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)
    

    其中w3.eth.accounts 只是不同帐户名称的列表。

    【讨论】:

      猜你喜欢
      • 2022-08-12
      • 1970-01-01
      • 2017-06-27
      • 2021-11-07
      • 2023-01-10
      • 2022-08-13
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      相关资源
      最近更新 更多