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
use crate::error::Error;
use crate::vest::*;
use crate::{consts::*, InitInstruction};
use borsh::{BorshDeserialize, BorshSerialize};
use human_common::entity::Entity;
use solana_program::program_memory::sol_memcpy;
use solana_program::{clock::UnixTimestamp, msg, program_error::ProgramError, pubkey::Pubkey};
use spl_math::precise_number::PreciseNumber;

/// Since accounts can't be resized this is constant size
/// to allow some headroom for future migrations
pub const STATE_ACC_SIZE: usize = 512 + 1;

pub type ContractState = ContractStateV4;

#[derive(Debug, BorshDeserialize, BorshSerialize)]
#[repr(C)]
pub struct ContractStateV4 {
    /// Associated token mint
    pub token: Pubkey,
    /// Alice
    pub owner: Pubkey,
    /// required for priveleged operations
    pub admin: Pubkey,
    /// comission WSOL account
    pub commission_addr: Pubkey,
    /// owner treasury
    pub treasury_addr: Pubkey,
    /// swap state
    pub swap_state: Pubkey,
    /// tokens sold
    pub sold: u64,
    /// vesting state
    pub vest: VestState,
    /// in progress drop. use helper methods to access this field
    pub drop: Option<DropV2>,
    /// round in progress, used for vesting 10% of tokens
    pub current_round: Option<Pubkey>,
    /// completed rounds count
    pub completed_rounds_count: u64,
}

impl Entity for ContractStateV4 {
    const SIZE: usize = STATE_ACC_SIZE;
    const MAGIC: u8 = 0x45;
}

#[derive(Debug, BorshDeserialize, BorshSerialize)]
#[repr(C)]
pub struct DropV2 {
    pub id: u64,
    /// price per chatlan in lamports
    pub price: u64,
    pub amount: u64,
    pub created_at: UnixTimestamp,
    pub start_date: UnixTimestamp,
    pub end_date: UnixTimestamp,
}

#[derive(Debug, BorshDeserialize, BorshSerialize)]
#[repr(C)]
pub struct Split {
    /// price per chatlan in lamports
    pub percent: u16,
    pub split_addr: Pubkey,
}

pub struct BuySplit {
    pub owner_split: u64,
    pub commission: u64,
    pub treasury_split: u64,

    pub token_commission: u64,
}

impl ContractState {
    pub fn calculate_buy_split(
        &self,
        now: UnixTimestamp,
        token_amount: u64,
        expected_price: u64,
    ) -> Result<BuySplit, ProgramError> {
        let price_per_chatlan = self.get_ongoing_drop_price(now)?;

        if price_per_chatlan > expected_price {
            return Error::ExpectedPriceMismatch.into();
        }

        let split =
            Self::calculate_split(price_per_chatlan, token_amount).ok_or(Error::Overflow)?;

        Ok(split)
    }

    fn calculate_split(price_per_chatlan: u64, token_amount: u64) -> Option<BuySplit> {
        let lamports = price_per_chatlan.checked_mul(token_amount)?;

        let tokens_precise = PreciseNumber::new(token_amount as u128)?;
        let commission_percent = PreciseNumber::new(BUY_COMMISSION as u128)?;

        let bsp = PreciseNumber::new(10_000)?;
        let token_commission: u64 = tokens_precise
            .checked_mul(&commission_percent)?
            .checked_div(&bsp)?
            .to_imprecise()?
            .try_into()
            .ok()?;

        let mut split = Self::calculate_split_by_lamports(lamports)?;

        split.token_commission = token_commission;

        Some(split)
    }

    pub fn calculate_split_by_lamports(lamports: u64) -> Option<BuySplit> {
        let lamports_precise = PreciseNumber::new(lamports as u128)?;

        let bsp = PreciseNumber::new(10_000)?;
        let commission_percent = PreciseNumber::new(BUY_COMMISSION as u128)?;

        let additional_split_percent = PreciseNumber::new(TREASURY_COMMISSION as u128)?;

        let commission: u64 = lamports_precise
            .checked_mul(&commission_percent)?
            .checked_div(&bsp)?
            .to_imprecise()?
            .try_into()
            .ok()?;

        let treasury_split: u64 = lamports_precise
            .checked_mul(&additional_split_percent)?
            .checked_div(&bsp)?
            .to_imprecise()?
            .try_into()
            .ok()?;

        let owner_split = lamports
            .checked_sub(commission)?
            .checked_sub(treasury_split)?;

        Some(BuySplit {
            commission,
            owner_split,
            treasury_split,
            token_commission: 0,
        })
    }

    /// try to get current drop price and validate if time window matches current time
    fn get_ongoing_drop_price(&self, now: UnixTimestamp) -> Result<u64, ProgramError> {
        let drop = self.drop.as_ref().ok_or(Error::NoDrop)?;

        if now < drop.start_date || now > drop.end_date {
            msg!(
                "missed drop time frame: start = {}, now = {}, end = {}",
                drop.start_date,
                now,
                drop.end_date
            );
            return Error::DropTimeframeExpired.into();
        }

        Ok(drop.price)
    }

    pub fn create_drop(
        &mut self,
        price: u64,
        id: u64,
        amount: u64,
        now: UnixTimestamp,
        start_date: UnixTimestamp,
        end_date: UnixTimestamp,
    ) -> Result<(), ProgramError> {
        if self.drop.is_some() {
            // drop already in progress
            return Error::DropInvalidDate.into();
        }

        if price == 0 {
            return Error::DropPriceZero.into();
        }

        if end_date <= start_date {
            return Error::DropInvalidDate.into();
        }

        self.drop = Some(DropV2 {
            id,
            amount,
            price,
            start_date,
            end_date,
            created_at: now,
        });

        Ok(())
    }

    pub fn clear_drop(&mut self) {
        self.drop = None;
    }
}

pub fn try_migrate_state(
    data: &mut [u8],
    swap_state: Pubkey,
    new_commission: Pubkey,
    treasury: Pubkey,
    now: UnixTimestamp,
) -> Result<(), ProgramError> {
    if data.is_empty() {
        return Err(ProgramError::InvalidAccountData);
    }

    let (magic, _payload) = (&data[0], &data[1..]);

    match *magic {
        ContractStateV4::MAGIC => {
            msg!("already up to date on v4");
            Ok(())
        }
        ContractStateV3::MAGIC => {
            msg!("migrating from v3 to v4");
            let v3 = ContractStateV3::deserialize_from(data)?;
            let v4 = v3
                .migrate(swap_state, new_commission, treasury, now)
                .try_to_vec()?;

            data[0] = ContractStateV4::MAGIC;
            copy_slice(&mut data[1..], &v4);

            Ok(())
        }
        _ => {
            msg!("invalid state");
            Err(ProgramError::InvalidAccountData)
        }
    }
}

pub fn init_state(
    data: &mut [u8],
    token: Pubkey,
    args: InitInstruction,
    now: UnixTimestamp,
) -> Result<(), ProgramError> {
    if data.is_empty() {
        return Err(ProgramError::AccountDataTooSmall);
    }

    if data[0] != 0 {
        return Err(ProgramError::AccountAlreadyInitialized);
    }

    let state = ContractState {
        token,
        owner: args.owner,
        admin: args.admin,
        commission_addr: args.commission,
        vest: VestState {
            deployed_at: now,
            vested_periods: 0,
        },
        drop: None,
        sold: 0,
        swap_state: args.swap_state,
        treasury_addr: args.treasury,
        current_round: None,
        completed_rounds_count: 0,
    };

    if ContractState::is_initialized(data) {
        return Err(ProgramError::AccountAlreadyInitialized);
    }

    state.serialize_to(data)?;

    Ok(())
}

#[inline]
fn copy_slice(dst: &mut [u8], src: &[u8]) {
    sol_memcpy(dst, src, src.len())
}

// old
#[derive(Debug, BorshDeserialize, BorshSerialize)]
#[repr(C)]
pub struct ContractStateV3 {
    /// Associated token mint
    pub token: Pubkey,
    /// Alice
    pub owner: Pubkey,
    /// required for priveleged operations
    pub admin: Pubkey,
    /// comission account, could be changed by the admin
    pub commission_addr: Pubkey,
    /// tokens sold
    pub sold: u64,
    /// vesting state
    pub vest: VestState,
    /// in progress drop. use helper methods to access this field
    pub drop: Option<DropV1>,
    /// additional split
    pub additional_split: Option<Split>,
}

impl Entity for ContractStateV3 {
    const SIZE: usize = STATE_ACC_SIZE;
    const MAGIC: u8 = 0x44;
}

impl ContractStateV3 {
    fn migrate(
        self,
        swap_state: Pubkey,
        new_commission: Pubkey,
        treasury: Pubkey,
        now: UnixTimestamp,
    ) -> ContractStateV4 {
        ContractStateV4 {
            token: self.token,
            owner: self.owner,
            admin: self.admin,
            commission_addr: new_commission,
            sold: self.sold,
            vest: self.vest,
            drop: self.drop.map(|d: DropV1| DropV2 {
                price: d.price,
                start_date: d.start_date,
                end_date: d.end_date,
                id: 0,
                amount: 1000_0000,
                created_at: now,
            }),
            swap_state,
            treasury_addr: self
                .additional_split
                .map(|s: Split| s.split_addr)
                .unwrap_or(treasury),
            current_round: None,
            completed_rounds_count: 0,
        }
    }
}

#[derive(Debug, BorshDeserialize, BorshSerialize)]
#[repr(C)]
pub struct DropV1 {
    /// price per chatlan in lamports
    pub price: u64,
    pub start_date: UnixTimestamp,
    pub end_date: UnixTimestamp,
}

#[derive(Debug, BorshDeserialize, BorshSerialize)]
#[repr(C)]
pub struct PostInfo {
    pub state: Pubkey, // state related to this
    pub post_id: [u8; 32],
    pub created_at: UnixTimestamp,
    pub repost_price: Option<u64>,
}

impl PostInfo {
    pub fn can_repost(&self, now: UnixTimestamp) -> bool {
        self.created_at
            .checked_sub(now)
            .map(|elapsed| elapsed < MAX_REPOST_TIME)
            .unwrap_or(false)
    }
}

impl Entity for PostInfo {
    const SIZE: usize = 128;
    const MAGIC: u8 = 0x44;
}

#[derive(Debug, BorshDeserialize, BorshSerialize)]
#[repr(C)]
pub struct RepostRecord {
    pub state: Pubkey, // state related to this
    pub token: Pubkey, // token to receive
    pub user: Pubkey,  // user that made the repost
    pub post_id: [u8; 32],
    pub reposted_at: UnixTimestamp,
    pub receive_amount: u64,
}

impl RepostRecord {
    pub fn can_redeem(&self, now: UnixTimestamp) -> bool {
        let cooldown_elapsed = self
            .reposted_at
            .checked_sub(now)
            .map(|elapsed| elapsed < REPOST_REDEEM_COOLDOWN)
            .unwrap_or(false);

        // thanks https://stackoverflow.com/questions/37847020/unix-time-stamp-day-of-the-week-pattern#38000383
        let week_progress = ((now - 345600) % 604800) as f64 / 86400.0;

        let is_thursday = week_progress > 3.0 && week_progress < 4.0;

        cooldown_elapsed && is_thursday
    }
}

impl Entity for RepostRecord {
    const SIZE: usize = 145;
    const MAGIC: u8 = 0x40;
}

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

    proptest! {
        #[test]
        fn proptest_vesting(price in 1u64..100000000000, token_amount in 0u64..10000_0000) {
            ContractState::calculate_split(price, token_amount).unwrap();
        }
    }
}