Get Methods
Before proceeding, it is recommended that readers have a basic understanding of the FunC programming language on TON Blockchain. This will help you grasp the information provided here more effectively.
Introduction
Get methods are special functions in smart contracts that are made for querying specific data from them. Their execution doesn't cost any fees and happens outside of the blockchain.
These functions are very common in most smart contracts. For example, the default Wallet contract has several get methods, such as seqno()
, get_subwallet_id()
and get_public_key()
. They are used by wallets, SDKs, and APIs to fetch data about wallets.
Design patterns for get methods
Basic get methods design patterns
-
Single data point retrieval: A basic design pattern is to create methods that return individual data points from the contract's state. These methods have no parameters and return a single value.
Example:
int get_balance() method_id {
return get_data().begin_parse().preload_uint(64);
} -
Aggregate data retrieval: Another common pattern is to create methods that return multiple data points from the contract's state in a single call. This is often used when certain data points are commonly used together. These are commonly used in Jetton and NFT contracts.
Example:
(int, slice, slice, cell) get_wallet_data() method_id {
return load_data();
}
Advanced get methods design patterns
-
Computed data retrieval: In some cases, the data that needs to be retrieved isn't stored directly in the contract's state, but instead is calculated based on the state and the input arguments.
Example:
slice get_wallet_address(slice owner_address) method_id {
(int total_supply, slice admin_address, cell content, cell jetton_wallet_code) = load_data();
return calculate_user_jetton_wallet_address(owner_address, my_address(), jetton_wallet_code);
} -
Conditional data retrieval: Sometimes, the data that needs to be retrieved depends on certain conditions, such as the current time.
Example:
(int) get_ready_to_be_used() method_id {
int ready? = now() >= 1686459600;
return ready?;
}