Skip to content

Commit b74c19f

Browse files
authored
NFTProviderAggregator: Make borrowNFT return Optional(NFT) (#96)
* make borrowNFT return Optional * add test script
1 parent f663bed commit b74c19f

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

nft-provider-aggregator/contracts/NFTProviderAggregator.cdc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ access(all) contract NFTProviderAggregator {
180180

181181
/// Borrow the collection of an NFT located in one of multiple collections through iterating over each collection
182182
///
183-
access(all) view fun borrowNFTCollection(id: UInt64): &{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic} {
183+
access(all) view fun borrowNFTCollection(id: UInt64): &{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}? {
184184
for collectionUUID in self.nftProviderCapabilities.keys {
185185
// Check capabilities can still be borrowed since a NFT provider capability may pass the
186186
// pre-condition checks at the time of being added with the addNFTWithdrawCapability method but
@@ -204,7 +204,7 @@ access(all) contract NFTProviderAggregator {
204204
}
205205
}
206206
}
207-
panic("missing NFT")
207+
return nil
208208
}
209209

210210
/// Withdraw an NFT located in one of multiple collections through iterating over each collection
@@ -215,8 +215,8 @@ access(all) contract NFTProviderAggregator {
215215

216216
/// Borrow an NFT located in one of multiple collections through iterating over each collection
217217
///
218-
access(all) view fun borrowNFT(id: UInt64): &{NonFungibleToken.NFT} {
219-
return self.borrowNFTCollection(id: id).borrowNFT(id)!
218+
access(all) view fun borrowNFT(id: UInt64): &{NonFungibleToken.NFT}? {
219+
return self.borrowNFTCollection(id: id)?.borrowNFT(id) ?? nil
220220
}
221221

222222
/// Create and return a Supplier resource
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import NFTProviderAggregator from "NFTProviderAggregator"
2+
import NonFungibleToken from "NonFungibleToken"
3+
4+
// Tries to borrow an NFT by ID from the Aggregator resource stored in `address`.
5+
// Returns the NFT's Type identifier if found, nil otherwise.
6+
7+
access(all) fun main(address: Address, nftID: UInt64): &{NonFungibleToken.NFT}? {
8+
let account = getAuthAccount<auth(BorrowValue) &Account>(address)
9+
10+
let aggregator = account.storage.borrow<&NFTProviderAggregator.Aggregator>(
11+
from: NFTProviderAggregator.AggregatorStoragePath
12+
) ?? panic("Aggregator resource not found at ".concat(NFTProviderAggregator.AggregatorStoragePath.toString()))
13+
14+
let nft = aggregator.borrowNFT(id: nftID)
15+
return nft
16+
}

0 commit comments

Comments
 (0)