Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update PDS & PackNFT contracts & txs to Cadence 1.0 #47

Merged
merged 15 commits into from
Apr 12, 2024

Conversation

loic1
Copy link
Contributor

@loic1 loic1 commented Mar 18, 2024

Changes:

  • Update PDS & PackNFT contracts and transactions to Cadence 1.0 / NFT Standard V2, including:
    • Create new entitlements:
      • Operatable for PackNFT operations.
      • DistCreation for distribution creation.
    • Update and add collection methods to enable compliance with NFT Standard v2.
  • Update test setup to fit with latest Cadence 1.0-based emulator, tooling, and standard contracts.

State migration from existing pre-1.0 contract is currently blocked by onflow/cadence#3182.

Capabilities migration will require more attention.

For context on Cadence 1.0:

@loic1 loic1 changed the base branch from main to stable-cadence March 18, 2024 19:16
pds/contracts/IPackNFT.cdc Outdated Show resolved Hide resolved
pub case Opened
}
// Enums cannot be declared anymore in interfaces in Cadence 1.0
// access(all) enum Status: UInt8 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Status enum remain needed in PackNFT contracts, but we remove it in IPackNFT because concrete type declarations are not allowed in contract interfaces anymore

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to know. We can just remove it then


access(all) contract PDS{
/// Entitlement that grants the ability to create a distribution
access(all) entitlement DistCreation
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New entitlement to control access for distribution creation, replacing dist creator resource interface in pre-Cadence 1.0 contract

access(self) let withdrawCap: Capability<&{NonFungibleToken.Provider}>
access(self) let operatorCap: Capability<&{IPackNFT.IOperator}>
access(all) resource SharedCapabilities {
access(self) let withdrawCap: Capability<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withdrawCap & operatorCap must now allow access to authorized references to the NFT Provider & PackNFT Operator, respectively.

pre {
cap.borrow() != nil: "Invalid capability"
}
self.cap = cap
}

pub fun create(sharedCap: @SharedCapabilities, title: String, metadata: {String: String}) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create is now a reserved keyword


pub fun reveal(openRequest: Bool){
access(NonFungibleToken.Update | NonFungibleToken.Owner) fun reveal(openRequest: Bool){
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revealing and opening a pack NFT requires NonFungibleToken.Update- or NonFungibleToken.Owner-authorized reference

PackNFT.totalSupply = PackNFT.totalSupply - (1 as UInt64)
/// Event emitted when a NFT is destroyed (replaces Burned event before Cadence 1.0 update)
///
access(all) event ResourceDestroyed(id: UInt64 = self.id)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the custom destroy function being removed from Cadence 1.0:

  • NFT destruction should now be tracked by the special event with the reserved name ResourceDestroyed.
  • TotalSupply will not account for NFTs being destroyed anymore.
  • The corresponding PackNFT resource will have to be deployed in a separate call (e.g., in the same tx or function call where the NFT is being destroyed).

@loic1 loic1 marked this pull request as ready for review March 20, 2024 20:31
@loic1 loic1 requested review from a team as code owners March 20, 2024 20:31
Copy link
Contributor

@joshuahannan joshuahannan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only reviewed for Cadence best practices. Didn't really review the business logic, transactions, or tests. Mostly looks good! Just a few small comments

pds/contracts/IPackNFT.cdc Outdated Show resolved Hide resolved
pds/contracts/PDS.cdc Show resolved Hide resolved
Comment on lines 195 to 203
access(all) view fun getViews(): [Type] {
return []
}

/// Resolve this NFT's metadata views.
///
access(all) view fun resolveView(_ view: Type): AnyStruct? {
return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that this can't be represented by any views? I would think that at the minimum, you would at least want the NFTCollectionData and NFTCollectionDisplay views, but there might be even more that apply

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The views depend on each individual PackNFT instance. The AllDay version of the PackNFT contract updated in this PR defines the views. Added a comment in the vanilla PackNFT contract to make it clear that the views should be implemented 👍

pds/contracts/PackNFT.cdc Outdated Show resolved Hide resolved
pds/contracts/PackNFT.cdc Outdated Show resolved Hide resolved

access(contract) fun reveal(id: UInt64, nfts: [{IPackNFT.Collectible}], salt: String)
access(contract) fun open(id: UInt64, nfts: [{IPackNFT.Collectible}])
init(commitHash: String, issuer: Address)
view init(commitHash: String, issuer: Address)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this init method read-only? this is a bit confusing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was confused too initially but the view keyword means the method is read-only with respect to the state outside of the method itself 👍 (for more details: https://cadence-lang.org/docs/1.0/language/functions#view-functions)

pub case Opened
}
// Enums cannot be declared anymore in interfaces in Cadence 1.0
// access(all) enum Status: UInt8 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to know. We can just remove it then

version: version,
)
} else {
log("has contract")
owner.contracts.update__experimental(name: contractName, code: code.decodeHex())
owner.contracts.add(name: contractName, code: code.decodeHex())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be an update?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, fixed it - thank you!

Copy link
Collaborator

@judezhu judezhu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work!

@loic1 loic1 merged commit 70d1970 into stable-cadence Apr 12, 2024
1 check passed
@loic1 loic1 deleted the loic/update-pds-packnft-contracts-v1 branch April 12, 2024 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants