How to interact with a solidity function and make transactions from a different address?

Question:

So I have a solidity contract which I have defined and then compiled via:

voting_contract_compiled = compile_contract('Voting')
voting_deployment_tx_receipt, Voting = deploy_contract(w3, voting_contract_compiled, 10)

When I do Voting.all_functions() I get:

[<Function getNumVoters()>,
 <Function getStatus()>,
 <Function getWinner()>,
 <Function isVotingOpen()>,
 <Function totalVotesFor(int256)>,
 <Function validateAndCacheVote()>,
 <Function voteForCandidate(int256)>,
 <Function votesReceived(int256)>]

which are the functions I have defined. What I want to do now is interact with those functions from a sender other than the default account. I can’t figure out how to do this. Do I need to compile another contract (that doesn’t seem like the right option) but seemingly whenever I do Voting.something it is referring to the default account there so making a new contract is the only thing I can think of but that also seems wrong given that then I would instantiate a new contract.

I would like to do something like:

account1 = {'from': w3.eth.accounts[1], 'value': w3.toWei(1, 'ether')}
Voting.functions.voteForCandidate(1).transact(account1)

but I get TransactionFailed: execution reverted: b''.

Asked By: logankilpatrick

||

Answers:

As it turns out, the way to do this is as follows:

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)

where w3.eth.accounts is just a list of different account names.

Answered By: logankilpatrick
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.