Skip to content

Commit

Permalink
Debug tx
Browse files Browse the repository at this point in the history
  • Loading branch information
lealobanov committed Dec 11, 2024
1 parent 70f84d1 commit 493971f
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 20 deletions.
61 changes: 49 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ You've added plays in your set, now it's time to mint them. This code will mint
- [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_moment.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 Down Expand Up @@ -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

Expand Down
45 changes: 37 additions & 8 deletions cadence/transactions/mint_moment.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,52 @@ transaction {
let borrowedSet: &TopShot.Set
let receiverRef: &{TopShot.MomentCollectionPublic}

prepare(acct: AuthAccount) {
// Borrow the admin resource from the account's storage
self.admin = acct.borrow<&TopShot.Admin>(from: /storage/TopShotAdmin)
prepare(acct: auth(Storage, Capabilities) &Account) {
// Issue a capability for the admin resource and publish it for borrowing
let adminCap = acct.capabilities.storage.issue<&TopShot.Admin>(/storage/TopShotAdmin)
acct.capabilities.publish(adminCap, at: /public/TopShotAdminCap)

// Borrow the admin resource using the published capability
self.admin = acct.capabilities.borrow<&TopShot.Admin>(/public/TopShotAdminCap)
?? panic("Cannot borrow admin resource from storage")

// Borrow the Set resource using the admin's borrowSet function
self.borrowedSet = self.admin.borrowSet(setID: 1)
// Ensure the Set resource exists
if acct.storage.borrow<&TopShot.Set>(from: /storage/TopShotSet) == nil {
let newSet = self.admin.createSet(name: "test_set")
acct.storage.save(newSet, to: /storage/TopShotSet)
}

// Borrow the specified Set from the admin
self.borrowedSet = self.admin.borrowSet(setID: 1)

// Issue a capability for the MomentCollection and publish it
let momentCap = acct.capabilities.storage.issue<&{TopShot.MomentCollectionPublic}>(/storage/MomentCollection)
acct.capabilities.publish(momentCap, at: /public/MomentCollectionCap)

// Borrow the recipient's MomentCollectionPublic reference
self.receiverRef = acct.getCapability<&{TopShot.MomentCollectionPublic}>(/public/MomentCollection)
.borrow()
self.receiverRef = acct.capabilities.borrow<&{TopShot.MomentCollectionPublic}>(/public/MomentCollectionCap)
?? panic("Cannot borrow the MomentCollection reference")
}

execute {
// Create plays if they don't already exist
let playIDs: [UInt32] = [1, 2, 3]
for playID in playIDs {
if TopShot.getPlayMetaData(playID: playID) == nil {
let metadata: {String: String} = {
"Player": "Player Name ".concat(playID.toString()),
"Play": "Play Description ".concat(playID.toString())
}
self.admin.createPlay(metadata: metadata)
}
}

self.borrowedSet.addPlay(playID: 1)
self.borrowedSet.addPlay(playID: 2)
self.borrowedSet.addPlay(playID: 3)

// Mint moments using the borrowed set
let mintedMoments <- self.borrowedSet.batchMintMoment(playID: 3, quantity: 3)
let mintedMoments <- self.borrowedSet.batchMintMoment(playID: 1, quantity: 3)

// Deposit the minted moments into the recipient's collection
self.receiverRef.batchDeposit(tokens: <-mintedMoments)
Expand Down

0 comments on commit 493971f

Please sign in to comment.