From e3dc73eea715b86ab577ee50d3cffe1a7226d936 Mon Sep 17 00:00:00 2001 From: Siddharth Bhattacharjee Date: Thu, 23 Apr 2026 13:48:06 -0400 Subject: [PATCH 1/2] make borrowNFT return Optional --- .../contracts/NFTProviderAggregator.cdc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 From e8ee7e6f11cbf8e929fa43e2fb26f8cef986193a Mon Sep 17 00:00:00 2001 From: Siddharth Bhattacharjee Date: Thu, 23 Apr 2026 15:26:07 -0400 Subject: [PATCH 2/2] add test script --- .../scripts/aggregator_borrow_nft.cdc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 nft-provider-aggregator/scripts/aggregator_borrow_nft.cdc 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 +}