Skip to content

Commit 24d3f67

Browse files
authored
Merge pull request #848 from UniqueNetwork/fix/find-parent
fix: find_parent
2 parents 030bc0d + 05c459d commit 24d3f67

15 files changed

Lines changed: 135 additions & 50 deletions

File tree

pallets/common/src/erc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ use pallet_evm_coder_substrate::dispatch_to_evm;
2828
use sp_std::{vec, vec::Vec};
2929
use sp_core::U256;
3030
use up_data_structs::{
31-
AccessMode, CollectionMode, CollectionPermissions, OwnerRestrictedSet, Property,
32-
SponsoringRateLimit, SponsorshipState,
31+
CollectionMode, CollectionPermissions, OwnerRestrictedSet, Property, SponsoringRateLimit,
32+
SponsorshipState,
3333
};
3434

3535
use crate::{

pallets/common/src/eth.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ impl CrossAddress {
8080
if cross_account_id.is_canonical_substrate() {
8181
Self::from_sub::<T>(cross_account_id.as_sub())
8282
} else {
83-
Self {
84-
eth: *cross_account_id.as_eth(),
85-
sub: Default::default(),
86-
}
83+
Self::from_eth(*cross_account_id.as_eth())
8784
}
8885
}
8986
/// Creates [`CrossAddress`] from Substrate account.
@@ -97,6 +94,13 @@ impl CrossAddress {
9794
sub: U256::from_big_endian(account_id.as_ref()),
9895
}
9996
}
97+
/// Creates [`CrossAddress`] from Ethereum account.
98+
pub fn from_eth(address: Address) -> Self {
99+
Self {
100+
eth: address,
101+
sub: Default::default(),
102+
}
103+
}
100104
/// Converts [`CrossAddress`] to `CrossAccountId`.
101105
pub fn into_sub_cross_account<T>(&self) -> evm_coder::execution::Result<T::CrossAccountId>
102106
where

pallets/common/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ use up_data_structs::{
100100
PropertyValue,
101101
PropertyPermission,
102102
PropertiesError,
103+
TokenOwnerError,
103104
PropertyKeyPermission,
104105
TokenData,
105106
TrySetProperty,
@@ -2134,7 +2135,7 @@ pub trait CommonCollectionOperations<T: Config> {
21342135
/// Get the owner of the token.
21352136
///
21362137
/// * `token` - The token for which you need to find out the owner.
2137-
fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId>;
2138+
fn token_owner(&self, token: TokenId) -> Result<T::CrossAccountId, TokenOwnerError>;
21382139

21392140
/// Returns 10 tokens owners in no particular order.
21402141
///

pallets/fungible/src/common.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
use core::marker::PhantomData;
1818

1919
use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight, traits::Get};
20-
use up_data_structs::{TokenId, CollectionId, CreateItemExData, budget::Budget, CreateItemData};
20+
use up_data_structs::{
21+
TokenId, CollectionId, CreateItemExData, budget::Budget, CreateItemData, TokenOwnerError,
22+
};
2123
use pallet_common::{
2224
CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,
2325
weights::WeightInfo as _,
@@ -404,8 +406,8 @@ impl<T: Config> CommonCollectionOperations<T> for FungibleHandle<T> {
404406
TokenId::default()
405407
}
406408

407-
fn token_owner(&self, _token: TokenId) -> Option<T::CrossAccountId> {
408-
None
409+
fn token_owner(&self, _token: TokenId) -> Result<T::CrossAccountId, TokenOwnerError> {
410+
Err(TokenOwnerError::MultipleOwners)
409411
}
410412

411413
/// Returns 10 tokens owners in no particular order.

pallets/nonfungible/src/common.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use core::marker::PhantomData;
1919
use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights::Weight};
2020
use up_data_structs::{
2121
TokenId, CreateItemExData, CollectionId, budget::Budget, Property, PropertyKey,
22-
PropertyKeyPermission, PropertyValue,
22+
PropertyKeyPermission, PropertyValue, TokenOwnerError,
2323
};
2424
use pallet_common::{
2525
CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,
@@ -460,13 +460,15 @@ impl<T: Config> CommonCollectionOperations<T> for NonfungibleHandle<T> {
460460
TokenId(<TokensMinted<T>>::get(self.id))
461461
}
462462

463-
fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId> {
464-
<TokenData<T>>::get((self.id, token)).map(|t| t.owner)
463+
fn token_owner(&self, token: TokenId) -> Result<T::CrossAccountId, TokenOwnerError> {
464+
<TokenData<T>>::get((self.id, token))
465+
.map(|t| t.owner)
466+
.ok_or(TokenOwnerError::NotFound)
465467
}
466468

467469
/// Returns token owners.
468470
fn token_owners(&self, token: TokenId) -> Vec<T::CrossAccountId> {
469-
self.token_owner(token).map_or_else(|| vec![], |t| vec![t])
471+
self.token_owner(token).map_or_else(|_| vec![], |t| vec![t])
470472
}
471473

472474
fn token_property(&self, token_id: TokenId, key: &PropertyKey) -> Option<PropertyValue> {

pallets/nonfungible/src/erc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ where
728728
fn cross_owner_of(&self, token_id: U256) -> Result<eth::CrossAddress> {
729729
Self::token_owner(&self, token_id.try_into()?)
730730
.map(|o| eth::CrossAddress::from_sub_cross_account::<T>(&o))
731-
.ok_or(Error::Revert("key too large".into()))
731+
.map_err(|_| Error::Revert("token not found".into()))
732732
}
733733

734734
/// Returns the token properties.

pallets/proxy-rmrk-core/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,8 @@ pub mod pallet {
741741
Some((collection_id, nft_id)),
742742
&target_nft_budget,
743743
)
744-
.map_err(Self::map_unique_err_to_proxy)?;
744+
.map_err(Self::map_unique_err_to_proxy)?
745+
.ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
745746

746747
approval_required = cross_sender != target_nft_owner;
747748

@@ -989,7 +990,8 @@ pub mod pallet {
989990

990991
let nft_owner =
991992
<PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)
992-
.map_err(|_| <Error<T>>::ResourceDoesntExist)?;
993+
.map_err(|_| <Error<T>>::ResourceDoesntExist)?
994+
.ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
993995

994996
Self::try_mutate_resource_info(collection_id, nft_id, resource_id, |res| {
995997
ensure!(res.pending, <Error<T>>::ResourceNotPending);
@@ -1044,7 +1046,8 @@ pub mod pallet {
10441046

10451047
let nft_owner =
10461048
<PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)
1047-
.map_err(|_| <Error<T>>::ResourceDoesntExist)?;
1049+
.map_err(|_| <Error<T>>::ResourceDoesntExist)?
1050+
.ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
10481051

10491052
ensure!(cross_sender == nft_owner, <Error<T>>::NoPermission);
10501053

@@ -1666,7 +1669,8 @@ impl<T: Config> Pallet<T> {
16661669
let budget = budget::Value::new(NESTING_BUDGET);
16671670

16681671
let nft_owner = <PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)
1669-
.map_err(Self::map_unique_err_to_proxy)?;
1672+
.map_err(Self::map_unique_err_to_proxy)?
1673+
.ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
16701674

16711675
let pending = sender != nft_owner;
16721676

@@ -1720,7 +1724,8 @@ impl<T: Config> Pallet<T> {
17201724

17211725
let budget = up_data_structs::budget::Value::new(NESTING_BUDGET);
17221726
let topmost_owner =
1723-
<PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)?;
1727+
<PalletStructure<T>>::find_topmost_owner(collection_id, nft_id, &budget)?
1728+
.ok_or::<DispatchError>(<Error<T>>::NoPermission.into())?;
17241729

17251730
let sender = T::CrossAccountId::from_sub(sender);
17261731
if topmost_owner == sender {

pallets/proxy-rmrk-core/src/rpc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ pub fn nft_by_id<T: Config>(
6868
}
6969

7070
let owner = match collection.token_owner(nft_id) {
71-
Some(owner) => match T::CrossTokenAddressMapping::address_to_token(&owner) {
71+
Ok(owner) => match T::CrossTokenAddressMapping::address_to_token(&owner) {
7272
Some((col, tok)) => {
7373
let rmrk_collection = <Pallet<T>>::rmrk_collection_id(col)?;
7474

7575
RmrkAccountIdOrCollectionNftTuple::CollectionAndNftTuple(rmrk_collection, tok.0)
7676
}
7777
None => RmrkAccountIdOrCollectionNftTuple::AccountId(owner.as_sub().clone()),
7878
},
79-
None => return Ok(None),
79+
_ => return Ok(None),
8080
};
8181

8282
Ok(Some(RmrkInstanceInfo {

pallets/refungible/src/common.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use frame_support::{dispatch::DispatchResultWithPostInfo, ensure, fail, weights:
2121
use up_data_structs::{
2222
CollectionId, TokenId, CreateItemExData, budget::Budget, Property, PropertyKey, PropertyValue,
2323
PropertyKeyPermission, CollectionPropertiesVec, CreateRefungibleExMultipleOwners,
24-
CreateRefungibleExSingleOwner,
24+
CreateRefungibleExSingleOwner, TokenOwnerError,
2525
};
2626
use pallet_common::{
2727
CommonCollectionOperations, CommonWeightInfo, RefungibleExtensions, with_weight,
@@ -478,7 +478,7 @@ impl<T: Config> CommonCollectionOperations<T> for RefungibleHandle<T> {
478478
TokenId(<TokensMinted<T>>::get(self.id))
479479
}
480480

481-
fn token_owner(&self, token: TokenId) -> Option<T::CrossAccountId> {
481+
fn token_owner(&self, token: TokenId) -> Result<T::CrossAccountId, TokenOwnerError> {
482482
<Pallet<T>>::token_owner(self.id, token)
483483
}
484484

pallets/refungible/src/erc.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use sp_core::{H160, U256, Get};
4343
use sp_std::{collections::btree_map::BTreeMap, vec::Vec, vec};
4444
use up_data_structs::{
4545
CollectionId, CollectionPropertiesVec, mapping::TokenAddressMapping, Property, PropertyKey,
46-
PropertyKeyPermission, PropertyPermission, TokenId,
46+
PropertyKeyPermission, PropertyPermission, TokenId, TokenOwnerError,
4747
};
4848

4949
use crate::{
@@ -411,9 +411,12 @@ impl<T: Config> RefungibleHandle<T> {
411411
self.consume_store_reads(2)?;
412412
let token = token_id.try_into()?;
413413
let owner = <Pallet<T>>::token_owner(self.id, token);
414-
Ok(owner
414+
owner
415415
.map(|address| *address.as_eth())
416-
.unwrap_or_else(|| ADDRESS_FOR_PARTIALLY_OWNED_TOKENS))
416+
.or_else(|err| match err {
417+
TokenOwnerError::NotFound => Err(Error::Revert("token not found".into())),
418+
TokenOwnerError::MultipleOwners => Ok(ADDRESS_FOR_PARTIALLY_OWNED_TOKENS),
419+
})
417420
}
418421

419422
/// @dev Not implemented
@@ -766,7 +769,12 @@ where
766769
fn cross_owner_of(&self, token_id: U256) -> Result<eth::CrossAddress> {
767770
Self::token_owner(&self, token_id.try_into()?)
768771
.map(|o| eth::CrossAddress::from_sub_cross_account::<T>(&o))
769-
.ok_or(Error::Revert("key too large".into()))
772+
.or_else(|err| match err {
773+
TokenOwnerError::NotFound => Err(Error::Revert("token not found".into())),
774+
TokenOwnerError::MultipleOwners => Ok(eth::CrossAddress::from_eth(
775+
ADDRESS_FOR_PARTIALLY_OWNED_TOKENS,
776+
)),
777+
})
770778
}
771779

772780
/// Returns the token properties.

0 commit comments

Comments
 (0)