Functions

In this section, we will learn how to leverage functions to implement core logic and interact with blockchain data.



Aptogotchi Example


Public Functions

Public functions can be consumed / accessed by other contracts.

Source Code


public fun get_aptogotchi(owner_addr: address) { }


Entry Functions

Entry functions are used directly by frontend apps.

The input/outputs should be primitive types and can be optional.

These functions are always public.

Source Code


public entry fun set_name(owner: signer, name: String) { }


View Functions

Read-only functions that can be consumed by apps. View functions are always public.

Source Code


#[view]
public fun get_name(user_addr: address): String acquires AptoGotchi { }


Inline Functions

The inline keyword is used to suggest to the compiler that a function should be in-lined.

In-lining is a compiler optimization technique where the code of a called function is inserted directly into the calling function.


inline fun get_aptogotchi_internal(creator_addr: &address) { }


Private Functions

A private function is a function in a contract without any prefix. It cannot be accessed by other Modules (contracts).

These are usually used as internal or helper functions.

Source Code


fun get_aptogotchi_address(creator_addr: &address): (address) { }


The acquires Keyword

A Move function (m::f) must be annotated with acquires <T> if and only if:

  • The body of m::f contains a move_from<T>borrow_global_mut<T>, or borrow_global<T> instruction

OR

  • The body of m::f invokes another function (m::g), that is declared in the same module that is annotated with acquires

fun get_token_signer(): signer acquires CollectionCapability { }



More Resources

Read more about the acquires keyword here.

Link