Skip to content

Cadence 1.0 #739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 14, 2024
Merged
34 changes: 17 additions & 17 deletions docs/build/advanced-concepts/metadata-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ which can be applied either by the contract (in the case of `ViewResolver`)
or by an individual NFT (in the case of `MetadataViews.Resolver`).

```cadence
pub fun getViews(): [Type] {
access(all) fun getViews(): [Type] {
return [
Type<MetadataViews.Display>(),
Type<MetadataViews.Royalties>(),
Expand All @@ -134,7 +134,7 @@ The caller provides the type of the view they want to query as the only argument
and the view is returned if it exists, and `nil` is returned if it doesn't.

```cadence
pub fun resolveView(_ view: Type): AnyStruct? {
access(all) fun resolveView(_ view: Type): AnyStruct? {
switch view {
case Type<MetadataViews.Display>():
...
Expand Down Expand Up @@ -170,11 +170,11 @@ and assures that the metadata of our NFT can be consistently accessed
and understood by various platforms and services that interact with NFTs.

```cadence
pub resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver {
pub let id: UInt64
pub let name: String
pub let description: String
pub let thumbnail: String
access(all) resource NFT: NonFungibleToken.INFT, MetadataViews.Resolver {
access(all) let id: UInt64
access(all) let name: String
access(all) let description: String
access(all) let thumbnail: String
access(self) let royalties: [MetadataViews.Royalty]
access(self) let metadata: {String: AnyStruct}
...
Expand Down Expand Up @@ -305,11 +305,11 @@ or other stakeholders on secondary sales.

Each royalty view contains a fungible token receiver capability where royalties should be paid:
```cadence
pub struct Royalty {
access(all) struct Royalty {

pub let receiver: Capability<&AnyResource{FungibleToken.Receiver}>
access(all) let receiver: Capability<&AnyResource{FungibleToken.Receiver}>

pub let cut: UFix64
access(all) let cut: UFix64
}
```

Expand Down Expand Up @@ -391,21 +391,21 @@ case Type<MetadataViews.ExternalURL>():
The [`Trait`](https://github.com/onflow/flow-nft/blob/master/contracts/MetadataViews.cdc#L655) view type encapsulates the unique attributes of an NFT, like any visual aspects or category-defining properties. These can be essential for marketplaces that need to sort or filter NFTs based on these characteristics.
By returning trait views as recommended, you can fit the data in the places you want.
```cadence
pub struct Trait {
access(all) struct Trait {
// The name of the trait. Like Background, Eyes, Hair, etc.
pub let name: String
access(all) let name: String

// The underlying value of the trait
pub let value: AnyStruct
access(all) let value: AnyStruct

// displayType is used to show some context about what this name and value represent
// for instance, you could set value to a unix timestamp, and specify displayType as "Date" to tell
// platforms to consume this trait as a date and not a number
pub let displayType: String?
access(all) let displayType: String?

// Rarity can also be used directly on an attribute.
// This is optional because not all attributes need to contribute to the NFT's rarity.
pub let rarity: Rarity?
access(all) let rarity: Rarity?
```

The traits view is extremely important to get right, because many third-party apps
Expand Down Expand Up @@ -434,7 +434,7 @@ When resolving the view, the wrapper view should be the returned value,
instead of returning the single view or just an array of several occurrences of the view.

```cadence
pub fun resolveView(_ view: Type): AnyStruct? {
access(all) fun resolveView(_ view: Type): AnyStruct? {
switch view {
case Type<MetadataViews.Editions>():
let editionInfo = MetadataViews.Edition(name: "Example NFT Edition", number: self.id, max: nil)
Expand Down Expand Up @@ -543,7 +543,7 @@ to get information about how to set up or show your collection.
import ViewResolver from 0xf8d6e0586b0a20c7
import MetadataViews from 0xf8d6e0586b0a20c7

pub fun main(addr: Address, name: String): StoragePath? {
access(all) fun main(addr: Address, name: String): StoragePath? {
let t = Type<MetadataViews.NFTCollectionData>()
let borrowedContract = getAccount(addr).contracts.borrow<&ViewResolver>(name: name) ?? panic("contract could not be borrowed")

Expand Down
14 changes: 7 additions & 7 deletions docs/build/basics/fees.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ The cost for transactions can be calculated using the following FCL scripts on m

```cadence
import FlowFees from 0xf919ee77447b7497
pub fun main(
access(all) fun main(
inclusionEffort: UFix64,
executionEffort: UFix64
): UFix64 {
Expand All @@ -164,7 +164,7 @@ pub fun main(

```cadence
import FlowFees from 0x912d5440f7e3769e
pub fun main(
access(all) fun main(
inclusionEffort: UFix64,
executionEffort: UFix64
): UFix64 {
Expand Down Expand Up @@ -256,7 +256,7 @@ Whenever you want to iterate over a list, make sure it is necessary to iterate t

```cadence
// Iterating over long lists can be costly
pub fun sum(list: [Int]): Int {
access(all) fun sum(list: [Int]): Int {
var total = 0
var i = 0
// if list grows too large, this might not be possible anymore
Expand All @@ -267,7 +267,7 @@ pub fun sum(list: [Int]): Int {
}

// Consider designing transactions (and scripts) in a way where work can be "chunked" into smaller pieces
pub fun partialSum(list: [Int], start: Int, end: Int): Int {
access(all) fun partialSum(list: [Int], start: Int, end: Int): Int {
var partialTotal = 0
var i = start
while i < end {
Expand All @@ -284,7 +284,7 @@ Some functions will require more execution efforts than others. You should caref
```cadence
// be aware functions that call a lot of other functions
// (or call themselves) might cost a lot
pub fun fib(_ x: Int): Int {
access(all) fun fib(_ x: Int): Int {
if x == 1 || x== 0 {
return x
}
Expand All @@ -293,15 +293,15 @@ pub fun fib(_ x: Int): Int {
}

// consider inlining functions with single statements, to reduce costs
pub fun add(_ a: Int, _ b: Int): Int {
access(all) fun add(_ a: Int, _ b: Int): Int {
// single statement; worth inlining
return a + b
}
```

**Avoid excessive load and save operations**

Avoid costly loading and storage operations and [borrow references](../smart-contracts/best-practices/design-patterns.md#avoid-excessive-load-and-save-storage-operations-prefer-in-place-mutations) where possible, for example:
Avoid costly loading and storage operations and [borrow references](https://cadence-lang.org/docs/1.0/design-patterns#avoid-excessive-load-and-save-storage-operations-prefer-in-place-mutations) where possible, for example:

```cadence
transaction {
Expand Down
8 changes: 4 additions & 4 deletions docs/build/basics/scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ Scripts are defined by the following the Cadence code:

```cadence
// The 'main' function is the entry point function and every script needs to have one.
pub fun main() {
access(all) fun main() {
// Cadence statements to be executed go here
}
```

Scripts can return a typed value:

```cadence
pub fun main(): Int {
access(all) fun main(): Int {
return 1 + 2
}
```

Scripts can also accept arguments:

```cadence
pub fun main(arg: String): String {
access(all) fun main(arg: String): String {
return "Hello ".concat(arg)
}
```
Expand All @@ -43,7 +43,7 @@ Scripts can call contract functions and query the state of a contract. To call a
```cadence
import World from 0x01

pub fun main(): String {
access(all) fun main(): String {
return World.hello()
}
```
Expand Down
14 changes: 7 additions & 7 deletions docs/build/core-contracts/03-flow-token.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Event that is emitted when the contract gets created.
- Testnet event: `A.7e60df042a9c0868.FlowToken.TokensInitialized`

```cadence
pub event TokensInitialized(initialSupply: UFix64)
access(all) event TokensInitialized(initialSupply: UFix64)
```

| Field | Type | Description |
Expand All @@ -72,7 +72,7 @@ Event that is emitted when tokens get withdrawn from a Vault.
- Testnet event: `A.7e60df042a9c0868.FlowToken.TokensWithdrawn`

```cadence
pub event TokensWithdrawn(amount: UFix64, from: Address?)
access(all) event TokensWithdrawn(amount: UFix64, from: Address?)
```

| Field | Type | Description |
Expand All @@ -90,7 +90,7 @@ Event that is emitted when tokens get deposited to a Vault.
- Testnet event: `A.7e60df042a9c0868.FlowToken.TokensDeposited`

```cadence
pub event TokensDeposited(amount: UFix64, to: Address?)
access(all) event TokensDeposited(amount: UFix64, to: Address?)
```

| Field | Type | Description |
Expand All @@ -107,7 +107,7 @@ Event that is emitted when new tokens gets minted.
- Testnet event: `A.7e60df042a9c0868.FlowToken.TokensMinted`

```cadence
pub event TokensMinted(amount: UFix64)
access(all) event TokensMinted(amount: UFix64)
```

| Field | Type | Description |
Expand All @@ -123,7 +123,7 @@ Event that is emitted when tokens get destroyed.
- Testnet event: `A.7e60df042a9c0868.FlowToken.TokensBurned`

```cadence
pub event TokensBurned(amount: UFix64)
access(all) event TokensBurned(amount: UFix64)
```

| Field | Type | Description |
Expand All @@ -140,7 +140,7 @@ Event that is emitted when a new minter resource gets created.
- Testnet event: `A.7e60df042a9c0868.FlowToken.MinterCreated`

```cadence
pub event MinterCreated(allowedAmount: UFix64)
access(all) event MinterCreated(allowedAmount: UFix64)
```

| Field | Type | Description |
Expand All @@ -156,7 +156,7 @@ Event that is emitted when a new burner Resource gets created.
- Testnet event: `A.7e60df042a9c0868.FlowToken.BurnerCreated`

```cadence
pub event BurnerCreated()
access(all) event BurnerCreated()
```

### Staking Events
Expand Down
42 changes: 21 additions & 21 deletions docs/build/core-contracts/06-staking-contract-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ The `FlowIDTableStaking` contract emits an event whenever an important action oc
See the [staking events Documentation](../../networks/staking/07-staking-scripts-events.md) for more information about each event.

```cadence
pub event NewEpoch(totalStaked: UFix64, totalRewardPayout: UFix64)
access(all) event NewEpoch(totalStaked: UFix64, totalRewardPayout: UFix64)

/// Node Events
pub event NewNodeCreated(nodeID: String, role: UInt8, amountCommitted: UFix64)
pub event TokensCommitted(nodeID: String, amount: UFix64)
pub event TokensStaked(nodeID: String, amount: UFix64)
pub event TokensUnstaking(nodeID: String, amount: UFix64)
pub event TokensUnstaked(nodeID: String, amount: UFix64)
pub event NodeRemovedAndRefunded(nodeID: String, amount: UFix64)
pub event RewardsPaid(nodeID: String, amount: UFix64)
pub event UnstakedTokensWithdrawn(nodeID: String, amount: UFix64)
pub event RewardTokensWithdrawn(nodeID: String, amount: UFix64)
access(all) event NewNodeCreated(nodeID: String, role: UInt8, amountCommitted: UFix64)
access(all) event TokensCommitted(nodeID: String, amount: UFix64)
access(all) event TokensStaked(nodeID: String, amount: UFix64)
access(all) event TokensUnstaking(nodeID: String, amount: UFix64)
access(all) event TokensUnstaked(nodeID: String, amount: UFix64)
access(all) event NodeRemovedAndRefunded(nodeID: String, amount: UFix64)
access(all) event RewardsPaid(nodeID: String, amount: UFix64)
access(all) event UnstakedTokensWithdrawn(nodeID: String, amount: UFix64)
access(all) event RewardTokensWithdrawn(nodeID: String, amount: UFix64)

/// Delegator Events
pub event NewDelegatorCreated(nodeID: String, delegatorID: UInt32)
pub event DelegatorTokensCommitted(nodeID: String, delegatorID: UInt32, amount: UFix64)
pub event DelegatorTokensStaked(nodeID: String, delegatorID: UInt32, amount: UFix64)
pub event DelegatorTokensUnstaking(nodeID: String, delegatorID: UInt32, amount: UFix64)
pub event DelegatorTokensUnstaked(nodeID: String, delegatorID: UInt32, amount: UFix64)
pub event DelegatorRewardsPaid(nodeID: String, delegatorID: UInt32, amount: UFix64)
pub event DelegatorUnstakedTokensWithdrawn(nodeID: String, delegatorID: UInt32, amount: UFix64)
pub event DelegatorRewardTokensWithdrawn(nodeID: String, delegatorID: UInt32, amount: UFix64)
access(all) event NewDelegatorCreated(nodeID: String, delegatorID: UInt32)
access(all) event DelegatorTokensCommitted(nodeID: String, delegatorID: UInt32, amount: UFix64)
access(all) event DelegatorTokensStaked(nodeID: String, delegatorID: UInt32, amount: UFix64)
access(all) event DelegatorTokensUnstaking(nodeID: String, delegatorID: UInt32, amount: UFix64)
access(all) event DelegatorTokensUnstaked(nodeID: String, delegatorID: UInt32, amount: UFix64)
access(all) event DelegatorRewardsPaid(nodeID: String, delegatorID: UInt32, amount: UFix64)
access(all) event DelegatorUnstakedTokensWithdrawn(nodeID: String, delegatorID: UInt32, amount: UFix64)
access(all) event DelegatorRewardTokensWithdrawn(nodeID: String, delegatorID: UInt32, amount: UFix64)

/// Contract Field Change Events
pub event NewDelegatorCutPercentage(newCutPercentage: UFix64)
pub event NewWeeklyPayout(newPayout: UFix64)
pub event NewStakingMinimums(newMinimums: {UInt8: UFix64})
access(all) event NewDelegatorCutPercentage(newCutPercentage: UFix64)
access(all) event NewWeeklyPayout(newPayout: UFix64)
access(all) event NewStakingMinimums(newMinimums: {UInt8: UFix64})
```
32 changes: 16 additions & 16 deletions docs/build/core-contracts/10-nft-storefront.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,13 @@ For NFT listings in marketplaces which don't require commission, commission rece

```cadence
resource interface ListingPublic {
pub fun borrowNFT(): &NonFungibleToken.NFT?
pub fun purchase(
access(all) fun borrowNFT(): &NonFungibleToken.NFT?
access(all) fun purchase(
payment: @FungibleToken.Vault,
commissionRecipient: Capability<&{FungibleToken.Receiver}>?,
): @NonFungibleToken.NFT
pub fun getDetails(): ListingDetail
pub fun getAllowedCommissionReceivers(): [Capability<&{FungibleToken.Receiver}>]?
access(all) fun getDetails(): ListingDetail
access(all) fun getAllowedCommissionReceivers(): [Capability<&{FungibleToken.Receiver}>]?
}
```
An interface providing a useful public interface to a Listing.
Expand Down Expand Up @@ -204,7 +204,7 @@ If it returns `nil` then commission is up to grab by anyone.

```cadence
resource Storefront {
pub fun createListing(
access(all) fun createListing(
nftProviderCapability: Capability<&{NonFungibleToken.Provider, NonFungibleToken.CollectionPublic}>,
nftType: Type,
nftID: UInt64,
Expand All @@ -215,11 +215,11 @@ resource Storefront {
commissionAmount: UFix64,
expiry: UInt64
): UInt64
pub fun removeListing(listingResourceID: UInt64)
pub fun getListingIDs(): [UInt64]
pub fun getDuplicateListingIDs(nftType: Type, nftID: UInt64, listingID: UInt64): [UInt64]
pub fun cleanupExpiredListings(fromIndex: UInt64, toIndex: UInt64)
pub fun borrowListing(listingResourceID: UInt64): &Listing{ListingPublic}?
access(all) fun removeListing(listingResourceID: UInt64)
access(all) fun getListingIDs(): [UInt64]
access(all) fun getDuplicateListingIDs(nftType: Type, nftID: UInt64, listingID: UInt64): [UInt64]
access(all) fun cleanupExpiredListings(fromIndex: UInt64, toIndex: UInt64)
access(all) fun borrowListing(listingResourceID: UInt64): &Listing{ListingPublic}?
}
```
A resource that allows its owner to manage a list of Listings, and anyone to interact with them
Expand Down Expand Up @@ -302,12 +302,12 @@ Returns a read-only view of the listing for the given listingID if it is contain

```cadence
resource interface StorefrontPublic {
pub fun getListingIDs(): [UInt64]
pub fun getDuplicateListingIDs(nftType: Type, nftID: UInt64, listingID: UInt64): [UInt64]
pub fun cleanupExpiredListings(fromIndex: UInt64, toIndex: UInt64)
pub fun borrowListing(listingResourceID: UInt64): &Listing{ListingPublic}?
pub fun cleanupPurchasedListings(listingResourceID: UInt64)
pub fun getExistingListingIDs(nftType: Type, nftID: UInt64): [UInt64]
access(all) fun getListingIDs(): [UInt64]
access(all) fun getDuplicateListingIDs(nftType: Type, nftID: UInt64, listingID: UInt64): [UInt64]
access(all) fun cleanupExpiredListings(fromIndex: UInt64, toIndex: UInt64)
access(all) fun borrowListing(listingResourceID: UInt64): &Listing{ListingPublic}?
access(all) fun cleanupPurchasedListings(listingResourceID: UInt64)
access(all) fun getExistingListingIDs(nftType: Type, nftID: UInt64): [UInt64]
}
```

Expand Down
10 changes: 5 additions & 5 deletions docs/build/core-contracts/11-staking-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ to be interacted with using the Staking Collection API.
The `StakingCollection` contract emits an event whenever an important action occurs.

```cadence
pub event NodeAddedToStakingCollection(nodeID: String, role: UInt8, amountCommitted: UFix64, address: Address?)
pub event DelegatorAddedToStakingCollection(nodeID: String, delegatorID: UInt32, amountCommitted: UFix64, address: Address?)
access(all) event NodeAddedToStakingCollection(nodeID: String, role: UInt8, amountCommitted: UFix64, address: Address?)
access(all) event DelegatorAddedToStakingCollection(nodeID: String, delegatorID: UInt32, amountCommitted: UFix64, address: Address?)

pub event NodeRemovedFromStakingCollection(nodeID: String, role: UInt8, address: Address?)
pub event DelegatorRemovedFromStakingCollection(nodeID: String, delegatorID: UInt32, address: Address?)
access(all) event NodeRemovedFromStakingCollection(nodeID: String, role: UInt8, address: Address?)
access(all) event DelegatorRemovedFromStakingCollection(nodeID: String, delegatorID: UInt32, address: Address?)

pub event MachineAccountCreated(nodeID: String, role: UInt8, address: Address)
access(all) event MachineAccountCreated(nodeID: String, role: UInt8, address: Address)
```
2 changes: 1 addition & 1 deletion docs/build/differences-vs-evm/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ The same powerful concept also exists for querying the blockchain state using Sc
import NonFungibleToken from 0x631e88ae7f1d7c20
import ExampleNFT from 0x2bd9d8989a3352a1

pub fun main(address: Address, collectionPublicPath: PublicPath): [UInt64] {
access(all) fun main(address: Address, collectionPublicPath: PublicPath): [UInt64] {

let account = getAccount(address)

Expand Down
Loading
Loading