From 11621bb1723cb0ac4ebfefd69579f911ae65393f Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 07 Apr 2022 10:15:02 +0000 Subject: [PATCH] feat(fungible): transfer from parent token --- --- a/pallets/fungible/Cargo.toml +++ b/pallets/fungible/Cargo.toml @@ -17,6 +17,7 @@ sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" } sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" } pallet-common = { default-features = false, path = '../common' } +pallet-structure = { default-features = false, path = '../structure' } up-data-structs = { default-features = false, path = '../../primitives/data-structs' } evm-coder = { default-features = false, path = '../../crates/evm-coder' } pallet-evm-coder-substrate = { default-features = false, path = '../evm-coder-substrate' } @@ -36,6 +37,7 @@ "sp-std/std", "up-data-structs/std", "pallet-common/std", + "pallet-structure/std", "evm-coder/std", "ethereum/std", "pallet-evm-coder-substrate/std", --- a/pallets/fungible/src/lib.rs +++ b/pallets/fungible/src/lib.rs @@ -26,6 +26,7 @@ Error as CommonError, Event as CommonEvent, Pallet as PalletCommon, CollectionHandle, dispatch::CollectionDispatch, }; +use pallet_structure::Pallet as PalletStructure; use pallet_evm_coder_substrate::WithRecorder; use sp_core::H160; use sp_runtime::{ArithmeticError, DispatchError, DispatchResult}; @@ -62,7 +63,9 @@ } #[pallet::config] - pub trait Config: frame_system::Config + pallet_common::Config { + pub trait Config: + frame_system::Config + pallet_common::Config + pallet_structure::Config + { type WeightInfo: WeightInfo; } @@ -353,21 +356,27 @@ Ok(()) } - pub fn transfer_from( + fn check_allowed( collection: &FungibleHandle, spender: &T::CrossAccountId, from: &T::CrossAccountId, - to: &T::CrossAccountId, amount: u128, - ) -> DispatchResult { + ) -> Result, DispatchError> { if spender.conv_eq(from) { - return Self::transfer(collection, from, to, amount); + return Ok(None); } if collection.access == AccessMode::AllowList { // `from`, `to` checked in [`transfer`] collection.check_allowlist(spender)?; } - + if let Some(source) = T::CrossTokenAddressMapping::address_to_token(from) { + // TODO: should collection owner be allowed to perform this transfer? + ensure!( + >::indirectly_owned(spender.clone(), source.0, source.1, 1)?, + >::ApprovedValueTooLow, + ); + return Ok(None); + } let allowance = >::get((collection.id, from, spender)).checked_sub(amount); if allowance.is_none() { ensure!( @@ -376,6 +385,18 @@ ); } + Ok(allowance) + } + + pub fn transfer_from( + collection: &FungibleHandle, + spender: &T::CrossAccountId, + from: &T::CrossAccountId, + to: &T::CrossAccountId, + amount: u128, + ) -> DispatchResult { + let allowance = Self::check_allowed(collection, spender, from, amount)?; + // ========= Self::transfer(collection, from, to, amount)?; @@ -391,21 +412,7 @@ from: &T::CrossAccountId, amount: u128, ) -> DispatchResult { - if spender.conv_eq(from) { - return Self::burn(collection, from, amount); - } - if collection.access == AccessMode::AllowList { - // `from` checked in [`burn`] - collection.check_allowlist(spender)?; - } - - let allowance = >::get((collection.id, from, spender)).checked_sub(amount); - if allowance.is_none() { - ensure!( - collection.ignores_allowance(spender), - >::ApprovedValueTooLow - ); - } + let allowance = Self::check_allowed(collection, spender, from, amount)?; // ========= -- gitstuff