Tact allows you to add common boilerplate behaviors to your contract by using traits.
The Ownable trait allows the contract to set an owner role, which can have higher priviliges from everybody else.
For example, if your contract allows upgrades, it would make sense to limit upgrades to the owner only, otherwise anyone could break the contract.
Be aware that dapps are supposed to be decentralized and an owner role is by definition centralized. If you're building a dapp, try to minimize reliance on Ownable.
Note that this trait doesn't allow the owner to transfer ownership to a different owner.
Define a state variable named owner: Address and call self.requireOwner() in priviliged receivers.
All ExamplesInfo: The Ownable trait is defined in the standard library
// this trait has to be imported
import "@stdlib/ownable";
import "@stdlib/deploy";
// the OwnableTransferable trait can limit certain actions to the owner only
contract Counter with Deployable, OwnableTransferable {
owner: Address; // The OwnableTransferable trait requires you to add this exact state variable
val: Int as uint32;
init() {
self.owner = sender(); // we can initialize owner to any value we want, the deployer in this case
self.val = 0;
}
// this message is available to anyone
receive("increment") {
self.val = self.val + 1;
}
// this message in only available to the owner
receive("double") {
self.requireOwner();
self.val = self.val * 2;
}
get fun value(): Int {
return self.val;
}
// receive(msg: ChangeOwner) is added automatically to transfer ownership
// get fun owner(): Address is added automatically to query who the owner is
}