Skip to content

Commit

Permalink
Repo restructure
Browse files Browse the repository at this point in the history
  • Loading branch information
lealobanov committed Dec 8, 2024
1 parent 4348cd8 commit d4a9a2a
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 156 deletions.
29 changes: 25 additions & 4 deletions .github/workflows/cadence_lint.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run Cadence Lint
name: Run Cadence Contract Compilation, Deployment, Transaction Execution, and Lint
on: push

jobs:
Expand All @@ -9,7 +9,7 @@ jobs:
uses: actions/checkout@v3
with:
submodules: 'true'

- name: Install Flow CLI
run: |
brew update
Expand All @@ -23,8 +23,29 @@ jobs:
else
echo "Flow project already initialized."
fi
flow dependencies install
- name: Start Flow Emulator
run: |
echo "Starting Flow emulator in the background..."
nohup flow emulator start > emulator.log 2>&1 &
sleep 5 # Wait for the emulator to start
flow project deploy --network=emulator # Deploy the recipe contracts indicated in flow.json
- name: Run All Transactions
run: |
echo "Running all transactions in the transactions folder..."
for file in ./cadence/transactions/*.cdc; do
echo "Running transaction: $file"
TRANSACTION_OUTPUT=$(flow transactions send "$file" --signer emulator-account)
echo "$TRANSACTION_OUTPUT"
if echo "$TRANSACTION_OUTPUT" | grep -q "Transaction Error"; then
echo "Transaction Error detected in $file, failing the action..."
exit 1
fi
done
- name: Run Cadence Lint
run: |
echo "Running Cadence linter on all .cdc files in the current repository"
flow cadence lint **/*.cdc
echo "Running Cadence linter on .cdc files in the current repository"
flow cadence lint ./cadence/**/*.cdc
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
.DS_Store
/imports/
/.idea/
147 changes: 0 additions & 147 deletions cadence/contract.cdc

This file was deleted.

145 changes: 145 additions & 0 deletions cadence/contracts/Recipe.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@

import "TopShot"

access(all) contract Recipe {
// This is a snippet extracting the relevant logic from the TopShot contract for demonstration purposes
// TopShot Contract Code Above
// Variable size dictionary of SetData structs
access(self) var setDatas: {UInt32: SetData}

// Variable size dictionary of Set resources
access(self) var sets: @{UInt32: Set}

// The ID that is used to create Sets. Every time a Set is created
// setID is assigned to the new set's ID and then is incremented by 1.
access(all) var nextSetID: UInt32

// A Set is a grouping of Plays that have occurred 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
// to modify any data in the private Set resource.
//
access(all) struct SetData {

// Unique ID for the Set
access(all) let setID: UInt32

// Name of the Set
access(all) let name: String

// Series that this Set belongs to.
access(all) let series: UInt32

init(name: String) {
pre {
name.length > 0: "New Set name cannot be empty"
}
self.setID = TopShot.nextSetID
self.name = name
self.series = TopShot.currentSeries
}
}

access(all) resource Set {

// Unique ID for the set
access(all) let setID: UInt32

// Array of plays that are a part of this set.
access(contract) var plays: [UInt32]

// Map of Play IDs that Indicates if a Play in this Set can be minted.
access(contract) var retired: {UInt32: Bool}

// Indicates if the Set is currently locked.
access(all) var locked: Bool

// Mapping of Play IDs that indicates the number of Moments
access(contract) var numberMintedPerPlay: {UInt32: UInt32}

init(name: String) {
self.setID = TopShot.nextSetID
self.plays = []
self.retired = {}
self.locked = false
self.numberMintedPerPlay = {}

TopShot.setDatas[self.setID] = SetData(name: name)
}

view fun getPlays(): [UInt32] {
return self.plays
}

view fun getRetired(): {UInt32: Bool} {
return self.retired
}

view fun getNumMintedPerPlay(): {UInt32: UInt32} {
return self.numberMintedPerPlay
}

access(all) fun addPlay(playID: UInt32) {
pre {
TopShot.playDatas[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."
}

self.plays.append(playID)
self.retired[playID] = false
self.numberMintedPerPlay[playID] = 0

emit PlayAddedToSet(setID: self.setID, playID: playID)
}

access(all) fun retirePlay(playID: UInt32) {
pre {
self.retired[playID] != nil: "Cannot retire the Play: Play doesn't exist in this set!"
}

if !self.retired[playID]! {
self.retired[playID] = true
emit PlayRetiredFromSet(setID: self.setID, playID: playID, numMoments: self.numberMintedPerPlay[playID]!)
}
}

access(all) fun lock() {
if !self.locked {
self.locked = true
emit SetLocked(setID: self.setID)
}
}

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."
}

let numInPlay = self.numberMintedPerPlay[playID]!
let newMoment: @NFT <- create NFT(serialNumber: numInPlay + UInt32(1), playID: playID, setID: self.setID)
self.numberMintedPerPlay[playID] = numInPlay + UInt32(1)
return <-newMoment
}
}

access(all) resource Admin {

access(all) fun createSet(name: String): UInt32 {
var newSet <- create Set(name: name)
TopShot.nextSetID = TopShot.nextSetID + UInt32(1)

let newID = newSet.setID
emit SetCreated(setID: newSet.setID, series: TopShot.currentSeries)
TopShot.sets[newID] <-! newSet
return newID
}
}
}
6 changes: 6 additions & 0 deletions cadence/tests/Recipe_test.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import Test

access(all) fun testExample() {
let array = [1, 2, 3]
Test.expect(array.length, Test.equal(3))
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TopShot from 0x01
import "TopShot"

transaction {

Expand Down
1 change: 1 addition & 0 deletions emulator-account.pkey
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0xdc07d83a937644ff362b279501b7f7a3735ac91a0f3647147acf649dda804e28
Loading

0 comments on commit d4a9a2a

Please sign in to comment.