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 all 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.

35 changes: 29 additions & 6 deletions lib/go/templates/internal/assets/assets.go

Large diffs are not rendered by default.

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
5 changes: 1 addition & 4 deletions transactions/generic_transfer_with_address.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ transaction(to: Address, id: UInt64, contractAddress: Address, contractName: Str
let recipient = getAccount(to)

// borrow a public reference to the receivers collection
let receiverCap = recipient.capabilities.get<&{NonFungibleToken.Receiver}>(self.collectionData.publicPath)
?? panic("Could not get the recipient's Receiver Capability")

let receiverRef = receiverCap.borrow()
let receiverRef = recipient.capabilities.borrow<&{NonFungibleToken.Receiver}>(self.collectionData.publicPath)
?? panic("Could not borrow reference to the recipient's receiver")

// Deposit the NFT to the receiver
Expand Down
5 changes: 1 addition & 4 deletions transactions/generic_transfer_with_paths.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ transaction(to: Address, id: UInt64, senderPathIdentifier: String, receiverPathI
let recipient = getAccount(to)

// borrow a public reference to the receivers collection
let receiverCap = recipient.capabilities.get<&{NonFungibleToken.Receiver}>(publicPath)
?? panic("Could not get the recipient's Receiver Capability")

let receiverRef = receiverCap.borrow()
let receiverRef = recipient.capabilities.borrow<&{NonFungibleToken.Receiver}>(publicPath)
?? panic("Could not borrow reference to the recipient's receiver")

// Deposit the NFT to the receiver
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