-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from onflow/c1-migration
C1 migration
- Loading branch information
Showing
14 changed files
with
389 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.DS_Store | ||
.DS_Store | ||
/imports/ | ||
/.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
./cadence/contracts/Recipe.cdc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/// ExampleNFT.cdc | ||
/// | ||
/// This is a complete version of the ExampleNFT contract | ||
/// that includes withdraw and deposit functionalities, as well as a | ||
/// collection resource that can be used to bundle NFTs together. | ||
/// | ||
/// Learn more about non-fungible tokens in this tutorial: https://developers.flow.com/cadence/tutorial/non-fungible-tokens-1 | ||
access(all) contract ExampleNFT { | ||
|
||
// Declare Path constants | ||
access(all) let CollectionStoragePath: StoragePath | ||
access(all) let CollectionPublicPath: PublicPath | ||
access(all) let MinterStoragePath: StoragePath | ||
|
||
// Tracks the unique IDs of the NFTs | ||
access(all) var idCount: UInt64 | ||
|
||
access(all) resource NFT { | ||
access(all) let id: UInt64 | ||
init(initID: UInt64) { | ||
self.id = initID | ||
} | ||
} | ||
|
||
access(all) entitlement Withdraw | ||
|
||
// Interface defining required methods for the Collection | ||
access(all) resource interface CollectionInterface { | ||
access(all) fun deposit(token: @NFT) | ||
access(Withdraw) fun withdraw(withdrawID: UInt64): @NFT | ||
access(all) view fun getIDs(): [UInt64] | ||
} | ||
|
||
// Collection resource conforming to CollectionInterface | ||
access(all) resource Collection: CollectionInterface { | ||
access(all) var ownedNFTs: @{UInt64: NFT} | ||
|
||
init () { | ||
self.ownedNFTs <- {} | ||
} | ||
|
||
access(Withdraw) fun withdraw(withdrawID: UInt64): @NFT { | ||
let token <- self.ownedNFTs.remove(key: withdrawID) | ||
?? panic("Could not withdraw NFT with id=".concat(withdrawID.toString())) | ||
return <-token | ||
} | ||
|
||
access(all) fun deposit(token: @NFT) { | ||
self.ownedNFTs[token.id] <-! token | ||
} | ||
|
||
access(all) view fun getIDs(): [UInt64] { | ||
return self.ownedNFTs.keys | ||
} | ||
} | ||
|
||
access(all) fun createEmptyCollection(): @Collection { | ||
return <- create Collection() | ||
} | ||
|
||
access(all) fun mintNFT(): @NFT { | ||
let newNFT <- create NFT(initID: self.idCount) | ||
self.idCount = self.idCount + 1 | ||
return <-newNFT | ||
} | ||
|
||
init() { | ||
self.CollectionStoragePath = /storage/nftTutorialCollection | ||
self.CollectionPublicPath = /public/nftTutorialCollection | ||
self.MinterStoragePath = /storage/nftTutorialMinter | ||
|
||
self.idCount = 1 | ||
|
||
self.account.storage.save(<-self.createEmptyCollection(), to: self.CollectionStoragePath) | ||
let cap = self.account.capabilities.storage.issue<&Collection>(self.CollectionStoragePath) | ||
self.account.capabilities.publish(cap, at: self.CollectionPublicPath) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: "ExampleNFT", | ||
path: "../contracts/Recipe.cdc", | ||
arguments: [], | ||
) | ||
|
||
Test.expect(err, Test.beNil()) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
./cadence/transactions/create_collection.cdc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import "NonFungibleToken" | ||
import "ExampleNFT" | ||
import "MetadataViews" | ||
|
||
// This transaction is what an account would run | ||
// to set itself up to receive NFTs | ||
transaction { | ||
|
||
prepare(signer: auth(Storage, Capabilities) &Account) { | ||
// Check if the account already has a collection | ||
let collectionRef = signer.storage.borrow<&ExampleNFT.Collection>(from: ExampleNFT.CollectionStoragePath) | ||
|
||
// If a collection already exists, return early | ||
if collectionRef != nil { | ||
return | ||
} | ||
|
||
// Create a new empty collection | ||
let collection <- ExampleNFT.createEmptyCollection() | ||
|
||
// Save it to the account storage | ||
signer.storage.save(<-collection, to: ExampleNFT.CollectionStoragePath) | ||
|
||
// Create a capability for the collection | ||
let collectionCap = signer.capabilities.storage.issue<&{NonFungibleToken.CollectionPublic}>( | ||
ExampleNFT.CollectionStoragePath | ||
) | ||
|
||
// Publish the capability | ||
signer.capabilities.publish(collectionCap, at: ExampleNFT.CollectionPublicPath) | ||
} | ||
|
||
execute { | ||
log("Setup account") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
0xdc07d83a937644ff362b279501b7f7a3735ac91a0f3647147acf649dda804e28 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
This is a function you would include in your smart contract to create collections for accounts without them. | ||
The createEmptyCollection function is part of the ExampleNFT contract and is used to create a new, empty collection for an account. A collection is a resource that allows users to store and manage their non-fungible tokens (NFTs) by bundling them together. | ||
|
||
When invoked, the createEmptyCollection function creates a new instance of the Collection resource. This resource is initialized with an empty set of NFTs. The collection resource adheres to the CollectionInterface, which defines methods for depositing NFTs, withdrawing NFTs by their ID, and viewing the list of owned NFT IDs. | ||
|
||
Once the collection is created, it is returned as a resource, which can then be stored in the account's private storage using a transaction. This enables the account to manage NFTs directly. Additionally, the function creates a public capability for the collection, which grants others access to the functionality provided by the collection (e.g., depositing and withdrawing NFTs) through the CollectionPublic interface. | ||
|
||
A collection resource is returned which you then save into your account with a transaction. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
This transaction is showing us how we would create a new empty collection and save that resource into a users account. | ||
This transaction demonstrates how to create a new empty collection for an account and save it as a resource in the user's storage. Initially, the transaction checks if the account already has a collection. If one doesn't exist, it proceeds to create a new ExampleNFT.Collection and stores it in the account’s storage under a specified path. | ||
|
||
We then create a public capability to this resource by linking the interface that contains functions the public can use on this resource for this account. | ||
After saving the collection, the transaction creates a public capability for the collection. This capability allows others to interact with the collection by providing access to specific functions defined in the NonFungibleToken.CollectionPublic interface, such as viewing or transferring NFTs. The capability is then published to a public path, making it accessible to others who wish to interact with the collection. |
Oops, something went wrong.