Skip to content

Commit

Permalink
add small refactor & test
Browse files Browse the repository at this point in the history
  • Loading branch information
loic1 committed Jun 12, 2024
1 parent 4bf9ff8 commit 83c5462
Show file tree
Hide file tree
Showing 11 changed files with 155 additions and 120 deletions.
14 changes: 2 additions & 12 deletions pds/contracts/IPackNFT.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -99,28 +99,18 @@ 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(contract) fun reveal(openRequest: Bool)
access(contract) fun open()
}

/// Resource interface for PackNFT Collection
///
access(all) resource interface IPackNFTCollectionPublic {
access(NonFungibleToken.Update) fun reveal(id: UInt64, openRequest: Bool)
access(NonFungibleToken.Update) fun open(id: UInt64)
access(NonFungibleToken.Update) fun emitRevealRequestEvent(id: UInt64, openRequest: Bool)
access(NonFungibleToken.Update) fun emitOpenRequestEvent(id: UInt64)
}

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

/// Emit a RevealRequest event to signal a Sealed Pack NFT should be revealed
///
access(contract) fun revealRequest(id: UInt64, openRequest: Bool)

/// Emit an OpenRequest event to signal a Revealed Pack NFT should be opened
///
access(contract) fun openRequest(id: UInt64)

/// Reveal a Sealed Pack NFT
///
access(all) fun publicReveal(id: UInt64, nfts: [{IPackNFT.Collectible}], salt: String)
Expand Down
54 changes: 14 additions & 40 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(contract) fun reveal(openRequest: Bool) {
PackNFT.revealRequest(id: self.id, openRequest: openRequest)
}

/// Open a Revealed Pack resource.
///
access(contract) 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,18 +229,24 @@ access(all) contract PackNFT: NonFungibleToken, IPackNFT {
destroy oldToken
}

/// Reveal a Sealed Pack resource.
/// Emit a RevealRequest event to signal a Sealed Pack NFT should be revealed.
///
access(NonFungibleToken.Update) fun reveal(id: UInt64, openRequest: Bool) {
let packRef = self.borrowNFT(id) as! &NFT
packRef.reveal(openRequest: openRequest)
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)
}

/// Open a Revealed Pack resource.
/// Emit an OpenRequest event to signal a Revealed Pack NFT should be opened.
///
access(NonFungibleToken.Update) fun open(id: UInt64) {
let packRef = self.borrowNFT(id) as! &NFT
packRef.open()
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.
Expand Down Expand Up @@ -297,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 @@ -409,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
8 changes: 4 additions & 4 deletions pds/lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pds/lib/go/templates/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions pds/lib/go/templates/transaction_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
filenameMintPackNFT = "transactions/pds/mint_packNFT.cdc"
filenameSettleDistribution = "transactions/pds/settle.cdc"
filenameOpenPackNFT = "transactions/pds/open_packNFT.cdc"
filenameRevealRequest = "transactions/packNFT/reveal_request.cdc"
)

// GenerateDeployPackNFTTx returns a transaction script that
Expand Down Expand Up @@ -77,3 +78,9 @@ func GenerateOpenPackNFTTx(pdsAddress, nftAddress flow.Address) []byte {
code := string(assets.MustAsset(filenameOpenPackNFT))
return replaceAddresses(code, nftAddress, flow.EmptyAddress, flow.EmptyAddress, flow.EmptyAddress, flow.EmptyAddress, pdsAddress, flow.EmptyAddress)
}

// GenerateRevealRequestTx returns a transaction script that reveals a request
func GenerateRevealRequestTx(iPackNFTAddress, packNFTAddress, nftAddress flow.Address) []byte {
code := string(assets.MustAsset(filenameRevealRequest))
return replaceAddresses(code, nftAddress, flow.EmptyAddress, flow.EmptyAddress, flow.EmptyAddress, iPackNFTAddress, flow.EmptyAddress, packNFTAddress)
}
45 changes: 44 additions & 1 deletion pds/lib/go/test/pds_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package test

import (
"strings"
"testing"

"github.com/dapperlabs/studio-platform-smart-contracts/lib/go/templates"
Expand Down Expand Up @@ -512,6 +513,8 @@ func TestOpenPackNFT(t *testing.T) {
)
assert.Equal(t, cadence.String(distributionTitle), title)

var packNftId uint64

t.Run("Should be able to mint a pack NFT", func(t *testing.T) {
// Assumes issuer is deployer of exampleNFT
tx := createTxWithTemplateAndAuthorizer(b,
Expand All @@ -525,7 +528,7 @@ func TestOpenPackNFT(t *testing.T) {

serviceSigner, _ := b.ServiceKey().Signer()

signAndSubmit(
txResult := signAndSubmitWithResult(
t, b, tx,
[]flow.Address{
b.ServiceKey().Address,
Expand All @@ -537,6 +540,46 @@ func TestOpenPackNFT(t *testing.T) {
},
false,
)
for _, e := range txResult.Events {
if strings.Contains(e.Type, "NonFungibleToken.Deposited") {
values := e.Value.GetFieldValues()
if len(values) < 2 {
t.Fatalf("expected at least 2 fields in NonFungibleToken.Deposited event, got %d", len(values))
}
packNftId = uint64(values[1].(cadence.UInt64))
}
}

assertCollectionLength(t, b, nftAddress, exampleNFTAddress, metadataAddress,
exampleNFTAddress,
1,
)
})

t.Run("Should be able to emit a reveal request", func(t *testing.T) {
// Assumes issuer is deployer of exampleNFT
tx := createTxWithTemplateAndAuthorizer(b,
templates.GenerateRevealRequestTx(iPackNFTAddress, exampleNFTAddress, nftAddress),
exampleNFTAddress,
)
// Set argument: issuer address
tx.AddArgument(cadence.UInt64(packNftId))
tx.AddArgument(cadence.NewBool(true))

serviceSigner, _ := b.ServiceKey().Signer()

signAndSubmit(
t, b, tx,
[]flow.Address{
b.ServiceKey().Address,
exampleNFTAddress,
},
[]crypto.Signer{
serviceSigner,
exampleNFTSigner,
},
false,
)

assertCollectionLength(t, b, nftAddress, exampleNFTAddress, metadataAddress,
exampleNFTAddress,
Expand Down
Loading

0 comments on commit 83c5462

Please sign in to comment.