Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NFT Storage Requirement to Collection and forEachID() #211

Merged
merged 5 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions contracts/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ access(all) contract ExampleNFT: NonFungibleToken {
access(all) resource Collection: NonFungibleToken.Collection, ExampleNFTCollectionPublic {
/// dictionary of NFT conforming tokens
/// NFT is a resource type with an `UInt64` ID field
access(contract) var ownedNFTs: @{UInt64: {NonFungibleToken.NFT}}
access(all) var ownedNFTs: @{UInt64: {NonFungibleToken.NFT}}
joshuahannan marked this conversation as resolved.
Show resolved Hide resolved

init () {
self.ownedNFTs <- {}
Expand All @@ -150,11 +150,7 @@ 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
}
return type == Type<@ExampleNFT.NFT>()
}

/// withdraw removes an NFT from the collection and moves it to the caller
Expand Down
16 changes: 15 additions & 1 deletion 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,10 @@ access(all) contract interface NonFungibleToken: ViewResolver {
///
access(all) resource interface Collection: Provider, Receiver, CollectionPublic, ViewResolver.ResolverCollection {

/// Cadence allows implementing types to specify less restrictive access
/// so implementing contracts can have this as `access(all)` with no problem
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 @@ -194,7 +199,16 @@ access(all) contract interface NonFungibleToken: ViewResolver {

/// Gets the amount of NFTs stored in the collection
/// @return An integer indicating the size of the collection
access(all) view fun getLength(): Int
access(all) view fun getLength(): Int {
return self.ownedNFTs.length
}

/// Allows a given function to iterate through the list
/// of owned NFT IDs in a collection without first
/// having to load the entire list into memory
access(all) fun forEachID(_ f: fun (UInt64): Bool): Void {
joshuahannan marked this conversation as resolved.
Show resolved Hide resolved
self.ownedNFTs.forEachKey(f)
joshuahannan marked this conversation as resolved.
Show resolved Hide resolved
}

/// Borrows a reference to an NFT stored in the collection
/// If the NFT with the specified ID is not in the collection,
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 nftRefArrayLength = scriptResult.returnValue! as! Int
Test.assertEqual(1, nftRefArrayLength)
}

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): Int {

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.length
}
Loading