Skip to content

Commit

Permalink
Change NFTOwner map to track holders portfolio (#1566)
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriqueNogara authored Nov 27, 2023
1 parent 5da6564 commit 99054ca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
8 changes: 4 additions & 4 deletions pallets/nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ decl_storage!(
pub NFTsInCollection get(fn nfts_in_collection): map hasher(blake2_128_concat) Ticker => NFTCount;

/// Tracks the owner of an NFT
pub NFTOwner get(fn nft_owner): double_map hasher(blake2_128_concat) Ticker, hasher(blake2_128_concat) NFTId => Option<IdentityId>;
pub NFTOwner get(fn nft_owner): double_map hasher(blake2_128_concat) Ticker, hasher(blake2_128_concat) NFTId => Option<PortfolioId>;

/// Storage version.
StorageVersion get(fn storage_version) build(|_| Version::new(1)): Version;
Expand Down Expand Up @@ -365,7 +365,7 @@ impl<T: Config> Module<T> {
MetadataValue::insert((&collection_id, &nft_id), metadata_key, metadata_value);
}
PortfolioNFT::insert(caller_portfolio, (ticker, nft_id), true);
NFTOwner::insert(ticker, nft_id, caller_portfolio.did);
NFTOwner::insert(ticker, nft_id, caller_portfolio);

Self::deposit_event(Event::NFTPortfolioUpdated(
caller_portfolio.did,
Expand Down Expand Up @@ -559,7 +559,7 @@ impl<T: Config> Module<T> {
for nft_id in nfts.ids() {
PortfolioNFT::remove(sender_portfolio, (nfts.ticker(), nft_id));
PortfolioNFT::insert(receiver_portfolio, (nfts.ticker(), nft_id), true);
NFTOwner::insert(nfts.ticker(), nft_id, receiver_portfolio.did);
NFTOwner::insert(nfts.ticker(), nft_id, receiver_portfolio);
}
}

Expand Down Expand Up @@ -638,7 +638,7 @@ pub mod migration {

fn initialize_nft_owner<T: Config>() {
for (portfolio_id, (ticker, nft_id), _) in PortfolioNFT::iter() {
NFTOwner::insert(ticker, nft_id, portfolio_id.did);
NFTOwner::insert(ticker, nft_id, portfolio_id);
}
}
}
10 changes: 7 additions & 3 deletions pallets/runtime/tests/src/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ fn mint_nft_successfully() {

let alice: User = User::new(AccountKeyring::Alice);
let ticker: Ticker = Ticker::from_slice_truncated(b"TICKER".as_ref());
let alice_default_portfolio = PortfolioId::new(alice.did, PortfolioKind::Default);
let collection_keys: NFTCollectionKeys =
vec![AssetMetadataKey::Local(AssetMetadataLocalKey(1))].into();

Expand Down Expand Up @@ -433,7 +434,10 @@ fn mint_nft_successfully() {
),
true
);
assert_eq!(NFTOwner::get(ticker, NFTId(1)), Some(alice.did));
assert_eq!(
NFTOwner::get(ticker, NFTId(1)),
Some(alice_default_portfolio)
);
});
}

Expand Down Expand Up @@ -909,7 +913,7 @@ fn transfer_nft() {
PortfolioNFT::get(PortfolioId::default_portfolio(bob.did), (&ticker, NFTId(1))),
true
);
assert_eq!(NFTOwner::get(ticker, NFTId(1)), Some(bob.did));
assert_eq!(NFTOwner::get(ticker, NFTId(1)), Some(receiver_portfolio));
assert_eq!(
super::storage::EventTest::Nft(Event::NFTPortfolioUpdated(
IdentityId::default(),
Expand Down Expand Up @@ -1002,7 +1006,7 @@ fn controller_transfer() {
alice_portfolio,
(ticker, NFTId(1))
));
assert_eq!(NFTOwner::get(ticker, NFTId(1)), Some(alice.did));
assert_eq!(NFTOwner::get(ticker, NFTId(1)), Some(alice_portfolio));
assert_eq!(
super::storage::EventTest::Nft(Event::NFTPortfolioUpdated(
alice.did,
Expand Down

0 comments on commit 99054ca

Please sign in to comment.