diff --git a/core/src/smartcontracts/isi/account.rs b/core/src/smartcontracts/isi/account.rs index 466a578f13d..236a3dc017e 100644 --- a/core/src/smartcontracts/isi/account.rs +++ b/core/src/smartcontracts/isi/account.rs @@ -95,14 +95,13 @@ pub mod isi { state_transaction: &mut StateTransaction<'_, '_>, ) -> Result<(), Error> { let asset_id = self.object_id; - let account_id = asset_id.account_id.clone(); let asset = state_transaction .world - .account_mut(&account_id) + .account_mut(&asset_id.account_id) .and_then(|account| { account - .remove_asset(&asset_id) + .remove_asset(&asset_id.definition_id) .ok_or_else(|| FindError::Asset(asset_id)) })?; @@ -613,9 +612,7 @@ pub mod query { .world() .map_domain(&asset_definition_id.domain_id.clone(), move |domain| { domain.accounts.values().filter(move |account| { - let asset_id = - AssetId::new(asset_definition_id.clone(), account.id().clone()); - account.assets.contains_key(&asset_id) + account.assets.contains_key(&asset_definition_id) }) })? .cloned(), diff --git a/core/src/smartcontracts/isi/asset.rs b/core/src/smartcontracts/isi/asset.rs index 78d7ff998b5..20752dd167f 100644 --- a/core/src/smartcontracts/isi/asset.rs +++ b/core/src/smartcontracts/isi/asset.rs @@ -143,14 +143,13 @@ pub mod isi { state_transaction, expected_asset_value_type_store, )?; - let account_id = asset_id.account_id.clone(); let asset = state_transaction .world - .account_mut(&account_id) + .account_mut(&asset_id.account_id) .and_then(|account| { account - .remove_asset(&asset_id) + .remove_asset(&asset_id.definition_id) .ok_or_else(|| FindError::Asset(asset_id.clone())) })?; @@ -239,7 +238,7 @@ pub mod isi { let account = state_transaction.world.account_mut(&asset_id.account_id)?; let asset = account .assets - .get_mut(&asset_id) + .get_mut(&asset_id.definition_id) .ok_or_else(|| FindError::Asset(asset_id.clone()))?; let AssetValue::Numeric(quantity) = &mut asset.value else { return Err(Error::Conversion("Expected numeric asset type".to_owned())); @@ -249,7 +248,7 @@ pub mod isi { .ok_or(MathError::NotEnoughQuantity)?; if asset.value.is_zero_value() { - assert!(account.remove_asset(&asset_id).is_some()); + assert!(account.remove_asset(&asset_id.definition_id).is_some()); } #[allow(clippy::float_arithmetic)] @@ -295,7 +294,7 @@ pub mod isi { let account = state_transaction.world.account_mut(&source_id.account_id)?; let asset = account .assets - .get_mut(&source_id) + .get_mut(&source_id.definition_id) .ok_or_else(|| FindError::Asset(source_id.clone()))?; let AssetValue::Numeric(quantity) = &mut asset.value else { return Err(Error::Conversion("Expected numeric asset type".to_owned())); @@ -304,7 +303,7 @@ pub mod isi { .checked_sub(self.object) .ok_or(MathError::NotEnoughQuantity)?; if asset.value.is_zero_value() { - assert!(account.remove_asset(&source_id).is_some()); + assert!(account.remove_asset(&source_id.definition_id).is_some()); } } diff --git a/core/src/smartcontracts/isi/domain.rs b/core/src/smartcontracts/isi/domain.rs index a2b2023c03e..15e5a6df06a 100644 --- a/core/src/smartcontracts/isi/domain.rs +++ b/core/src/smartcontracts/isi/domain.rs @@ -188,11 +188,10 @@ pub mod isi { let mut events = Vec::with_capacity(assets_to_remove.len() + 1); for asset_id in assets_to_remove { - let account_id = asset_id.account_id.clone(); if state_transaction .world - .account_mut(&account_id)? - .remove_asset(&asset_id) + .account_mut(&asset_id.account_id)? + .remove_asset(&asset_id.definition_id) .is_none() { error!(%asset_id, "asset not found. This is a bug"); diff --git a/core/src/state.rs b/core/src/state.rs index 38a74462a71..0358a0e562d 100644 --- a/core/src/state.rs +++ b/core/src/state.rs @@ -410,7 +410,8 @@ pub trait WorldReadOnly { fn account_assets( &self, id: &AccountId, - ) -> Result, QueryExecutionFail> { + ) -> Result, QueryExecutionFail> + { self.map_account(id, |account| account.assets.values()) } @@ -493,7 +494,7 @@ pub trait WorldReadOnly { |account| -> Result { account .assets - .get(id) + .get(&id.definition_id) .ok_or_else(|| QueryExecutionFail::from(FindError::Asset(id.clone()))) .cloned() }, @@ -708,7 +709,7 @@ impl WorldTransaction<'_, '_> { self.account_mut(&id.account_id).and_then(move |account| { account .assets - .get_mut(id) + .get_mut(&id.definition_id) .ok_or_else(|| FindError::Asset(id.clone())) }) } @@ -747,15 +748,18 @@ impl WorldTransaction<'_, '_> { .get_mut(account_id) .ok_or(FindError::Account(account_id.clone()))?; - Ok(account.assets.entry(asset_id.clone()).or_insert_with(|| { - let asset = Asset::new(asset_id, default_asset_value.into()); - Self::emit_events_impl( - &mut self.triggers, - &mut self.events_buffer, - Some(AccountEvent::Asset(AssetEvent::Created(asset.clone()))), - ); - asset - })) + Ok(account + .assets + .entry(asset_id.definition_id.clone()) + .or_insert_with(|| { + let asset = Asset::new(asset_id, default_asset_value.into()); + Self::emit_events_impl( + &mut self.triggers, + &mut self.events_buffer, + Some(AccountEvent::Asset(AssetEvent::Created(asset.clone()))), + ); + asset + })) } /// Get mutable reference to [`AssetDefinition`] diff --git a/data_model/src/account.rs b/data_model/src/account.rs index 303c21b352a..41d74388e7b 100644 --- a/data_model/src/account.rs +++ b/data_model/src/account.rs @@ -15,10 +15,7 @@ use serde_with::{DeserializeFromStr, SerializeDisplay}; pub use self::model::*; use crate::{ - asset::{ - prelude::{Asset, AssetId}, - AssetsMap, - }, + asset::{Asset, AssetDefinitionId, AssetsMap}, domain::prelude::*, metadata::Metadata, HasMetadata, Identifiable, ParseError, PublicKey, Registered, @@ -137,7 +134,7 @@ impl Account { /// Return a reference to the [`Asset`] corresponding to the asset id. #[inline] - pub fn asset(&self, asset_id: &AssetId) -> Option<&Asset> { + pub fn asset(&self, asset_id: &AssetDefinitionId) -> Option<&Asset> { self.assets.get(asset_id) } @@ -153,12 +150,13 @@ impl Account { /// Add [`Asset`] into the [`Account`] returning previous asset stored under the same id #[inline] pub fn add_asset(&mut self, asset: Asset) -> Option { - self.assets.insert(asset.id().clone(), asset) + assert_eq!(self.id, asset.id.account_id); + self.assets.insert(asset.id.definition_id.clone(), asset) } /// Remove asset from the [`Account`] and return it #[inline] - pub fn remove_asset(&mut self, asset_id: &AssetId) -> Option { + pub fn remove_asset(&mut self, asset_id: &AssetDefinitionId) -> Option { self.assets.remove(asset_id) } } diff --git a/data_model/src/asset.rs b/data_model/src/asset.rs index 53fa815730e..82366bb21b0 100644 --- a/data_model/src/asset.rs +++ b/data_model/src/asset.rs @@ -22,7 +22,7 @@ use crate::{ }; /// API to work with collections of [`Id`] : [`Asset`] mappings. -pub type AssetsMap = btree_map::BTreeMap; +pub type AssetsMap = btree_map::BTreeMap; /// [`AssetDefinitionsMap`] provides an API to work with collection of key([`AssetDefinitionId`])-value([`AssetDefinition`]) /// pairs.