Skip to content

Commit

Permalink
Update the core contracts to their latest version (#84)
Browse files Browse the repository at this point in the history
* update the core contracts

* fix test cases

* minor fix
  • Loading branch information
satyamakgec authored Apr 13, 2023
1 parent e45f660 commit 5d0c203
Show file tree
Hide file tree
Showing 12 changed files with 599 additions and 99 deletions.
140 changes: 125 additions & 15 deletions contracts/utility/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,37 @@

import NonFungibleToken from "./NonFungibleToken.cdc"
import MetadataViews from "./MetadataViews.cdc"
import ViewResolver from "./ViewResolver.cdc"

pub contract ExampleNFT: NonFungibleToken {
pub contract ExampleNFT: NonFungibleToken, ViewResolver {

/// Total supply of ExampleNFTs in existence
pub var totalSupply: UInt64

/// The event that is emitted when the contract is created
pub event ContractInitialized()

/// The event that is emitted when an NFT is withdrawn from a Collection
pub event Withdraw(id: UInt64, from: Address?)

/// The event that is emitted when an NFT is deposited to a Collection
pub event Deposit(id: UInt64, to: Address?)

/// Storage and Public Paths
pub let CollectionStoragePath: StoragePath
pub let CollectionPublicPath: PublicPath
pub let MinterStoragePath: StoragePath

/// The core resource that represents a Non Fungible Token.
/// New instances will be created using the NFTMinter resource
/// and stored in the Collection resource
///
pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver {

/// The unique ID that each NFT has
pub let id: UInt64

/// Metadata fields
pub let name: String
pub let description: String
pub let thumbnail: String
Expand All @@ -48,7 +63,12 @@ pub contract ExampleNFT: NonFungibleToken {
self.royalties = royalties
self.metadata = metadata
}


/// Function that returns all the Metadata Views implemented by a Non Fungible Token
///
/// @return An array of Types defining the implemented views. This value will be used by
/// developers to know which parameter to pass to the resolveView() method.
///
pub fun getViews(): [Type] {
return [
Type<MetadataViews.Display>(),
Expand All @@ -62,6 +82,11 @@ pub contract ExampleNFT: NonFungibleToken {
]
}

/// Function that resolves a metadata view for this token.
///
/// @param view: The Type of the desired view.
/// @return A structure representing the requested view.
///
pub fun resolveView(_ view: Type): AnyStruct? {
switch view {
case Type<MetadataViews.Display>():
Expand Down Expand Up @@ -140,6 +165,8 @@ pub contract ExampleNFT: NonFungibleToken {
}
}

/// Defines the methods that are particular to this NFT contract collection
///
pub resource interface ExampleNFTCollectionPublic {
pub fun deposit(token: @NonFungibleToken.NFT)
pub fun getIDs(): [UInt64]
Expand All @@ -152,6 +179,10 @@ pub contract ExampleNFT: NonFungibleToken {
}
}

/// The resource that will be holding the NFTs inside any account.
/// In order to be able to manage NFTs any account will need to create
/// an empty collection first
///
pub resource Collection: ExampleNFTCollectionPublic, NonFungibleToken.Provider, NonFungibleToken.Receiver, NonFungibleToken.CollectionPublic, MetadataViews.ResolverCollection {
// dictionary of NFT conforming tokens
// NFT is a resource type with an `UInt64` ID field
Expand All @@ -161,7 +192,11 @@ pub contract ExampleNFT: NonFungibleToken {
self.ownedNFTs <- {}
}

// withdraw removes an NFT from the collection and moves it to the caller
/// Removes an NFT from the collection and moves it to the caller
///
/// @param withdrawID: The ID of the NFT that wants to be withdrawn
/// @return The NFT resource that has been taken out of the collection
///
pub fun withdraw(withdrawID: UInt64): @NonFungibleToken.NFT {
let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")

Expand All @@ -170,8 +205,10 @@ pub contract ExampleNFT: NonFungibleToken {
return <-token
}

// deposit takes a NFT and adds it to the collections dictionary
// and adds the ID to the id array
/// Adds an NFT to the collections dictionary and adds the ID to the id array
///
/// @param token: The NFT resource to be included in the collection
///
pub fun deposit(token: @NonFungibleToken.NFT) {
let token <- token as! @ExampleNFT.NFT

Expand All @@ -185,17 +222,30 @@ pub contract ExampleNFT: NonFungibleToken {
destroy oldToken
}

// getIDs returns an array of the IDs that are in the collection
/// Helper method for getting the collection IDs
///
/// @return An array containing the IDs of the NFTs in the collection
///
pub fun getIDs(): [UInt64] {
return self.ownedNFTs.keys
}

// borrowNFT gets a reference to an NFT in the collection
// so that the caller can read its metadata and call its methods
/// Gets a reference to an NFT in the collection so that
/// the caller can read its metadata and call its methods
///
/// @param id: The ID of the wanted NFT
/// @return A reference to the wanted NFT resource
///
pub fun borrowNFT(id: UInt64): &NonFungibleToken.NFT {
return (&self.ownedNFTs[id] as &NonFungibleToken.NFT?)!
}

/// Gets a reference to an NFT in the collection so that
/// the caller can read its metadata and call its methods
///
/// @param id: The ID of the wanted NFT
/// @return A reference to the wanted NFT resource
///
pub fun borrowExampleNFT(id: UInt64): &ExampleNFT.NFT? {
if self.ownedNFTs[id] != nil {
// Create an authorized reference to allow downcasting
Expand All @@ -206,6 +256,13 @@ pub contract ExampleNFT: NonFungibleToken {
return nil
}

/// Gets a reference to the NFT only conforming to the `{MetadataViews.Resolver}`
/// interface so that the caller can retrieve the views that the NFT
/// is implementing and resolve them
///
/// @param id: The ID of the wanted NFT
/// @return The resource reference conforming to the Resolver interface
///
pub fun borrowViewResolver(id: UInt64): &AnyResource{MetadataViews.Resolver} {
let nft = (&self.ownedNFTs[id] as auth &NonFungibleToken.NFT?)!
let exampleNFT = nft as! &ExampleNFT.NFT
Expand All @@ -217,18 +274,28 @@ pub contract ExampleNFT: NonFungibleToken {
}
}

// public function that anyone can call to create a new empty collection
/// Allows anyone to create a new empty collection
///
/// @return The new Collection resource
///
pub fun createEmptyCollection(): @NonFungibleToken.Collection {
return <- create Collection()
}

// Resource that an admin or something similar would own to be
// able to mint new NFTs
//
/// Resource that an admin or something similar would own to be
/// able to mint new NFTs
///
pub resource NFTMinter {

// mintNFT mints a new NFT with a new ID
// and deposit it in the recipients collection using their collection reference
/// Mints a new NFT with a new ID and deposit it in the
/// recipients collection using their collection reference
///
/// @param recipient: A capability to the collection where the new NFT will be deposited
/// @param name: The name for the NFT metadata
/// @param description: The description for the NFT metadata
/// @param thumbnail: The thumbnail for the NFT metadata
/// @param royalties: An array of Royalty structs, see MetadataViews docs
///
pub fun mintNFT(
recipient: &{NonFungibleToken.CollectionPublic},
name: String,
Expand Down Expand Up @@ -262,6 +329,48 @@ pub contract ExampleNFT: NonFungibleToken {
}
}

/// Function that resolves a metadata view for this contract.
///
/// @param view: The Type of the desired view.
/// @return A structure representing the requested view.
///
pub fun resolveView(_ view: Type): AnyStruct? {
switch view {
case Type<MetadataViews.NFTCollectionData>():
return MetadataViews.NFTCollectionData(
storagePath: ExampleNFT.CollectionStoragePath,
publicPath: ExampleNFT.CollectionPublicPath,
providerPath: /private/exampleNFTCollection,
publicCollection: Type<&ExampleNFT.Collection{ExampleNFT.ExampleNFTCollectionPublic}>(),
publicLinkedType: Type<&ExampleNFT.Collection{ExampleNFT.ExampleNFTCollectionPublic,NonFungibleToken.CollectionPublic,NonFungibleToken.Receiver,MetadataViews.ResolverCollection}>(),
providerLinkedType: Type<&ExampleNFT.Collection{ExampleNFT.ExampleNFTCollectionPublic,NonFungibleToken.CollectionPublic,NonFungibleToken.Provider,MetadataViews.ResolverCollection}>(),
createEmptyCollectionFunction: (fun (): @NonFungibleToken.Collection {
return <-ExampleNFT.createEmptyCollection()
})
)
case Type<MetadataViews.NFTCollectionDisplay>():
let media = MetadataViews.Media(
file: MetadataViews.HTTPFile(
url: "https://assets.website-files.com/5f6294c0c7a8cdd643b1c820/5f6294c0c7a8cda55cb1c936_Flow_Wordmark.svg"
),
mediaType: "image/svg+xml"
)
}
return nil
}

/// Function that returns all the Metadata Views implemented by a Non Fungible Token
///
/// @return An array of Types defining the implemented views. This value will be used by
/// developers to know which parameter to pass to the resolveView() method.
///
pub fun getViews(): [Type] {
return [
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>()
]
}

init() {
// Initialize the total supply
self.totalSupply = 0
Expand All @@ -287,4 +396,5 @@ pub contract ExampleNFT: NonFungibleToken {

emit ContractInitialized()
}
}
}

Loading

0 comments on commit 5d0c203

Please sign in to comment.