Object Model


Objects are the building blocks for digital assets and fungible assets. While it's possible to create and utilize these assets without a deep understanding of objects, having this knowledge significantly enhances your ability to effectively manage and use them.


Overview

Object is a core primitive in Aptos Move and created via the object module at 0x1::object. Object supports Digital Asset Standard and Fungible Asset Standard.

Object is a container for resources that are stored within a single address. These resources usually represent related data often accessed together and should be stored within a single address for data locality and cost savings.


Object Creation

The object creation functions all return a transient ConstructorRef that cannot be stored. ConstructorRef allows adding resources to an object. ConstructorRef can also be used to generate the


/// Create an object that can be deleted.
public fun create_object(creator: &signer): ConstructorRef;
/// Create an object that cannot be deleted.
public fun create_sticky_object(creator: &signer): ConstructorRef;
/// Create a named object that cannot not deleted.
/// Named object can be queried globally by knowing the seed used to create them.
public fun create_named_object(creator: &signer, seed: vector<u8>): ConstructorRef;
/// Get the address of named object using creator_address and seed.
public fun create_object_address(source: &address, seed: vector<u8>): address;


Object Capabilities

There are capabilities (or "refs") that we often use to manage the object, they can only be generated using the construtor ref upon object creation:


/// Generate ref to remove Object from global storage.
public fun generate_delete_ref(ref: &ConstructorRef): DeleteRef;
/// Generate ref to add new events and resources to the object.
public fun generate_extend_ref(ref: &ConstructorRef): ExtendRef;
/// Generate ref to manage object transfers.
public fun generate_transfer_ref(ref: &ConstructorRef): TransferRef;


Extending Object

In Aptogotchi, we have a function upgrade_aptogotchi_with_battle_extension that holder can call to upgrade their Aptogotchi with a new battle extension. This function will add a new resource to the object using the extend_ref of the Aptogotchi NFT object.


struct AptogotchiBattleExt has key {
attack_point: u64,
defence_point: u64,
}
#[view]
public fun get_aptogotchi_address(creator_addr: address): (address) {
let token_address = token::create_token_address(
&get_app_signer_address(),
&string::utf8(APTOGOTCHI_COLLECTION_NAME),
&get_aptogotchi_token_name(creator_addr),
);
token_address
}
public entry fun upgrade_aptogotchi_with_battle_extension(owner: &signer) acquires Aptogotchi {
let owner_addr = address_of(owner);
assert!(has_aptogotchi(owner_addr), error::unavailable(E_APTOGOTCHI_DOES_NOT_EXIST));
let gotchi_address = get_aptogotchi_address(owner_addr);
// Each address can only hold up to 1 resource from the same type
assert!(!has_battle_ext(gotchi_address), E_BATTLE_EXTENSION_EXISTS);
let gotchi = borrow_global<Aptogotchi>(gotchi_address);
let gotchi_signer = generate_signer_for_extending(&gotchi.extend_ref);
move_to(&gotchi_signer, AptogotchiBattleExt {
attack_point: 1,
defence_point: 2,
});
}