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
use super::{BondingCurve, BondingCurveError, BondingCurveWithCheckedOperations, OperationSide};
/// Represents a quadratic bonding curve.
///
/// This struct defines a quadratic bonding curve with quadratic, linear, and base coefficients.
///
/// # Fields
///
/// * `quadratic`: The quadratic coefficient that determines the rate of price increase.
/// * `linear`: The linear coefficient that affects the price linearly.
/// * `base`: The base price, which is the minimum price for the first token.
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct QuadraticBondingCurve {
pub quadratic: u64,
pub linear: u64,
pub base: u64,
}
impl QuadraticBondingCurve {
/// Creates a new `QuadraticBondingCurve` with the specified coefficients.
///
/// # Arguments
///
/// * `quadratic` - The quadratic coefficient that determines the rate of price increase.
/// * `linear` - The linear coefficient that affects the price linearly.
/// * `base` - The base price, which is the minimum price for the first token.
///
/// # Returns
///
/// A new instance of `QuadraticBondingCurve`.
///
/// # Example
///
/// ```
/// use magic_curves::QuadraticBondingCurve;
///
/// let curve = QuadraticBondingCurve::new(10, 100, 1000);
/// ```
pub fn new(quadratic: u64, linear: u64, base: u64) -> Self {
Self {
quadratic,
linear,
base,
}
}
}
impl BondingCurve<u64> for QuadraticBondingCurve {
/// Calculates the price based on the supply.
///
/// # Formula
///
/// ```ignore
/// f(x) = quadratic * x^2 + 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.quadratic * supply * supply + self.linear * supply + self.base
}
/// Calculates the price for a given amount of tokens.
///
/// # Formula
///
/// The formula uses the sum of quadratic, linear, and constant terms based on the operation side.
///
/// # 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 n = amount;
let a = starting_supply;
let sum_quadratic = match side {
OperationSide::Add => {
// Sum of quadratic terms: (a^2 * n) + (a * n * (n-1)) + (n * (n-1) * (2n-1) / 6)
(self.quadratic * a * a * n)
+ (self.quadratic * a * n * (n - 1))
+ (self.quadratic * n * (n - 1) * (2 * n - 1) / 6)
}
OperationSide::Remove => {
// Sum of quadratic terms: (a^2 * n) - (a * n * (n-1)) + (n * (n-1) * (2n-1) / 6)
(self.quadratic * a * a * n) - (self.quadratic * a * n * (n - 1))
+ (self.quadratic * n * (n - 1) * (2 * n - 1) / 6)
}
};
let sum_linear = match side {
OperationSide::Add => {
// Sum of linear terms: b * (a * n + n * (n-1) / 2)
self.linear * (a * n + n * (n - 1) / 2)
}
OperationSide::Remove => {
// Sum of linear terms: b * (a * n - n * (n-1) / 2)
self.linear * (a * n - n * (n - 1) / 2)
}
};
// Sum of constant terms: c * n
let sum_constant = self.base * n;
sum_quadratic + sum_linear + sum_constant
}
}
impl BondingCurveWithCheckedOperations<u64> for QuadraticBondingCurve {
/// Calculates the price based on the supply with overflow checking.
///
/// # Arguments
///
/// * `supply` - The current supply of tokens.
///
/// # Returns
///
/// A `Result` containing the price of the token based on the supply,
/// or a `BondingCurveError` if the calculation overflows.
fn calculate_price_checked(&self, supply: u64) -> Result<u64, BondingCurveError> {
let result = self
.quadratic
.checked_mul(supply)
.and_then(|x| x.checked_mul(supply))
.and_then(|x| x.checked_add(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 with overflow checking.
///
/// # 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
///
/// A `Result` containing the total price for the given amount of tokens,
/// or a `BondingCurveError` if the calculation overflows.
fn calculate_price_many_checked(
&self,
starting_supply: u64,
amount: u64,
side: OperationSide,
) -> Result<u64, BondingCurveError> {
let n = amount;
let a = starting_supply;
let n_minus_1 = n.checked_sub(1).ok_or(BondingCurveError::Overflow)?;
// Sum of quadratic terms
let first_term = self
.quadratic
.checked_mul(
a.checked_mul(a)
.and_then(|x| x.checked_mul(n))
.ok_or(BondingCurveError::Overflow)?,
)
.ok_or(BondingCurveError::Overflow)?;
let second_term = self
.quadratic
.checked_mul(
a.checked_mul(n)
.and_then(|x| x.checked_mul(n_minus_1))
.ok_or(BondingCurveError::Overflow)?,
)
.ok_or(BondingCurveError::Overflow)?;
let third_term_pow = 2u64
.checked_mul(n)
.and_then(|x| x.checked_sub(1))
.ok_or(BondingCurveError::Overflow)?;
let third_term = self
.quadratic
.checked_mul(
n.checked_mul(n_minus_1)
.and_then(|x| x.checked_mul(third_term_pow))
.and_then(|x| x.checked_div(6))
.ok_or(BondingCurveError::Overflow)?,
)
.ok_or(BondingCurveError::Overflow)?;
let sum_quadratic = match side {
OperationSide::Add => first_term
.checked_add(second_term)
.and_then(|x| x.checked_add(third_term)),
OperationSide::Remove => first_term
.checked_sub(second_term)
.and_then(|x| x.checked_add(third_term)),
}
.ok_or(BondingCurveError::Overflow)?;
// Sum of linear terms
let sum_linear = match side {
OperationSide::Add => self
.linear
.checked_mul(
a.checked_mul(n)
.and_then(|x| x.checked_add(n.checked_mul(n_minus_1)?.checked_div(2)?))
.ok_or(BondingCurveError::Overflow)?,
)
.ok_or(BondingCurveError::Overflow)?,
OperationSide::Remove => self
.linear
.checked_mul(
a.checked_mul(n)
.and_then(|x| x.checked_sub(n.checked_mul(n_minus_1)?.checked_div(2)?))
.ok_or(BondingCurveError::Overflow)?,
)
.ok_or(BondingCurveError::Overflow)?,
};
// Sum of constant terms
let sum_constant = self
.base
.checked_mul(n)
.ok_or(BondingCurveError::Overflow)?;
// Final sum
sum_quadratic
.checked_add(sum_linear)
.and_then(|x| x.checked_add(sum_constant))
.ok_or(BondingCurveError::Overflow)
}
}
#[cfg(test)]
mod test {
use crate::{
BondingCurve, BondingCurveWithCheckedOperations, OperationSide, QuadraticBondingCurve,
};
#[test]
pub fn test_quadratic_price_calculus() {
let curve = QuadraticBondingCurve::new(10_000_000, 500_000_000, 1_000_000_000);
let r1 = curve.base;
let r2 = 1_510_000_000u64;
let r3 = 5_640_000_000u64;
let r4 = 6_801_000_000_000u64;
// Minimum cost is base.
let price = curve.calculate_price_checked(0).unwrap();
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_quadratic_price_many_calculus() {
let quadratic = 10_000_000u64;
let linear = 500_000_000u64;
let base = 1_000_000_000u64;
let amount = 10u64;
let starting_supply = 100u64;
let curve = QuadraticBondingCurve::new(quadratic, 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!(looped_price_add, many_price_add);
let checked_many_price_add = curve
.calculate_price_many_checked(starting_supply, amount, OperationSide::Add)
.unwrap();
assert_eq!(checked_many_price_add, many_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!(looped_price_remove, many_price_remove);
let checked_many_price_remove = curve
.calculate_price_many_checked(starting_supply, amount, OperationSide::Remove)
.unwrap();
assert_eq!(checked_many_price_remove, many_price_remove);
}
}