-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.rs
executable file
·145 lines (129 loc) · 4.74 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#![cfg_attr(not(feature = "std"), no_std, no_main)]
// # ✒️ Challenge 3: Connect your DAO to the Super DAO with registration and voting
//
// - **Difficulty**: Mid
// - **Submission Criteria:** ink! contract must
// - Import the Super DAO trait>
// - Store Super DAO contract address.
// - Register contract as member of Super DAO - using trait-based contract calling.
// - Vote on proposals in the Super DAO - using trait-based contract calling.
// - Create proposals to call another contract - using trait-based contract calling.
// - E2E test for cross-contract call.
// - **Submission Guidelines:**
// - Verify with R0GUE DevRel, and post on X.
// - **Prize:** Sub0 Merch & ink! sports towel
#[ink::contract]
mod dao {
use ink::{
contract_ref,
prelude::{string::String, vec},
storage::StorageVec,
};
use minidao_common::*;
use superdao_traits::{Call, ContractCall, SuperDao, Vote};
#[ink(storage)]
pub struct Dao {
superdao: contract_ref!(SuperDao),
name: String,
voters: StorageVec<AccountId>,
}
impl Dao {
// Constructor that initializes the values for the contract.
#[ink(constructor)]
pub fn new(name: String, superdao: AccountId) -> Self {
// Register your Dao as a member of the Superdao.
let mut instance = Self {
name,
superdao: superdao.into(),
voters: StorageVec::new(),
};
instance.superdao.register_member();
instance
}
#[ink(message)]
pub fn get_name(&self) -> String {
// - Returns the name of the Dao
self.name.clone()
}
#[ink(message)]
pub fn register_voter(&mut self) -> Result<(), DaoError> {
let caller = self.env().caller();
// - Error: Throw error `DaoError::VoterAlreadyRegistered` if the voter is registered
if self.has_voter(caller) {
return Err(DaoError::VoterAlreadyRegistered);
}
// - Success: Register a new `voter` to the Dao
self.voters.push(&caller);
Ok(())
}
#[ink(message)]
pub fn deregister_voter(&mut self) -> Result<(), DaoError> {
let caller = self.env().caller();
// - Error: Throw error `DaoError::VoterNotRegistered` if the voter is not registered
if !self.has_voter(caller) {
return Err(DaoError::VoterNotRegistered);
}
// - Success: Deregister a new `voter` from the Dao
for i in 0..self.voters.len() {
if let Some(voter) = self.voters.get(i) {
if voter == caller {
self.voters.clear_at(i);
}
}
}
Ok(())
}
#[ink(message)]
pub fn has_voter(&self, voter: AccountId) -> bool {
let mut registered = false;
for i in 0..self.voters.len() {
if let Some(v) = self.voters.get(i) {
if v == voter {
registered = true;
break;
}
}
}
registered
}
#[ink(message)]
pub fn create_superdao_contract_call_proposal(
&mut self,
call: ContractCall,
) -> Result<(), DaoError> {
let caller = self.env().caller();
// - Error: Throw error `DaoError::VoterNotRegistered` if the voter is not registered
if !self.has_voter(caller) {
return Err(DaoError::VoterNotRegistered);
}
// - Success: Create a SuperDao proposal to call a contract method.
self.superdao.create_proposal(Call::Contract(call).clone());
Ok(())
}
#[ink(message)]
pub fn vote_proposal(&mut self, proposal_id: u32, vote: bool) -> Result<(), DaoError> {
let caller = self.env().caller();
// - Error: Throw error `DaoError::VoterNotRegistered` if the voter is not registered
if !self.has_voter(caller) {
return Err(DaoError::VoterNotRegistered);
}
// - Success: Vote a SuperDao proposal.
let vote = if vote { Vote::Aye } else { Vote::Nay };
self.superdao.vote(proposal_id, vote);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::dao::Dao;
#[ink::test]
fn test_create_superdao_contract_call_proposal() {
todo!("Challenge 3");
}
#[ink::test]
fn test_vote_superdao_proposal() {
todo!("Challenge 3");
}
}
}