multiversx_sc_modules/bonding_curve/curves/
linear_function.rs

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
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

use crate::bonding_curve::{curves::curve_function::CurveFunction, utils::structs::CurveArguments};

#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone)]
pub struct LinearFunction<M: ManagedTypeApi> {
    pub initial_price: BigUint<M>,
    pub linear_coefficient: BigUint<M>,
}

impl<M: ManagedTypeApi> CurveFunction<M> for LinearFunction<M> {
    fn calculate_price(
        &self,
        token_start: &BigUint<M>,
        amount: &BigUint<M>,
        _arguments: &CurveArguments<M>,
    ) -> BigUint<M> {
        &self.linear_coefficient * &sum_interval(amount, token_start) + &self.initial_price * amount
    }
}

fn sum_interval<M: ManagedTypeApi>(n: &BigUint<M>, x: &BigUint<M>) -> BigUint<M> {
    x * n + &(n - 1u32) * n / 2u32
}