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
use super::{BondingCurve, BondingCurveError, BondingCurveWithCheckedOperations, OperationSide};
/// Represents a linear bonding curve.
///
/// This struct defines a linear bonding curve with a linear coefficient and a base price.
///
/// # Fields
///
/// * `linear`: The linear coefficient that determines the rate of price increase.
/// * `base`: The base price, which is the minimum price for the first token.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct LinearBondingCurve {
pub linear: u64,
pub base: u64,
}
impl LinearBondingCurve {
/// Creates a new `LinearBondingCurve` with the specified linear coefficient and base price.
///
/// # Arguments
///
/// * `linear` - The linear coefficient that determines the rate of price increase.
/// * `base` - The base price, which is the minimum price for the first token.
///
/// # Returns
///
/// A new instance of `LinearBondingCurve`.
///
/// # Example
///
/// ```
/// use magic_curves::LinearBondingCurve;
///
/// let curve = LinearBondingCurve::new(100, 1000);
/// ```
pub fn new(linear: u64, base: u64) -> Self {
Self { linear, base }
}
}
impl BondingCurve<u64> for LinearBondingCurve {
/// Calculates the price based on the supply.
///
/// # Formula
///
/// ```ignore
/// f(x) = linear * x + base
/// ```
///
/// # Arguments
///
/// * `supply` - The current supply of tokens.
///
/// # Returns
///
/// The price of the token based on the supply.
fn calculate_price(&self, supply: u64) -> u64 {
self.linear * supply + self.base
}
/// Calculates the price for a given amount of tokens.
///
/// # Formula
///
/// ```ignore
/// f(x) = (amount * (a1 + an)) / 2
/// ```
///
/// where:
///
/// - a1 = linear * starting_supply + base
/// - an = linear * (starting_supply + amount - 1) + base
///
/// # Arguments
///
/// * `starting_supply` - The current supply of tokens.
/// * `amount` - The amount of tokens to calculate the price for.
/// * `side` - The side of the operation (add or remove).
///
/// # Returns
///
/// The total price for the given amount of tokens.
fn calculate_price_many(&self, starting_supply: u64, amount: u64, side: OperationSide) -> u64 {
let a1 = self.linear * starting_supply + self.base;
let an = match side {
OperationSide::Add => self.linear * (starting_supply + amount - 1) + self.base,
OperationSide::Remove => self.linear * (starting_supply - amount + 1) + self.base,
};
(amount * (a1 + an)) / 2
}
}
impl BondingCurveWithCheckedOperations<u64> for LinearBondingCurve {
/// Calculates the price based on the supply.
///
/// # Arguments
///
/// * `supply` - The current supply of tokens.
///
/// # Returns
///
/// The price of the token based on the supply. If the operation would cause an overflow, it returns an error.
fn calculate_price_checked(&self, supply: u64) -> Result<u64, BondingCurveError> {
let result = self
.linear
.checked_mul(supply)
.and_then(|x| x.checked_add(self.base));
result.ok_or(BondingCurveError::Overflow)
}
/// Calculates the price for a given amount of tokens.
///
/// # Arguments
///
/// * `starting_supply` - The current supply of tokens.
/// * `amount` - The amount of tokens to calculate the price for.
/// * `side` - The side of the operation (add or remove).
///
/// # Returns
///
/// The total price for the given amount of tokens. If the operation would cause an overflow, it returns an error.
fn calculate_price_many_checked(
&self,
starting_supply: u64,
amount: u64,
side: OperationSide,
) -> Result<u64, BondingCurveError> {
let a1 = self
.linear
.checked_mul(starting_supply)
.and_then(|x| x.checked_add(self.base))
.ok_or(BondingCurveError::Overflow)?;
let an = match side {
OperationSide::Add => self
.linear
.checked_mul(starting_supply + amount - 1)
.and_then(|x| x.checked_add(self.base))
.ok_or(BondingCurveError::Overflow)?,
OperationSide::Remove => self
.linear
.checked_mul(starting_supply - amount + 1)
.and_then(|x| x.checked_add(self.base))
.ok_or(BondingCurveError::Overflow)?,
};
let sum = a1
.checked_add(an)
.and_then(|x| x.checked_mul(amount))
.and_then(|x| x.checked_div(2))
.ok_or(BondingCurveError::Overflow)?;
Ok(sum)
}
}
#[cfg(test)]
mod test {
use crate::{
BondingCurve, BondingCurveWithCheckedOperations, LinearBondingCurve, OperationSide,
};
#[test]
pub fn test_linear_price_calculus() {
let linear = 500_000_000u64;
let base = 1_000_000_000u64;
let curve = LinearBondingCurve::new(linear, base);
let r1 = curve.base;
let r2 = 1_500_000_000u64;
let r3 = 5_000_000_000u64;
let r4 = 401_000_000_000u64;
let price = curve.calculate_price(0);
assert_eq!(price, r1);
let price = curve.calculate_price_checked(0).unwrap();
assert_eq!(price, r1);
let price = curve.calculate_price(1);
assert_eq!(price, r2);
let price = curve.calculate_price_checked(1).unwrap();
assert_eq!(price, r2);
let price = curve.calculate_price(8);
assert_eq!(price, r3);
let price = curve.calculate_price_checked(8).unwrap();
assert_eq!(price, r3);
let price = curve.calculate_price(800);
assert_eq!(price, r4);
let price = curve.calculate_price_checked(800).unwrap();
assert_eq!(price, r4);
}
#[test]
pub fn test_increase_linear_price_many() {
let linear = 500_000_000u64;
let base = 1_000_000_000u64;
let amount = 10u64;
let starting_supply = 100u64;
let curve = LinearBondingCurve::new(linear, base);
let many_price_add =
curve.calculate_price_many(starting_supply, amount, OperationSide::Add);
// Do it with a loop with calculate_price
let mut looped_price_add = 0u64;
for i in 0..amount {
looped_price_add += curve.calculate_price(starting_supply + i);
}
assert_eq!(many_price_add, looped_price_add);
let checked_many_price_add = curve
.calculate_price_many_checked(starting_supply, amount, OperationSide::Add)
.unwrap();
assert_eq!(checked_many_price_add, looped_price_add);
let many_price_remove =
curve.calculate_price_many(starting_supply, amount, OperationSide::Remove);
// Do it with a loop with calculate_price
let mut looped_price_remove = 0u64;
for i in 0..amount {
looped_price_remove += curve.calculate_price(starting_supply - i);
}
assert_eq!(many_price_remove, looped_price_remove);
let checked_many_price_remove = curve
.calculate_price_many_checked(starting_supply, amount, OperationSide::Remove)
.unwrap();
assert_eq!(checked_many_price_remove, looped_price_remove);
}
}