Configuring objects
Configuring objects
At this point, you have an object, but how do you specialize it? Objects must be configured for their capabilities at creation time. If they are not configured with the correct capabilities at creation time, it will be impossible to change later.
Adding Resources
An object must store data in resources. The signer of the object is required to move resources to the object's storage. Below we'll go through a deletable object example.
When I create my object, I can use the special ConstructorRef to create resources only available at creation time. For example, you can create a signer at creation time to move a resource into the object.
module my_addr::object_playground {
use std::signer;
use endless_framework::object;
#[resource_group_member(group = endless_framework::object::ObjectGroup)]
struct MyStruct has key {
num: u8
}
entry fun create_my_object(caller: &signer) {
let caller_address = signer::address_of(caller);
// Creates the object
let constructor_ref = object::create_object(caller_address);
// Retrieves a signer for the object
let object_signer = object::generate_signer(&constructor_ref);
// Moves the MyStruct resource into the object
move_to(&object_signer, MyStruct { num: 0 });
// ...
}
}Extending the object
The object was created, but the user decided to add extra data. The ExtendRef provides this functionality to retrieve the object's signer at a later time.
The ExtendRef can be used to generate a signer for the object. Permissions on who can retrieve it must be defined by the contract.
Disabling or re-enabling Transfers
Objects can be able to be transferred or not. By default, all objects are transferable. However, this functionality can be toggled on and off, or chosen at creation time. It is enabled by the TransferRef, which we'll illustrate below.
Controlled transfers
Additionally, if the creator wants to control all transfers, a LinearTransferRef can be created from the TransferRef to provide a one time use transfer functionality. The LinearTransferRef has to be used by the owner of the object.
Allowing deletion of an Object
Deleting an object can be useful to get rid of clutter, as well as retrieve back storage refunds. Deletion can be done with a DeleteRef, which must be created at object creation time.
Note that you cannot create a DeleteRef for a non-deletable object.
Immutability
An object can be made immutable by making the contract associated immutable, and removing any ability to extend or mutate the object. By default, contracts are not immutable, and objects can be extended with an ExtendRef, and resources can be mutated if the contract allows for it.
Last updated