1+ /// Polynomial Bonding Curve Implementation.
2+ ///
3+ /// This module provides an implementation of a polynomial bonding curve.
4+ /// The current implementation supports a polynomial function of order 2, with the integral precomputed for efficiency.
5+ ///
6+ /// ### Cost Function
7+ /// The cost function is defined as:
8+ /// ```text
9+ /// c(s) = m * s^2 + n * s + o
10+ /// ```
11+ /// This function, `c(s)`, determines the price for purchasing or selling assets at any supply point `s`.
12+ /// The total cost of transactions is computed as the integral of `c(s)` between the start point and `s`.
13+ ///
14+ /// ### Antiderivative
15+ /// The indefinite integral of the cost function is:
16+ /// ```text
17+ /// C(s) = (m / 3) * s^3 + (n / 2) * s^2 + o * s
18+ /// ```
19+ /// Where:
20+ /// - `m` is the coefficient for the quadratic term,
21+ /// - `n` is the coefficient for the linear term,
22+ /// - `o` is the constant term.
23+ ///
24+ ///
25+ /// `C(s)` represents the accumulated cost of purchasing or selling assets up to the current supply `s`.
26+ /// The integral between two supply points, `s*` (initial supply) and `s` (current supply), gives the incremental cost:
27+ /// ```text
28+ /// Incremental Cost = C(s) - C(s*)
29+ /// ```
30+ /// This captures the total cost for changing the supply from `s*` to `s`.
31+ ///
32+ /// ### Optimization for Numerical Stability
33+ /// The computation of `s^3` can cause overflow in fixed-point arithmetic. To mitigate this, the calculation is factored as:
34+ /// ```text
35+ /// x^3 - y^3 = (x^2 + x * y + y^2) * (x - y)
36+ /// ```
37+ /// Where:
38+ /// - `x` is the upper limit of the integral,
39+ /// - `y` is the lower limit of the integral.
40+ ///
41+ /// By breaking down the computation in this way, we reduce the risk of overflow while maintaining precision.
142use parity_scale_codec:: { Decode , Encode , MaxEncodedLen } ;
243use scale_info:: TypeInfo ;
344use sp_arithmetic:: ArithmeticError ;
@@ -6,20 +47,50 @@ use substrate_fixed::traits::{FixedSigned, FixedUnsigned};
647use super :: { calculate_accumulated_passive_issuance, square, BondingFunction } ;
748use crate :: PassiveSupply ;
849
50+ /// A struct representing the unchecked input parameters for a polynomial bonding curve.
51+ /// This struct is used to convert the input parameters to the correct fixed-point type.
52+ ///
53+ /// The input struct assumes that the coefficients are precomputed according to the integral rules of the polynomial function.
54+ ///
55+ /// ### Example
56+ ///
57+ /// For a polynomial cost function `c(s) = 3 * s^2 + 2 * s + 2`
58+ ///
59+ /// which is resulting into the antiderivative `C(s) = (3 / 3) * s^3 + (2 / 2) * s^2 + 2 * s`
60+ /// the input parameters would be:
61+ /// ```rust, ignore
62+ /// PolynomialParametersInput {
63+ /// m: 1,
64+ /// n: 1,
65+ /// o: 2,
66+ /// }
67+ /// ```
968#[ derive( Clone , Debug , Encode , Decode , PartialEq , Eq , TypeInfo , MaxEncodedLen ) ]
1069pub struct PolynomialParametersInput < Parameter > {
70+ /// Coefficient for the cubic part.
1171 pub m : Parameter ,
72+ /// Coefficient for the quadratic part.
1273 pub n : Parameter ,
74+ /// Coefficient for the linear part.
1375 pub o : Parameter ,
1476}
1577
78+ /// A struct representing the validated parameters for a polynomial bonding curve.
79+ /// This struct is used to store the parameters for a polynomial bonding
80+ /// curve and to perform calculations using the polynomial bonding curve.
1681#[ derive( Clone , Debug , Encode , Decode , PartialEq , Eq , TypeInfo , MaxEncodedLen ) ]
1782pub struct PolynomialParameters < Parameter > {
83+ /// Coefficient for the cubic part.
1884 pub m : Parameter ,
85+ /// Coefficient for the quadratic part.
1986 pub n : Parameter ,
87+ /// Coefficient for the linear part.
2088 pub o : Parameter ,
2189}
2290
91+ /// Implementation of the TryFrom trait for `PolynomialParametersInput` to convert the input parameters to
92+ /// the correct fixed-point type. The TryFrom implementation for `PolynomialParameters` will fail if the
93+ /// conversion to the fixed-point type fails.
2394impl < I : FixedUnsigned , C : FixedSigned > TryFrom < PolynomialParametersInput < I > > for PolynomialParameters < C > {
2495 type Error = ( ) ;
2596 fn try_from ( value : PolynomialParametersInput < I > ) -> Result < Self , Self :: Error > {
@@ -35,6 +106,7 @@ impl<Parameter> BondingFunction<Parameter> for PolynomialParameters<Parameter>
35106where
36107 Parameter : FixedSigned ,
37108{
109+ /// Calculate the cost of purchasing/selling assets using the polynomial bonding curve.
38110 fn calculate_costs (
39111 & self ,
40112 low : Parameter ,
0 commit comments