multiversx_sc_modules/bonding_curve/utils/
structs.rs

1use crate::bonding_curve::curves::curve_function::CurveFunction;
2
3multiversx_sc::imports!();
4multiversx_sc::derive_imports!();
5
6#[type_abi]
7#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone)]
8pub struct CurveArguments<M: ManagedTypeApi> {
9    pub available_supply: BigUint<M>,
10    pub balance: BigUint<M>,
11}
12
13impl<M: ManagedTypeApi> CurveArguments<M> {
14    pub fn first_token_available(&self) -> BigUint<M> {
15        &self.available_supply - &self.balance
16    }
17}
18
19#[type_abi]
20#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone)]
21pub struct BondingCurve<
22    M: ManagedTypeApi,
23    T: CurveFunction<M> + TopEncode + TopDecode + NestedEncode + NestedDecode + TypeAbi,
24> {
25    pub curve: T,
26    pub arguments: CurveArguments<M>,
27    pub sell_availability: bool,
28    pub payment: EgldOrEsdtTokenPayment<M>,
29}
30
31impl<
32        M: ManagedTypeApi,
33        T: CurveFunction<M> + TopEncode + TopDecode + NestedEncode + NestedDecode + TypeAbi,
34    > BondingCurve<M, T>
35{
36    pub fn payment_token(&self) -> EgldOrEsdtTokenIdentifier<M> {
37        self.payment.token_identifier.clone()
38    }
39    pub fn payment_is_egld(&self) -> bool {
40        self.payment.token_identifier.is_egld()
41    }
42}
43
44#[type_abi]
45#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, Eq, Clone)]
46pub struct TokenOwnershipData<M: ManagedTypeApi> {
47    pub token_nonces: ManagedVec<M, u64>,
48    pub owner: ManagedAddress<M>,
49}
50
51impl<M: ManagedTypeApi> TokenOwnershipData<M> {
52    pub fn add_nonce(&mut self, nonce: u64) {
53        if !self.token_nonces.contains(&nonce) {
54            self.token_nonces.push(nonce);
55        }
56    }
57    pub fn remove_nonce(&mut self, nonce: u64) {
58        let index = self.token_nonces.iter().position(|n| n == nonce);
59
60        match index {
61            Some(value) => self.token_nonces.remove(value),
62            None => M::error_api_impl().signal_error(b"Nonce requested is not available"),
63        };
64    }
65}