Skip to main content

Ownership

This template is used in many contracts whenever there needs a special address to administrate the contract. The ownership of the contract may be transferred.

Repository


Author

Code

info

It is required to copy this code in the created contract to benefit from the ownership pattern.

/* OWNERSHIP TRANSFER ------------------------------------------------------- */

variable owner_candidate : option<address> = none

entry declare_ownership(candidate : address) {
called by owner
effect {
owner_candidate := some(candidate);
}
}

entry claim_ownership() {
require {
ownership_r1: (owner_candidate ? the = caller : false) otherwise "INVALID_CALLER"
}
effect {
owner := caller;
owner_candidate := none
}
}

variable option address none entry require ?: effect := caller none address called by some

Storage

owner

Contract parameter for the contract owner.

It is typically used to protect an entrypoint against other callers with a called by section:

entry exec() {
called by owner
/* ... */
}

Code


archetype my_contract(owner : address)

archetype address

Type


Related

owner_candidate

Variable used during transfer of ownership to store the owner candidate address.

Code


variable owner_candidate : option<address> = none

variable option address none

Type


Related

Entrypoints

declare_candidate(candidate)

Declares a condidate address to be the contract's owner.

Code


entry declare_ownership(candidate : address) {
called by owner
effect {
owner_candidate := some(candidate);
}
}

entry address called by effect := some

Parameter


candidate :

Candidate address for next contract's owner

Fails with

"INVALID_CALLER"

When caller is not owner


Related

claim_candidate()

Declared candidate calls this method to confirm it is the new contract owner.

Code


entry claim_ownership() {
require {
ownership_r1: (owner_candidate ? the = caller : false) otherwise "INVALID_CALLER"
}
effect {
owner := caller;
owner_candidate := none
}
}

entry require ?: effect := caller none

Fails with

"INVALID_CALLER"

When caller is not owner_candidate


Related