Skip to content

Commit

Permalink
remove get NFT types, update basic NFT and UniversalCollection, and u…
Browse files Browse the repository at this point in the history
…pdate transactions
  • Loading branch information
joshuahannan committed Jan 22, 2024
1 parent ded9217 commit 3c2b2d2
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 139 deletions.
56 changes: 51 additions & 5 deletions contracts/BasicNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import MetadataViews from "MetadataViews"
import ViewResolver from "ViewResolver"
import UniversalCollection from "UniversalCollection"

access(all) contract BasicNFT {
access(all) contract BasicNFT: NonFungibleToken {

/// The only thing that an NFT really needs to have is this resource definition
access(all) resource NFT: NonFungibleToken.NFT, ViewResolver.Resolver {
access(all) resource NFT: NonFungibleToken.NFT {
/// Arbitrary trait mapping metadata
access(self) let metadata: {String: AnyStruct}

Expand All @@ -33,15 +33,17 @@ access(all) contract BasicNFT {
}

access(all) fun createEmptyCollection(): @{NonFungibleToken.Collection} {
return <- BasicNFT.createEmptyCollection()
return <- BasicNFT.createEmptyCollection(nftType: self.getType())
}

/// Uses the basic NFT views
access(all) view fun getViews(): [Type] {
return [
Type<MetadataViews.Display>(),
Type<MetadataViews.Serial>(),
Type<MetadataViews.Traits>()
Type<MetadataViews.Traits>(),
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>()
]
}

Expand All @@ -61,24 +63,68 @@ access(all) contract BasicNFT {
)
case Type<MetadataViews.Traits>():
return MetadataViews.dictToTraits(dict: self.metadata, excludedNames: nil)
case Type<MetadataViews.NFTCollectionData>():
return BasicNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionData>())
case Type<MetadataViews.NFTCollectionDisplay>():
return BasicNFT.resolveContractView(resourceType: nil, viewType: Type<MetadataViews.NFTCollectionDisplay>())
}
return nil
}
}

access(all) view fun getContractViews(resourceType: Type?): [Type] {
return [
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>()
]
}

access(all) fun resolveContractView(resourceType: Type?, viewType: Type): AnyStruct? {
switch viewType {
case Type<MetadataViews.NFTCollectionData>():
let collectionRef = self.account.storage.borrow<&ExampleNFT.Collection>(
from: /storage/cadenceExampleNFTCollection
) ?? panic("Could not borrow a reference to the stored collection")

return collectionRef.getNFTCollectionDataView()
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 MetadataViews.NFTCollectionDisplay(
name: "The Example Collection",
description: "This collection is used as an example to help you develop your next Flow NFT.",
externalURL: MetadataViews.ExternalURL("https://example-nft.onflow.org"),
squareImage: media,
bannerImage: media,
socials: {
"twitter": MetadataViews.ExternalURL("https://twitter.com/flow_blockchain")
}
)
}
return nil
}

access(all) resource NFTMinter {
access(all) fun mintNFT(metadata: {String: AnyStruct}): @BasicNFT.NFT {
return <- create NFT(metadata: metadata)
}
}

access(all) fun createEmptyCollection(): @{NonFungibleToken.Collection} {
access(all) fun createEmptyCollection(nftType: Type): @{NonFungibleToken.Collection} {
return <- UniversalCollection.createEmptyCollection(identifier: "flowBasicNFTCollection", type: Type<@BasicNFT.NFT>())
}

init() {
let minter <- create NFTMinter()
self.account.storage.save(<-minter, to: /storage/flowBasicNFTMinterPath)

let collection <- self.createEmptyCollection(nftType: Type<@BasicNFT.NFT>()>)
let dataView = collection.getNFTCollectionDataView()
self.account.storage.save(collection, to: dataView.storagePath)
}
}

4 changes: 2 additions & 2 deletions contracts/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import NonFungibleToken from "NonFungibleToken"
import ViewResolver from "ViewResolver"
import MetadataViews from "MetadataViews"

access(all) contract ExampleNFT: ViewResolver {
access(all) contract ExampleNFT: NonFungibleToken {

/// Path where the minter should be stored
/// The standard paths for the collection are stored in the collection resource type
Expand Down Expand Up @@ -229,7 +229,7 @@ access(all) contract ExampleNFT: ViewResolver {
/// @return A structure representing the requested view.
///
access(all) fun resolveContractView(resourceType: Type?, viewType: Type): AnyStruct? {
switch view {
switch viewType {
case Type<MetadataViews.NFTCollectionData>():
let collectionRef = self.account.storage.borrow<&ExampleNFT.Collection>(
from: /storage/cadenceExampleNFTCollection
Expand Down
35 changes: 0 additions & 35 deletions contracts/MultipleNFT.cdc

This file was deleted.

8 changes: 0 additions & 8 deletions contracts/NonFungibleToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -218,14 +218,6 @@ access(all) contract interface NonFungibleToken: ViewResolver {
}
}

/// Function to return the types that the contract implements
/// @return An array of NFT Types that the implementing contract defines.
access(all) view fun getVaultTypes(): [Type] {
post {
result.length > 0: "Must indicate what non-fungible token types this contract defines"
}
}

/// createEmptyCollection creates an empty Collection for the specified NFT type
/// and returns it to the caller so that they can own NFTs
/// @param nftType: The desired nft type to return a collection for.
Expand Down
3 changes: 1 addition & 2 deletions contracts/UniversalCollection.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ access(all) contract UniversalCollection {
}

/// withdraw removes an NFT from the collection and moves it to the caller
access(NonFungibleToken.Withdrawable) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
access(NonFungibleToken.Withdraw | NonFungibleToken.Owner) fun withdraw(withdrawID: UInt64): @{NonFungibleToken.NFT} {
let token <- self.ownedNFTs.remove(key: withdrawID)
?? panic("Could not withdraw an NFT with the ID: ".concat(withdrawID.toString()).concat(" from the collection"))

Expand Down Expand Up @@ -120,5 +120,4 @@ access(all) contract UniversalCollection {
access(all) fun createEmptyCollection(identifier: String, type: Type): @{NonFungibleToken.Collection} {
return <- create Collection(identifier: identifier, type:type)
}

}
7 changes: 0 additions & 7 deletions contracts/utility/FungibleToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,6 @@ access(all) contract interface FungibleToken: ViewResolver {
}
}

/// Function to return the types that the contract implements
access(all) view fun getVaultTypes(): [Type] {
post {
result.length > 0: "Must indicate what fungible token types this contract defines"
}
}

/// createEmptyVault allows any user to create a new Vault that has a zero balance
///
access(all) fun createEmptyVault(vaultType: Type): @{FungibleToken.Vault} {
Expand Down
2 changes: 1 addition & 1 deletion contracts/utility/NFTForwarding.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ access(all) contract NFTForwarding {

/// Resource that forwards deposited NFTs to a designated recipient's Collection
///
access(all) resource NFTForwarder : NonFungibleToken.Receiver {
access(all) resource NFTForwarder: NonFungibleToken.Receiver {

/// Recipient to which NFTs will be forwarded
///
Expand Down
22 changes: 4 additions & 18 deletions lib/go/contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,41 +22,27 @@ var (
)

const (
filenameMultipleNFT = "MultipleNFT.cdc"
filenameNonFungibleToken = "NonFungibleToken.cdc"
filenameExampleNFT = "ExampleNFT.cdc"
filenameMetadataViews = "MetadataViews.cdc"
filenameNFTMetadataViews = "NFTMetadataViews.cdc"
filenameResolver = "ViewResolver.cdc"
filenameViewResolver = "ViewResolver.cdc"
filenameUniversalCollection = "UniversalCollection.cdc"
filenameBasicNFT = "BasicNFT.cdc"
filenameFungibleToken = "utility/FungibleToken.cdc"
)

func MultipleNFT(nftAddress flow.Address) []byte {
code := assets.MustAssetString(filenameMultipleNFT)
code = placeholderNonFungibleToken.ReplaceAllString(code, "0x"+nftAddress.String())
return []byte(code)
}

// NonFungibleToken returns the NonFungibleToken contract interface.
func NonFungibleToken(resolverAddress flow.Address) []byte {
code := assets.MustAssetString(filenameNonFungibleToken)
code = placeholderResolver.ReplaceAllString(code, "0x"+resolverAddress.String())
return []byte(code)
}

// NonFungibleToken returns the NonFungibleToken contract interface.
func NonFungibleTokenV2(resolverAddress flow.Address) []byte {
code := assets.MustAssetString(filenameNonFungibleToken)
code = placeholderResolver.ReplaceAllString(code, "0x"+resolverAddress.String())
return []byte(code)
}

// ExampleNFT returns the ExampleNFT contract.
//
// The returned contract will import the NonFungibleToken contract from the specified address.
func ExampleNFT(nftAddress, metadataAddress, resolverAddress, multipleNFTAddress flow.Address) []byte {
func ExampleNFT(nftAddress, metadataAddress, resolverAddress flow.Address) []byte {
code := assets.MustAssetString(filenameExampleNFT)

code = placeholderNonFungibleToken.ReplaceAllString(code, "0x"+nftAddress.String())
Expand All @@ -76,8 +62,8 @@ func MetadataViews(ftAddress, nftAddress, resolverAddress flow.Address) []byte {
return []byte(code)
}

func Resolver() []byte {
code := assets.MustAssetString(filenameResolver)
func ViewResolver() []byte {
code := assets.MustAssetString(filenameViewResolver)
return []byte(code)
}

Expand Down
Loading

0 comments on commit 3c2b2d2

Please sign in to comment.