Skip to content

Commit

Permalink
Repo structure
Browse files Browse the repository at this point in the history
  • Loading branch information
lealobanov committed Dec 11, 2024
1 parent 9212ebd commit c976fb4
Showing 1 changed file with 97 additions and 34 deletions.
131 changes: 97 additions & 34 deletions cadence/contracts/Recipe.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,54 +4,117 @@ access(all) contract Recipe {
// This is a snippet extracting the relevant logic from the TopShot contract for demonstration purposes
// More TopShot Code Above
access(all) event PlayAddedToSet(setID: UInt32, playID: UInt32)
access(all)
resource Set {
// mintMoment mints a new Moment and returns the newly minted Moment
//
// Parameters: playID: The ID of the Play that the Moment references
//
// Pre-Conditions:
// The Play must exist in the Set and be allowed to mint new Moments
//
// Returns: The NFT that was minted
//
access(all)
fun mintMoment(playID: UInt32): @NFT {
pre {
self.retired[playID] != nil: "Cannot mint the moment: This play doesn't exist."
!self.retired[playID]!: "Cannot mint the moment from this play: This play has been retired."
}

// Gets the number of Moments that have been minted for this Play
// to use as this Moment's serial number
let numInPlay = self.numberMintedPerPlay[playID]!

// Mint the new moment
let newMoment: @NFT <- create NFT(
serialNumber: numInPlay + UInt32(1),
playID: playID,
setID: self.setID
)

access(all) resource Set {
// Increment the count of Moments minted for this Play
self.numberMintedPerPlay[playID] = numInPlay + UInt32(1)

// addPlay adds a play to the set
return <-newMoment
}

// batchMintMoment mints an arbitrary quantity of Moments
// and returns them as a Collection
//
// Parameters: playID: The ID of the Play that is being added
// Parameters: playID: the ID of the Play that the Moments are minted for
// quantity: The quantity of Moments to be minted
//
// Pre-Conditions:
// The Play needs to be an existing play
// The Set needs to be not locked
// The Play can't have already been added to the Set
// Returns: Collection object that contains all the Moments that were minted
//
access(all)
fun batchMintMoment(playID: UInt32, quantity: UInt64): @Collection {
let newCollection <- create Collection()

/// Resource fields
access(all) var locked: Bool
access(all) let plays: [UInt32]
access(all) let retired: {UInt32: Bool}
access(all) let numberMintedPerPlay: {UInt32: UInt32}
access(all) let setID: UInt32

// Resource initializer
init(setID: UInt32) {
self.locked = false
self.plays = []
self.retired = {}
self.numberMintedPerPlay = {}
var i: UInt64 = 0
while i < quantity {
newCollection.deposit(token: <-self.mintMoment(playID: playID))
i = i + UInt64(1)
}

return <-newCollection
}
}

access(all)
struct MomentData {

// The ID of the Set that the Moment comes from
access(all)
let setID: UInt32

// The ID of the Play that the Moment references
access(all)
let playID: UInt32

// The place in the edition that this Moment was minted
// Otherwise know as the serial number
access(all)
let serialNumber: UInt32

init(setID: UInt32, playID: UInt32, serialNumber: UInt32) {
self.setID = setID
self.playID = playID
self.serialNumber = serialNumber
}
}

access(all) fun addPlay(playID: UInt32) {
pre {
TopShot.getPlayMetaData(playID: playID) != nil: "Cannot add the Play to Set: Play doesn't exist."
!self.locked: "Cannot add the play to the Set after the set has been locked."
self.numberMintedPerPlay[playID] == nil: "The play has already been added to the set."
}
// The resource that represents the Moment NFTs
//
access(all)
resource NFT: NonFungibleToken.INFT {

// Add the Play to the array of Plays
self.plays.append(playID)
// Global unique moment ID
access(all)
let id: UInt64

// Struct of Moment metadata
access(all)
let data: MomentData

// Open the Play up for minting
self.retired[playID] = false
init(serialNumber: UInt32, playID: UInt32, setID: UInt32) {
// Increment the global Moment IDs
TopShot.totalSupply = TopShot.totalSupply + UInt64(1)

// Initialize the Moment count to zero
self.numberMintedPerPlay[playID] = 0
self.id = TopShot.totalSupply

emit PlayAddedToSet(setID: self.setID, playID: playID)
// Set the metadata struct
self.data = MomentData(setID: setID, playID: playID, serialNumber: serialNumber)

emit MomentMinted(
momentID: self.id,
playID: playID,
setID: self.data.setID,
serialNumber: self.data.serialNumber
)
}

}

// More TopShot Code Below
}

0 comments on commit c976fb4

Please sign in to comment.