sunshine-util 0.1.1

objects and relationships for sunshine-bounty pallets
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use sp_runtime::{
    DispatchError,
    DispatchResult,
};
use sp_std::prelude::*;

pub type Result<T> = sp_std::result::Result<T, DispatchError>;

// === Unique ID Logic, Useful for All Modules ===

pub trait IDIsAvailable<Id> {
    fn id_is_available(id: Id) -> bool;
}

pub trait GenerateUniqueID<Id> {
    fn generate_unique_id() -> Id;
}

pub trait SeededGenerateUniqueID<Id, Seed> {
    fn seeded_generate_unique_id(seed: Seed) -> Id;
}

pub trait Increment: Sized {
    fn increment(self) -> Self;
}

// ====== Permissions ACL ======

pub trait OrganizationSupervisorPermissions<OrgId, AccountId> {
    fn is_organization_supervisor(org: OrgId, who: &AccountId) -> bool;
    // removes any existing sudo and places None
    fn clear_organization_supervisor(org: OrgId) -> DispatchResult;
    // removes any existing sudo and places `who`
    fn put_organization_supervisor(
        org: OrgId,
        who: AccountId,
    ) -> DispatchResult;
}

// ---------- Membership Logic ----------

/// Checks that the `AccountId` is a member of a share group in an organization
pub trait GroupMembership<OrgId, AccountId> {
    fn is_member_of_group(org_id: OrgId, who: &AccountId) -> bool;
}
use orml_utilities::OrderedSet;
pub trait GetGroup<OrgId, AccountId> {
    fn get_group(organization: OrgId) -> Option<OrderedSet<AccountId>>;
}
/// Checks that the `total` field is correct by summing all assigned share quantities
pub trait VerifyShape {
    // required bound on GenesisAllocation
    fn verify_shape(&self) -> bool;
}
pub trait AccessGenesis<AccountId, Shares> {
    fn total(&self) -> Shares;
    fn account_ownership(&self) -> Vec<(AccountId, Shares)>;
}
pub trait AccessProfile<Shares> {
    fn total(&self) -> Shares;
}
use crate::share::SimpleShareGenesis;
pub trait ShareInformation<OrgId, AccountId, Shares> {
    type Profile: AccessProfile<Shares>;
    type Genesis: From<Vec<(AccountId, Shares)>>
        + Into<SimpleShareGenesis<AccountId, Shares>>
        + VerifyShape
        + AccessGenesis<AccountId, Shares>;
    /// Gets the total number of shares issued for an organization's share identifier
    fn outstanding_shares(organization: OrgId) -> Shares;
    // get who's share profile
    fn get_share_profile(
        organization: OrgId,
        who: &AccountId,
    ) -> Option<Self::Profile>;
    /// Returns the entire membership group associated with a share identifier, fallible bc checks existence
    fn get_membership_with_shape(organization: OrgId) -> Option<Self::Genesis>;
}
pub trait ShareIssuance<OrgId, AccountId, Shares>:
    ShareInformation<OrgId, AccountId, Shares>
{
    fn issue(
        organization: OrgId,
        new_owner: AccountId,
        amount: Shares,
        batch: bool,
    ) -> DispatchResult;
    fn burn(
        organization: OrgId,
        old_owner: AccountId,
        amount: Option<Shares>, // default None => burn all shares
        batch: bool,
    ) -> DispatchResult;
    fn batch_issue(
        organization: OrgId,
        genesis: Self::Genesis,
    ) -> DispatchResult;
    fn batch_burn(
        organization: OrgId,
        genesis: Self::Genesis,
    ) -> DispatchResult;
}
pub trait ReserveProfile<OrgId, AccountId, Shares>:
    ShareIssuance<OrgId, AccountId, Shares>
{
    fn reserve(
        organization: OrgId,
        who: &AccountId,
        amount: Option<Shares>,
    ) -> Result<Shares>;
    fn unreserve(
        organization: OrgId,
        who: &AccountId,
        amount: Option<Shares>,
    ) -> Result<Shares>;
}
pub trait LockProfile<OrgId, AccountId> {
    fn lock_profile(organization: OrgId, who: &AccountId) -> DispatchResult;
    fn unlock_profile(organization: OrgId, who: &AccountId) -> DispatchResult;
}
pub trait RegisterOrganization<OrgId, AccountId, Hash> {
    type OrgSrc;
    type OrganizationState;
    // called to form the organization in the method below
    fn organization_from_src(
        src: Self::OrgSrc,
        org_id: OrgId,
        parent_id: Option<OrgId>,
        supervisor: Option<AccountId>,
        value_constitution: Hash,
    ) -> Result<Self::OrganizationState>;
    fn register_organization(
        source: Self::OrgSrc,
        supervisor: Option<AccountId>,
        value_constitution: Hash,
    ) -> Result<OrgId>; // returns OrgId in this module's context
    fn register_sub_organization(
        parent_id: OrgId,
        source: Self::OrgSrc,
        supervisor: Option<AccountId>,
        value_constitution: Hash,
    ) -> Result<OrgId>;
}
pub trait RemoveOrganization<OrgId> {
    // returns Ok(Some(child_id)) or Ok(None) if leaf org
    fn remove_organization(id: OrgId) -> Result<Option<Vec<OrgId>>>;
    fn recursive_remove_organization(id: OrgId) -> DispatchResult;
}

// ====== Vote Logic ======

/// Retrieves the outcome of a vote associated with the vote identifier `vote_id`
pub trait GetVoteOutcome<VoteId> {
    type Outcome;

    fn get_vote_outcome(vote_id: VoteId) -> Result<Self::Outcome>;
}

/// Open a new vote for the organization, share_id and a custom threshold requirement
pub trait OpenVote<OrgId, Threshold, BlockNumber, Hash> {
    type VoteIdentifier;
    fn open_vote(
        topic: Option<Hash>,
        organization: OrgId,
        passage_threshold: Threshold,
        rejection_threshold: Option<Threshold>,
        duration: Option<BlockNumber>,
    ) -> Result<Self::VoteIdentifier>;
    fn open_unanimous_consent(
        topic: Option<Hash>,
        organization: OrgId,
        duration: Option<BlockNumber>,
    ) -> Result<Self::VoteIdentifier>;
}

pub trait OpenThresholdVote<OrgId, Threshold, BlockNumber, Hash, FineArithmetic>:
    OpenVote<OrgId, Threshold, BlockNumber, Hash>
{
    const THIRTY_FOUR_PERCENT: FineArithmetic;
    const FIFTY_ONE_PERCENT: FineArithmetic;
    const SIXTY_SEVEN_PERCENT: FineArithmetic;
    const SEVENTY_SIX_PERCENT: FineArithmetic;
    const NINETY_ONE_PERCENT: FineArithmetic;
    fn open_threshold_vote(
        topic: Option<Hash>,
        organization: OrgId,
        passage_threshold_pct: FineArithmetic,
        rejection_threshold_pct: Option<FineArithmetic>,
        duration: Option<BlockNumber>,
    ) -> Result<Self::VoteIdentifier>;
    fn open_34_pct_passage_threshold_vote(
        topic: Option<Hash>,
        organization: OrgId,
        duration: Option<BlockNumber>,
    ) -> Result<Self::VoteIdentifier>;
    fn open_51_pct_passage_threshold_vote(
        topic: Option<Hash>,
        organization: OrgId,
        duration: Option<BlockNumber>,
    ) -> Result<Self::VoteIdentifier>;
    fn open_67_pct_passage_threshold_vote(
        topic: Option<Hash>,
        organization: OrgId,
        duration: Option<BlockNumber>,
    ) -> Result<Self::VoteIdentifier>;
    fn open_76_pct_passage_threshold_vote(
        topic: Option<Hash>,
        organization: OrgId,
        duration: Option<BlockNumber>,
    ) -> Result<Self::VoteIdentifier>;
    fn open_91_pct_passage_threshold_vote(
        topic: Option<Hash>,
        organization: OrgId,
        duration: Option<BlockNumber>,
    ) -> Result<Self::VoteIdentifier>;
}

pub trait UpdateVoteTopic<VoteId, Hash> {
    fn update_vote_topic(
        vote_id: VoteId,
        new_topic: Hash,
        clear_previous_vote_state: bool,
    ) -> DispatchResult;
}

pub trait Approved {
    fn approved(&self) -> bool;
}
pub trait Rejected {
    fn rejected(&self) -> Option<bool>;
}
pub trait Apply<Signal, View>: Sized {
    fn apply(
        &self,
        magnitude: Signal,
        old_direction: View,
        new_direction: View,
    ) -> Option<Self>;
}

pub trait VoteVector<Signal, Direction, Hash> {
    fn magnitude(&self) -> Signal;
    fn direction(&self) -> Direction;
    fn justification(&self) -> Option<Hash>;
}

pub trait ApplyVote<Hash> {
    type Signal;
    type Direction;
    type Vote: VoteVector<Self::Signal, Self::Direction, Hash>;
    type State: Approved + Apply<Self::Signal, Self::Direction>;
    fn apply_vote(
        state: Self::State,
        vote_magnitude: Self::Signal,
        old_vote_view: Self::Direction,
        new_vote_view: Self::Direction,
    ) -> Option<Self::State>;
}

pub trait CheckVoteStatus<Hash, VoteId>:
    ApplyVote<Hash> + GetVoteOutcome<VoteId>
{
    fn check_vote_expired(state: &Self::State) -> bool;
}

pub trait MintableSignal<AccountId, OrgId, Threshold, BlockNumber, VoteId, Hash>:
    OpenVote<OrgId, Threshold, BlockNumber, Hash> + ApplyVote<Hash>
{
    fn mint_custom_signal_for_account(
        vote_id: VoteId,
        who: &AccountId,
        signal: Self::Signal,
    );
    fn batch_mint_equal_signal(
        vote_id: VoteId,
        organization: OrgId,
    ) -> Result<Self::Signal>;
    fn batch_mint_signal(
        vote_id: VoteId,
        organization: OrgId,
    ) -> Result<Self::Signal>;
}

/// Define the rate at which signal is burned to unreserve shares in an organization
pub trait BurnableSignal<AccountId, OrgId, Threshold, BlockNumber, VoteId, Hash>:
    MintableSignal<AccountId, OrgId, Threshold, BlockNumber, VoteId, Hash>
{
    fn burn_signal(
        vote_id: VoteId,
        who: &AccountId,
        amount: Option<Self::Signal>, // if None, then all
    ) -> DispatchResult;
}

pub trait VoteOnProposal<AccountId, OrgId, Threshold, BlockNumber, VoteId, Hash>:
    OpenVote<OrgId, Threshold, BlockNumber, Hash> + CheckVoteStatus<Hash, VoteId>
{
    fn vote_on_proposal(
        vote_id: VoteId,
        voter: AccountId,
        direction: Self::Direction,
        justification: Option<Hash>,
    ) -> DispatchResult;
}

// ====== Court Logic ======

pub trait RegisterDisputeType<AccountId, Currency, VoteMetadata, BlockNumber> {
    type DisputeIdentifier;
    fn register_dispute_type(
        locker: AccountId,
        amount_to_lock: Currency,
        dispute_raiser: AccountId,
        resolution_path: VoteMetadata,
        expiry: Option<BlockNumber>,
    ) -> Result<Self::DisputeIdentifier>;
}

// ~~~~~~~~ Bank Module ~~~~~~~~

pub trait BankPermissions<BankId, OrgId, AccountId> {
    fn can_open_bank_account_for_org(org: OrgId, who: &AccountId) -> bool;
    fn can_propose_spend(bank: BankId, who: &AccountId) -> Result<bool>;
    fn can_trigger_vote_on_spend_proposal(
        bank: BankId,
        who: &AccountId,
    ) -> Result<bool>;
    fn can_sudo_approve_spend_proposal(
        bank: BankId,
        who: &AccountId,
    ) -> Result<bool>;
    fn can_poll_spend_proposal(bank: BankId, who: &AccountId) -> Result<bool>;
    fn can_spend(bank: BankId, who: &AccountId) -> Result<bool>;
}

pub trait OpenBankAccount<OrgId, Currency, AccountId> {
    type BankId;
    fn open_bank_account(
        opener: AccountId,
        org: OrgId,
        deposit: Currency,
        controller: Option<AccountId>,
    ) -> Result<Self::BankId>;
}

pub trait SpendGovernance<BankId, Currency, AccountId> {
    type SpendId;
    type VoteId;
    type SpendState;
    fn propose_spend(
        bank_id: BankId,
        amount: Currency,
        dest: AccountId,
    ) -> Result<Self::SpendId>;
    fn trigger_vote_on_spend_proposal(
        spend_id: Self::SpendId,
    ) -> Result<Self::VoteId>;
    fn sudo_approve_spend_proposal(spend_id: Self::SpendId) -> DispatchResult;
    fn poll_spend_proposal(spend_id: Self::SpendId)
        -> Result<Self::SpendState>;
}

// ~~~~~~~~ Bounty Module ~~~~~~~~

pub trait ReturnsBountyIdentifier {
    type BountyId;
}

pub trait PostBounty<
    AccountId,
    OrgId,
    SpendableBank,
    Currency,
    Hash,
    ReviewCommittee,
>: ReturnsBountyIdentifier
{
    type BountyInfo;
    fn post_bounty(
        poster: AccountId,
        on_behalf_of: Option<SpendableBank>,
        description: Hash,
        amount_reserved_for_bounty: Currency,
        acceptance_committee: ReviewCommittee,
        supervision_committee: Option<ReviewCommittee>,
    ) -> Result<Self::BountyId>;
}
// TODO: make an issue for this and prefer an impl that presents a multisig
pub trait UseTermsOfAgreement<OrgId> {
    type VoteIdentifier;
    fn request_consent_on_terms_of_agreement(
        team_org: OrgId,
    ) -> Result<Self::VoteIdentifier>;
}
pub trait StartTeamConsentPetition<VoteIdentifier>: Sized {
    fn start_team_consent_petition(
        &self,
        vote_id: VoteIdentifier,
    ) -> Option<Self>;
    fn get_team_consent_vote_id(&self) -> Option<VoteIdentifier>;
}

pub trait StartReview<VoteIdentifier>: Sized {
    fn start_review(&self, vote_id: VoteIdentifier) -> Option<Self>;
    fn get_review_id(&self) -> Option<VoteIdentifier>;
}

pub trait ApproveWithoutTransfer: Sized {
    // infallible
    fn approve_without_transfer(&self) -> Self;
}

pub trait ApproveGrant: Sized {
    fn approve_grant(&self) -> Self;
    fn grant_approved(&self) -> bool;
}
// TODO: RevokeApprovedGrant<VoteID> => vote to take away the team's grant and clean storage

pub trait SpendApprovedGrant<Currency>: Sized {
    fn spend_approved_grant(&self, amount: Currency) -> Option<Self>;
}

pub trait SubmitGrantApplication<AccountId, VoteId, BankId, Currency, Hash>:
    ReturnsBountyIdentifier
{
    type GrantApp: StartReview<VoteId> + ApproveGrant; //+ StartTeamConsentPetition<VoteId>
    fn submit_grant_application(
        submitter: AccountId,
        bank: Option<BankId>,
        bounty_id: Self::BountyId,
        description: Hash,
        total_amount: Currency,
    ) -> Result<Self::BountyId>; // returns application identifier
}

pub trait SuperviseGrantApplication<BountyId, AccountId> {
    type AppState;
    fn trigger_application_review(
        bounty_id: BountyId,
        application_id: BountyId,
    ) -> Result<Self::AppState>;
    fn sudo_approve_application(
        sudo: AccountId,
        bounty_id: BountyId,
        application_id: BountyId,
    ) -> Result<Self::AppState>;
    fn poll_application(
        bounty_id: BountyId,
        application_id: BountyId,
    ) -> Result<Self::AppState>;
}

pub trait SubmitMilestone<AccountId, BountyId, Hash, Currency, VoteId, BankId> {
    type Milestone: StartReview<VoteId> + ApproveWithoutTransfer;
    type MilestoneState;
    fn submit_milestone(
        submitter: AccountId,
        bounty_id: BountyId,
        application_id: BountyId,
        submission_reference: Hash,
        amount_requested: Currency,
    ) -> Result<BountyId>; // returns milestone identifier
    fn trigger_milestone_review(
        bounty_id: BountyId,
        milestone_id: BountyId,
    ) -> Result<Self::MilestoneState>;
    fn sudo_approves_milestone(
        caller: AccountId,
        bounty_id: BountyId,
        milestone_id: BountyId,
    ) -> Result<Self::MilestoneState>;
    fn poll_milestone(
        bounty_id: BountyId,
        milestone_id: BountyId,
    ) -> Result<Self::MilestoneState>;
}

// We could remove`can_submit_grant_app` or `can_submit_milestone` because both of these paths log the submitter
// in the associated state anyway so we might as well pass the caller into the methods that do this logic and
// perform any context-based authentication there, but readability is more important at this point
pub trait BountyPermissions<OrgId, TermsOfAgreement, AccountId, BountyId>:
    UseTermsOfAgreement<OrgId>
{
    fn can_create_bounty(who: &AccountId, hosting_org: OrgId) -> bool;
    fn can_submit_grant_app(who: &AccountId, terms: TermsOfAgreement) -> bool;
    fn can_trigger_grant_app_review(
        who: &AccountId,
        bounty_id: BountyId,
    ) -> Result<bool>;
    fn can_poll_grant_app(who: &AccountId, bounty_id: BountyId)
        -> Result<bool>;
    fn can_submit_milestone(
        who: &AccountId,
        bounty_id: BountyId,
        application_id: BountyId,
    ) -> Result<bool>;
    fn can_poll_milestone(who: &AccountId, bounty_id: BountyId)
        -> Result<bool>;
    fn can_trigger_milestone_review(
        who: &AccountId,
        bounty_id: BountyId,
    ) -> Result<bool>;
}