defuse_core/
tokens.rs

1use near_sdk::Gas;
2
3pub const MAX_TOKEN_ID_LEN: usize = 127;
4
5pub const MT_ON_TRANSFER_GAS_MIN: Gas = Gas::from_tgas(5);
6pub const MT_ON_TRANSFER_GAS_DEFAULT: Gas = Gas::from_tgas(30);
7
8#[cfg(feature = "imt")]
9pub mod imt {
10    use std::collections::BTreeMap;
11
12    use defuse_token_id::{TokenId, imt::ImtTokenId};
13    use near_sdk::AccountIdRef;
14
15    use crate::{DefuseError, Result, amounts::Amounts, tokens::MAX_TOKEN_ID_LEN};
16
17    pub type ImtTokens = Amounts<BTreeMap<defuse_nep245::TokenId, u128>>;
18
19    impl ImtTokens {
20        #[inline]
21        pub fn into_generic_tokens(
22            self,
23            minter_id: &AccountIdRef,
24        ) -> Result<Amounts<BTreeMap<TokenId, u128>>> {
25            let tokens = self
26                .into_iter()
27                .map(|(token_id, amount)| {
28                    if token_id.len() > MAX_TOKEN_ID_LEN {
29                        return Err(DefuseError::TokenIdTooLarge(token_id.len()));
30                    }
31
32                    let token = ImtTokenId::new(minter_id, token_id).into();
33
34                    Ok((token, amount))
35                })
36                .collect::<Result<_, _>>()?;
37
38            Ok(Amounts::new(tokens))
39        }
40    }
41}