diff --git a/nft-provider-aggregator/contracts/NFTProviderAggregator.cdc b/nft-provider-aggregator/contracts/NFTProviderAggregator.cdc index 83d3dad..6f47a1e 100644 --- a/nft-provider-aggregator/contracts/NFTProviderAggregator.cdc +++ b/nft-provider-aggregator/contracts/NFTProviderAggregator.cdc @@ -180,7 +180,7 @@ access(all) contract NFTProviderAggregator { /// Borrow the collection of an NFT located in one of multiple collections through iterating over each collection /// - access(all) view fun borrowNFTCollection(id: UInt64): &{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic} { + access(all) view fun borrowNFTCollection(id: UInt64): &{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}? { for collectionUUID in self.nftProviderCapabilities.keys { // Check capabilities can still be borrowed since a NFT provider capability may pass the // pre-condition checks at the time of being added with the addNFTWithdrawCapability method but @@ -204,7 +204,7 @@ access(all) contract NFTProviderAggregator { } } } - panic("missing NFT") + return nil } /// Withdraw an NFT located in one of multiple collections through iterating over each collection @@ -215,8 +215,8 @@ access(all) contract NFTProviderAggregator { /// Borrow an NFT located in one of multiple collections through iterating over each collection /// - access(all) view fun borrowNFT(id: UInt64): &{NonFungibleToken.NFT} { - return self.borrowNFTCollection(id: id).borrowNFT(id)! + access(all) view fun borrowNFT(id: UInt64): &{NonFungibleToken.NFT}? { + return self.borrowNFTCollection(id: id)?.borrowNFT(id) ?? nil } /// Create and return a Supplier resource diff --git a/nft-provider-aggregator/scripts/aggregator_borrow_nft.cdc b/nft-provider-aggregator/scripts/aggregator_borrow_nft.cdc new file mode 100644 index 0000000..50565b7 --- /dev/null +++ b/nft-provider-aggregator/scripts/aggregator_borrow_nft.cdc @@ -0,0 +1,16 @@ +import NFTProviderAggregator from "NFTProviderAggregator" +import NonFungibleToken from "NonFungibleToken" + +// Tries to borrow an NFT by ID from the Aggregator resource stored in `address`. +// Returns the NFT's Type identifier if found, nil otherwise. + +access(all) fun main(address: Address, nftID: UInt64): &{NonFungibleToken.NFT}? { + let account = getAuthAccount(address) + + let aggregator = account.storage.borrow<&NFTProviderAggregator.Aggregator>( + from: NFTProviderAggregator.AggregatorStoragePath + ) ?? panic("Aggregator resource not found at ".concat(NFTProviderAggregator.AggregatorStoragePath.toString())) + + let nft = aggregator.borrowNFT(id: nftID) + return nft +}