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
use std::cmp::{max, min};

use cosmwasm_schema::cw_serde;
use cosmwasm_std::{Decimal, Decimal256, Fraction, Timestamp, Uint128, Uint256};

#[cw_serde]
pub struct Schedule {
    pub start: Timestamp,
    pub end: Timestamp,
    pub amount: Uint128,
    pub release: Release,
}

#[cw_serde]
pub enum Release {
    Fixed,
    Decay,
}

impl Schedule {
    pub fn released(&self, start: &Timestamp, end: &Timestamp) -> Uint128 {
        if self.start.seconds() > end.seconds() {
            return Uint128::zero();
        }
        match self.release {
            Release::Fixed => {
                let total_duration = self.end.seconds() - self.start.seconds();
                let start = max(self.start.seconds(), start.seconds());
                let end = min(self.end.seconds(), end.seconds());
                if end <= start {
                    return Uint128::zero();
                }
                let duration = end - start;

                Decimal::from_ratio(duration, total_duration) * self.amount
            }
            Release::Decay => {
                let total_duration = self.end.seconds() - self.start.seconds();
                let start = max(self.start.seconds(), start.seconds());
                let end = min(self.end.seconds(), end.seconds());
                if end <= start {
                    return Uint128::zero();
                }
                let c = Decimal256::from_ratio(
                    Uint256::from(self.amount) * Uint256::from(2u128),
                    total_duration,
                );
                let div = Decimal256::from_ratio(total_duration * total_duration, self.amount);

                let b = Uint256::from(end - self.start.seconds());
                let a = Uint256::from(start - self.start.seconds());
                let b = c * b
                    - Decimal256::from_ratio(b * b * div.denominator(), div.numerator())
                        * Uint256::one();

                let a = c * a
                    - Decimal256::from_ratio(a * a * div.denominator(), div.numerator())
                        * Uint256::one();

                let diff = b.checked_sub(a).unwrap_or_default();
                diff.try_into().unwrap()
            }
        }
    }
}

#[cfg(test)]
mod tests {

    use super::*;

    #[test]
    fn decay_schedule() {
        let s = Schedule {
            start: Timestamp::from_seconds(1670940900),
            end: Timestamp::from_seconds(1671545400),
            amount: Uint128::from(3000000000u128),
            release: Release::Decay,
        };

        let extra = s.released(
            &Timestamp::from_seconds(1671545401),
            &Timestamp::from_seconds(1671963618),
        );

        println!("extra {extra}");

        let s = Schedule {
            start: Timestamp::from_seconds(0),
            end: Timestamp::from_seconds(1000),
            amount: Uint128::from(5000u128),
            release: Release::Decay,
        };

        //

        assert_eq!(
            s.released(&Timestamp::from_seconds(0), &Timestamp::from_seconds(0)),
            Uint128::zero()
        );
        assert_eq!(
            s.released(&Timestamp::from_seconds(0), &Timestamp::from_seconds(1000)),
            Uint128::from(5000u128)
        );
        assert_eq!(
            s.released(&Timestamp::from_seconds(0), &Timestamp::from_seconds(100)),
            Uint128::from(950u128)
        );
        assert_eq!(
            s.released(&Timestamp::from_seconds(400), &Timestamp::from_seconds(600)),
            Uint128::from(1000u128)
        );

        // last 400 seconds out of 1000
        assert_eq!(
            s.released(&Timestamp::from_seconds(0), &Timestamp::from_seconds(600)),
            Uint128::from(4200u128)
        );
        assert_eq!(
            s.released(
                &Timestamp::from_seconds(600),
                &Timestamp::from_seconds(1200)
            ),
            Uint128::from(800u128)
        );

        // assert_eq!(
        //     s.released(
        //         &Timestamp::from_seconds(1200),
        //         &Timestamp::from_seconds(1400)
        //     ),
        //     Uint128::zero()
        // );

        let s = Schedule {
            start: Timestamp::from_seconds(0),
            end: Timestamp::from_seconds(45000),
            amount: Uint128::from(154600u128),
            release: Release::Decay,
        };

        //

        assert_eq!(
            s.released(&Timestamp::from_seconds(0), &Timestamp::from_seconds(6750)),
            Uint128::from(42901u128)
        );

        let s = Schedule {
            start: Timestamp::from_seconds(20000),
            end: Timestamp::from_seconds(45000),
            amount: Uint128::from(154600u128),
            release: Release::Decay,
        };

        //

        assert_eq!(
            s.released(&Timestamp::from_seconds(0), &Timestamp::from_seconds(6750)),
            Uint128::zero()
        );

        let s = Schedule {
            start: Timestamp::from_seconds(1670940900),
            end: Timestamp::from_seconds(1671545400),
            amount: Uint128::from(3000000000u128),
            release: Release::Decay,
        };

        // total 3k distribution
        // start 1670940900
        // last distributed 1671780641
        // end 1671545400

        // This was distributed since it ended. make sure it's not offering extra rewards

        let extra = s.released(
            &Timestamp::from_seconds(1671545401),
            &Timestamp::from_seconds(1671963618),
        );

        println!("extra {extra}");

        assert_eq!(
            s.released(
                &Timestamp::from_seconds(1671780641),
                &Timestamp::from_seconds(1671785290)
            ),
            Uint128::zero()
        );
    }

    #[test]
    fn decimals() {
        let s = Schedule {
            start: Timestamp::from_seconds(1703083387),
            end: Timestamp::from_seconds(1710974700),
            amount: Uint128::from(65_000_000_000_000_000_000_000u128),
            release: Release::Decay,
        };

        s.released(
            &Timestamp::from_seconds(1703083387),
            &Timestamp::from_seconds(1710974700),
        );
    }
}