Contracts
Accounts store contracts. A contract can also just be an interface.
An account exposes its inbox through the contracts
field,
which has the type Account.Contracts
.
Account.Contracts
Deployed contract
Accounts store "deployed contracts," that is, the code of the contract:
Note that this is type only provides information about a deployed contract, it is not the contract instance, the result of importing a contract.
Getting a deployed contract
The function contracts.get
retrieves a deployed contract:
The function returns the deployed contract with the given name, if any.
If no contract with the given name exists in the account, the function returns nil
.
For example, assuming that an account has a contract named Test
deployed to it,
the contract can be retrieved as follows:
Borrowing a deployed contract
Contracts can be "borrowed" to effectively perform a dynamic import dependent on a specific execution path.
This is in contrast to a typical import statement, for example import T from 0x1
,
which statically imports a contract.
The contracts.borrow
function obtains a reference to a contract instance:
The functions returns a reference to the contract instance stored with that name on the account,
if it exists, and if it has the provided type T
.
If no contract with the given name exists in the account, the function returns nil
.
For example, assuming that a contract named Test
which conforms to the TestInterface
interface is deployed to an account,
a reference to the contract instance can obtained be as follows:
This is similar to the import statement
Deploying a new contract
The contracts.add
function deploys a new contract to an account:
Calling the add
function requires access to an account via a reference which is authorized
with the coarse-grained Contracts
entitlement (auth(Contracts) &Account
),
or the fine-grained AddContract
entitlement (auth(AddContract) &Account
).
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.
The add
function passes all extra arguments of the call (contractInitializerArguments
)
to the initializer of the contract.
If a contract with the given name already exists in the account, if the given code does not declare exactly one contract or contract interface, or if the given name does not match the name of the contract declaration in the code, then the function aborts the program.
When the deployment succeeded, the function returns the deployed contract.
For example, assuming the following contract code should be deployed:
The contract can be deployed as follows:
Updating a deployed contract
The contracts.update
function updates the code of an existing contract:
Calling the update
function requires access to an account via a reference which is authorized
with the coarse-grained Contracts
entitlement (auth(Contracts) &Account
),
or the fine-grained UpdateContract
entitlement (auth(UpdateContract) &Account
).
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.
If no contract with the given name exists in the account, if the given code does not declare exactly one contract or contract interface, or if the given name does not match the name of the contract declaration in the code, then the function aborts the program.
When the update succeeded, the function returns the deployed contract.
The update
function does not run the initializer of the contract again.
Updating a contract does not change the contract instance and its existing stored data. A contract update only changes the code a contract.
Is only possible to update contracts in ways that keep data consistency. Certain restrictions apply.
For example, assuming that a contract named Test
is already deployed to the account,
and it should be updated with the following contract code:
The contract can be updated as follows:
Removing a deployed contract
The contracts.remove
function removes a deployed contract from an account:
Calling the remove
function requires access to an account via a reference which is authorized
with the coarse-grained Contracts
entitlement (auth(Contracts) &Account
),
or the fine-grained RemoveContract
entitlement (auth(RemoveContract) &Account
).
The function removes the contract from the account which has the given name and returns it.
If no contract with the given name exists in the account, the function returns nil
.
For example, assuming that a contract named Test
is deployed to an account,
the contract can be removed as follows: