multiversx_sc_modules/token_merge/
merged_token_instances.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
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
use core::ops::Deref;

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

pub const MAX_MERGED_TOKENS: usize = 25;

pub static TOO_MANY_TOKENS_ERR_MSG: &[u8] = b"Too many tokens to merge";
pub static INSUFFICIENT_BALANCE_IN_MERGED_INST_ERR_MSG: &[u8] =
    b"Insufficient token balance to deduct from merged instance";

pub type InstanceArray<M> = ArrayVec<EsdtTokenPayment<M>, MAX_MERGED_TOKENS>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MergedTokenInstances<M: ManagedTypeApi> {
    instances: InstanceArray<M>,
}

impl<M: ManagedTypeApi> Default for MergedTokenInstances<M> {
    #[inline]
    fn default() -> Self {
        Self {
            instances: ArrayVec::new(),
        }
    }
}

impl<M: ManagedTypeApi> MergedTokenInstances<M> {
    #[inline]
    pub fn new() -> Self {
        Self::default()
    }

    #[inline]
    pub fn new_from_instances(instances: InstanceArray<M>) -> Self {
        Self { instances }
    }

    pub fn decode_from_first_uri(uris: &ManagedVec<M, ManagedBuffer<M>>) -> Self {
        let first_uri = uris.get(0);
        let decode_result = InstanceArray::<M>::top_decode(first_uri.deref().clone());
        match decode_result {
            core::result::Result::Ok(instances) => Self::new_from_instances(instances),
            core::result::Result::Err(_) => {
                M::error_api_impl().signal_error(b"Error decoding tokens from URI")
            },
        }
    }

    #[inline]
    pub fn get_instances(&self) -> &InstanceArray<M> {
        &self.instances
    }

    pub fn add_or_update_instance(&mut self, new_instance: EsdtTokenPayment<M>) {
        let search_result =
            self.find_instance(&new_instance.token_identifier, new_instance.token_nonce);
        match search_result {
            Some(existing_index) => {
                self.instances[existing_index].amount += new_instance.amount;
            },
            None => {
                if self.instances.len() >= MAX_MERGED_TOKENS {
                    M::error_api_impl().signal_error(TOO_MANY_TOKENS_ERR_MSG);
                }

                unsafe {
                    self.instances.push_unchecked(new_instance);
                }
            },
        }
    }

    pub fn merge_with_other(&mut self, other: Self) {
        for inst in other.instances {
            self.add_or_update_instance(inst);
        }
    }

    pub fn deduct_balance_for_instance(&mut self, tokens_to_deduct: &EsdtTokenPayment<M>) {
        let search_result = self.find_instance(
            &tokens_to_deduct.token_identifier,
            tokens_to_deduct.token_nonce,
        );
        match search_result {
            Some(index) => {
                let found_instance = &mut self.instances[index];
                if tokens_to_deduct.amount == 0 || found_instance.amount < tokens_to_deduct.amount {
                    M::error_api_impl().signal_error(INSUFFICIENT_BALANCE_IN_MERGED_INST_ERR_MSG);
                }

                found_instance.amount -= &tokens_to_deduct.amount;
                if found_instance.amount == 0 {
                    let _ = self.instances.remove(index);
                }
            },
            None => M::error_api_impl().signal_error(INSUFFICIENT_BALANCE_IN_MERGED_INST_ERR_MSG),
        }
    }

    #[inline]
    pub fn into_instances(self) -> InstanceArray<M> {
        self.instances
    }

    fn find_instance(
        &self,
        original_token_id: &TokenIdentifier<M>,
        original_token_nonce: u64,
    ) -> Option<usize> {
        self.instances.iter().position(|item| {
            &item.token_identifier == original_token_id && item.token_nonce == original_token_nonce
        })
    }
}