diff --git a/cadence/contracts/example-assets/ExampleNFT.cdc b/cadence/contracts/example-assets/ExampleNFT.cdc index 61f57017..9cfab633 100644 --- a/cadence/contracts/example-assets/ExampleNFT.cdc +++ b/cadence/contracts/example-assets/ExampleNFT.cdc @@ -123,7 +123,7 @@ access(all) contract ExampleNFT: NonFungibleToken { access(all) resource Collection: NonFungibleToken.Collection { /// dictionary of NFT conforming tokens /// NFT is a resource type with an `UInt64` ID field - access(contract) var ownedNFTs: @{UInt64: ExampleNFT.NFT} + access(all) var ownedNFTs: @{UInt64: {NonFungibleToken.NFT}} access(all) var storagePath: StoragePath access(all) var publicPath: PublicPath @@ -187,7 +187,7 @@ access(all) contract ExampleNFT: NonFungibleToken { /// Borrow the view resolver for the specified NFT ID access(all) view fun borrowViewResolver(id: UInt64): &{ViewResolver.Resolver}? { - if let nft = &self.ownedNFTs[id] as &ExampleNFT.NFT? { + if let nft = &self.ownedNFTs[id] as &{NonFungibleToken.NFT}? { return nft as &{ViewResolver.Resolver} } return nil diff --git a/cadence/contracts/standards/NonFungibleToken.cdc b/cadence/contracts/standards/NonFungibleToken.cdc index 1eba76ed..bf6d5f8a 100644 --- a/cadence/contracts/standards/NonFungibleToken.cdc +++ b/cadence/contracts/standards/NonFungibleToken.cdc @@ -36,7 +36,7 @@ Collection to complete the transfer. */ -import ViewResolver from "ViewResolver" +import "ViewResolver" /// The main NFT contract. Other NFT contracts will /// import and implement the interfaces defined in this contract @@ -63,7 +63,7 @@ access(all) contract interface NonFungibleToken: ViewResolver { /// and query the updated metadata from the owners' collections. /// access(all) event Updated(type: String, id: UInt64, uuid: UInt64, owner: Address?) - access(contract) view fun emitNFTUpdated(_ nftRef: auth(Update | Owner) &{NonFungibleToken.NFT}) + access(all) view fun emitNFTUpdated(_ nftRef: auth(Update | Owner) &{NonFungibleToken.NFT}) { emit Updated(type: nftRef.getType().identifier, id: nftRef.id, uuid: nftRef.uuid, owner: nftRef.owner?.address) } @@ -85,9 +85,6 @@ access(all) contract interface NonFungibleToken: ViewResolver { /// access(all) event Deposited(type: String, id: UInt64, uuid: UInt64, to: Address?, collectionUUID: UInt64) - /// Included for backwards-compatibility - access(all) resource interface INFT: NFT {} - /// Interface that the NFTs must conform to /// access(all) resource interface NFT: ViewResolver.Resolver { @@ -105,6 +102,7 @@ access(all) contract interface NonFungibleToken: ViewResolver { access(all) fun createEmptyCollection(): @{Collection} { post { result.getLength() == 0: "The created collection must be empty!" + result.isSupportedNFTType(type: self.getType()): "The created collection must support this NFT type" } } @@ -174,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}? } @@ -182,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}) { @@ -196,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 { + 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, @@ -231,4 +243,4 @@ access(all) contract interface NonFungibleToken: ViewResolver { result.getIDs().length == 0: "The created collection must be empty!" } } -} +} \ No newline at end of file