diff --git a/.github/workflows/cadence_lint.yml b/.github/workflows/cadence_lint.yml new file mode 100644 index 0000000..1100626 --- /dev/null +++ b/.github/workflows/cadence_lint.yml @@ -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 diff --git a/.github/workflows/cadence_tests.yml b/.github/workflows/cadence_tests.yml new file mode 100644 index 0000000..9a51f78 --- /dev/null +++ b/.github/workflows/cadence_tests.yml @@ -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 diff --git a/.gitignore b/.gitignore index 496ee2c..b1d92af 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.DS_Store \ No newline at end of file +.DS_Store +/imports/ +/.idea/ \ No newline at end of file diff --git a/README.md b/README.md index 5edea06..f63e25e 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Create a secondary marketplace for your NFTS using the NFT Storefront Contract. - [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 @@ -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). @@ -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/create_storefront.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 @@ -95,6 +95,43 @@ export const sampleRecipe= { 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 diff --git a/cadence/contract.cdc b/cadence/contract.cdc deleted file mode 100644 index 77c650e..0000000 --- a/cadence/contract.cdc +++ /dev/null @@ -1,119 +0,0 @@ -pub contract NFTStorefront { - - ..... - - // Storefront - // A resource that allows its owner to manage a list of Listings, and anyone to interact with them - // in order to query their details and purchase the NFTs that they represent. - // - pub resource Storefront : StorefrontManager, StorefrontPublic { - // The dictionary of Listing uuids to Listing resources. - access(self) var listings: @{UInt64: Listing} - - // insert - // Create and publish a Listing for an NFT. - // - pub fun createListing( - nftProviderCapability: Capability<&{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>, - nftType: Type, - nftID: UInt64, - salePaymentVaultType: Type, - saleCuts: [SaleCut] - ): UInt64 { - let listing <- create Listing( - nftProviderCapability: nftProviderCapability, - nftType: nftType, - nftID: nftID, - salePaymentVaultType: salePaymentVaultType, - saleCuts: saleCuts, - storefrontID: self.uuid - ) - - let listingResourceID = listing.uuid - let listingPrice = listing.getDetails().salePrice - - // Add the new listing to the dictionary. - let oldListing <- self.listings[listingResourceID] <- listing - // Note that oldListing will always be nil, but we have to handle it. - destroy oldListing - - emit ListingAvailable( - storefrontAddress: self.owner?.address!, - listingResourceID: listingResourceID, - nftType: nftType, - nftID: nftID, - ftVaultType: salePaymentVaultType, - price: listingPrice - ) - - return listingResourceID - } - - // removeListing - // Remove a Listing that has not yet been purchased from the collection and destroy it. - // - pub fun removeListing(listingResourceID: UInt64) { - let listing <- self.listings.remove(key: listingResourceID) - ?? panic("missing Listing") - - // This will emit a ListingCompleted event. - destroy listing - } - - // getListingIDs - // Returns an array of the Listing resource IDs that are in the collection - // - pub fun getListingIDs(): [UInt64] { - return self.listings.keys - } - - // borrowSaleItem - // Returns a read-only view of the SaleItem for the given listingID if it is contained by this collection. - // - pub fun borrowListing(listingResourceID: UInt64): &Listing{ListingPublic}? { - if self.listings[listingResourceID] != nil { - return &self.listings[listingResourceID] as! &Listing{ListingPublic} - } else { - return nil - } - } - - // cleanup - // Remove an listing *if* it has been purchased. - // Anyone can call, but at present it only benefits the account owner to do so. - // Kind purchasers can however call it if they like. - // - pub fun cleanup(listingResourceID: UInt64) { - pre { - self.listings[listingResourceID] != nil: "could not find listing with given id" - } - - let listing <- self.listings.remove(key: listingResourceID)! - assert(listing.getDetails().purchased == true, message: "listing is not purchased, only admin can remove") - destroy listing - } - - // destructor - // - destroy () { - destroy self.listings - - // Let event consumers know that this storefront will no longer exist - emit StorefrontDestroyed(storefrontResourceID: self.uuid) - } - - // constructor - // - init () { - self.listings <- {} - - // Let event consumers know that this storefront exists - emit StorefrontInitialized(storefrontResourceID: self.uuid) - } - } - - pub fun createStorefront(): @Storefront { - return <-create Storefront() - } -.... -} \ No newline at end of file diff --git a/cadence/transaction.cdc b/cadence/transaction.cdc deleted file mode 100644 index ab50e63..0000000 --- a/cadence/transaction.cdc +++ /dev/null @@ -1,13 +0,0 @@ -import NFTStorefront from 0x03 - -// This transaction sets up account 0x01 for the marketplace tutorial -// by publishing a Vault reference and creating an empty NFT Collection. -transaction { - prepare(acct: AuthAccount) { - - acct.save<@NFTStorefront.Storefront>(<- NFTStorefront.createStorefront() , to: NFTStorefront.StorefrontStoragePath) - - log("storefront created") - } -} - diff --git a/cadence/transaction.cdc b/cadence/transaction.cdc new file mode 120000 index 0000000..bb6daf2 --- /dev/null +++ b/cadence/transaction.cdc @@ -0,0 +1 @@ +./cadence/transactions/create_storefront.cdc \ No newline at end of file diff --git a/cadence/transactions/create_storefront.cdc b/cadence/transactions/create_storefront.cdc new file mode 100644 index 0000000..1f66c3b --- /dev/null +++ b/cadence/transactions/create_storefront.cdc @@ -0,0 +1,13 @@ +import "NFTStorefrontV2" + +// This transaction sets up the emulator account for the marketplace tutorial +// by publishing a Vault reference and creating an empty NFT Collection. +transaction { + prepare(acct: auth(Storage) &Account) { + + // Save the newly created storefront resource into account storage + acct.storage.save(<-NFTStorefrontV2.createStorefront(), to: NFTStorefrontV2.StorefrontStoragePath) + + log("Storefront created") + } +} diff --git a/emulator-account.pkey b/emulator-account.pkey new file mode 100644 index 0000000..75611bd --- /dev/null +++ b/emulator-account.pkey @@ -0,0 +1 @@ +0xdc07d83a937644ff362b279501b7f7a3735ac91a0f3647147acf649dda804e28 \ No newline at end of file diff --git a/explanations/contract.txt b/explanations/contract.txt index 3b2f81a..ddcfc5d 100644 --- a/explanations/contract.txt +++ b/explanations/contract.txt @@ -1,5 +1,4 @@ -The NFT Storefront contract is used to be able to list NFTS that a user owns for sale on your marketplace. Users can also purchase other NFTS, borrow listings, and do tons of other things related to the Storefront contract. -Before doing any of this, you need to create a Storefront resource from the NFTStorefront contract and save it to your account. Once that resource is saved you then have access to the Storefront resource which will give you the ability to create listings, remove listings, getListings, borrowListings, cleanupListings, and destroy your own Storefront. +The NFT Storefront contract enables users to list their NFTs for sale on a marketplace, purchase NFTs from others, borrow listings, and perform various other operations related to managing and interacting with the Storefront. To use these features, a user must first create a Storefront resource from the NFT Storefront contract and save it to their account. This Storefront resource acts as a gateway to functionalities such as creating and removing listings, retrieving and borrowing listings, cleaning up outdated listings, and even destroying the Storefront itself when no longer needed. -The createStorefront function allows anyone to create a storefront and save it to their account. +The createStorefront function facilitates this process, allowing anyone to easily create and save a Storefront resource to their account, unlocking full access to the Storefront’s capabilities. \ No newline at end of file diff --git a/explanations/transaction.txt b/explanations/transaction.txt index 6bd0a59..bec8f16 100644 --- a/explanations/transaction.txt +++ b/explanations/transaction.txt @@ -1 +1 @@ -Here you are simply calling the createStorefront function to save a storefront to your account. \ No newline at end of file +This transaction demonstrates how to set up a user account to participate in the marketplace by creating and saving a Storefront resource using the NFTStorefront contract. The createStorefront function is called to generate a new Storefront resource, which is then stored in the account's storage at the predefined NFTStorefront.StorefrontStoragePath. \ No newline at end of file diff --git a/flow.json b/flow.json new file mode 100644 index 0000000..7a88364 --- /dev/null +++ b/flow.json @@ -0,0 +1,78 @@ +{ + "contracts": {}, + "dependencies": { + "Burner": { + "source": "mainnet://f233dcee88fe0abe.Burner", + "hash": "71af18e227984cd434a3ad00bb2f3618b76482842bae920ee55662c37c8bf331", + "aliases": { + "emulator": "f8d6e0586b0a20c7", + "mainnet": "f233dcee88fe0abe", + "testnet": "9a0766d93b6608b7" + } + }, + "FungibleToken": { + "source": "mainnet://f233dcee88fe0abe.FungibleToken", + "hash": "050328d01c6cde307fbe14960632666848d9b7ea4fef03ca8c0bbfb0f2884068", + "aliases": { + "emulator": "ee82856bf20e2aa6", + "mainnet": "f233dcee88fe0abe", + "testnet": "9a0766d93b6608b7" + } + }, + "MetadataViews": { + "source": "mainnet://1d7e57aa55817448.MetadataViews", + "hash": "10a239cc26e825077de6c8b424409ae173e78e8391df62750b6ba19ffd048f51", + "aliases": { + "emulator": "f8d6e0586b0a20c7", + "mainnet": "1d7e57aa55817448", + "testnet": "631e88ae7f1d7c20" + } + }, + "NFTStorefrontV2": { + "source": "mainnet://4eb8a10cb9f87357.NFTStorefrontV2", + "hash": "f455b556f350f20b71de45ed71da42f0c60ab4d003e4e164e9f6e9d35e3c4c4b", + "aliases": { + "mainnet": "4eb8a10cb9f87357", + "testnet": "2d55b98eb200daef" + } + }, + "NonFungibleToken": { + "source": "mainnet://1d7e57aa55817448.NonFungibleToken", + "hash": "b63f10e00d1a814492822652dac7c0574428a200e4c26cb3c832c4829e2778f0", + "aliases": { + "emulator": "f8d6e0586b0a20c7", + "mainnet": "1d7e57aa55817448", + "testnet": "631e88ae7f1d7c20" + } + }, + "ViewResolver": { + "source": "mainnet://1d7e57aa55817448.ViewResolver", + "hash": "374a1994046bac9f6228b4843cb32393ef40554df9bd9907a702d098a2987bde", + "aliases": { + "emulator": "f8d6e0586b0a20c7", + "mainnet": "1d7e57aa55817448", + "testnet": "631e88ae7f1d7c20" + } + } + }, + "networks": { + "emulator": "127.0.0.1:3569", + "mainnet": "access.mainnet.nodes.onflow.org:9000", + "testing": "127.0.0.1:3569", + "testnet": "access.devnet.nodes.onflow.org:9000" + }, + "accounts": { + "emulator-account": { + "address": "f8d6e0586b0a20c7", + "key": { + "type": "file", + "location": "emulator-account.pkey" + } + } + }, + "deployments": { + "emulator": { + "emulator-account": ["NFTStorefrontV2"] + } + } +} diff --git a/index.js b/index.js index 429c57e..f5e9efa 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ const transactionPath = `${recipe}/cadence/transaction.cdc`; const smartContractExplanationPath = `${recipe}/explanations/contract.txt`; const transactionExplanationPath = `${recipe}/explanations/transaction.txt`; -export const createAMarketplace= { +export const createAMarketplace = { slug: recipe, title: "Create a Marketplace", createdAt: new Date(2022, 9, 14), @@ -23,7 +23,6 @@ export const createAMarketplace= { transactionCode: transactionPath, transactionExplanation: transactionExplanationPath, filters: { - difficulty: "intermediate" - } + difficulty: "intermediate", + }, }; -