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

Add remix #163

Merged
merged 25 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 116 additions & 16 deletions contracts/TopShot.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ pub contract TopShot: NonFungibleToken {
// Variable size dictionary of Set resources
access(self) var sets: @{UInt32: Set}

// The ID that is used to create Plays.
// Every time a Play is created, playID is assigned
// The ID that is used to create Plays.
jrkhan marked this conversation as resolved.
Show resolved Hide resolved
// Every time a Play is created, playID is assigned
// to the new Play's ID and then is incremented by 1.
pub var nextPlayID: UInt32

Expand All @@ -127,8 +127,8 @@ pub contract TopShot: NonFungibleToken {
// can be created by this contract that contains stored values.
// -----------------------------------------------------------------------

// Play is a Struct that holds metadata associated
// with a specific NBA play, like the legendary moment when
// Play is a Struct that holds metadata associated
// with a specific NBA play, like the legendary moment when
// Ray Allen hit the 3 to tie the Heat and Spurs in the 2013 finals game 6
// or when Lance Stephenson blew in the ear of Lebron James.
//
Expand Down Expand Up @@ -159,11 +159,11 @@ pub contract TopShot: NonFungibleToken {
// A Set is a grouping of Plays that have occured in the real world
// that make up a related group of collectibles, like sets of baseball
// or Magic cards. A Play can exist in multiple different sets.
//
//
// SetData is a struct that is stored in a field of the contract.
// Anyone can query the constant information
// about a set by calling various getters located
// at the end of the contract. Only the admin has the ability
// about a set by calling various getters located
// at the end of the contract. Only the admin has the ability
// to modify any data in the private Set resource.
//
pub struct SetData {
Expand Down Expand Up @@ -200,14 +200,14 @@ pub contract TopShot: NonFungibleToken {
// that reference that playdata.
// The Moments that are minted by a Set will be listed as belonging to
// the Set that minted it, as well as the Play it references.
//
//
// Admin can also retire Plays from the Set, meaning that the retired
// Play can no longer have Moments minted from it.
//
// If the admin locks the Set, no more Plays can be added to it, but
// If the admin locks the Set, no more Plays can be added to it, but
// Moments can still be minted.
//
// If retireAll() and lock() are called back-to-back,
// If retireAll() and lock() are called back-to-back,
// the Set is closed off forever and nothing more can be done with it.
pub resource Set {

Expand All @@ -225,7 +225,7 @@ pub contract TopShot: NonFungibleToken {
access(contract) var retired: {UInt32: Bool}

// Indicates if the Set is currently locked.
// When a Set is created, it is unlocked
// When a Set is created, it is unlocked
// and Plays are allowed to be added to it.
// When a set is locked, Plays cannot be added.
// A Set can never be changed from locked to unlocked,
Expand All @@ -235,7 +235,7 @@ pub contract TopShot: NonFungibleToken {
// that exist in the Set.
pub var locked: Bool

// Mapping of Play IDs that indicates the number of Moments
// Mapping of Play IDs that indicates the number of Moments
// that have been minted for specific Plays in this Set.
// When a Moment is minted, this value is stored in the Moment to
// show its place in the Set, eg. 13 of 60.
Expand Down Expand Up @@ -297,7 +297,7 @@ pub contract TopShot: NonFungibleToken {
//
// Pre-Conditions:
// The Play is part of the Set and not retired (available for minting).
//
//
pub fun retirePlay(playID: UInt32) {
pre {
self.retired[playID] != nil: "Cannot retire the Play: Play doesn't exist in this set!"
Expand Down Expand Up @@ -331,14 +331,14 @@ pub contract TopShot: NonFungibleToken {
}

// 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
//
//
pub fun mintMoment(playID: UInt32): @NFT {
pre {
self.retired[playID] != nil: "Cannot mint the moment: This play doesn't exist."
Expand All @@ -360,7 +360,7 @@ pub contract TopShot: NonFungibleToken {
return <-newMoment
}

// batchMintMoment mints an arbitrary quantity of Moments
// batchMintMoment mints an arbitrary quantity of Moments
// and returns them as a Collection
//
// Parameters: playID: the ID of the Play that the Moments are minted for
Expand All @@ -380,6 +380,51 @@ pub contract TopShot: NonFungibleToken {
return <-newCollection
}

pub fun mintMomentWithSubEdition(playID: UInt32, subEditionID: 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 subEditionCap = TopShot.account.getCapability<&{AdminSubEdition}>(/private/AdminSubEdition)
let subEditionRef = subEditionCap.borrow()!

let numInSubEdition = subEditionRef.getNumberMintedPerSubEdition(setID: self.setID,
playID: playID,
subEditionID: subEditionID)

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

jrkhan marked this conversation as resolved.
Show resolved Hide resolved
// Increment the count of Moments minted for this Play
subEditionRef.addToNumberMintedPerSubEdition(setID: self.setID,
playID: playID,
subEditionID: subEditionID)

subEditionRef.setMomentsSubEdition(nftID: newMoment.id, subEditionID: subEditionID)

self.numberMintedPerPlay[playID] = self.numberMintedPerPlay[playID]! + UInt32(1)

return <-newMoment
}

pub fun batchMintMomentWithSubEdition(playID: UInt32, quantity: UInt64, subEditionID: UInt32): @Collection {
let newCollection <- create Collection()

var i: UInt64 = 0
while i < quantity {
newCollection.deposit(token: <-self.mintMomentWithSubEdition(playID: playID,
subEditionID: subEditionID))
i = i + UInt64(1)
}

return <-newCollection
}

pub fun getPlays(): [UInt32] {
return self.plays
}
Expand Down Expand Up @@ -1152,6 +1197,56 @@ pub contract TopShot: NonFungibleToken {
}
}

pub resource interface AdminSubEdition {
pub fun getNumberMintedPerSubEdition(setID: UInt32, playID: UInt32, subEditionID: UInt32): UInt32
pub fun addToNumberMintedPerSubEdition(setID: UInt32, playID: UInt32, subEditionID: UInt32)
pub fun setMomentsSubEdition(nftID: UInt64, subEditionID: UInt32)
}

pub resource interface PublicSubEdition {
pub fun getMomentsSubEdition( nftID: UInt64):UInt32?
}

pub resource SubEdition:AdminSubEdition,PublicSubEdition {

access(self) var numberMintedPerSubEdition: {String:UInt32}

access(self) var momentsSubEdition: {UInt64:UInt32}

pub fun getMomentsSubEdition( nftID: UInt64):UInt32? {
return self.momentsSubEdition[nftID]
}

pub fun getNumberMintedPerSubEdition(setID: UInt32, playID: UInt32, subEditionID: UInt32): UInt32 {
let setPlaySubEdition = setID.toString().concat(playID.toString()).concat(subEditionID.toString())
if !self.numberMintedPerSubEdition.containsKey(setPlaySubEdition) {
self.numberMintedPerSubEdition.insert(key: setPlaySubEdition,UInt32(0))
return UInt32(0)
}
return self.numberMintedPerSubEdition[setPlaySubEdition]!
}

pub fun addToNumberMintedPerSubEdition(setID: UInt32, playID: UInt32, subEditionID: UInt32) {
let setPlaySubEdition = setID.toString().concat(playID.toString()).concat(subEditionID.toString())

if self.numberMintedPerSubEdition.containsKey(setPlaySubEdition) {
self.numberMintedPerSubEdition[setPlaySubEdition]!= self.numberMintedPerSubEdition[setPlaySubEdition]! + UInt32(1)
} else {
panic("Could not find specified SubEdition!")
}
}

pub fun setMomentsSubEdition(nftID: UInt64, subEditionID: UInt32){
self.momentsSubEdition.insert(key: nftID, subEditionID)
}

init() {
self.momentsSubEdition = {}
self.numberMintedPerSubEdition = {}
}
}


// -----------------------------------------------------------------------
// TopShot initialization function
// -----------------------------------------------------------------------
Expand All @@ -1175,6 +1270,11 @@ pub contract TopShot: NonFungibleToken {
// Put the Minter in storage
self.account.save<@Admin>(<- create Admin(), to: /storage/TopShotAdmin)

self.account.save<@SubEdition>(<- create SubEdition(), to: /storage/TopShotSubEdition)
jrkhan marked this conversation as resolved.
Show resolved Hide resolved

self.account.link<&{AdminSubEdition}>(/private/AdminSubEdition, target: /storage/TopShotSubEdition)
self.account.link<&{PublicSubEdition}>(/public/PublicSubEdition, target: /storage/TopShotSubEdition)

emit ContractInitialized()
}
}
20 changes: 10 additions & 10 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

Loading