From 31bae1fc2671a2a40d3984fba83b17aea9643d29 Mon Sep 17 00:00:00 2001 From: Daniel Shiposha Date: Mon, 16 Oct 2023 12:37:17 +0000 Subject: [PATCH] fix: separate depositor from sender in transfer_internal (#1013) * fix: transfer-from * fix: remove Option from nester type * fix: check_nesting comment * fix: rename nester to depositor, add docs --- --- a/pallets/balances-adapter/src/common.rs +++ b/pallets/balances-adapter/src/common.rs @@ -221,7 +221,7 @@ fn check_nesting( &self, - _sender: ::CrossAccountId, + _sender: &::CrossAccountId, _from: (up_data_structs::CollectionId, TokenId), _under: TokenId, _budget: &dyn up_data_structs::budget::Budget, --- a/pallets/common/src/lib.rs +++ b/pallets/common/src/lib.rs @@ -2194,7 +2194,7 @@ /// * `budget` - The maximum budget that can be spent on the check. fn check_nesting( &self, - sender: T::CrossAccountId, + sender: &T::CrossAccountId, from: (CollectionId, TokenId), under: TokenId, budget: &dyn Budget, --- a/pallets/fungible/src/common.rs +++ b/pallets/fungible/src/common.rs @@ -335,7 +335,7 @@ fn check_nesting( &self, - _sender: ::CrossAccountId, + _sender: &::CrossAccountId, _from: (CollectionId, TokenId), _under: TokenId, _nesting_budget: &dyn Budget, --- a/pallets/fungible/src/lib.rs +++ b/pallets/fungible/src/lib.rs @@ -384,6 +384,21 @@ amount: u128, nesting_budget: &dyn Budget, ) -> DispatchResultWithPostInfo { + let depositor = from; + Self::transfer_internal(collection, depositor, from, to, amount, nesting_budget) + } + + /// Transfers tokens from the `from` account to the `to` account. + /// The `depositor` is the account who deposits the tokens. + /// For instance, the nesting rules will be checked against the `depositor`'s permissions. + fn transfer_internal( + collection: &FungibleHandle, + depositor: &T::CrossAccountId, + from: &T::CrossAccountId, + to: &T::CrossAccountId, + amount: u128, + nesting_budget: &dyn Budget, + ) -> DispatchResultWithPostInfo { ensure!( collection.limits.transfers_enabled(), >::TransferNotAllowed, @@ -416,7 +431,7 @@ // from != to && amount != 0 >::nest_if_sent_to_token( - from.clone(), + depositor, to, collection.id, TokenId::default(), @@ -473,7 +488,7 @@ for (to, _) in data.iter() { >::check_nesting( - sender.clone(), + sender, to, collection.id, TokenId::default(), @@ -730,7 +745,8 @@ // ========= - let mut result = Self::transfer(collection, from, to, amount, nesting_budget); + let mut result = + Self::transfer_internal(collection, spender, from, to, amount, nesting_budget); add_weight_to_post_info(&mut result, >::check_allowed_raw()); result?; --- a/pallets/nonfungible/src/common.rs +++ b/pallets/nonfungible/src/common.rs @@ -407,7 +407,7 @@ fn check_nesting( &self, - sender: T::CrossAccountId, + sender: &T::CrossAccountId, from: (CollectionId, TokenId), under: TokenId, nesting_budget: &dyn Budget, --- a/pallets/nonfungible/src/lib.rs +++ b/pallets/nonfungible/src/lib.rs @@ -718,6 +718,21 @@ token: TokenId, nesting_budget: &dyn Budget, ) -> DispatchResultWithPostInfo { + let depositor = from; + Self::transfer_internal(collection, depositor, from, to, token, nesting_budget) + } + + /// Transfers an NFT from the `from` account to the `to` account. + /// The `depositor` is the account who deposits the NFT. + /// For instance, the nesting rules will be checked against the `depositor`'s permissions. + pub fn transfer_internal( + collection: &NonfungibleHandle, + depositor: &T::CrossAccountId, + from: &T::CrossAccountId, + to: &T::CrossAccountId, + token: TokenId, + nesting_budget: &dyn Budget, + ) -> DispatchResultWithPostInfo { ensure!( collection.limits.transfers_enabled(), >::TransferNotAllowed @@ -754,7 +769,7 @@ }; >::nest_if_sent_to_token( - from.clone(), + depositor, to, collection.id, token, @@ -860,7 +875,7 @@ let token = TokenId(first_token + i as u32 + 1); >::check_nesting( - sender.clone(), + sender, &data.owner, collection.id, token, @@ -1154,7 +1169,8 @@ // ========= // Allowance is reset in [`transfer`] - let mut result = Self::transfer(collection, from, to, token, nesting_budget); + let mut result = + Self::transfer_internal(collection, spender, from, to, token, nesting_budget); add_weight_to_post_info(&mut result, >::check_allowed_raw()); result } @@ -1183,7 +1199,7 @@ /// pub fn check_nesting( handle: &NonfungibleHandle, - sender: T::CrossAccountId, + sender: &T::CrossAccountId, from: (CollectionId, TokenId), under: TokenId, nesting_budget: &dyn Budget, --- a/pallets/refungible/src/common.rs +++ b/pallets/refungible/src/common.rs @@ -416,7 +416,7 @@ fn check_nesting( &self, - _sender: ::CrossAccountId, + _sender: &::CrossAccountId, _from: (CollectionId, TokenId), _under: TokenId, _nesting_budget: &dyn Budget, --- a/pallets/refungible/src/lib.rs +++ b/pallets/refungible/src/lib.rs @@ -615,6 +615,30 @@ amount: u128, nesting_budget: &dyn Budget, ) -> DispatchResult { + let depositor = from; + Self::transfer_internal( + collection, + depositor, + from, + to, + token, + amount, + nesting_budget, + ) + } + + /// Transfers RFT tokens from the `from` account to the `to` account. + /// The `depositor` is the account who deposits the tokens. + /// For instance, the nesting rules will be checked against the `depositor`'s permissions. + pub fn transfer_internal( + collection: &RefungibleHandle, + depositor: &T::CrossAccountId, + from: &T::CrossAccountId, + to: &T::CrossAccountId, + token: TokenId, + amount: u128, + nesting_budget: &dyn Budget, + ) -> DispatchResult { ensure!( collection.limits.transfers_enabled(), >::TransferNotAllowed @@ -683,7 +707,7 @@ // from != to && amount != 0 >::nest_if_sent_to_token( - from.clone(), + depositor, to, collection.id, token, @@ -847,7 +871,7 @@ let token_id = TokenId(first_token_id + i as u32 + 1); for (to, _) in token.users.iter() { >::check_nesting( - sender.clone(), + sender, to, collection.id, token_id, @@ -1146,7 +1170,7 @@ // ========= - Self::transfer(collection, from, to, token, amount, nesting_budget)?; + Self::transfer_internal(collection, spender, from, to, token, amount, nesting_budget)?; if let Some(allowance) = allowance { Self::set_allowance_unchecked(collection, from, spender, token, allowance); } --- a/pallets/structure/src/lib.rs +++ b/pallets/structure/src/lib.rs @@ -300,7 +300,7 @@ /// /// - `nesting_budget`: Limit for searching parents in depth. pub fn check_nesting( - from: T::CrossAccountId, + from: &T::CrossAccountId, under: &T::CrossAccountId, collection_id: CollectionId, token_id: TokenId, @@ -317,7 +317,7 @@ /// /// - `nesting_budget`: Limit for searching parents in depth. pub fn nest_if_sent_to_token( - from: T::CrossAccountId, + from: &T::CrossAccountId, under: &T::CrossAccountId, collection_id: CollectionId, token_id: TokenId, -- gitstuff