From 16dd10153369c790ca8a98b282cebe218e295457 Mon Sep 17 00:00:00 2001 From: Lea Lobanov Date: Fri, 13 Dec 2024 03:17:51 +0400 Subject: [PATCH] Repo structure --- README.md | 61 +++++++++++++++++++++++++++++++++-------- cadence/contract.cdc | 22 +-------------- cadence/transaction.cdc | 37 +------------------------ 3 files changed, 51 insertions(+), 69 deletions(-) mode change 100644 => 120000 cadence/contract.cdc mode change 100644 => 120000 cadence/transaction.cdc diff --git a/README.md b/README.md index ad3d72d..1918983 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ This is included in your smart contract when you would like to implement token w - [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/withdraw_token.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 38f629f..0000000 --- a/cadence/contract.cdc +++ /dev/null @@ -1,21 +0,0 @@ -access(all) -resource interface Provider { - - // withdraw - // - // Function that subtracts tokens from the owner's Vault - // and returns a Vault resource (@Vault) with the removed tokens. - // - // The function's access level is public, but this isn't a problem - // because even public functions are not fully accessible unless the owner - // grants access by publishing a resource that exposes the withdraw function. - // - access(all) - fun withdraw(amount: UFix64): @Vault { - post { - // 'result' refers to the return value of the function - result.balance == UFix64(amount): - "Withdrawal amount must match the balance of the withdrawn Vault" - } - } -} diff --git a/cadence/contract.cdc b/cadence/contract.cdc new file mode 120000 index 0000000..b64184f --- /dev/null +++ b/cadence/contract.cdc @@ -0,0 +1 @@ +./cadence/contracts/Recipe.cdc \ No newline at end of file diff --git a/cadence/transaction.cdc b/cadence/transaction.cdc deleted file mode 100644 index 8d28d43..0000000 --- a/cadence/transaction.cdc +++ /dev/null @@ -1,36 +0,0 @@ -import ExampleToken from 0x01 - -// This transaction is a template for a transaction that -// could be used by anyone to send tokens to another account -// that owns a Vault -transaction { - - // Temporary Vault object that holds the balance that is being transferred - var temporaryVault: @ExampleToken.Vault - - prepare(acct: auth(Storage, Capabilities) &Account) { - // Withdraw tokens from your vault by borrowing a reference to it - // and calling the withdraw function with that reference - let vaultRef = acct.capabilities.storage.borrow<&ExampleToken.Vault>( - from: /storage/MainVault - ) ?? panic("Could not borrow a reference to the owner's vault") - - self.temporaryVault <- vaultRef.withdraw(amount: 10.0) - } - - execute { - // Get the recipient's public account object - let recipient = getAccount(0x01) - - // Get the recipient's Receiver reference to their Vault - // by borrowing the reference from the public capability - let receiverRef = recipient.capabilities.borrow<&ExampleToken.Vault{ExampleToken.Receiver}>( - /public/MainReceiver - ) ?? panic("Could not borrow a reference to the receiver") - - // Deposit your tokens to their Vault - receiverRef.deposit(from: <-self.temporaryVault) - - log("Transfer succeeded!") - } -} diff --git a/cadence/transaction.cdc b/cadence/transaction.cdc new file mode 120000 index 0000000..06714b4 --- /dev/null +++ b/cadence/transaction.cdc @@ -0,0 +1 @@ +./cadence/transactions/withdraw_token.cdc \ No newline at end of file