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
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{program_error::ProgramError, pubkey::Pubkey};
use spl_math::precise_number::PreciseNumber;

use crate::error::Error;
use human_common::entity::Entity;

#[derive(Debug, BorshDeserialize, BorshSerialize)]
#[must_use]
pub struct State {
    // token wallet associated with this state
    pub wallet: Pubkey,
    pub owner: Pubkey,
    pub host: Pubkey,

    /// total amount distributed on the contract
    pub total_distributed: u64,
    /// total amount distributed minus host and owner split
    pub total_user_distributed: u64,

    pub settings: Settings,

    /// drop_idx is monotonically growing counter to track redeemed voucher
    /// voucher could only redeemed if it's drop_idx matches state's one
    /// on redeem it's drop_idx is incremented, so it can only be redeemed in next drop.
    /// Vouchers are created with current drop_idx, but since they can't be
    /// issued in the middle of distribution, we are safe
    pub drop_idx: u32,

    /// total vouchers in circulation
    pub vouchers_count: u32,

    /// total token balance in stake.
    /// counted separately to prevent someone from sending tokens directly
    /// to wallet and breaking math in the middle of distribution
    /// these tokens will also reduce everyone's share
    pub tokens_held: u64,

    /// current distribution
    pub distribution: Option<Distribution>,

    /// Staking token mint
    pub token_mint: Pubkey,

    /// While forming voting record, use this field
    pub realm_addr: Pubkey,

    /// Vault addr to add weight to creator vote
    pub vault_addr: Pubkey,
}

impl Entity for State {
    // Hardcoded size to allow for future migrations
    const SIZE: usize = 512;
    const MAGIC: u8 = 0x77;
}

#[derive(Debug, PartialEq, Eq)]
pub struct Split {
    pub distribute_amount: u64,
    pub owner_comission: u64,
    pub host_comission: u64,
}

#[derive(Debug, BorshDeserialize, BorshSerialize)]
pub struct Settings {
    /// minimum amount of tokens for user
    pub min_token_to_enroll: u64,
    /// owner commission % in basis points
    pub owner_fee: u16,
    /// host (us) commission % in basis points
    /// https://www.investopedia.com/terms/b/basispoint.asp
    pub host_fee: u16,
    /// host per user flat comission in lamports
    pub host_flat_fee: u32,
}

impl Settings {
    pub fn valid(&self) -> bool {
        if self.owner_fee.saturating_add(self.host_fee) >= 10000 {
            return false;
        }

        true
    }

    pub fn calculate_split(&self, balance: u64, num_vouchers: u32) -> Option<Split> {
        let bsp: PreciseNumber = PreciseNumber::new(10_000).unwrap();

        let balance_precise = PreciseNumber::new(balance as u128)?;
        let owner_fee = PreciseNumber::new(self.owner_fee as u128)?;
        let host_fee = PreciseNumber::new(self.host_fee as u128)?;

        // balance * 1500/10000 if comission is 15%
        let owner_split: u64 = balance_precise
            .checked_mul(&owner_fee)?
            .checked_div(&bsp)?
            .to_imprecise()?
            .try_into()
            .ok()?;

        dbg!(owner_split);

        let host_split: u64 = balance_precise
            .checked_mul(&host_fee)?
            .checked_div(&bsp)?
            .to_imprecise()?
            .try_into()
            .ok()?;

        let host_flat_fee = self.host_flat_fee.checked_mul(num_vouchers)?;

        let host_total_comission = host_split.checked_add(host_flat_fee as u64)?;

        let remaining_balance = balance
            .checked_sub(owner_split)?
            .checked_sub(host_total_comission)?;

        Some(Split {
            distribute_amount: remaining_balance,
            owner_comission: owner_split,
            host_comission: host_total_comission,
        })
    }
}

#[derive(Debug, BorshDeserialize, BorshSerialize, Clone)]
pub struct Distribution {
    pub distribute_amount: u64,
    pub seen_vouchers: u32,
}

impl Distribution {
    /// calculate distribution for this address. returns amount of lamports to transfer to user
    pub fn distribute_to(
        &mut self,
        voucher: &mut Voucher,
        drop_idx: u32,
        total_tokens: u64,
    ) -> Result<u64, ProgramError> {
        if voucher.drop_idx != drop_idx {
            return Error::VoucherNotEligible.into();
        }

        // two cases where vouches would not be valid for current drop:
        // 1. Voucher was created during distribution phase, so it has not been enumerated
        // 2. Voucher was already redeeemed (distributed to)
        let balance = PreciseNumber::new(voucher.balance as u128).unwrap();
        let total_tokens = PreciseNumber::new(total_tokens as u128).unwrap();
        let distribute_amount = PreciseNumber::new(self.distribute_amount as u128).unwrap();

        // TODO calculation precision
        // e.g. 0.25 (25%) of total tokens
        let owned_percentage = balance.checked_div(&total_tokens).ok_or(Error::Overflow)?;
        assert!(owned_percentage.less_than_or_equal(&PreciseNumber::new(1).unwrap()));

        // part of total tokens
        let part = distribute_amount
            .checked_mul(&owned_percentage)
            .ok_or(Error::Overflow)?;

        self.seen_vouchers = self.seen_vouchers.checked_add(1).ok_or(Error::Overflow)?;

        let lamports = part
            .to_imprecise()
            .ok_or(Error::Overflow)?
            .try_into()
            .map_err(|_| Error::Overflow)?;

        voucher.drop_idx = voucher.drop_idx.checked_add(1).ok_or(Error::Overflow)?;

        Ok(lamports)
    }
}

pub type Voucher = VoucherV2;

#[derive(Debug, BorshDeserialize, BorshSerialize)]
pub struct VoucherV2 {
    pub user: Pubkey,
    pub state: Pubkey,
    pub drop_idx: u32,
    pub balance: u64,
}

impl Entity for VoucherV2 {
    const SIZE: usize = 128;
    const MAGIC: u8 = 0x55;
}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;

    #[test]
    fn test_split() {
        let s = Settings {
            min_token_to_enroll: 1000,
            owner_fee: 1000,
            host_fee: 500,
            host_flat_fee: 5000,
        };

        assert_eq!(
            s.calculate_split(1_000_000, 5).unwrap(),
            Split {
                owner_comission: 100_000,
                host_comission: 50000 + 25000,
                distribute_amount: 825_000,
            }
        );

        // make sure weird values are calculated in a sane way
        assert_eq!(
            s.calculate_split(3_333_333, 5).unwrap(),
            Split {
                owner_comission: 333333,
                host_comission: 166667 + 25000,
                distribute_amount: 2808333,
            }
        );
    }

    #[test]
    fn test_distribute() {
        let mut ds = Distribution {
            distribute_amount: 100_000,
            seen_vouchers: 0,
        };

        let total_tokens = 1000;
        let drop_idx = 123;

        let mut v1 = Voucher {
            user: Pubkey::new_unique(),
            state: Pubkey::new_unique(),
            balance: 500,
            drop_idx,
        };

        assert_eq!(
            ds.distribute_to(&mut v1, drop_idx, total_tokens).unwrap(),
            50_000
        );
        assert_eq!(v1.drop_idx, drop_idx + 1);
        assert_eq!(ds.seen_vouchers, 1);

        let mut v2 = Voucher {
            user: Pubkey::new_unique(),
            state: Pubkey::new_unique(),
            balance: 250,
            drop_idx,
        };

        assert_eq!(
            ds.distribute_to(&mut v2, drop_idx, total_tokens).unwrap(),
            25_000
        );

        // can't redeem same voucher twice
        ds.distribute_to(&mut v2, drop_idx, total_tokens)
            .unwrap_err();
        assert_eq!(v2.drop_idx, drop_idx + 1);
        assert_eq!(ds.seen_vouchers, 2);

        // test invalid voucher
        let mut prev_idx_voucher = Voucher {
            user: Pubkey::new_unique(),
            state: Pubkey::new_unique(),
            balance: 250,
            drop_idx: drop_idx - 1,
        };
        ds.distribute_to(&mut prev_idx_voucher, drop_idx, total_tokens)
            .unwrap_err();

        let mut v3 = Voucher {
            user: Pubkey::new_unique(),
            state: Pubkey::new_unique(),
            balance: 250,
            drop_idx,
        };
        assert_eq!(
            ds.distribute_to(&mut v3, drop_idx, total_tokens).unwrap(),
            25_000
        );
        assert_eq!(v3.drop_idx, drop_idx + 1);
        assert_eq!(ds.seen_vouchers, 3);
    }

    proptest! {
        #[test]
        fn proptest_calculate_split(
            of in 0u16..8000,
            hf in 0u16..1000,
            hff in 0u32..100000,
            lamports: u64,
        ) {
            let settings = Settings {
                min_token_to_enroll: 1_0000,
                owner_fee: of,
                host_fee: hf,
                host_flat_fee: hff,
            };

            let split = settings.calculate_split(lamports, 10).unwrap();

            assert_eq!(split.distribute_amount + split.host_comission + split.owner_comission, lamports)
        }

        #[test]
        fn proptest_distribute(
            distribution_amount: u64, (total_tokens, mut vouchers) in vouchers(),
        ) {
            proptest_distribute_inner(distribution_amount, total_tokens, &mut vouchers);
        }
    }

    prop_compose! {
        fn vouchers()(vec in prop::collection::vec(0u64..100000, 1..100)) -> (u64, Vec<Voucher>) {
            let vouchers = vec.iter().map(|amount| Voucher { user: Pubkey::new_unique(), state: Pubkey::new_unique(), drop_idx: 1, balance: *amount }).collect();
            (vec.iter().sum(), vouchers)
        }
    }

    fn proptest_distribute_inner(
        total_amount: u64,
        total_tokens: u64,
        vouchers: &mut Vec<Voucher>,
    ) {
        let mut x = Distribution {
            distribute_amount: total_amount,
            seen_vouchers: 0,
        };

        let l = vouchers.len();
        let mut amounts = Vec::new();
        for v in vouchers {
            amounts.push(x.distribute_to(v, 1, total_tokens).unwrap());
        }

        let sum = amounts.iter().copied().sum::<u64>() as i64;
        let diff = sum.checked_sub(total_amount as i64).unwrap().abs();
        println!("{} {} {}", sum, total_amount, diff);
        assert!(diff as f64 <= total_amount as f64 * 0.0001);
        assert_eq!(x.seen_vouchers as usize, l);
    }
}