Skip to content

Commit 9f0278d

Browse files
authored
Create interact_dao.py
1 parent b040fb2 commit 9f0278d

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

scripts/interact_dao.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import json
2+
from web3 import Web3
3+
4+
def load_contract(w3):
5+
# Load the contract information
6+
with open('dao_contract_info.json', 'r') as f:
7+
contract_info = json.load(f)
8+
9+
# Create the contract instance
10+
dao_contract = w3.eth.contract(address=contract_info['address'], abi=contract_info['abi'])
11+
return dao_contract
12+
13+
def create_proposal(w3, dao_contract, admin_account):
14+
description = input("Enter proposal description: ")
15+
tx_hash = dao_contract.functions.createProposal(description).transact({'from': admin_account})
16+
w3.eth.waitForTransactionReceipt(tx_hash)
17+
print("Proposal created successfully.")
18+
19+
def vote(w3, dao_contract, voter_account):
20+
proposal_id = int(input("Enter proposal ID to vote on: "))
21+
tx_hash = dao_contract.functions.vote(proposal_id).transact({'from': voter_account})
22+
w3.eth.waitForTransactionReceipt(tx_hash)
23+
print("Vote recorded successfully.")
24+
25+
def execute_proposal(w3, dao_contract, admin_account):
26+
proposal_id = int(input("Enter proposal ID to execute: "))
27+
tx_hash = dao_contract.functions.executeProposal(proposal_id).transact({'from': admin_account})
28+
w3.eth.waitForTransactionReceipt(tx_hash)
29+
print("Proposal executed successfully.")
30+
31+
def main():
32+
# Connect to the Ethereum network (e.g., Ganache)
33+
w3 = Web3(Web3.HTTPProvider('http://127.0.0.1:7545')) # Change to your provider
34+
assert w3.isConnected(), "Failed to connect to the Ethereum network"
35+
36+
# Load the DAO contract
37+
dao_contract = load_contract(w3)
38+
39+
# Set the admin account (the account that deployed the contract)
40+
admin_account = w3.eth.accounts[0] # Change as needed
41+
print(f"Using admin account: {admin_account}")
42+
43+
while True:
44+
print("\nOptions:")
45+
print("1. Create Proposal")
46+
print("2. Vote on Proposal")
47+
print("3. Execute Proposal")
48+
print("4. Exit")
49+
50+
choice = input("Select an option (1-4): ")
51+
52+
if choice == '1':
53+
create_proposal(w3, dao_contract, admin_account)
54+
elif choice == '2':
55+
voter_account = input("Enter your account address: ")
56+
vote(w3, dao_contract, voter_account)
57+
elif choice == '3':
58+
execute_proposal(w3, dao_contract, admin_account)
59+
elif choice == '4':
60+
print("Exiting...")
61+
break
62+
else:
63+
print("Invalid option. Please try again.")
64+
65+
if __name__ == '__main__':
66+
main()

0 commit comments

Comments
 (0)