Skip to content

Commit

Permalink
Merge pull request #2367 from onflow/sainati/sync-stable-cadence
Browse files Browse the repository at this point in the history
Sync Stable Cadence
  • Loading branch information
dsainati1 authored Mar 3, 2023
2 parents 17ad61e + 9c7b2b8 commit 929f849
Show file tree
Hide file tree
Showing 131 changed files with 14,570 additions and 1,990 deletions.
3 changes: 3 additions & 0 deletions docs/flow-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@
{
"href": "capability-based-access-control"
},
{
"href": "attachments"
},
{
"href": "contracts"
},
Expand Down
2 changes: 1 addition & 1 deletion docs/json-cadence-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ These are basic types like `Int`, `String`, or `StoragePath`.

```json
{
"kind": "Any" | "AnyStruct" | "AnyResource" | "Type" |
"kind": "Any" | "AnyStruct" | "AnyResource" | "AnyStructAttachment" | "AnyResourceAttachment" | "Type" |
"Void" | "Never" | "Bool" | "String" | "Character" |
"Bytes" | "Address" | "Number" | "SignedNumber" |
"Integer" | "SignedInteger" | "FixedPoint" |
Expand Down
203 changes: 109 additions & 94 deletions docs/language/accounts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,61 @@ Every account can be accessed through two types, `PublicAccount` and `AuthAccoun
**Public Account** objects have the type `PublicAccount`,
which represents the publicly available portion of an account.

```cadence
struct PublicAccount {
```cadence
struct PublicAccount {
let address: Address
// The FLOW balance of the default vault of this account
let balance: UFix64
// The FLOW balance of the default vault of this account that is available to be moved
let availableBalance: UFix64
// Amount of storage used by the account, in bytes
let storageUsed: UInt64
// storage capacity of the account, in bytes
let storageCapacity: UInt64
let address: Address
// The FLOW balance of the default vault of this account
let balance: UFix64
// The FLOW balance of the default vault of this account that is available to be moved
let availableBalance: UFix64
// Amount of storage used by the account, in bytes
let storageUsed: UInt64
// storage capacity of the account, in bytes
let storageCapacity: UInt64
// Contracts deployed to the account
let contracts: PublicAccount.Contracts
// Contracts deployed to the account
let contracts: PublicAccount.Contracts
// Keys assigned to the account
let keys: PublicAccount.Keys
// Keys assigned to the account
let keys: PublicAccount.Keys
// The public paths associated with this account
let publicPaths: [PublicPath]
// The public paths associated with this account
let publicPaths: [PublicPath]
// Storage operations
// Storage operations
fun getCapability<T>(_ path: PublicPath): Capability<T>
fun getLinkTarget(_ path: CapabilityPath): Path?
fun getCapability<T>(_ path: PublicPath): Capability<T>
fun getLinkTarget(_ path: CapabilityPath): Path?
// Storage iteration
fun forEachPublic(_ function: fun(PublicPath, Type): Bool)
// Storage iteration
fun forEachPublic(_ function: fun(PublicPath, Type): Bool)
struct Contracts {
struct Contracts {
let names: [String]
let names: [String]
fun get(name: String): DeployedContract?
fun get(name: String): DeployedContract?
fun borrow<T: &Any>(name: String): T?
}
fun borrow<T: &Any>(name: String): T?
}
struct Keys {
// Returns the key at the given index, if it exists.
// Revoked keys are always returned, but they have \`isRevoked\` field set to true.
fun get(keyIndex: Int): AccountKey?
struct Keys {
// Returns the key at the given index, if it exists.
// Revoked keys are always returned, but they have \`isRevoked\` field set to true.
fun get(keyIndex: Int): AccountKey?
// Iterate over all unrevoked keys in this account,
// passing each key in turn to the provided function.
// Iteration is stopped early if the function returns `false`.
// The order of iteration is undefined.
fun forEach(function: fun(AccountKey): Bool): Void
// Iterate over all unrevoked keys in this account,
// passing each key in turn to the provided function.
// Iteration is stopped early if the function returns `false`.
// The order of iteration is undefined.
fun forEach(function: fun(AccountKey): Bool): Void
// The total number of unrevoked keys in this account.
let count: UInt64
}
}
```
// The total number of unrevoked keys in this account.
let count: UInt64
}
}
```

Any code can get the `PublicAccount` for an account address
using the built-in `getAccount` function:
Expand Down Expand Up @@ -451,15 +451,17 @@ Both `StoragePath` and `CapabilityPath` are subtypes of `Path`.

#### Path Functions

- `cadence•fun toString(): String`
```cadence
fun toString(): String
````

Returns the string representation of the path.
Returns the string representation of the path.

```cadence
let storagePath = /storage/path
```cadence
let storagePath = /storage/path
storagePath.toString() // is "/storage/path"
```
storagePath.toString() // is "/storage/path"
```

There are also utilities to produce paths from strings:

Expand All @@ -481,62 +483,73 @@ let path = PublicPath(identifier: pathID) // is /public/foo
Account storage is accessed through the following functions of `AuthAccount`.
This means that any code that has access to the authorized account has access
to all its stored objects.

```cadence
fun save<T>(_ value: T, to: StoragePath)
```

- `cadence•fun save<T>(_ value: T, to: StoragePath)`
Saves an object to account storage.
Resources are moved into storage, and structures are copied.

Saves an object to account storage.
Resources are moved into storage, and structures are copied.
`T` is the type parameter for the object type.
It can be inferred from the argument's type.

`T` is the type parameter for the object type.
It can be inferred from the argument's type.
If there is already an object stored under the given path, the program aborts.

If there is already an object stored under the given path, the program aborts.
The path must be a storage path, i.e., only the domain `storage` is allowed.

The path must be a storage path, i.e., only the domain `storage` is allowed.

- `cadence•fun type(at path: StoragePath): Type?`
```cadence
fun type(at path: StoragePath): Type?
```

Reads the type of an object from the account's storage which is stored under the given path, or nil if no object is stored under the given path.
Reads the type of an object from the account's storage which is stored under the given path, or nil if no object is stored under the given path.

If there is an object stored, the type of the object is returned without modifying the stored object.
If there is an object stored, the type of the object is returned without modifying the stored object.

The path must be a storage path, i.e., only the domain `storage` is allowed
The path must be a storage path, i.e., only the domain `storage` is allowed

- `cadence•fun load<T>(from: StoragePath): T?`

Loads an object from account storage.
If no object is stored under the given path, the function returns `nil`.
If there is an object stored, the stored resource or structure is moved
out of storage and returned as an optional.
When the function returns, the storage no longer contains an object
under the given path.
```cadence
fun load<T>(from: StoragePath): T?
```

`T` is the type parameter for the object type.
A type argument for the parameter must be provided explicitly.
Loads an object from account storage.
If no object is stored under the given path, the function returns `nil`.
If there is an object stored, the stored resource or structure is moved
out of storage and returned as an optional.
When the function returns, the storage no longer contains an object
under the given path.

The type `T` must be a supertype of the type of the loaded object.
If it is not, execution will abort with an error.
The given type does not necessarily need to be exactly the same as the type of the loaded object.
`T` is the type parameter for the object type.
A type argument for the parameter must be provided explicitly.

The path must be a storage path, i.e., only the domain `storage` is allowed.
The type `T` must be a supertype of the type of the loaded object.
If it is not, execution will abort with an error.
The given type does not necessarily need to be exactly the same as the type of the loaded object.

- `cadence•fun copy<T: AnyStruct>(from: StoragePath): T?`
The path must be a storage path, i.e., only the domain `storage` is allowed.

Returns a copy of a structure stored in account storage, without removing it from storage.

If no structure is stored under the given path, the function returns `nil`.
If there is a structure stored, it is copied.
The structure stays stored in storage after the function returns.
```cadence
fun copy<T: AnyStruct>(from: StoragePath): T?
```

Returns a copy of a structure stored in account storage, without removing it from storage.

`T` is the type parameter for the structure type.
A type argument for the parameter must be provided explicitly.
If no structure is stored under the given path, the function returns `nil`.
If there is a structure stored, it is copied.
The structure stays stored in storage after the function returns.

The type `T` must be a supertype of the type of the copied structure.
If it is not, execution will abort with an error.
The given type does not necessarily need to be exactly the same as
the type of the copied structure.
`T` is the type parameter for the structure type.
A type argument for the parameter must be provided explicitly.

The path must be a storage path, i.e., only the domain `storage` is allowed.
The type `T` must be a supertype of the type of the copied structure.
If it is not, execution will abort with an error.
The given type does not necessarily need to be exactly the same as
the type of the copied structure.

The path must be a storage path, i.e., only the domain `storage` is allowed.

```cadence
// Declare a resource named `Counter`.
Expand Down Expand Up @@ -627,20 +640,22 @@ as it is necessary for resources,
it is also possible to create references to objects in storage:
This is possible using the `borrow` function of an `AuthAccount`:

- `cadence•fun borrow<T: &Any>(from: StoragePath): T?`
```cadence
fun borrow<T: &Any>(from: StoragePath): T?
```

Returns a reference to an object in storage without removing it from storage.
If no object is stored under the given path, the function returns `nil`.
If there is an object stored, a reference is returned as an optional.
Returns a reference to an object in storage without removing it from storage.
If no object is stored under the given path, the function returns `nil`.
If there is an object stored, a reference is returned as an optional.

`T` is the type parameter for the object type.
A type argument for the parameter must be provided explicitly.
The type argument must be a reference to any type (`&Any`; `Any` is the supertype of all types).
It must be possible to create the given reference type `T` for the stored / borrowed object.
If it is not, execution will abort with an error.
The given type does not necessarily need to be exactly the same as the type of the borrowed object.
`T` is the type parameter for the object type.
A type argument for the parameter must be provided explicitly.
The type argument must be a reference to any type (`&Any`; `Any` is the supertype of all types).
It must be possible to create the given reference type `T` for the stored / borrowed object.
If it is not, execution will abort with an error.
The given type does not necessarily need to be exactly the same as the type of the borrowed object.

The path must be a storage path, i.e., only the domain `storage` is allowed.
The path must be a storage path, i.e., only the domain `storage` is allowed.

```cadence
// Declare a resource interface named `HasCount`, that has a field `count`
Expand Down
Loading

0 comments on commit 929f849

Please sign in to comment.