Skip to content

Commit

Permalink
Merge pull request #53 from dapperlabs/judez/added-embed-pds
Browse files Browse the repository at this point in the history
Updated PDS/IPackNFT/PackNFT contract
  • Loading branch information
judezhu authored Jun 28, 2024
2 parents 0551248 + d342b27 commit 9cb77cc
Show file tree
Hide file tree
Showing 34 changed files with 458 additions and 312 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/dapperlabs/studio-platform-smart-contracts

go 1.20
Empty file added go.sum
Empty file.
18 changes: 7 additions & 11 deletions pds/contracts/IPackNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,17 @@ access(all) contract interface IPackNFT{
access(all) resource interface NFT: NonFungibleToken.NFT, IPackNFTToken, IPackNFTOwnerOperator {
access(all) let id: UInt64
access(all) let issuer: Address
access(NonFungibleToken.Update) fun reveal(openRequest: Bool)
access(NonFungibleToken.Update) fun open()
}

// Included for backwards compatibility
access(all) resource interface IPackNFTOwnerOperator{}
access(all) resource interface IPackNFTCollectionPublic {}

/// Emit a RevealRequest event to signal a Sealed Pack NFT should be revealed
/// Resource interface for PackNFT Collection
///
access(contract) fun revealRequest(id: UInt64, openRequest: Bool)
access(all) resource interface IPackNFTCollectionPublic: NonFungibleToken.Collection {
access(NonFungibleToken.Update) fun emitRevealRequestEvent(id: UInt64, openRequest: Bool)
access(NonFungibleToken.Update) fun emitOpenRequestEvent(id: UInt64)
}

/// Emit an OpenRequest event to signal a Revealed Pack NFT should be opened
///
access(contract) fun openRequest(id: UInt64)
// Included for backwards compatibility
access(all) resource interface IPackNFTOwnerOperator{}

/// Reveal a Sealed Pack NFT
///
Expand Down
30 changes: 16 additions & 14 deletions pds/contracts/PDS.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import IPackNFT from "IPackNFT"
/// The Pack Distribution Service (PDS) contract is responsible for creating and managing distributions of packs.
///
access(all) contract PDS{
/// Entitlement that grants the ability to create a distribution.
/// Entitlement that grants the ability to operate PDS functionalities.
///
access(all) entitlement CreateDist
access(all) entitlement Operate

access(all) var version: String
access(all) let PackIssuerStoragePath: StoragePath
Expand Down Expand Up @@ -49,7 +49,7 @@ access(all) contract PDS{
access(all) let metadata: {String: String}
access(all) var state: PDS.DistState

access(all) fun setState(newState: PDS.DistState) {
access(contract) fun setState(newState: PDS.DistState) {
self.state = newState
}

Expand Down Expand Up @@ -113,14 +113,14 @@ access(all) contract PDS{

/// Withdraw an NFT from the issuer.
///
access(all) fun withdrawFromIssuer(withdrawID: UInt64): @{NonFungibleToken.NFT} {
access(contract) fun withdrawFromIssuer(withdrawID: UInt64): @{NonFungibleToken.NFT} {
let c = self.withdrawCap.borrow() ?? panic("no such cap")
return <- c.withdraw(withdrawID: withdrawID)
}

/// Mint Pack NFTs.
///
access(all) fun mintPackNFT(distId: UInt64, commitHashes: [String], issuer: Address, recvCap: &{NonFungibleToken.CollectionPublic}) {
access(contract) fun mintPackNFT(distId: UInt64, commitHashes: [String], issuer: Address, recvCap: &{NonFungibleToken.CollectionPublic}) {
var i = 0
let c = self.operatorCap.borrow() ?? panic("no such cap")
while i < commitHashes.length{
Expand All @@ -133,14 +133,14 @@ access(all) contract PDS{

/// Reveal Pack NFTs.
///
access(all) fun revealPackNFT(packId: UInt64, nfts: [{IPackNFT.Collectible}], salt: String) {
access(contract) fun revealPackNFT(packId: UInt64, nfts: [{IPackNFT.Collectible}], salt: String) {
let c = self.operatorCap.borrow() ?? panic("no such cap")
c.reveal(id: packId, nfts: nfts, salt: salt)
}

/// Open Pack NFTs.
///
access(all) fun openPackNFT(packId: UInt64, nfts: [{IPackNFT.Collectible}], recvCap: &{NonFungibleToken.CollectionPublic}, collectionStoragePath: StoragePath) {
access(contract) fun openPackNFT(packId: UInt64, nfts: [{IPackNFT.Collectible}], recvCap: &{NonFungibleToken.CollectionPublic}, collectionStoragePath: StoragePath) {
let c = self.operatorCap.borrow() ?? panic("no such cap")
let toReleaseNFTs: [UInt64] = []
var i = 0
Expand Down Expand Up @@ -172,14 +172,16 @@ access(all) contract PDS{
access(all) resource PackIssuer: PackIssuerCapReciever {
access(self) var cap: Capability<&DistributionCreator>?

/// Set the capability to create a distribution; the function is publicly accessible but requires a capability argument to a DistrubutionCreator admin resource.
///
access(all) fun setDistCap(cap: Capability<&DistributionCreator>) {
pre {
cap.check(): "Invalid capability"
}
self.cap = cap
}

access(CreateDist) fun createDist(sharedCap: @SharedCapabilities, title: String, metadata: {String: String}) {
access(Operate) fun createDist(sharedCap: @SharedCapabilities, title: String, metadata: {String: String}) {
assert(title.length > 0, message: "Title must not be empty")
let c = self.cap!.borrow()!
c.createNewDist(sharedCap: <- sharedCap, title: title, metadata: metadata)
Expand All @@ -198,7 +200,7 @@ access(all) contract PDS{
/// Resource that defines the creator of a distribution.
///
access(all) resource DistributionCreator: IDistCreator {
access(all) fun createNewDist(sharedCap: @SharedCapabilities, title: String, metadata: {String: String}) {
access(contract) fun createNewDist(sharedCap: @SharedCapabilities, title: String, metadata: {String: String}) {
let currentId = PDS.nextDistId
PDS.DistSharedCap[currentId] <-! sharedCap
PDS.Distributions[currentId] = DistInfo(title: title, metadata: metadata)
Expand All @@ -210,14 +212,14 @@ access(all) contract PDS{
/// Resource that defines the manager of a distribution.
///
access(all) resource DistributionManager {
access(all) fun updateDistState(distId: UInt64, state: PDS.DistState) {
access(Operate) fun updateDistState(distId: UInt64, state: PDS.DistState) {
let d = PDS.Distributions.remove(key: distId) ?? panic ("No such distribution")
d.setState(newState: state)
PDS.Distributions.insert(key: distId, d)
emit DistributionStateUpdated(DistId: distId, state: state.rawValue)
}

access(all) fun withdraw(distId: UInt64, nftIDs: [UInt64], escrowCollectionPublic: PublicPath) {
access(Operate) fun withdraw(distId: UInt64, nftIDs: [UInt64], escrowCollectionPublic: PublicPath) {
assert(PDS.DistSharedCap.containsKey(distId), message: "No such distribution")
let d <- PDS.DistSharedCap.remove(key: distId)!
let pdsCollection = PDS.getManagerCollectionCap(escrowCollectionPublic: escrowCollectionPublic).borrow()!
Expand All @@ -230,14 +232,14 @@ access(all) contract PDS{
PDS.DistSharedCap[distId] <-! d
}

access(all) fun mintPackNFT(distId: UInt64, commitHashes: [String], issuer: Address, recvCap: &{NonFungibleToken.CollectionPublic}) {
access(Operate) fun mintPackNFT(distId: UInt64, commitHashes: [String], issuer: Address, recvCap: &{NonFungibleToken.CollectionPublic}) {
assert(PDS.DistSharedCap.containsKey(distId), message: "No such distribution")
let d <- PDS.DistSharedCap.remove(key: distId)!
d.mintPackNFT(distId: distId, commitHashes: commitHashes, issuer: issuer, recvCap: recvCap)
PDS.DistSharedCap[distId] <-! d
}

access(all) fun revealPackNFT(distId: UInt64, packId: UInt64, nftContractAddrs: [Address], nftContractNames: [String], nftIds: [UInt64], salt: String) {
access(Operate) fun revealPackNFT(distId: UInt64, packId: UInt64, nftContractAddrs: [Address], nftContractNames: [String], nftIds: [UInt64], salt: String) {
assert(PDS.DistSharedCap.containsKey(distId), message: "No such distribution")
assert(
nftContractAddrs.length == nftContractNames.length &&
Expand All @@ -256,7 +258,7 @@ access(all) contract PDS{
PDS.DistSharedCap[distId] <-! d
}

access(all) fun openPackNFT(
access(Operate) fun openPackNFT(
distId: UInt64,
packId: UInt64,
nftContractAddrs: [Address],
Expand Down
52 changes: 20 additions & 32 deletions pds/contracts/PackNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -153,18 +153,6 @@ access(all) contract PackNFT: NonFungibleToken, IPackNFT {
/// This NFT's issuer.
access(all) let issuer: Address

/// Reveal a Sealed Pack resource.
///
access(NonFungibleToken.Update) fun reveal(openRequest: Bool) {
PackNFT.revealRequest(id: self.id, openRequest: openRequest)
}

/// Open a Revealed Pack resource.
///
access(NonFungibleToken.Update) fun open() {
PackNFT.openRequest(id: self.id)
}

/// Event emitted when a NFT is destroyed (replaces Burned event before Cadence 1.0 update)
///
access(all) event ResourceDestroyed(id: UInt64 = self.id)
Expand Down Expand Up @@ -241,6 +229,26 @@ access(all) contract PackNFT: NonFungibleToken, IPackNFT {
destroy oldToken
}

/// Emit a RevealRequest event to signal a Sealed Pack NFT should be revealed.
///
access(NonFungibleToken.Update) fun emitRevealRequestEvent(id: UInt64, openRequest: Bool) {
pre {
self.borrowNFT(id) != nil: "NFT with provided ID must exist in the collection"
PackNFT.borrowPackRepresentation(id: id)!.status.rawValue == Status.Sealed.rawValue: "Pack status must be Sealed for reveal request"
}
emit RevealRequest(id: id, openRequest: openRequest)
}

/// Emit an OpenRequest event to signal a Revealed Pack NFT should be opened.
///
access(NonFungibleToken.Update) fun emitOpenRequestEvent(id: UInt64) {
pre {
self.borrowNFT(id) != nil: "NFT with provided ID must exist in the collection"
PackNFT.borrowPackRepresentation(id: id)!.status.rawValue == Status.Revealed.rawValue: "Pack status must be Revealed for open request"
}
emit OpenRequest(id: id)
}

/// Return an array of the IDs that are in the collection.
///
access(all) view fun getIDs(): [UInt64] {
Expand Down Expand Up @@ -283,22 +291,6 @@ access(all) contract PackNFT: NonFungibleToken, IPackNFT {
}
}

/// Emit a RevealRequest event to signal a Sealed Pack NFT should be revealed.
///
access(contract) fun revealRequest(id: UInt64, openRequest: Bool ) {
let p = PackNFT.borrowPackRepresentation(id: id) ?? panic ("No such pack")
assert(p.status.rawValue == Status.Sealed.rawValue, message: "Pack status must be Sealed for reveal request")
emit RevealRequest(id: id, openRequest: openRequest)
}

/// Emit an OpenRequest event to signal a Revealed Pack NFT should be opened.
///
access(contract) fun openRequest(id: UInt64) {
let p = PackNFT.borrowPackRepresentation(id: id) ?? panic ("No such pack")
assert(p.status.rawValue == Status.Revealed.rawValue, message: "Pack status must be Revealed for open request")
emit OpenRequest(id: id)
}

/// Reveal a Sealed Pack NFT.
///
access(all) fun publicReveal(id: UInt64, nfts: [{IPackNFT.Collectible}], salt: String) {
Expand Down Expand Up @@ -395,10 +387,6 @@ access(all) contract PackNFT: NonFungibleToken, IPackNFT {
self.account.capabilities.storage.issue<&{NonFungibleToken.CollectionPublic}>(self.CollectionStoragePath),
at: self.CollectionPublicPath
)
self.account.capabilities.publish(
self.account.capabilities.storage.issue<&{IPackNFT.IPackNFTCollectionPublic}>(self.CollectionStoragePath),
at: self.CollectionIPackNFTPublicPath
)

// Create a Pack NFT operator to share mint capability with proxy.
self.account.storage.save(<- create PackNFTOperator(), to: self.OperatorStoragePath)
Expand Down
48 changes: 20 additions & 28 deletions pds/contracts/PackNFT_AllDay.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,6 @@ access(all) contract PackNFT: NonFungibleToken, IPackNFT {
///
access(all) let issuer: Address

/// Reveal a Sealed Pack resource.
///
access(NonFungibleToken.Update) fun reveal(openRequest: Bool) {
PackNFT.revealRequest(id: self.id, openRequest: openRequest)
}

/// Open a Revealed Pack resource.
///
access(NonFungibleToken.Update) fun open() {
PackNFT.openRequest(id: self.id)
}

/// Event emitted when a NFT is destroyed (replaces Burned event before Cadence 1.0 update)
///
access(all) event ResourceDestroyed(id: UInt64 = self.id)
Expand Down Expand Up @@ -330,6 +318,26 @@ access(all) contract PackNFT: NonFungibleToken, IPackNFT {
destroy oldToken
}

/// Emit a RevealRequest event to signal a Sealed Pack NFT should be revealed.
///
access(NonFungibleToken.Update) fun emitRevealRequestEvent(id: UInt64, openRequest: Bool) {
pre {
self.borrowNFT(id) != nil: "NFT with provided ID must exist in the collection"
PackNFT.borrowPackRepresentation(id: id)!.status.rawValue == Status.Sealed.rawValue: "Pack status must be Sealed for reveal request"
}
emit RevealRequest(id: id, openRequest: openRequest)
}

/// Emit an OpenRequest event to signal a Revealed Pack NFT should be opened.
///
access(NonFungibleToken.Update) fun emitOpenRequestEvent(id: UInt64) {
pre {
self.borrowNFT(id) != nil: "NFT with provided ID must exist in the collection"
PackNFT.borrowPackRepresentation(id: id)!.status.rawValue == Status.Revealed.rawValue: "Pack status must be Revealed for open request"
}
emit OpenRequest(id: id)
}

/// Return an array of the IDs that are in the collection.
///
access(all) view fun getIDs(): [UInt64] {
Expand Down Expand Up @@ -372,22 +380,6 @@ access(all) contract PackNFT: NonFungibleToken, IPackNFT {
}
}

/// Emit a RevealRequest event to signal a Sealed Pack NFT should be revealed.
///
access(contract) fun revealRequest(id: UInt64, openRequest: Bool ) {
let p = PackNFT.borrowPackRepresentation(id: id) ?? panic ("No such pack")
assert(p.status.rawValue == Status.Sealed.rawValue, message: "Pack status must be Sealed for reveal request")
emit RevealRequest(id: id, openRequest: openRequest)
}

/// Emit an OpenRequest event to signal a Revealed Pack NFT should be opened.
///
access(contract) fun openRequest(id: UInt64) {
let p = PackNFT.borrowPackRepresentation(id: id) ?? panic ("No such pack")
assert(p.status.rawValue == Status.Revealed.rawValue, message: "Pack status must be Revealed for open request")
emit OpenRequest(id: id)
}

access(all) fun publicReveal(id: UInt64, nfts: [{IPackNFT.Collectible}], salt: String) {
let p = PackNFT.borrowPackRepresentation(id: id) ?? panic ("No such pack")
p.reveal(id: id, nfts: nfts, salt: salt)
Expand Down
48 changes: 20 additions & 28 deletions pds/contracts/PackNFT_TopShot.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,6 @@ access(all) contract PackNFT: NonFungibleToken, IPackNFT {
///
access(all) let issuer: Address

/// Reveal a Sealed Pack resource.
///
access(NonFungibleToken.Update) fun reveal(openRequest: Bool) {
PackNFT.revealRequest(id: self.id, openRequest: openRequest)
}

/// Open a Revealed Pack resource.
///
access(NonFungibleToken.Update) fun open() {
PackNFT.openRequest(id: self.id)
}

/// Event emitted when a NFT is destroyed (replaces Burned event before Cadence 1.0 update)
///
access(all) event ResourceDestroyed(id: UInt64 = self.id)
Expand Down Expand Up @@ -330,6 +318,26 @@ access(all) contract PackNFT: NonFungibleToken, IPackNFT {
destroy oldToken
}

/// Emit a RevealRequest event to signal a Sealed Pack NFT should be revealed.
///
access(NonFungibleToken.Update) fun emitRevealRequestEvent(id: UInt64, openRequest: Bool) {
pre {
self.borrowNFT(id) != nil: "NFT with provided ID must exist in the collection"
PackNFT.borrowPackRepresentation(id: id)!.status.rawValue == Status.Sealed.rawValue: "Pack status must be Sealed for reveal request"
}
emit RevealRequest(id: id, openRequest: openRequest)
}

/// Emit an OpenRequest event to signal a Revealed Pack NFT should be opened.
///
access(NonFungibleToken.Update) fun emitOpenRequestEvent(id: UInt64) {
pre {
self.borrowNFT(id) != nil: "NFT with provided ID must exist in the collection"
PackNFT.borrowPackRepresentation(id: id)!.status.rawValue == Status.Revealed.rawValue: "Pack status must be Revealed for open request"
}
emit OpenRequest(id: id)
}

/// Return an array of the IDs that are in the collection.
///
access(all) view fun getIDs(): [UInt64] {
Expand Down Expand Up @@ -372,22 +380,6 @@ access(all) contract PackNFT: NonFungibleToken, IPackNFT {
}
}

/// Emit a RevealRequest event to signal a Sealed Pack NFT should be revealed.
///
access(contract) fun revealRequest(id: UInt64, openRequest: Bool ) {
let p = PackNFT.borrowPackRepresentation(id: id) ?? panic ("No such pack")
assert(p.status.rawValue == Status.Sealed.rawValue, message: "Pack status must be Sealed for reveal request")
emit RevealRequest(id: id, openRequest: openRequest)
}

/// Emit an OpenRequest event to signal a Revealed Pack NFT should be opened.
///
access(contract) fun openRequest(id: UInt64) {
let p = PackNFT.borrowPackRepresentation(id: id) ?? panic ("No such pack")
assert(p.status.rawValue == Status.Revealed.rawValue, message: "Pack status must be Revealed for open request")
emit OpenRequest(id: id)
}

access(all) fun publicReveal(id: UInt64, nfts: [{IPackNFT.Collectible}], salt: String) {
let p = PackNFT.borrowPackRepresentation(id: id) ?? panic ("No such pack")
p.reveal(id: id, nfts: nfts, salt: salt)
Expand Down
4 changes: 2 additions & 2 deletions pds/contracts/imports/ExampleNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,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 @@ -191,7 +191,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
Loading

0 comments on commit 9cb77cc

Please sign in to comment.