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
504
505
506
507
508
509
510
511
use crate::{
    bank::OnChainTreasuryID,
    organization::{ShareID, TermsOfAgreement},
    traits::{
        ApproveGrant, ApproveWithoutTransfer, SetMakeTransfer, SpendApprovedGrant, StartReview,
        StartTeamConsentPetition,
    },
};
use codec::{Decode, Encode};
use frame_support::Parameter;
use sp_runtime::RuntimeDebug;
use sp_std::prelude::*;

#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, RuntimeDebug)]
pub enum BountyMapID {
    ApplicationId,
    MilestoneId,
}

impl Default for BountyMapID {
    fn default() -> BountyMapID {
        BountyMapID::ApplicationId
    }
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
/// The information most often read after a specific bounty is GOT
pub struct BountyInformation<AccountId, Hash, WeightedThreshold, Currency> {
    // Storage cid
    // - title, description, team requirements (all subjective metadata uses one reference)
    description: Hash,
    // registered organization associated with bounty
    foundation_id: u32,
    // On chain bank account associated with this bounty
    bank_account: OnChainTreasuryID,
    // Spend reservation identifier for funds set aside for bounty
    // TODO: update when funds are spent and a new spend reservation is required (gc)
    spend_reservation_id: u32,
    // Collateral amount in the bank account (TODO: refresh method for syncing balance)
    funding_reserved: Currency,
    // Amount claimed to have on hand to fund projects related to the bounty
    // - used to derive the collateral ratio for this bounty, which must be above the module lower bound
    claimed_funding_available: Currency,
    // Committee metadata for approving an application
    acceptance_committee: ReviewBoard<AccountId, Hash, WeightedThreshold>,
    // Committee metadata for approving milestones
    // -- if None, same as acceptance_committee by default
    supervision_committee: Option<ReviewBoard<AccountId, Hash, WeightedThreshold>>,
}

impl<AccountId: Clone, Hash: Parameter, WeightedThreshold: Clone, Currency: Parameter>
    BountyInformation<AccountId, Hash, WeightedThreshold, Currency>
{
    pub fn new(
        description: Hash,
        foundation_id: u32,
        bank_account: OnChainTreasuryID,
        spend_reservation_id: u32,
        funding_reserved: Currency,
        claimed_funding_available: Currency,
        acceptance_committee: ReviewBoard<AccountId, Hash, WeightedThreshold>,
        supervision_committee: Option<ReviewBoard<AccountId, Hash, WeightedThreshold>>,
    ) -> BountyInformation<AccountId, Hash, WeightedThreshold, Currency> {
        BountyInformation {
            description,
            foundation_id,
            bank_account,
            spend_reservation_id,
            funding_reserved,
            claimed_funding_available,
            acceptance_committee,
            supervision_committee,
        }
    }
    // get OrgId for sponsor org basically
    pub fn foundation(&self) -> u32 {
        self.foundation_id
    }
    pub fn bank_account(&self) -> OnChainTreasuryID {
        self.bank_account
    }
    pub fn spend_reservation(&self) -> u32 {
        self.spend_reservation_id
    }
    pub fn claimed_funding_available(&self) -> Currency {
        self.claimed_funding_available.clone()
    }
    pub fn acceptance_committee(&self) -> ReviewBoard<AccountId, Hash, WeightedThreshold> {
        self.acceptance_committee.clone()
    }
    pub fn supervision_committee(&self) -> Option<ReviewBoard<AccountId, Hash, WeightedThreshold>> {
        self.supervision_committee.clone()
    }
}

#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, RuntimeDebug)]
/// Identifier for each registered team
/// -> RULE: same org as bounty_info.foundation()
pub struct TeamID<AccountId> {
    org: u32,
    // this should be optional and in the future, I want to orient it towards revocable representative democracy
    sudo: Option<AccountId>,
    flat_share_id: u32,
    weighted_share_id: u32,
}

impl<AccountId: Clone + PartialEq> TeamID<AccountId> {
    pub fn new(
        org: u32,
        sudo: Option<AccountId>,
        flat_share_id: u32,
        weighted_share_id: u32,
    ) -> TeamID<AccountId> {
        TeamID {
            org,
            sudo,
            flat_share_id,
            weighted_share_id,
        }
    }
    pub fn org(&self) -> u32 {
        self.org
    }
    pub fn flat_share_id(&self) -> u32 {
        self.flat_share_id
    }
    pub fn weighted_share_id(&self) -> u32 {
        self.weighted_share_id
    }
    pub fn is_sudo(&self, who: &AccountId) -> bool {
        if let Some(the_sudo) = &self.sudo {
            the_sudo == who
        } else {
            false
        }
    }
}

#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, RuntimeDebug)]
/// Metadata that represents pre-dispatch, grant milestone reviews
pub enum ReviewBoard<AccountId, Hash, WeightedThreshold> {
    /// Petition pre-call-metadata
    /// optional sudo, org_id, flat_share_id, signature_approval_threshold, signature_rejection_threshold, topic
    FlatPetitionReview(Option<AccountId>, u32, u32, u32, Option<u32>, Option<Hash>),
    /// Vote-YesNo pre-call-metadata
    /// optional sudo, org_id, weighted_share_id, threshold expressed generically
    WeightedThresholdReview(
        Option<AccountId>,
        u32,
        u32,
        crate::voteyesno::SupportedVoteTypes,
        WeightedThreshold,
    ),
}

impl<AccountId: PartialEq, Hash, WeightedThreshold>
    ReviewBoard<AccountId, Hash, WeightedThreshold>
{
    pub fn new_flat_petition_review(
        sudo: Option<AccountId>,
        org_id: u32,
        flat_share_id: u32,
        approval_threshold: u32,
        reject_threshold: Option<u32>,
        topic: Option<Hash>,
    ) -> ReviewBoard<AccountId, Hash, WeightedThreshold> {
        ReviewBoard::FlatPetitionReview(
            sudo,
            org_id,
            flat_share_id,
            approval_threshold,
            reject_threshold,
            topic,
        )
    }
    pub fn new_weighted_threshold_review(
        sudo: Option<AccountId>,
        org_id: u32,
        weighted_share_id: u32,
        vote_type: crate::voteyesno::SupportedVoteTypes,
        threshold: WeightedThreshold,
    ) -> ReviewBoard<AccountId, Hash, WeightedThreshold> {
        ReviewBoard::WeightedThresholdReview(sudo, org_id, weighted_share_id, vote_type, threshold)
    }
    pub fn is_sudo(&self, acc: &AccountId) -> bool {
        match self {
            ReviewBoard::FlatPetitionReview(Some(the_sudo), _, _, _, _, _) => the_sudo == acc,
            ReviewBoard::WeightedThresholdReview(Some(the_sudo), _, _, _, _) => the_sudo == acc,
            _ => false,
        }
    }
}

#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, RuntimeDebug)]
/// Strongly typed vote identifier
pub enum VoteID {
    Petition(u32),
    Threshold(u32),
}

#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, RuntimeDebug)]
pub enum MilestoneStatus {
    SubmittedAwaitingResponse,
    SubmittedReviewStarted(VoteID),
    // if the milestone is approved but the approved application does not
    // have enough funds to satisfy milestone requirement, then this is set and we try again later...
    ApprovedButNotTransferred,
    // wraps Some(transfer_id) (bank_id is provided for convenient lookup, must equal bounty.bank)
    // None if the transfer wasn't able to be afforded at the time so it hasn't happened yet
    ApprovedAndTransferEnabled(OnChainTreasuryID, u32),
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub struct MilestoneSubmission<Hash, Currency, AccountId> {
    submitter: AccountId,
    // the approved application from which the milestone derives its legitimacy
    referenced_application: u32,
    team: TeamID<AccountId>, // to save a storage lookup at application_id
    submission: Hash,
    amount: Currency,
    // the review status, none upon immediate submission
    state: MilestoneStatus,
}

impl<Hash: Clone, Currency: Clone, AccountId: Clone>
    MilestoneSubmission<Hash, Currency, AccountId>
{
    pub fn new(
        submitter: AccountId,
        referenced_application: u32,
        team: TeamID<AccountId>,
        submission: Hash,
        amount: Currency,
    ) -> MilestoneSubmission<Hash, Currency, AccountId> {
        MilestoneSubmission {
            submitter,
            referenced_application,
            team,
            submission,
            amount,
            state: MilestoneStatus::SubmittedAwaitingResponse,
        }
    }
    pub fn application_id(&self) -> u32 {
        self.referenced_application
    }
    pub fn team(&self) -> TeamID<AccountId> {
        self.team.clone()
    }
    pub fn submission(&self) -> Hash {
        self.submission.clone()
    }
    pub fn amount(&self) -> Currency {
        self.amount.clone()
    }
    pub fn state(&self) -> MilestoneStatus {
        self.state
    }
    pub fn ready_for_review(&self) -> bool {
        match self.state {
            MilestoneStatus::SubmittedAwaitingResponse => true,
            _ => false,
        }
    }
}

impl<Hash: Clone, Currency: Clone, AccountId: Clone> StartReview<VoteID>
    for MilestoneSubmission<Hash, Currency, AccountId>
{
    fn start_review(&self, vote_id: VoteID) -> Self {
        MilestoneSubmission {
            submitter: self.submitter.clone(),
            referenced_application: self.referenced_application,
            team: self.team.clone(),
            submission: self.submission.clone(),
            amount: self.amount.clone(),
            state: MilestoneStatus::SubmittedReviewStarted(vote_id),
        }
    }
    fn get_review_id(&self) -> Option<VoteID> {
        match self.state {
            MilestoneStatus::SubmittedReviewStarted(vote_id) => Some(vote_id),
            _ => None,
        }
    }
}

impl<Hash: Clone, Currency: Clone, AccountId: Clone> ApproveWithoutTransfer
    for MilestoneSubmission<Hash, Currency, AccountId>
{
    fn approve_without_transfer(&self) -> Self {
        MilestoneSubmission {
            submitter: self.submitter.clone(),
            referenced_application: self.referenced_application,
            team: self.team.clone(),
            submission: self.submission.clone(),
            amount: self.amount.clone(),
            state: MilestoneStatus::ApprovedButNotTransferred,
        }
    }
}

impl<Hash: Clone, Currency: Clone, AccountId: Clone> SetMakeTransfer<OnChainTreasuryID, u32>
    for MilestoneSubmission<Hash, Currency, AccountId>
{
    fn set_make_transfer(&self, bank_id: OnChainTreasuryID, transfer_id: u32) -> Self {
        MilestoneSubmission {
            submitter: self.submitter.clone(),
            referenced_application: self.referenced_application,
            team: self.team.clone(),
            submission: self.submission.clone(),
            amount: self.amount.clone(),
            state: MilestoneStatus::ApprovedAndTransferEnabled(bank_id, transfer_id),
        }
    }
    fn get_bank_id(&self) -> Option<OnChainTreasuryID> {
        match self.state {
            MilestoneStatus::ApprovedAndTransferEnabled(bank_id, _) => Some(bank_id),
            _ => None,
        }
    }
    fn get_transfer_id(&self) -> Option<u32> {
        match self.state {
            MilestoneStatus::ApprovedAndTransferEnabled(_, transfer_id) => Some(transfer_id),
            _ => None,
        }
    }
}

#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, RuntimeDebug)]
pub enum ApplicationState<AccountId> {
    SubmittedAwaitingResponse,
    // wraps a VoteId for the acceptance committee
    UnderReviewByAcceptanceCommittee(VoteID),
    // includes the flat_share_id, and the
    ApprovedByFoundationAwaitingTeamConsent(ShareID, VoteID),
    // wraps outer_weighted_share_id associated with the team
    ApprovedAndLive(TeamID<AccountId>),
    // closed for some reason
    Closed,
}

impl<AccountId: PartialEq> ApplicationState<AccountId> {
    // basically, can be approved (notably not when already approved)
    pub fn live(&self) -> bool {
        match self {
            ApplicationState::SubmittedAwaitingResponse => true,
            ApplicationState::UnderReviewByAcceptanceCommittee(_) => true,
            _ => false,
        }
    }
    pub fn matches_registered_team(&self, team_id: TeamID<AccountId>) -> bool {
        match self {
            ApplicationState::ApprovedAndLive(tid) => tid == &team_id,
            _ => false,
        }
    }
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub struct GrantApplication<AccountId, Shares, Currency, Hash> {
    /// Useful and necessary metadata
    submitter: AccountId,
    /// The ipfs reference to the application information
    description: Hash,
    /// total amount
    total_amount: Currency,
    /// The terms of agreement that must agreed to by all members before the bounty execution starts
    terms_of_agreement: TermsOfAgreement<AccountId, Shares>,
    /// state of the application
    state: ApplicationState<AccountId>,
}

impl<AccountId: Clone, Shares: Clone, Currency: Clone, Hash: Clone>
    GrantApplication<AccountId, Shares, Currency, Hash>
{
    pub fn new(
        submitter: AccountId,
        description: Hash,
        total_amount: Currency,
        terms_of_agreement: TermsOfAgreement<AccountId, Shares>,
    ) -> GrantApplication<AccountId, Shares, Currency, Hash> {
        GrantApplication {
            submitter,
            description,
            total_amount,
            terms_of_agreement,
            state: ApplicationState::SubmittedAwaitingResponse,
        }
    }
    pub fn state(&self) -> ApplicationState<AccountId> {
        self.state.clone()
    }
    pub fn total_amount(&self) -> Currency {
        self.total_amount.clone()
    }
    pub fn terms_of_agreement(&self) -> TermsOfAgreement<AccountId, Shares> {
        self.terms_of_agreement.clone()
    }
}

impl<AccountId: Clone, Shares: Clone, Currency: Clone, Hash: Clone> StartReview<VoteID>
    for GrantApplication<AccountId, Shares, Currency, Hash>
{
    fn start_review(&self, vote_id: VoteID) -> Self {
        GrantApplication {
            submitter: self.submitter.clone(),
            description: self.description.clone(),
            total_amount: self.total_amount.clone(),
            terms_of_agreement: self.terms_of_agreement.clone(),
            state: ApplicationState::UnderReviewByAcceptanceCommittee(vote_id),
        }
    }
    fn get_review_id(&self) -> Option<VoteID> {
        match self.state() {
            ApplicationState::UnderReviewByAcceptanceCommittee(vote_id) => Some(vote_id),
            _ => None,
        }
    }
}

impl<AccountId: Clone, Shares: Clone, Currency: Clone, Hash: Clone>
    StartTeamConsentPetition<ShareID, VoteID>
    for GrantApplication<AccountId, Shares, Currency, Hash>
{
    fn start_team_consent_petition(&self, share_id: ShareID, vote_id: VoteID) -> Self {
        // could type check the flat_share_id and vote_petition_id
        GrantApplication {
            submitter: self.submitter.clone(),
            description: self.description.clone(),
            total_amount: self.total_amount.clone(),
            terms_of_agreement: self.terms_of_agreement.clone(),
            state: ApplicationState::ApprovedByFoundationAwaitingTeamConsent(share_id, vote_id),
        }
    }
    fn get_team_flat_id(&self) -> Option<ShareID> {
        match self.state() {
            ApplicationState::ApprovedByFoundationAwaitingTeamConsent(share_id, _) => {
                Some(share_id)
            }
            _ => None,
        }
    }
    fn get_team_consent_id(&self) -> Option<VoteID> {
        match self.state() {
            ApplicationState::ApprovedByFoundationAwaitingTeamConsent(_, vote_id) => Some(vote_id),
            _ => None,
        }
    }
}

impl<AccountId: Clone, Shares: Clone, Currency: Clone, Hash: Clone> ApproveGrant<TeamID<AccountId>>
    for GrantApplication<AccountId, Shares, Currency, Hash>
{
    fn approve_grant(&self, team_id: TeamID<AccountId>) -> Self {
        GrantApplication {
            submitter: self.submitter.clone(),
            description: self.description.clone(),
            total_amount: self.total_amount.clone(),
            terms_of_agreement: self.terms_of_agreement.clone(),
            state: ApplicationState::ApprovedAndLive(team_id),
        }
    }
    fn get_full_team_id(&self) -> Option<TeamID<AccountId>> {
        match self.state() {
            ApplicationState::ApprovedAndLive(team_id) => Some(team_id),
            _ => None,
        }
    }
}

impl<
        AccountId: Clone,
        Shares: Clone,
        Currency: Clone + sp_std::ops::Sub<Currency, Output = Currency> + PartialOrd,
        Hash: Clone,
    > SpendApprovedGrant<Currency> for GrantApplication<AccountId, Shares, Currency, Hash>
{
    fn spend_approved_grant(&self, amount: Currency) -> Option<Self> {
        match self.state {
            // grant must be in an approved state
            ApplicationState::ApprovedAndLive(_) => {
                // && amount must be below the grant application's amount
                if self.total_amount() >= amount {
                    let new_amount = self.total_amount() - amount;
                    Some(GrantApplication {
                        submitter: self.submitter.clone(),
                        description: self.description.clone(),
                        total_amount: new_amount,
                        terms_of_agreement: self.terms_of_agreement.clone(),
                        state: self.state.clone(),
                    })
                } else {
                    None
                }
            }
            _ => None,
        }
    }
}

#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
/// This struct is designed to track the payment for an ongoing bounty
pub struct BountyPaymentTracker<Currency> {
    received: Currency,
    due: Currency,
}

// upon posting a grant, the organization should assign reviewers for applications and state a formal review process for every bounty posted

// upon accepting a grant, the organization giving it should assign supervisors `=>` easy to make reviewers the supervisors