[英]How to make transactions using web3.py from any account?
I am using Flask at the backend and ganache for my blockchain project.我在后端使用Flask并为我的区块链项目使用ganache 。 I am also using Metamask .
我也在使用Metamask 。 My smart contract is on the remix website.
我的智能合约在混音网站上。 And here is my contract:
这是我的合同:
pragma solidity ^0.4.21;
contract Election{
struct Candidate {
uint voteCount;
string name;
}
struct voter {
bool authorized;
bool voted;
uint vote;
}
address public owner;
string public electionName;
mapping(address => voter) public voters;
Candidate[] public candidates;
uint public totalVotes;
modifier ownerOnly() {
require(msg.sender == owner);
_;
}
constructor(string _name) public {
owner = msg.sender;
electionName = _name;
}
function addCandidate(string _name) ownerOnly public {
candidates.push(Candidate(0, _name));
}
function getNumCandidates() public view returns(uint) {
return candidates.length;
}
function authorize(address _person) ownerOnly public {
voters[_person].authorized = true;
}
function vote(uint _voteIndex) public {
require(!voters[msg.sender].voted);
require(voters[msg.sender].authorized);
voters[msg.sender].vote = _voteIndex;
voters[msg.sender].voted = true;
candidates[_voteIndex].voteCount += 1;
totalVotes += 1;
}
function end() ownerOnly public {
selfdestruct(owner);
}
}
I am facing a problem with Vote function.我遇到了投票function 的问题。 When I run this whole contract after deploying on the remix website it is working fine.
当我在混音网站上部署后运行整个合同时,它工作正常。 But when I am making transaction from my flask based app there is revert error even after having the voter in Voters list.
但是,当我从基于 flask 的应用程序进行交易时,即使在选民名单中有选民之后也会出现还原错误。 So using metamask I want to make my transaction for voting.
因此,我想使用元掩码进行投票交易。 I deploy the contract with a default account.
我使用默认帐户部署合同。
w3 = Web3(HTTPProvider('http://127.0.0.1:7545'))
w3.eth.defaultAccount = w3.eth.accounts[0]
contract = w3.eth.contract(address=address, abi=data["abi"])
tx_hash = contract.functions.vote(candidate_index).transact()
w3.eth.wait_for_transaction_receipt(tx_hash)
Now I want to make transactions from the account which I choose in Metamask.现在我想从我在 Metamask 中选择的帐户进行交易。 Because the above method is not working.
因为上面的方法行不通。 Is there any way or any example to do it?
有什么方法或例子可以做到吗? I also visited metamask documentation where the use of ethereum.request() is suggested.
我还访问了 metamask 文档,其中建议使用ethereum.request() 。 But I was unable to implement it in web3py.
但是我无法在 web3py 中实现它。
Your code is correct.你的代码是正确的。 There are 2 require statements in
vote
. vote
中有 2 个要求声明。 I think that is why you are getting the error.我认为这就是您收到错误的原因。 You can add custom message to
require
functions so you can see which one is causing the error:您可以将自定义消息添加到
require
函数,以便查看是哪一个导致了错误:
require(!voters[msg.sender].voted,"You already voted");
require(voters[msg.sender].authorized,"You are not authorized");
I believe since you already call vote
with your default account, you are not passing the first require
statement.我相信,由于您已经使用默认帐户进行了
vote
,因此您没有通过第一个require
语句。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.