multiversx_sc_modules/bonding_curve/utils/
structs.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
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
use crate::bonding_curve::curves::curve_function::CurveFunction;

multiversx_sc::imports!();
multiversx_sc::derive_imports!();

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

impl<M: ManagedTypeApi> CurveArguments<M> {
    pub fn first_token_available(&self) -> BigUint<M> {
        &self.available_supply - &self.balance
    }
}

#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone)]
pub struct BondingCurve<
    M: ManagedTypeApi,
    T: CurveFunction<M> + TopEncode + TopDecode + NestedEncode + NestedDecode + TypeAbi,
> {
    pub curve: T,
    pub arguments: CurveArguments<M>,
    pub sell_availability: bool,
    pub payment: EgldOrEsdtTokenPayment<M>,
}

impl<
        M: ManagedTypeApi,
        T: CurveFunction<M> + TopEncode + TopDecode + NestedEncode + NestedDecode + TypeAbi,
    > BondingCurve<M, T>
{
    pub fn payment_token(&self) -> EgldOrEsdtTokenIdentifier<M> {
        self.payment.token_identifier.clone()
    }
    pub fn payment_is_egld(&self) -> bool {
        self.payment.token_identifier.is_egld()
    }
}

#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone)]
pub struct TokenOwnershipData<M: ManagedTypeApi> {
    pub token_nonces: ManagedVec<M, u64>,
    pub owner: ManagedAddress<M>,
}

impl<M: ManagedTypeApi> TokenOwnershipData<M> {
    pub fn add_nonce(&mut self, nonce: u64) {
        if !self.token_nonces.contains(&nonce) {
            self.token_nonces.push(nonce);
        }
    }
    pub fn remove_nonce(&mut self, nonce: u64) {
        let index = self.token_nonces.iter().position(|n| n == nonce);

        match index {
            Some(value) => self.token_nonces.remove(value),
            None => M::error_api_impl().signal_error(b"Nonce requested is not available"),
        };
    }
}