Building a Contract

Contracts can be created, updated, and removed using the contracts object of authorized accounts. This functionality is covered in the next section

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

Contracts cannot be nested in each other.

pub contract SomeContract {

// ...

}

pub contract Invalid {

pub contract Nested {

}

}

Each lesson will include a short explanation of the concept, followed by a quiz. The quiz will present you with a series of questions about the concept. You will be able to answer the questions by writing Cadence code in the editor on the right side of the screen.

Deploying a Contract

A new contract can be deployed to an account using the add function:

The code parameter is the UTF-8 encoded representation of the source code. The code must contain exactly one contract or contract interface, which must have the same name as the name parameter.

fun add(

name: String,

code: [UInt8],

... contractInitializerArguments

): DeployedContract

Getting a Deployed Contract

A deployed contract can be gotten from an account using the get function, it returns the deployed contract for the contract/contract interface with the given name in the account, if any.

Returns nil if no contract/contract interface with the given name exists in the account.

let signer: AuthAccount = ...

let contract = signer.contracts.get(name: "Test")

Borrowing a Deployed Contract

In contrast to a static contract import import T from 0x1, which will always perform an import of a type, contracts can be "borrowed" to effectively perform a dynamic import dependent on a specific execution path.

For example, assuming that a contract named Test which conforms to the TestInterface interface is deployed to an account, the contract can be retrieved as follows:

let signer: AuthAccount = ...

let contract: &TestInterface = signer.contracts.borrow<&TestInterface>(name: "Test")

// A deployed contract can be removed from an account

// using the remove function:

fun remove(name: String): DeployedContract?