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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use codec::{
    Codec,
    Decode,
    Encode,
};
use sp_runtime::{
    traits::Zero,
    RuntimeDebug,
};
use sp_std::prelude::*;

#[derive(new, PartialEq, Eq, Default, Clone, Encode, Decode, RuntimeDebug)]
/// The struct to track the organization's state
pub struct Organization<
    AccountId,
    Id: Codec + PartialEq + Zero + From<u32> + Copy,
    Hash,
> {
    /// The default sudo for this organization, optional because not _encouraged_
    sudo: Option<AccountId>,
    /// The parent organization for this organization
    parent_id: Option<Id>,
    /// The constitution
    constitution: Hash,
}

impl<
        AccountId: Clone + PartialEq,
        Id: Codec + PartialEq + Zero + From<u32> + Copy,
        Hash: Clone,
    > Organization<AccountId, Id, Hash>
{
    pub fn parent(&self) -> Option<Id> {
        self.parent_id
    }
    pub fn constitution(&self) -> Hash {
        self.constitution.clone()
    }
    pub fn is_parent(&self, cmp: Id) -> bool {
        if let Some(unwrapped_parent) = self.parent_id {
            unwrapped_parent == cmp
        } else {
            false
        }
    }
    pub fn is_sudo(&self, cmp: &AccountId) -> bool {
        if let Some(unwrapped_sudo) = &self.sudo {
            unwrapped_sudo == cmp
        } else {
            false
        }
    }
    pub fn clear_sudo(&self) -> Self {
        Organization {
            sudo: None,
            parent_id: self.parent_id,
            constitution: self.constitution.clone(),
        }
    }
    pub fn put_sudo(&self, new_sudo: AccountId) -> Self {
        Organization {
            sudo: Some(new_sudo),
            parent_id: self.parent_id,
            constitution: self.constitution.clone(),
        }
    }
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
/// The pieces of information used to register an organization in `org`
pub enum OrganizationSource<AccountId, Shares> {
    /// Will be initialized as an organization with a single ShareId and equal governance strength from all members
    Accounts(Vec<AccountId>),
    /// "" weighted governance strength by Shares
    AccountsWeighted(Vec<(AccountId, Shares)>),
}
impl<AccountId: PartialEq, Shares> From<Vec<(AccountId, Shares)>>
    for OrganizationSource<AccountId, Shares>
{
    fn from(
        other: Vec<(AccountId, Shares)>,
    ) -> OrganizationSource<AccountId, Shares> {
        OrganizationSource::AccountsWeighted(other)
    }
}
impl<AccountId: PartialEq, Shares> Default
    for OrganizationSource<AccountId, Shares>
{
    fn default() -> OrganizationSource<AccountId, Shares> {
        OrganizationSource::Accounts(Vec::new())
    }
}

#[derive(new, PartialEq, Eq, Default, Clone, Encode, Decode, RuntimeDebug)]
/// Static terms of agreement, define how the enforced payout structure for grants
pub struct TermsOfAgreement<AccountId, Shares, Hash> {
    /// Value constitution
    constitution: Hash,
    /// If Some(account), then account is the sudo for the duration of the grant
    supervisor: Option<AccountId>,
    /// The share allocation for metadata
    share_metadata: Vec<(AccountId, Shares)>,
}

impl<AccountId: Clone, Shares: Clone, Hash: Clone>
    TermsOfAgreement<AccountId, Shares, Hash>
{
    pub fn constitution(&self) -> Hash {
        self.constitution.clone()
    }
    pub fn supervisor(&self) -> Option<AccountId> {
        self.supervisor.clone()
    }
    pub fn flat(&self) -> Vec<AccountId> {
        self.share_metadata
            .clone()
            .into_iter()
            .map(|(account, _)| account)
            .collect::<Vec<AccountId>>()
    }
    pub fn weighted(&self) -> Vec<(AccountId, Shares)> {
        self.share_metadata.clone()
    }
}

#[derive(PartialEq, Eq, Default, Clone, Encode, Decode, RuntimeDebug)]
/// Defined paths for how the terms of agreement can change
pub struct FullTermsOfAgreement<AccountId, Rules, Decisions, Outcomes> {
    /// The starting state for the group
    basic_terms: Rules,
    /// This represents the metagovernance configuration, how the group can coordinate changes
    allowed_changes:
        Vec<(Catalyst<AccountId>, Option<Decisions>, Option<Outcomes>)>,
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
/// Authenticates that the given user can do the action in question to
/// trigger the `VoteConfig`
pub enum Catalyst<AccountId> {
    ReportBadBehavior(AccountId),
    SubmitMilestone(AccountId),
    RequestMilestoneAdjustment(AccountId),
    SwapRole(AccountId, AccountId),
} // TODO: upgrade path from suborganization to separate organization

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
/// These are all the vote configs planned to be supported
/// TODO: we have n = 1, 2 so do it with AccountId, up to `12`, one day somehow
pub enum VoteConfig<AccountId, OrgId, BlockNumber> {
    /// Only one supervisor approval is required but everyone has veto rights, for BlockNumber after approval
    OneSupervisorApprovalWithFullOrgShareVetoRights(
        AccountId,
        OrgId,
        BlockNumber,
    ),
    /// Two supervisor approvals is required but everyone has veto rights, for BlockNumber after approval
    TwoSupervisorsApprovalWithFullOrgShareVetoRights(
        AccountId,
        AccountId,
        OrgId,
        BlockNumber,
    ),
    /// Only one supervisor approval is required but everyone can vote to veto must reach threshold, for BlockNumber after approval
    OneSupervisorApprovalWith1P1VCountThresholdVetoRights(
        AccountId,
        OrgId,
        u32,
        BlockNumber,
    ),
    /// Two supervisor approvals is required but everyone can vote to veto must reach threshold, for BlockNumber after approval
    TwoSupervisorsApprovalWith1P1VCountThresholdVetoRights(
        AccountId,
        AccountId,
        OrgId,
        u32,
        BlockNumber,
    ),
    /// Only one supervisor approval is required but everyone can vote to veto must reach share weighted threshold, for BlockNumber after approval
    OneSupervisorApprovalWithShareWeightedVetoRights(
        AccountId,
        OrgId,
        u32,
        BlockNumber,
    ),
    /// Two supervisor approvals is required but everyone can vote to veto must reach share weighted threshold, for BlockNumber after approval
    TwoSupervisorsApprovalWithShareWeightedVetoRights(
        AccountId,
        AccountId,
        OrgId,
        u32,
        BlockNumber,
    ),
    /// Warning: Dictatorial and Centralized Governance, some say _practical_
    OnePersonOneVoteThresholdWithOneSupervisorVetoRights(
        OrgId,
        u32,
        AccountId,
        BlockNumber,
    ),
    OnePersonOneVoteThresholdWithTwoSupervisorsVetoRights(
        OrgId,
        u32,
        AccountId,
        AccountId,
        BlockNumber,
    ),
    ShareWeightedVoteThresholdWithOneSupervisorVetoRights(
        OrgId,
        u32,
        AccountId,
        BlockNumber,
    ),
    ShareWeightedVoteThresholdWithTwoSupervisorsVetoRights(
        OrgId,
        u32,
        AccountId,
        AccountId,
        BlockNumber,
    ),
    /// 1 person 1 vote, u32 threshold for approval, but everyone has veto rights, for BlockNumber after approval
    OnePersonOneVoteThresholdWithFullOrgShareVetoRights(
        OrgId,
        u32,
        BlockNumber,
    ),
    /// 1 person 1 vote, u32 threshold; only the second share group has veto rights (also must be flat!), for BlockNumber after approval
    OnePersonOneVoteThresholdANDVetoEnabledGroup(
        OrgId,
        u32,
        OrgId,
        BlockNumber,
    ),
    /// ShareWeighted vote, u32 threshold for approval, but everyone has veto rights, for BlockNumber after approval
    ShareWeightedVoteThresholdWithFullOrgShareVetoRights(
        OrgId,
        u32,
        BlockNumber,
    ),
    /// ShareWeighted vote, u32 threshold for approval, but everyone in second org has veto rights, for BlockNumber after approval
    ShareWeightedVoteThresholdANDVetoEnabledGroup(
        OrgId,
        u32,
        OrgId,
        BlockNumber,
    ),
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub enum EnforcedOutcome<AccountId> {
    /// Grant paid out as per bounty (hosting org, bounty recipient, milestone in question)
    /// (OrgId, ShareId, BountyId, MilestoneId)
    GrantPayoutBasedOnShareDistribution(u32, u32, u32, u32),
    /// Remove member for unacceptable behavior
    /// (OrgId, ShareId)
    RemoveMemberForBadBehavior(u32, u32, AccountId),
    /// Swap the first account for the second account in the same role for a grant team
    /// (OrgId, ShareId)
    SwapRoleOnGrantTeam(u32, u32, AccountId, AccountId),
}