Skip to content

Commit

Permalink
remove path functions, add view function, update view resolver names
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuahannan committed Jan 11, 2024
1 parent aebb56b commit c0ff8f7
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 91 deletions.
45 changes: 8 additions & 37 deletions contracts/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,17 @@ access(all) contract ExampleNFT: ViewResolver {
access(self) var storagePath: StoragePath
access(self) var publicPath: PublicPath

/// Return the default storage path for the collection
access(all) view fun getDefaultStoragePath(): StoragePath? {
return self.storagePath
}

/// Return the default public path for the collection
access(all) view fun getDefaultPublicPath(): PublicPath? {
return self.publicPath
}

init () {
self.ownedNFTs <- {}
let identifier = "cadenceExampleNFTCollection"
self.storagePath = StoragePath(identifier: identifier)!
self.publicPath = PublicPath(identifier: identifier)!
}

access(all) view fun getNFTCollectionDataView(): AnyStruct {
return ExampleNFT.resolveContractView(Type<MetadataViews.NFTCollectionData>())
}

/// getSupportedNFTTypes returns a list of NFT types that this receiver accepts
access(all) view fun getSupportedNFTTypes(): {Type: Bool} {
let supportedTypes: {Type: Bool} = {}
Expand Down Expand Up @@ -222,7 +216,7 @@ access(all) contract ExampleNFT: ViewResolver {
/// @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.
///
access(all) view fun getViews(): [Type] {
access(all) view fun getContractViews(): [Type] {
return [
Type<MetadataViews.NFTCollectionData>(),
Type<MetadataViews.NFTCollectionDisplay>()
Expand All @@ -234,45 +228,23 @@ access(all) contract ExampleNFT: ViewResolver {
/// @param view: The Type of the desired view.
/// @return A structure representing the requested view.
///
access(all) fun resolveView(_ view: Type): AnyStruct? {
access(all) fun resolveContractView(_ view: Type): AnyStruct? {
switch view {
case Type<MetadataViews.NFTCollectionData>():
return ExampleNFT.getCollectionData(nftType: Type<@ExampleNFT.NFT>())
case Type<MetadataViews.NFTCollectionDisplay>():
return ExampleNFT.getCollectionDisplay(nftType: Type<@ExampleNFT.NFT>())
}
return nil
}

/// resolve a type to its CollectionData so you know where to store it
/// Returns `nil` if no collection type exists for the specified NFT type
access(all) view fun getCollectionData(nftType: Type): MetadataViews.NFTCollectionData? {
switch nftType {
case Type<@ExampleNFT.NFT>():
let collectionRef = self.account.storage.borrow<&ExampleNFT.Collection>(
from: /storage/cadenceExampleNFTCollection
) ?? panic("Could not borrow a reference to the stored collection")
let collectionData = MetadataViews.NFTCollectionData(
storagePath: collectionRef.getDefaultStoragePath()!,
publicPath: collectionRef.getDefaultPublicPath()!,
providerPath: /private/cadenceExampleNFTCollection,
publicCollection: Type<&ExampleNFT.Collection>(),
publicLinkedType: Type<&ExampleNFT.Collection>(),
providerLinkedType: Type<auth(NonFungibleToken.Withdrawable) &ExampleNFT.Collection>(),
createEmptyCollectionFunction: (fun(): @{NonFungibleToken.Collection} {
return <-ExampleNFT.createEmptyCollection()
})
)
return collectionData
default:
return nil
}
}

/// Returns the CollectionDisplay view for the NFT type that is specified
access(all) view fun getCollectionDisplay(nftType: Type): MetadataViews.NFTCollectionDisplay? {
switch nftType {
case Type<@ExampleNFT.NFT>():
case Type<MetadataViews.NFTCollectionDisplay>():
let media = MetadataViews.Media(
file: MetadataViews.HTTPFile(
url: "https://assets.website-files.com/5f6294c0c7a8cdd643b1c820/5f6294c0c7a8cda55cb1c936_Flow_Wordmark.svg"
Expand All @@ -289,9 +261,8 @@ access(all) contract ExampleNFT: ViewResolver {
"twitter": MetadataViews.ExternalURL("https://twitter.com/flow_blockchain")
}
)
default:
return nil
}
return nil
}

/// Resource that an admin or something similar would own to be
Expand Down
13 changes: 0 additions & 13 deletions contracts/MetadataViews.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,6 @@ access(all) contract MetadataViews {
/// including standard NFT interfaces and metadataviews interfaces
access(all) let publicPath: PublicPath

/// Private path which should be linked to expose the provider
/// capability to withdraw NFTs from the collection holding NFTs
access(all) let providerPath: PrivatePath

/// Public collection type that is expected to provide sufficient read-only access to standard
/// functions (deposit + getIDs + borrowNFT). For new
/// collections, this may be set to be equal to the type specified in `publicLinkedType`.
Expand All @@ -619,33 +615,24 @@ access(all) contract MetadataViews {
/// `NFT.Receiver`, and `ViewResolver.ResolverCollection` interfaces are required.
access(all) let publicLinkedType: Type

/// Type that should be linked at the aforementioned private path. This is normally
/// a restricted type with at a minimum the `NFT.Provider` interface
access(all) let providerLinkedType: Type

/// Function that allows creation of an empty NFT collection that is intended to store
/// this NFT.
access(all) let createEmptyCollection: fun(): @{NonFungibleToken.Collection}

view init(
storagePath: StoragePath,
publicPath: PublicPath,
providerPath: PrivatePath,
publicCollection: Type,
publicLinkedType: Type,
providerLinkedType: Type,
createEmptyCollectionFunction: fun(): @{NonFungibleToken.Collection}
) {
pre {
publicLinkedType.isSubtype(of: Type<&{NonFungibleToken.Receiver, ViewResolver.ResolverCollection}>()): "Public type must include NonFungibleToken.Receiver and ViewResolver.ResolverCollection interfaces."
providerLinkedType.isSubtype(of: Type<&{NonFungibleToken.Provider, ViewResolver.ResolverCollection}>()): "Provider type must include NonFungibleToken.Provider and ViewResolver.ResolverCollection interface."
}
self.storagePath=storagePath
self.publicPath=publicPath
self.providerPath = providerPath
self.publicCollection=publicCollection
self.publicLinkedType=publicLinkedType
self.providerLinkedType = providerLinkedType
self.createEmptyCollection=createEmptyCollectionFunction
}
}
Expand Down
10 changes: 4 additions & 6 deletions contracts/NonFungibleToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,11 @@ access(all) contract NonFungibleToken {
/// Requirement for the concrete resource type
/// to be declared in the implementing contract
///
access(all) resource interface Collection: Provider, Receiver, ViewResolver.ResolverCollection {
access(all) resource interface Collection: Provider, Receiver {

/// Return the default storage path for the collection
access(all) view fun getDefaultStoragePath(): StoragePath?

/// Return the default public path for the collection
access(all) view fun getDefaultPublicPath(): PublicPath?
/// Return the NFT CollectionData View
/// has to be AnyStruct and cast to the view later to avoid circular dependency issues
access(all) view fun getNFTCollectionDataView(): AnyStruct

/// Normally we would require that the collection specify
/// a specific dictionary to store the NFTs, but this isn't necessary any more
Expand Down
18 changes: 12 additions & 6 deletions contracts/UniversalCollection.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,20 @@ access(all) contract UniversalCollection {
access(self) var storagePath: StoragePath
access(self) var publicPath: PublicPath

/// Return the default storage path for the collection
access(all) view fun getDefaultStoragePath(): StoragePath? {
return self.storagePath
access(all) view fun getNFTCollectionDataView(): AnyStruct {
return MetadataViews.NFTCollectionData(
storagePath: StoragePath(identifier: self.identifier)!,
publicPath: PublicPath(identifier: self.identifier)!,
publicCollection: Type<&UniversalCollection.Collection>(),
publicLinkedType: Type<&UniversalCollection.Collection>(),
createEmptyCollectionFunction: (fun(): @{NonFungibleToken.Collection} {
return <-self.createEmptyCollection()
})
)
}

/// Return the default public path for the collection
access(all) view fun getDefaultPublicPath(): PublicPath? {
return self.publicPath
access(all) fun createEmptyCollection(): @{NonFungibleToken.Collection} {
return <- create Collection(identifier: self.identifier, type: self.supportedType)
}

init (identifier: String, type:Type) {
Expand Down
4 changes: 2 additions & 2 deletions contracts/ViewResolver.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ access(all) contract interface ViewResolver {
/// @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.
///
access(all) view fun getViews(): [Type] {
access(all) view fun getContractViews(): [Type] {
return []
}

Expand All @@ -19,7 +19,7 @@ access(all) contract interface ViewResolver {
/// @param view: The Type of the desired view.
/// @return A structure representing the requested view.
///
access(all) fun resolveView(_ view: Type): AnyStruct? {
access(all) fun resolveContractView(_ view: Type): AnyStruct? {
return nil
}

Expand Down
15 changes: 3 additions & 12 deletions contracts/utility/FungibleToken.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,9 @@ access(all) contract FungibleToken {
return self.getSupportedVaultTypes()[type] ?? false
}

/// Returns the storage path where the vault should typically be stored
access(all) view fun getDefaultStoragePath(): StoragePath?

/// Returns the public path where this vault should have a public capability
access(all) view fun getDefaultPublicPath(): PublicPath?

/// Returns the public path where this vault's Receiver should have a public capability
/// Publishing a Receiver Capability at a different path enables alternate Receiver implementations to be used
/// in the same canonical namespace as the underlying Vault.
access(all) view fun getDefaultReceiverPath(): PublicPath? {
return nil
}
/// Returns the FTVaultData view for this Vault, which contains
/// all relevant paths, types, and create vault function
access(all) view fun getFTVaultDataView(): AnyStruct

/// withdraw subtracts `amount` from the Vault's balance
/// and returns a new Vault with the subtracted balance
Expand Down
Loading

0 comments on commit c0ff8f7

Please sign in to comment.