From f96cf656288f9d0d74f9e51c00c7f0ffdec59116 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Mon, 18 Nov 2024 15:46:17 +0100 Subject: [PATCH] moved get_balance to currentStorage --- .../mappers/token/fungible_token_mapper.rs | 39 +++++++------ .../token/non_fungible_token_mapper.rs | 55 ++++++++++--------- .../mappers/token/token_attributes_mapper.rs | 8 ++- 3 files changed, 56 insertions(+), 46 deletions(-) diff --git a/framework/base/src/storage/mappers/token/fungible_token_mapper.rs b/framework/base/src/storage/mappers/token/fungible_token_mapper.rs index a906df0eb5..e3cd41702e 100644 --- a/framework/base/src/storage/mappers/token/fungible_token_mapper.rs +++ b/framework/base/src/storage/mappers/token/fungible_token_mapper.rs @@ -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, }, }; @@ -38,6 +41,7 @@ pub(crate) const DEFAULT_ISSUE_WITH_INIT_SUPPLY_CALLBACK_NAME: &str = pub struct FungibleTokenMapper where SA: StorageMapperApi + CallTypeApi, + A: StorageAddress, { key: StorageKey, token_state: TokenMapperState, @@ -265,13 +269,27 @@ where &TokenMapperState::Token(token_id.clone()), ); } + + pub fn get_balance(&self) -> BigUint { + 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 { + let b_wrapper = BlockchainWrapper::new(); + b_wrapper.get_sc_address() + } } -impl FungibleTokenMapper +impl FungibleTokenMapper where SA: StorageMapperApi + CallTypeApi, + A: StorageAddress, { - pub fn get_storage_key(&self) -> crate::types::ManagedRef> { + pub fn get_storage_key(&self) -> ManagedRef> { self.key.as_ref() } @@ -295,11 +313,6 @@ where } } - pub fn get_sc_address() -> ManagedAddress { - let b_wrapper = BlockchainWrapper::new(); - b_wrapper.get_sc_address() - } - pub fn is_empty(&self) -> bool { storage_get_len(self.get_storage_key()) == 0 } @@ -344,14 +357,6 @@ where cb_closure } - pub fn get_balance(&self) -> BigUint { - 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 = storage_get(self.get_storage_key()); match storage_value { diff --git a/framework/base/src/storage/mappers/token/non_fungible_token_mapper.rs b/framework/base/src/storage/mappers/token/non_fungible_token_mapper.rs index 546be9e000..3a4d63ab97 100644 --- a/framework/base/src/storage/mappers/token/non_fungible_token_mapper.rs +++ b/framework/base/src/storage/mappers/token/non_fungible_token_mapper.rs @@ -395,11 +395,38 @@ where &TokenMapperState::Token(token_id.clone()), ); } + + pub fn get_balance(&self, token_nonce: u64) -> BigUint { + 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 { + let b_wrapper = BlockchainWrapper::new(); + b_wrapper.get_sc_address() + } + + pub fn get_all_token_data(&self, token_nonce: u64) -> EsdtTokenData { + 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(&self, token_nonce: u64) -> T { + let token_data = self.get_all_token_data(token_nonce); + token_data.decode_attributes() + } } -impl NonFungibleTokenMapper +impl NonFungibleTokenMapper where SA: StorageMapperApi + CallTypeApi, + A: StorageAddress, { pub(crate) fn check_not_set(&self) { let storage_value: TokenMapperState = storage_get(self.get_storage_key()); @@ -418,11 +445,6 @@ where storage_get_len(self.get_storage_key()) == 0 } - pub fn get_sc_address() -> ManagedAddress { - 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); @@ -469,27 +491,6 @@ where } } - pub fn get_all_token_data(&self, token_nonce: u64) -> EsdtTokenData { - 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 { - 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(&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 { let initial_caller = BlockchainWrapper::::new().get_caller(); let cb_name = DEFAULT_ISSUE_CALLBACK_NAME; diff --git a/framework/base/src/storage/mappers/token/token_attributes_mapper.rs b/framework/base/src/storage/mappers/token/token_attributes_mapper.rs index d5bd32698e..f9631cf983 100644 --- a/framework/base/src/storage/mappers/token/token_attributes_mapper.rs +++ b/framework/base/src/storage/mappers/token/token_attributes_mapper.rs @@ -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, }; @@ -192,9 +195,10 @@ where } } -impl TokenAttributesMapper +impl TokenAttributesMapper where SA: StorageMapperApi, + A: StorageAddress, { pub fn has_attributes( &self,