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

C1 migration #1

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
51 changes: 51 additions & 0 deletions .github/workflows/cadence_lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Run Cadence Contract Compilation, Deployment, Transaction Execution, and Lint
on: push

jobs:
run-cadence-lint:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: 'true'

- name: Install Flow CLI
run: |
brew update
brew install flow-cli
- name: Initialize Flow
run: |
if [ ! -f flow.json ]; then
echo "Initializing Flow project..."
flow init
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 .cdc files in the current repository"
flow cadence lint ./cadence/**/*.cdc
34 changes: 34 additions & 0 deletions .github/workflows/cadence_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Run Cadence Tests
on: push

jobs:
run-cadence-tests:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: 'true'

- name: Install Flow CLI
run: |
brew update
brew install flow-cli

- name: Initialize Flow
run: |
if [ ! -f flow.json ]; then
echo "Initializing Flow project..."
flow init
else
echo "Flow project already initialized."
fi

- name: Run Cadence Tests
run: |
if test -f "cadence/tests.cdc"; then
echo "Running Cadence tests in the current repository"
flow test cadence/tests.cdc
else
echo "No Cadence tests found. Skipping tests."
fi
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/
83 changes: 50 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ You've created your set in your series and now you're ready to mint your NFTS. T
- [Description](#description)
- [What is included in this repository?](#what-is-included-in-this-repository)
- [Supported Recipe Data](#recipe-data)
- [Deploying Recipe Contracts and Running Transactions Locally (Flow Emulator)](#deploying-recipe-contracts-and-running-transactions-locally-flow-emulator)
- [License](#license)

## Description
Expand All @@ -19,7 +20,6 @@ The Cadence Cookbook is a collection of code examples, recipes, and tutorials de

Each recipe in the Cadence Cookbook is a practical coding example that showcases a specific aspect of Cadence or use-case on Flow, including smart contract development, interaction, and best practices. By following these recipes, you can gain hands-on experience and learn how to leverage Cadence for your blockchain projects.


### Contributing to the Cadence Cookbook

Learn more about the contribution process [here](https://github.com/onflow/cadence-cookbook/blob/main/contribute.md).
Expand All @@ -34,17 +34,17 @@ Recipe metadata, such as title, author, and category labels, is stored in `index

```
recipe-name/
├── cadence/ # Cadence files for recipe examples
│ ├── contract.cdc # Contract code
│ ├── transaction.cdc # Transaction code
│ ├── tests.cdc # Tests code
├── explanations/ # Explanation files for recipe examples
│ ├── contract.txt # Contract code explanation
├── transaction.txt # Transaction code explanation
├── tests.txt # Tests code explanation
├── index.js # Root file for storing recipe metadata
├── README.md # This README file
└── LICENSE # License information
├── cadence/ # Cadence files for recipe examples
│ ├── contracts/Recipe.cdc # Contract code
│ ├── transactions/mint_set_nft.cdc # Transaction code
│ ├── tests/Recipe_test.cdc # Tests code
├── explanations/ # Explanation files for recipe examples
│ ├── contract.txt # Contract code explanation
│ ├── transaction.txt # Transaction code explanation
│ ├── tests.txt # Tests code explanation
├── index.js # Root file for storing recipe metadata
├── README.md # This README file
└── LICENSE # License information
```

## Supported Recipe Data
Expand All @@ -57,12 +57,6 @@ recipe-name/
- `author`: contributor of the recipe
- `playgroundLink`: a link to Flow Playground containing the deployed recipe code
- `excerpt`: a brief description of the recipe contents
- `smartContractCode`: path to location of Cadence smart contract code example
- `smartContractExplanation`: path to location of smart contract code explanation
- `transactionCode`: path to location of Cadence transaction code example
- `transactionExplanation`: path to location of transaction code explanation
- `testsPath`: path to location of Cadence test cases code example
- `testsExplanationPath`: path to location of test cases code explanation
- `filters`: the filters object is used to perform filtering on recipes in the cookbook
- `difficulty`: the difficulty filter supports one of ['beginner', 'intermediate', 'advanced']

Expand All @@ -71,30 +65,53 @@ recipe-name/
// Pass the repo name
const recipe = "sample-recipe-name";

//Generate paths of each code file to render
const contractPath = `${recipe}/cadence/contract.cdc`;
const transactionPath = `${recipe}/cadence/transaction.cdc`;
const testsPath = `${recipe}/cadence/tests.cdc`;

//Generate paths of each explanation file to render
const smartContractExplanationPath = `${recipe}/explanations/contract.txt`;
const transactionExplanationPath = `${recipe}/explanations/transaction.txt`;
const testsExplanationPath = `${recipe}/explanations/tests.txt`;

export const sampleRecipe= {
export const sampleRecipe = {
slug: recipe,
title: "",
featuredText: "",
createdAt: Date(2022, 3, 1),
author: "",
playgroundLink: "",
excerpt: "",
smartContractCode: contractPath,
smartContractExplanation: smartContractExplanationPath,
transactionCode: transactionPath,
transactionExplanation: transactionExplanationPath,
};
```
## Deploying Recipe Contracts and Running Transactions Locally (Flow Emulator)

This section explains how to deploy the recipe's contracts to the Flow emulator, run the associated transaction with sample arguments, and verify the results.

### Prerequisites

Before deploying and running the recipe:

1. Install the Flow CLI. You can find installation instructions [here](https://docs.onflow.org/flow-cli/install/).
2. Ensure the Flow emulator is installed and ready to use with `flow version`.

### Step 1: Start the Flow Emulator

Start the Flow emulator to simulate the blockchain environment locally

```bash
flow emulator start
```

### Step 2: Install Dependencies and Deploy Project Contracts

Deploy contracts to the emulator. This will deploy all the contracts specified in the _deployments_ section of `flow.json` whether project contracts or dependencies.

```bash
flow dependencies install
flow project deploy --network=emulator
```

### Step 3: Run the Transaction

Transactions associated with the recipe are located in `./cadence/transactions`. To run a transaction, execute the following command:

```bash
flow transactions send cadence/transactions/TRANSACTION_NAME.cdc --signer emulator-account
```

To verify the transaction's execution, check the emulator logs printed during the transaction for confirmation messages. You can add the `--log-level debug` flag to your Flow CLI command for more detailed output during contract deployment or transaction execution.

## License

Expand Down
32 changes: 0 additions & 32 deletions cadence/contract.cdc

This file was deleted.

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

access(all) contract Recipe {
// More code from Series resource above...

access(all)
fun mintSetAndSeriesNFT(
recipient: &{NonFungibleToken.CollectionPublic},
tokenId: UInt64,
setId: UInt32
) {
pre {
// Validate that the set exists
self.numberEditionsMintedPerSet[setId] != nil: "The Set does not exist."
// Ensure the number of editions minted does not exceed the maximum allowed
self.numberEditionsMintedPerSet[setId]! < SetAndSeries.getSetMaxEditions(setId: setId)!:
"Set has reached maximum NFT edition capacity."
}

// Gets the number of editions that have been minted so far in this set
let editionNum: UInt32 = self.numberEditionsMintedPerSet[setId]! + (1 as UInt32)

// Deposit the new NFT into the recipient's collection
recipient.deposit(token: <-create SetAndSeries.NFT(
tokenId: tokenId,
setId: setId,
editionNum: editionNum
))

// Increment the total supply of NFTs globally
SetAndSeries.totalSupply = SetAndSeries.totalSupply + (1 as UInt64)

// Update the count of editions minted in the set
self.numberEditionsMintedPerSet[setId] = editionNum
}

// More code from Series resource below...
}
17 changes: 17 additions & 0 deletions cadence/tests/Recipe_test.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Test

access(all) fun testExample() {
let array = [1, 2, 3]
Test.expect(array.length, Test.equal(3))
}

access(all)
fun setup() {
let err = Test.deployContract(
name: "Recipe",
path: "../contracts/Recipe.cdc",
arguments: [],
)

Test.expect(err, Test.beNil())
}
29 changes: 0 additions & 29 deletions cadence/transaction.cdc

This file was deleted.

34 changes: 34 additions & 0 deletions cadence/transactions/mint_set_nft.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import "SetAndSeries"
import "NonFungibleToken"

transaction {

let adminCheck: auth(AdminEntitlement) &SetAndSeries.Admin
let seriesRef: &SetAndSeries.Series
let receiver: &{NonFungibleToken.CollectionPublic}

prepare(acct: auth(Storage, Capabilities) &Account) {
// Borrow the admin reference from storage
self.adminCheck = acct.capabilities.storage.borrow<&SetAndSeries.Admin>(
from: SetAndSeries.AdminStoragePath
) ?? panic("Could not borrow admin reference")

// Borrow the series reference
self.seriesRef = self.adminCheck.borrowSeries(seriesId: 1)

// Borrow the receiver's capability reference
self.receiver = acct.capabilities.borrow<&SetAndSeries.Collection{NonFungibleToken.CollectionPublic}>(
SetAndSeries.CollectionPublicPath
) ?? panic("Could not borrow capability")
}

execute {
// Mint an NFT and deposit it to the recipient's collection
self.seriesRef.mintSetAndSeriesNFT(
recipient: self.receiver,
tokenId: 1,
setId: 1
)
log("Minted NFT in account 1")
}
}
1 change: 1 addition & 0 deletions emulator-account.pkey
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0xdc07d83a937644ff362b279501b7f7a3735ac91a0f3647147acf649dda804e28
Loading
Loading