Contract Structure

In the Aptos protocol, a Move module is a smart contract. A basic move contract is composed of the following:


module aptogotchi::main {
use std::vector;
use std::string;
struct AptoGotchi has key {
}
fun init_module(account: &signer) {
}
public entry fun create_aptogotchi() {
}
#[view]
public fun get_name(user_addr: address): String acquires AptoGotchi, CollectionCapability {
}
public entry fun set_name(user_addr: address, name: String) acquires AptoGotchi, CollectionCapability {
}
};


1. Imports

Import any libraries used in the smart contract.


use std::vector;
use std::string;

2. Struct definitions

Define any custom data types. Structs are the way to create custom types in a Move contract. All Structs are private and can only be accessed from the module itself.


struct AptoGotchi has key { }

3. Init module function

The init function will be executed once, when the contract is deployed.


fun init_module(account: &signer) { }

4. Token creation function

Creates the digital asset instance.


public entry fun create_aptogotchi() { }

5. Read and Write functions (getters & setters)

All structs are private. Create getter/setter functions to access & modify the structs.


// Read: view functions
#[view]
public fun get_name(user_addr: address): String acquires AptoGotchi, CollectionCapability {
}
// Write: entry functions
public entry fun set_name(user_addr: address, name: String) acquires AptoGotchi, CollectionCapability {
}



More Resources

See list of Move types here.

Link