Reference
@stdlib/ownable

@stdlib/ownable

Provides traits for ownable contracts. This is most commonly used trait that is required by most other traits.

To use this library, import @stdlib/ownable:

import "@stdlib/ownable";

Messages

ChangeOwner

message ChangeOwner {
    queryId: Int as uint64;
    newOwner: Address;
}

ChangeOwnerOk

message ChangeOwnerOk {
    queryId: Int as uint64;
    newOwner: Address;
}

Traits

Ownable

Trait Ownable declares an owner (non-editable) of a contract and provides a helper function requireOwner() that checks that a message was sent by an owner.

This trait requires a field owner: Address to be declared and exposes a getter function owner(), which reads it from the contract.

Source code:

@interface("org.ton.ownable")
trait Ownable {
    owner: Address;
 
    fun requireOwner() {
        nativeThrowUnless(132, sender() == self.owner);
    }
 
    get fun owner(): Address {
        return self.owner;
    }
}

Usage example:

import "@stdlib/ownable";
 
contract ExampleContract with Ownable {
    owner: Address;
 
    init(owner: Address) {
        self.owner = owner;
    }
}

OwnableTransferable

OwnableTransferable is an extension of an Ownable that allows to transfer ownership of a contract to another address. It provides a secure handle Message ChangeOwner that could be called by an owner to transfer ownership.

If the owner transfer request succeeds, the contract will reply with a ChangeOwnerOk Message.

Source code:

@interface("org.ton.ownable.transferable.v2")
trait OwnableTransferable with Ownable {
    owner: Address;
 
    receive(msg: ChangeOwner) {
        // Check if the sender is the owner
        self.requireOwner();
 
        // Update owner
        self.owner = msg.newOwner;
 
        // Reply result
        self.reply(ChangeOwnerOk{ queryId: msg.queryId, newOwner: msg.newOwner }.toCell());
    }
}

Usage example:

import "@stdlib/ownable";
 
contract ExampleContract with OwnableTransferable {
    owner: Address;
 
    init(owner: Address) {
        self.owner = owner;
    }
}

Sources