Skip to content

Commit

Permalink
moved get_balance to currentStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaicalinluca committed Nov 18, 2024
1 parent 6db55f8 commit f96cf65
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 46 deletions.
39 changes: 22 additions & 17 deletions framework/base/src/storage/mappers/token/fungible_token_mapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ use crate::{
abi::{TypeAbi, TypeAbiFrom},
api::ErrorApiImpl,
codec::{EncodeErrorHandler, TopEncodeMulti, TopEncodeMultiOutput},
storage::mappers::{set_mapper::CurrentStorage, StorageMapperFromAddress},
storage::mappers::{
set_mapper::{CurrentStorage, StorageAddress},
StorageMapperFromAddress,
},
storage_clear, storage_get, storage_get_len, storage_set,
types::{
system_proxy::{ESDTSystemSCProxy, FungibleTokenProperties},
ESDTSystemSCAddress, Tx,
ESDTSystemSCAddress, ManagedRef, Tx,
},
};

Expand Down Expand Up @@ -38,6 +41,7 @@ pub(crate) const DEFAULT_ISSUE_WITH_INIT_SUPPLY_CALLBACK_NAME: &str =
pub struct FungibleTokenMapper<SA, A = CurrentStorage>
where
SA: StorageMapperApi + CallTypeApi,
A: StorageAddress<SA>,
{
key: StorageKey<SA>,
token_state: TokenMapperState<SA>,
Expand Down Expand Up @@ -265,13 +269,27 @@ where
&TokenMapperState::Token(token_id.clone()),
);
}

pub fn get_balance(&self) -> BigUint<SA> {
let b_wrapper = BlockchainWrapper::new();
let own_sc_address = Self::get_sc_address();
let token_id = self.get_token_id_ref();

b_wrapper.get_esdt_balance(&own_sc_address, token_id, 0)
}

pub fn get_sc_address() -> ManagedAddress<SA> {
let b_wrapper = BlockchainWrapper::new();
b_wrapper.get_sc_address()
}
}

impl<SA> FungibleTokenMapper<SA>
impl<SA, A> FungibleTokenMapper<SA, A>
where
SA: StorageMapperApi + CallTypeApi,
A: StorageAddress<SA>,
{
pub fn get_storage_key(&self) -> crate::types::ManagedRef<SA, StorageKey<SA>> {
pub fn get_storage_key(&self) -> ManagedRef<SA, StorageKey<SA>> {
self.key.as_ref()
}

Expand All @@ -295,11 +313,6 @@ where
}
}

pub fn get_sc_address() -> ManagedAddress<SA> {
let b_wrapper = BlockchainWrapper::new();
b_wrapper.get_sc_address()
}

pub fn is_empty(&self) -> bool {
storage_get_len(self.get_storage_key()) == 0
}
Expand Down Expand Up @@ -344,14 +357,6 @@ where
cb_closure
}

pub fn get_balance(&self) -> BigUint<SA> {
let b_wrapper = BlockchainWrapper::new();
let own_sc_address = Self::get_sc_address();
let token_id = self.get_token_id_ref();

b_wrapper.get_esdt_balance(&own_sc_address, token_id, 0)
}

pub(crate) fn check_not_set(&self) {
let storage_value: TokenMapperState<SA> = storage_get(self.get_storage_key());
match storage_value {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,38 @@ where
&TokenMapperState::Token(token_id.clone()),
);
}

pub fn get_balance(&self, token_nonce: u64) -> BigUint<SA> {
let b_wrapper = BlockchainWrapper::new();
let own_sc_address = Self::get_sc_address();
let token_id = self.get_token_id_ref();

b_wrapper.get_esdt_balance(&own_sc_address, token_id, token_nonce)
}

pub fn get_sc_address() -> ManagedAddress<SA> {
let b_wrapper = BlockchainWrapper::new();
b_wrapper.get_sc_address()
}

pub fn get_all_token_data(&self, token_nonce: u64) -> EsdtTokenData<SA> {
let b_wrapper = BlockchainWrapper::new();
let own_sc_address = Self::get_sc_address();
let token_id = self.get_token_id_ref();

b_wrapper.get_esdt_token_data(&own_sc_address, token_id, token_nonce)
}

pub fn get_token_attributes<T: TopDecode>(&self, token_nonce: u64) -> T {
let token_data = self.get_all_token_data(token_nonce);
token_data.decode_attributes()
}
}

impl<SA> NonFungibleTokenMapper<SA>
impl<SA, A> NonFungibleTokenMapper<SA, A>
where
SA: StorageMapperApi + CallTypeApi,
A: StorageAddress<SA>,
{
pub(crate) fn check_not_set(&self) {
let storage_value: TokenMapperState<SA> = storage_get(self.get_storage_key());
Expand All @@ -418,11 +445,6 @@ where
storage_get_len(self.get_storage_key()) == 0
}

pub fn get_sc_address() -> ManagedAddress<SA> {
let b_wrapper = BlockchainWrapper::new();
b_wrapper.get_sc_address()
}

pub fn require_issued_or_set(&self) {
if self.is_empty() {
SA::error_api_impl().signal_error(MUST_SET_TOKEN_ID_ERR_MSG);
Expand Down Expand Up @@ -469,27 +491,6 @@ where
}
}

pub fn get_all_token_data(&self, token_nonce: u64) -> EsdtTokenData<SA> {
let b_wrapper = BlockchainWrapper::new();
let own_sc_address = Self::get_sc_address();
let token_id = self.get_token_id_ref();

b_wrapper.get_esdt_token_data(&own_sc_address, token_id, token_nonce)
}

pub fn get_balance(&self, token_nonce: u64) -> BigUint<SA> {
let b_wrapper = BlockchainWrapper::new();
let own_sc_address = Self::get_sc_address();
let token_id = self.get_token_id_ref();

b_wrapper.get_esdt_balance(&own_sc_address, token_id, token_nonce)
}

pub fn get_token_attributes<T: TopDecode>(&self, token_nonce: u64) -> T {
let token_data = self.get_all_token_data(token_nonce);
token_data.decode_attributes()
}

pub fn default_callback_closure_obj(&self) -> CallbackClosure<SA> {
let initial_caller = BlockchainWrapper::<SA>::new().get_caller();
let cb_name = DEFAULT_ISSUE_CALLBACK_NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ use core::marker::PhantomData;

use crate::{
codec::{NestedDecode, NestedEncode, TopDecode, TopEncode},
storage::mappers::{set_mapper::CurrentStorage, StorageMapperFromAddress},
storage::mappers::{
set_mapper::{CurrentStorage, StorageAddress},
StorageMapperFromAddress,
},
types::ManagedAddress,
};

Expand Down Expand Up @@ -192,9 +195,10 @@ where
}
}

impl<SA> TokenAttributesMapper<SA>
impl<SA, A> TokenAttributesMapper<SA, A>
where
SA: StorageMapperApi,
A: StorageAddress<SA>,
{
pub fn has_attributes<M: ManagedTypeApi>(
&self,
Expand Down

0 comments on commit f96cf65

Please sign in to comment.