|
| 1 | +import json |
| 2 | +from web3 import Web3 |
| 3 | +from solcx import compile_source |
| 4 | +import os |
| 5 | + |
| 6 | +# Sample Solidity code for the DAO contract |
| 7 | +DAO_CONTRACT_SOURCE = ''' |
| 8 | +// SPDX-License-Identifier: MIT |
| 9 | +pragma solidity ^0.8.0; |
| 10 | +
|
| 11 | +contract DAO { |
| 12 | + struct Proposal { |
| 13 | + string description; |
| 14 | + uint256 voteCount; |
| 15 | + mapping(address => bool) voters; |
| 16 | + bool executed; |
| 17 | + } |
| 18 | +
|
| 19 | + address public admin; |
| 20 | + Proposal[] public proposals; |
| 21 | + uint256 public proposalCount; |
| 22 | + uint256 public votingPeriod; |
| 23 | + uint256 public quorum; |
| 24 | +
|
| 25 | + event ProposalCreated(uint256 proposalId, string description); |
| 26 | + event Voted(uint256 proposalId, address voter); |
| 27 | + event ProposalExecuted(uint256 proposalId); |
| 28 | +
|
| 29 | + modifier onlyAdmin() { |
| 30 | + require(msg.sender == admin, "Only admin can call this function"); |
| 31 | + _; |
| 32 | + } |
| 33 | +
|
| 34 | + modifier proposalExists(uint256 _proposalId) { |
| 35 | + require(_proposalId < proposalCount, "Proposal does not exist"); |
| 36 | + _; |
| 37 | + } |
| 38 | +
|
| 39 | + modifier notVoted(uint256 _proposalId) { |
| 40 | + require(!proposals[_proposalId].voters[msg.sender], "You have already voted"); |
| 41 | + _; |
| 42 | + } |
| 43 | +
|
| 44 | + modifier notExecuted(uint256 _proposalId) { |
| 45 | + require(!proposals[_proposalId].executed, "Proposal already executed"); |
| 46 | + _; |
| 47 | + } |
| 48 | +
|
| 49 | + constructor(uint256 _votingPeriod, uint256 _quorum) { |
| 50 | + admin = msg.sender; |
| 51 | + votingPeriod = _votingPeriod; |
| 52 | + quorum = _quorum; |
| 53 | + } |
| 54 | +
|
| 55 | + function createProposal(string memory _description) public onlyAdmin { |
| 56 | + Proposal storage newProposal = proposals.push(); |
| 57 | + newProposal.description = _description; |
| 58 | + newProposal.voteCount = 0; |
| 59 | + newProposal.executed = false; |
| 60 | + proposalCount++; |
| 61 | +
|
| 62 | + emit ProposalCreated(proposalCount - 1, _description); |
| 63 | + } |
| 64 | +
|
| 65 | + function vote(uint256 _proposalId) public |
| 66 | + proposalExists(_proposalId) |
| 67 | + notVoted(_proposalId) |
| 68 | + notExecuted(_proposalId) |
| 69 | + { |
| 70 | + Proposal storage proposal = proposals[_proposalId]; |
| 71 | + proposal.voters[msg.sender] = true; |
| 72 | + proposal.voteCount++; |
| 73 | +
|
| 74 | + emit Voted(_proposalId, msg.sender); |
| 75 | + } |
| 76 | +
|
| 77 | + function executeProposal(uint256 _proposalId) public |
| 78 | + proposalExists(_proposalId) |
| 79 | + notExecuted(_proposalId) |
| 80 | + { |
| 81 | + Proposal storage proposal = proposals[_proposalId]; |
| 82 | + require(proposal.voteCount >= quorum, "Not enough votes to execute proposal"); |
| 83 | +
|
| 84 | + proposal.executed = true; |
| 85 | +
|
| 86 | + emit ProposalExecuted(_proposalId); |
| 87 | + } |
| 88 | +
|
| 89 | + function getProposal(uint256 _proposalId) public view |
| 90 | + proposalExists(_proposalId) |
| 91 | + returns (string memory, uint256, bool) |
| 92 | + { |
| 93 | + Proposal storage proposal = proposals[_proposalId]; |
| 94 | + return (proposal.description, proposal.voteCount, proposal.executed); |
| 95 | + } |
| 96 | +
|
| 97 | + function getProposalCount() public view returns (uint256) { |
| 98 | + return proposalCount; |
| 99 | + } |
| 100 | +} |
| 101 | +''' |
| 102 | + |
| 103 | +def main(): |
| 104 | + # Connect to the Ethereum network (e.g., Ganache) |
| 105 | + w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7545')) # Change to your provider |
| 106 | + assert w3.isConnected(), "Failed to connect to the Ethereum network" |
| 107 | + |
| 108 | + # Compile the contract |
| 109 | + compiled_sol = compile_source(DAO_CONTRACT_SOURCE) |
| 110 | + contract_interface = compiled_sol['<stdin>:DAO'] |
| 111 | + |
| 112 | + # Set up the account to deploy the contract |
| 113 | + account = w3.eth.accounts[0] # Use the first account from Ganache |
| 114 | + w3.eth.defaultAccount = account |
| 115 | + |
| 116 | + # Deploy the contract |
| 117 | + voting_period = 7 * 24 * 60 * 60 # 7 days in seconds |
| 118 | + quorum = 2 # Minimum votes required for execution |
| 119 | + dao_contract = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin']) |
| 120 | + |
| 121 | + # Create the transaction |
| 122 | + tx_hash = dao_contract.constructor(voting_period, quorum).transact() |
| 123 | + tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash) |
| 124 | + |
| 125 | + print(f"DAO contract deployed at address: {tx_receipt.contractAddress}") |
| 126 | + |
| 127 | + # Optionally save the contract address and ABI for future interactions |
| 128 | + with open('dao_contract_info.json', 'w') as f: |
| 129 | + json.dump({ |
| 130 | + 'address': tx_receipt.contractAddress, |
| 131 | + 'abi': contract_interface['abi'] |
| 132 | + }, f) |
| 133 | + |
| 134 | +if __name__ == '__main__': |
| 135 | + main() |
0 commit comments