Skip to content

Commit

Permalink
update NFT standard and ExampleNFT implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Apr 23, 2024
1 parent 01d3988 commit b69b907
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cadence/contracts/example-assets/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
26 changes: 19 additions & 7 deletions cadence/contracts/standards/NonFungibleToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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"
}
}

Expand Down Expand Up @@ -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}?
}

Expand All @@ -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}) {
Expand All @@ -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,
Expand Down Expand Up @@ -231,4 +243,4 @@ access(all) contract interface NonFungibleToken: ViewResolver {
result.getIDs().length == 0: "The created collection must be empty!"
}
}
}
}

0 comments on commit b69b907

Please sign in to comment.