Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,24 @@ keywords:
- Flowscan
---

In this tutorial, you will **compose with someone else's contracts** on Flow testnet. You'll write a Cadence transaction that reads public state from a contract named `Counter` and only increments the counter when it is odd. Then you will extend the transaction to mint NFTs when the counter is odd, demonstrating how to compose multiple contracts in a single transaction. Everything runs against testnet using the Flow CLI and the dependency manager.
# Compose wth Cadence Transactions

## Overview

In this tutorial, you'll **compose with someone else's contracts** on Flow testnet. You'll write a Cadence transaction that reads public state from a contract named `Counter` and only increments the counter when it is odd. Then you'll extend the transaction to mint NFTs when the counter is odd, demonstrating how to compose multiple contracts in a single transaction. Everything runs against testnet using the Flow CLI and the dependency manager.

You can use transactions developed and tested this way from the frontend of your app.

## Objectives

After completing this guide, you will be able to:
After you complete this guide, you will be able to:

- Configure the Flow CLI _dependency manager_ to import named contracts from **testnet**
- Write a Cadence **transaction** that reads and writes to a public contract you did not deploy
- Run the transaction on **testnet** with a funded account using the Flow CLI
- Extend the transaction to compose multiple public contracts (`Counter` + `ExampleNFT` + `NonFungibleToken`) without redeploying anything
- Set up NFT collections and mint NFTs conditionally based on on-chain state
- View transaction results and NFT transfers using Flowscan
- Configure the Flow CLI _dependency manager_ to import named contracts from **testnet**.
- Write a Cadence **transaction** that reads and writes to a public contract you didn't deploy.
- Run the transaction on **testnet** with a funded account using the Flow command line interface (CLI).
- Extend the transaction to compose multiple public contracts (`Counter` + `ExampleNFT` + `NonFungibleToken`) without redeploying anything.
- Set up NFT collections and mint NFTs conditionally based on on-chain state.
- View transaction results and NFT transfers using Flowscan.

## Prerequisites

Expand All @@ -54,17 +58,17 @@ Follow the prompts and create a `Basic Cadence project (no dependencies)`.

### Install dependencies

We will resolve imports **using string format** (`import "Counter"`) using the [dependency manager].
We will resolve imports **using string format** (`import "Counter"`) with the [dependency manager].

This is the recommended way of working with imports of already-deployed contracts. You should also use the CLI to create new files and add existing ones to `flow.json`.
We recommend that you work this way with imports of already-deployed contracts. You should also use the CLI to create new files and add existing ones to `flow.json`.

:::warning

For this exercise, you need to **delete** the existing contract entry for `Counter` from your `flow.json`. You could also use an alias here, but this is simpler since you won't be deploying the `Counter` contract.
For this exercise, **delete** the existing contract entry for `Counter` from your `flow.json`. You could also use an alias here, but this is simpler since you won't deploy the `Counter` contract.

:::

You can install dependencies for already deployed contracts, whether yours or those deployed by others:
You can install dependencies for already deployed contracts, whether yours or those that others deployed:

```bash
# Add a deployed instance of the Counter contract
Expand All @@ -73,19 +77,19 @@ flow dependencies install testnet://0x8a4dce54554b225d.Counter

Pick `none` for the deployment account as you won't need to redeploy these contracts.

Once installed with the dependency manager, Cadence imports like `import "Counter"` will resolve to the testnet address when sending transactions on testnet.
After they're installed with the dependency manager, Cadence imports like `import "Counter"` will resolve to the testnet address when they send transactions on testnet.

:::info

In Cadence, contracts are deployed to the account storage of the deploying address. Due to security reasons, the same private key produces different address on Cadence testnet and mainnet. One of the features of the dependency manager is to automatically select the right address for imports based on the network you're working on.
In Cadence, contracts deploy to the account storage of the deploying address. Due to security reasons, the same private key produces different address on Cadence testnet and mainnet. One of the features of the dependency manager is to automatically select the right address for imports based on the network you're working on.

:::

---

## Compose with the public `Counter` contract

Review the `Counter` contract that's created as an example by `flow init`:
Review the `Counter` simple contract that's created as an example by `flow init`:

```cadence
access(all) contract Counter {
Expand Down Expand Up @@ -121,8 +125,6 @@ access(all) contract Counter {
}
```

It's an example of a simple contract.

Unlike in Solidity, apps aren't limited to the functionality deployed in a smart contract. One of the ways you can expand your app is to write new transactions that call multiple functions in multiple contracts, with branching based on conditions and state, using a single call and a single signature. You don't need to deploy a new contract, use a proxy, or switch to V2.

In this simple example, imagine that you've already deployed a product that has thousands of users and is dependent on the `Counter` smart contract. After a time, you realize that a significant portion of your users only wish to use the `increment` feature if the current `count` is odd, to try and make the number be even.
Expand Down Expand Up @@ -207,7 +209,7 @@ flow accounts fund testnet-account

:::danger

As with other blockchain accounts, once the private key for an account is compromised, anyone with that key has complete control over an account and it's assets. **Never** put private keys directly in `flow.json`.
As with other blockchain accounts, after an account's private key is compromised, anyone with that key completely controls an account and it's assets. **Never** put private keys directly in `flow.json`.

:::

Expand All @@ -219,11 +221,11 @@ Creating an account using the CLI automatically puts the private key in a `.pkey
flow transactions send cadence/transactions/IncrementIfOdd.cdc --signer testnet-account --network testnet
```

You should see logs that show the prior value and whether the increment occurred.
You will see logs that show the prior value and whether the increment occurred.

:::tip

This same transaction could be triggered **from an app** and **signed by a wallet** with a single user click. Your dApp would assemble and submit this exact Cadence transaction using your preferred client library, and the user's wallet would authorize it.
You could trigger this same transaction **from an app** and **signed by a wallet** with a single user click. Your dApp would assemble and submit this exact Cadence transaction using your preferred client library, and the user's wallet would authorize it.

:::

Expand All @@ -245,7 +247,7 @@ flow dependencies install testnet://012e4d204a60ac6f.ExampleNFT

:::warning

This repository uses different deployments for core contracts than those installed by the Flow CLI. If you previously installed core contract dependencies (like `NonFungibleToken`, `MetadataViews`, etc.) using the CLI, you should manually delete all `dependencies` except `Counter` from your `flow.json` file to avoid conflicts.
This repository uses different deployments for core contracts than those that the Flow CLI installs. If you previously installed core contract dependencies (like `NonFungibleToken`, `MetadataViews`, etc.) using the CLI, manually delete all `dependencies` except `Counter` from your `flow.json` file to avoid conflicts.

:::

Expand Down Expand Up @@ -341,7 +343,7 @@ transaction() {

### Setup NFT Collection

Before you can mint NFTs, you need to set up an NFT collection in your account. Create a transaction to do this:
Before you can mint NFTs, set up an NFT collection in your account. Create a transaction to do this:

```bash
flow generate transaction SetupCollection
Expand Down Expand Up @@ -392,7 +394,7 @@ flow transactions send cadence/transactions/IncrementCounter.cdc --signer testne

### View Your NFT

Click the transaction link in the console to view the transaction in [testnet Flowscan]. After running the transaction **while the counter is odd**, you'll see an NFT in the `Asset Transfers` tab.
Click the transaction link in the console to view the transaction in [testnet Flowscan]. After you run the transaction **while the counter is odd**, you'll see an NFT in the `Asset Transfers` tab.

:::info

Expand All @@ -416,12 +418,12 @@ In this tutorial, you learned how to compose with multiple on-chain contracts us

Now that you have completed the tutorial, you should be able to:

- Configure the Flow CLI _dependency manager_ to import named contracts from **testnet**
- Write a Cadence **transaction** that reads and writes to a public contract you did not deploy
- Run the transaction on **testnet** with a funded account using the Flow CLI
- Extend the transaction to compose multiple public contracts (`Counter` + `ExampleNFT` + `NonFungibleToken`) without redeploying anything
- Set up NFT collections and mint NFTs conditionally based on on-chain state
- View transaction results and NFT transfers using Flowscan
- Configure the Flow CLI _dependency manager_ to import named contracts from **testnet**.
- Write a Cadence **transaction** that reads and writes to a public contract you did not deploy.
- Run the transaction on **testnet** with a funded account using the Flow CLI.
- Extend the transaction to compose multiple public contracts (`Counter` + `ExampleNFT` + `NonFungibleToken`) without redeploying anything.
- Set up NFT collections and mint NFTs conditionally based on on-chain state.
- View transaction results and NFT transfers using Flowscan.

This approach gives you the freedom to build complex application logic that composes with any public contracts on Flow, making Cadence's composition model a powerful tool for developers building on Flow.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,30 @@ keywords:

# Cadence Advantages

This series explores the unique advantages and capabilities of Cadence smart contracts on Flow, demonstrating how Cadence's innovative features enable powerful development patterns that aren't possible on other blockchain platforms. From native data availability to seamless transaction composition, these tutorials showcase why Cadence represents the future of smart contract development.
This series explores the unique advantages and capabilities of Cadence smart contracts on Flow, and demonstrates how Cadence's innovative features allow powerful development patterns that aren't possible on other blockchain platforms. From native data availability to seamless transaction composition, these tutorials showcase why Cadence represents the future of smart contract development.

## Tutorials

### [Compose with Cadence Transactions]

Learn how to compose with someone else's on-chain contracts by writing a Cadence transaction that conditionally calls a public contract on testnet, then extend it to mint NFTs when conditions are metno redeploy required. This tutorial demonstrates Cadence's powerful composition model, showing how you can build complex application logic that interacts with multiple contracts in a single atomic transaction. You'll work with the Flow CLI dependency manager, learn to set up NFT collections, and view results using Flowscan.
Learn how to compose with someone else's on-chain contracts by writing a Cadence transaction that conditionally calls a public contract on testnet, then extend it to mint NFTs when conditions are met, with no redeploy required. This tutorial demonstrates Cadence's powerful composition model, which lets you build complex application logic that interacts with multiple contracts in a single atomic transaction. You'll work with the Flow command line interface (CLI) dependency manager, learn to set up NFT collections, and view results using Flowscan.

### [Native Data Availability with Cadence Scripts]

Discover how Cadence scripts provide native data availability, allowing you to query any on-chain data directly from Flow's state without relying on external indexers or APIs. This comprehensive tutorial shows you how to build scripts that can discover and query NFT collections across multiple child accounts using Hybrid Custody, then extend it to include both NBA Top Shot and NFL All Day NFTs. You'll learn to filter and process NFT collections, extract specific metadata, and compare Cadence's native data availability with Solidity's limitations.

### [Upgrading Cadence Contracts]

Learn how to upgrade deployed Cadence contracts through multiple incremental upgrades while preserving existing state and maintaining the same contract address. This tutorial demonstrates Cadence's sophisticated contract upgrade system through two realistic scenarios: first adding an event to notify users when the counter reaches an even number, then extending the contract with additional functionality like incrementing by 2 and checking if numbers are even. You'll understand what can and cannot be changed during upgrades, perform multiple contract updates using Flow CLI, and test upgraded functionality with comprehensive transactions and scripts.
Learn how to upgrade deployed Cadence contracts through multiple incremental upgrades while preserving existing state and maintaining the same contract address. This tutorial demonstrates Cadence's sophisticated contract upgrade system through two realistic scenarios:

- Adding an event to notify users when the counter reaches an even number.
- Extending the contract with additional functionality like incrementing by two and checking if numbers are even.

You'll understand what you can and can't change during upgrades, perform multiple contract updates using Flow CLI, and test upgraded functionality with comprehensive transactions and scripts.

## Conclusion

Cadence's unique features—from resource-oriented programming to native data availability, seamless transaction composition, and sophisticated contract upgrade capabilitiesrepresent a fundamental advancement in smart contract development. These tutorials demonstrate how Cadence enables developers to build sophisticated applications with capabilities that simply aren't possible on other blockchain platforms, while maintaining security and developer experience as core principles.
Cadence's unique features, such as resource-oriented programming to native data availability, seamless transaction composition, and sophisticated contract upgrade capabilities, represent a fundamental advancement in smart contract development. These tutorials demonstrate how Cadence allows developers to build sophisticated applications with capabilities that simply aren't possible on other blockchain platforms, while maintaining security and developer experience as core principles.

[Compose with Cadence Transactions]: ./compose-with-cadence-transactions.md
[Native Data Availability with Cadence Scripts]: ./native-data-availibility-with-cadence-scripts.md
Expand Down
Loading