Skip to main content
Version: 1.0

Contracts

A contract is a collection of type definitions, data (its state), and code (its functions), that is stored in the contract storage area of an account.

Contracts are where all composite types interfaces for these types have to be defined. Therefore, an object of one of these types cannot exist without having been defined in a deployed Cadence contract.

Contracts can be deployed to accounts, updated, and removed from accounts using the contracts object of authorized accounts. See the account contracts page for more information about these operations.

Contracts are types. They are similar to composite types, but are stored differently than structs or resources and cannot be used as values, copied, or moved like resources or structs.

Contracts stay in an account's contract storage area and can only be added, updated, or removed by the account owner with special commands.

Contracts are declared using the contract keyword. The keyword is followed by the name of the contract.


_10
access(all)
_10
contract SomeContract {
_10
// ...
_10
}

Contracts cannot be nested in each other.


_10
access(all)
_10
contract Invalid {
_10
_10
// Invalid: Contracts cannot be nested in any other type.
_10
//
_10
access(all)
_10
contract Nested {
_10
// ...
_10
}
_10
}

One of the simplest forms of a contract would just be one with a state field, a function, and an initializer that initializes the field:


_20
access(all)
_20
contract HelloWorld {
_20
_20
// Declare a stored state field in HelloWorld
_20
//
_20
access(all)
_20
let greeting: String
_20
_20
// Declare a function that can be called by anyone
_20
// who imports the contract
_20
//
_20
access(all)
_20
fun hello(): String {
_20
return self.greeting
_20
}
_20
_20
init() {
_20
self.greeting = "Hello World!"
_20
}
_20
}

Transactions and other contracts can interact with contracts by importing them at the beginning of a transaction or contract definition.

Anyone could call the above contract's hello function by importing the contract from the account it was deployed to and using the imported object to call the hello function.


_18
import HelloWorld from 0x42
_18
_18
// Invalid: The contract does not know where hello comes from
_18
//
_18
log(hello()) // Error
_18
_18
// Valid: Using the imported contract object to call the hello
_18
// function
_18
//
_18
log(HelloWorld.hello()) // prints "Hello World!"
_18
_18
// Valid: Using the imported contract object to read the greeting
_18
// field.
_18
log(HelloWorld.greeting) // prints "Hello World!"
_18
_18
// Invalid: Cannot call the init function after the contract has been created.
_18
//
_18
HelloWorld.init() // Error

There can be any number of contracts per account and they can include an arbitrary amount of data. This means that a contract can have any number of fields, functions, and type definitions, but they have to be in the contract and not another top-level definition.


_14
// Invalid: Top-level declarations are restricted to only be contracts
_14
// or contract interfaces. Therefore, all of these would be invalid
_14
// if they were deployed to the account contract storage and
_14
// the deployment would be rejected.
_14
//
_14
access(all)
_14
resource Vault {}
_14
_14
access(all)
_14
struct Hat {}
_14
_14
access(all)
_14
fun helloWorld(): String {}
_14
let num: Int

Another important feature of contracts is that instances of resources and events that are declared in contracts can only be created/emitted within functions or types that are declared in the same contract.

It is not possible create instances of resources and events outside the contract.

The contract below defines a resource interface Receiver and a resource Vault that implements that interface. The way this example is written, there is no way to create this resource, so it would not be usable.


_52
// Valid
_52
access(all)
_52
contract FungibleToken {
_52
_52
access(all)
_52
resource interface Receiver {
_52
_52
access(all)
_52
balance: Int
_52
_52
access(all)
_52
fun deposit(from: @{Receiver}) {
_52
pre {
_52
from.balance > 0:
_52
"Deposit balance needs to be positive!"
_52
}
_52
post {
_52
self.balance == before(self.balance) + before(from.balance):
_52
"Incorrect amount removed"
_52
}
_52
}
_52
}
_52
_52
access(all)
_52
resource Vault: Receiver {
_52
_52
// keeps track of the total balance of the accounts tokens
_52
access(all)
_52
var balance: Int
_52
_52
init(balance: Int) {
_52
self.balance = balance
_52
}
_52
_52
// withdraw subtracts amount from the vaults balance and
_52
// returns a vault object with the subtracted balance
_52
access(all)
_52
fun withdraw(amount: Int): @Vault {
_52
self.balance = self.balance - amount
_52
return <-create Vault(balance: amount)
_52
}
_52
_52
// deposit takes a vault object as a parameter and adds
_52
// its balance to the balance of the Account's vault, then
_52
// destroys the sent vault because its balance has been consumed
_52
access(all)
_52
fun deposit(from: @{Receiver}) {
_52
self.balance = self.balance + from.balance
_52
destroy from
_52
}
_52
}
_52
}

If a user tried to run a transaction that created an instance of the Vault type, the type checker would not allow it because only code in the FungibleToken contract can create new Vaults.


_10
import FungibleToken from 0x42
_10
_10
// Invalid: Cannot create an instance of the `Vault` type outside
_10
// of the contract that defines `Vault`
_10
//
_10
let newVault <- create FungibleToken.Vault(balance: 10)

Account access​

Contracts can access the account they are deployed to: contracts have the implicit field named account which is only accessible within the contract.


_10
let account: auth(Storage, Keys, Contracts, Inbox, Capabilities) &Account`,

The account reference is fully entitled, so grants access to the account's storage, keys, contracts, etc.

For example, this gives the contract the ability to write to the account's storage when the contract is initialized.


_10
init(balance: Int) {
_10
self.account.storage.save(
_10
<-create Vault(balance: 1000),
_10
to: /storage/initialVault
_10
)
_10
}

Contract interfaces​

Like composite types, contracts can have interfaces that specify rules about their behavior, their types, and the behavior of their types.

Contract interfaces have to be declared globally. Declarations cannot be nested in other types.

Contract interfaces may not declare concrete types (other than events), but they can declare interfaces. If a contract interface declares an interface type, the implementing contract does not have to also define that interface. They can refer to that nested interface by saying {ContractInterfaceName}.{NestedInterfaceName}


_31
// Declare a contract interface that declares an interface and a resource
_31
// that needs to implement that interface in the contract implementation.
_31
//
_31
access(all)
_31
contract interface InterfaceExample {
_31
_31
// Implementations do not need to declare this
_31
// They refer to it as InterfaceExample.NestedInterface
_31
//
_31
access(all)
_31
resource interface NestedInterface {}
_31
_31
// Implementations must declare this type
_31
//
_31
access(all)
_31
resource Composite: NestedInterface {}
_31
}
_31
_31
access(all)
_31
contract ExampleContract: InterfaceExample {
_31
_31
// The contract doesn't need to redeclare the `NestedInterface` interface
_31
// because it is already declared in the contract interface
_31
_31
// The resource has to refer to the resource interface using the name
_31
// of the contract interface to access it
_31
//
_31
access(all)
_31
resource Composite: InterfaceExample.NestedInterface {
_31
}
_31
}