Skip to content

Commit

Permalink
add NFT storage requirement and forEachID function
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahannan committed Apr 11, 2024
1 parent cd08f8b commit 47b5bfc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 11 deletions.
10 changes: 5 additions & 5 deletions contracts/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ access(all) contract ExampleNFT: NonFungibleToken {
/// Returns whether or not the given type is accepted by the collection
/// A collection that can accept any type should just return true by default
access(all) view fun isSupportedNFTType(type: Type): Bool {
if type == Type<@ExampleNFT.NFT>() {
return true
} else {
return false
}
if type == Type<@ExampleNFT.NFT>() {
return true
} else {
return false
}
}

/// withdraw removes an NFT from the collection and moves it to the caller
Expand Down
10 changes: 10 additions & 0 deletions contracts/NonFungibleToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ access(all) contract interface NonFungibleToken: ViewResolver {
access(all) fun deposit(token: @{NFT})
access(all) view fun getLength(): Int
access(all) view fun getIDs(): [UInt64]
access(all) fun forEachID(_ f: fun (UInt64): Bool): Void
access(all) view fun borrowNFT(_ id: UInt64): &{NFT}?
}

Expand All @@ -180,6 +181,8 @@ access(all) contract interface NonFungibleToken: ViewResolver {
///
access(all) resource interface Collection: Provider, Receiver, CollectionPublic, ViewResolver.ResolverCollection {

access(contract) var ownedNFTs: @{UInt64: {NonFungibleToken.NFT}}

/// deposit takes a NFT as an argument and stores it in the collection
/// @param token: The NFT to deposit into the collection
access(all) fun deposit(token: @{NonFungibleToken.NFT}) {
Expand All @@ -196,6 +199,13 @@ access(all) contract interface NonFungibleToken: ViewResolver {
/// @return An integer indicating the size of the collection
access(all) view fun getLength(): Int

/// Returns an iterator that allows callers to iterate
/// through the list of owned NFT IDs in a collection
/// without having to load the entire list first
access(all) fun forEachID(_ f: fun (UInt64): Bool): Void {
self.ownedNFTs.forEachKey(f)
}

/// Borrows a reference to an NFT stored in the collection
/// If the NFT with the specified ID is not in the collection,
/// the function should return `nil` and not panic.
Expand Down
12 changes: 6 additions & 6 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions lib/go/templates/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions tests/example_nft_test.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,18 @@ fun testGetCollectionLength() {
Test.assertEqual(1, collectionLength)
}

access(all)
fun testGetIterator() {
let scriptResult = executeScript(
"../transactions/scripts/iterate_ids.cdc",
[admin.address, 10]
)
Test.expect(scriptResult, Test.beSucceeded())

let nftRefArray = scriptResult.returnValue! as! [&{NonFungibleToken.NFT}]
Test.assertEqual(1, nftRefArray.length)
}

access(all)
fun testGetContractStoragePath() {
let scriptResult = executeScript(
Expand Down
31 changes: 31 additions & 0 deletions transactions/scripts/iterate_ids.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import "NonFungibleToken"

access(all) fun main(ownerAddress: Address, limit: Int): [&{NonFungibleToken.NFT}] {

let response: [&{NonFungibleToken.NFT}] = []

let account = getAuthAccount<auth(BorrowValue) &Account>(ownerAddress)

account.storage.forEachStored(fun (path: StoragePath, type: Type): Bool {

if (!type.isSubtype(of: Type<@{NonFungibleToken.Collection}>())) {

return true
}

let storageCollection = account.storage.borrow<&{NonFungibleToken.Collection}>(from: path)!

storageCollection.forEachID(fun (nftId: UInt64): Bool {

let nft = storageCollection.borrowNFT(nftId)!

response.append(nft)

return response.length < limit
})

return response.length < limit
})

return response
}

0 comments on commit 47b5bfc

Please sign in to comment.