Introduction
On the TON (The Open Network) blockchain platform, Gas is the unit used to measure the resources required for executing smart contracts. Gas is a fundamental aspect of blockchain technology that ensures the integrity and efficiency of the network. Developers need to pay close attention to Gas consumption to ensure the efficiency and cost control of contract execution. This article delves into the intricacies of Gas handling on the TON blockchain by interpreting several key built-in functions in the context of the TON NotCoin contract. By doing so, we aim to help you gain a deeper understanding of TON’s Gas handling mechanism, which is crucial for effective smart contract development and optimization.
Overview of Built-in Functions
TON smart contracts provide a robust set of built-in functions designed to handle Gas fees efficiently. These functions are the backbone of the Gas management system on the TON blockchain. Here is a detailed introduction to these functions:
get_compute_fee
: This function calculates the computation fee required for executing the contract. It is essential for determining the cost of running complex operations within a smart contract.get_storage_fee
: This function calculates the storage fee required for storing contract data. It ensures that the state of the contract is maintained over time without incurring unnecessary costs.get_forward_fee
: This function calculates the fee required for message forwarding. It is critical for the proper functioning of contracts that need to interact with other contracts or accounts on the network.get_precompiled_gas_consumption
: This function obtains the Gas consumption of precompiled contracts. Precompiled contracts are an integral part of the TON ecosystem, offering optimized performance for certain operations.
Interpretation of Built-in Functions
1. get_compute_fee
Function Definition:
int get_compute_fee(int workchain, int gas_used) asm(gas_used workchain) "GETGASFEE";
Interpretation:
workchain
: This parameter specifies the workchain number. In the TON blockchain, a workchain is a logical chain of blocks where the computation takes place. The workchain number is used to determine the base for computation fees, which can vary between different workchains.gas_used
: This parameter represents the amount of Gas consumed during contract execution. It is a measure of the computational complexity of the operations performed by the contract.- The function returns the computation fee required for executing the contract, measured in nanoton. The nanoton is the smallest unit of the TON currency, and it allows for precise fee calculations.
In the NotCoin contract,get_compute_fee
is used to calculate the computation fees for transfer and burn operations. It ensures that sufficient computation resource fees are paid during contract execution, preventing the depletion of resources and maintaining the network’s stability.
2. get_storage_fee
Function Definition:
int get_storage_fee(int workchain, int seconds, int bits, int cells) asm(cells bits seconds workchain) "GETSTORAGEFEE";
Interpretation:
workchain
: Similar toget_compute_fee
, this parameter specifies the workchain number to determine the base for storage fees.seconds
: This parameter indicates the duration for which data is stored, measured in seconds. The longer the data is stored, the higher the storage fee.bits
: This parameter represents the total number of bits required for storage. It is a measure of the size of the data being stored.cells
: This parameter indicates the total number of cells required for storage. In TON, a cell is a unit of data storage that can hold up to 1023 bits.- The function returns the fee required for storing data, measured in nanoton.
In the NotCoin contract,get_storage_fee
is used to calculate the storage fee for the JettonWallet, which is a part of the contract responsible for managing digital assets. Ensuring the persistence of contract data storage is crucial for the long-term viability of the contract and the trust of its users.
3. get_forward_fee
Function Definition:
int get_forward_fee(int workchain, int bits, int cells) asm(cells bits workchain) "GETFORWARDFEE";
Interpretation:
workchain
: This parameter specifies the workchain number for determining the base for forwarding fees.bits
: This parameter represents the total number of bits for message forwarding. It is a measure of the size of the message being sent.cells
: This parameter indicates the total number of cells for message forwarding. Larger messages require more cells and, consequently, a higher fee.- The function returns the fee required for message forwarding, measured in nanoton.
In the NotCoin contract,get_forward_fee
is used to calculate the message forwarding fee for transfer operations. It ensures that the contract can handle message delivery correctly and that the sender is willing to pay for the resources consumed during the process. This is particularly important in a decentralized network where message passing is a common operation.
4. get_precompiled_gas_consumption
Function Definition:
int get_precompiled_gas_consumption() asm "GETPRECOMPILEDGAS";
Interpretation:
- This function does not take any parameters. It is designed to return the Gas consumption when executing precompiled contracts.
- Precompiled contracts are a special set of standard contracts provided by the TON platform. They are designed to perform certain operations with high execution efficiency and relatively fixed Gas consumption. These contracts are often used for common tasks such as cryptography, which would otherwise be computationally expensive to execute within a regular smart contract.
In the NotCoin contract,get_precompiled_gas_consumption
is used to obtain the Gas consumption of precompiled contracts. This information is vital for correct calculation of total Gas consumption in fee checks, ensuring that the contract operations that rely on precompiled contracts are appropriately priced.
Application in Fee Checks
The built-in functions described above play a critical role in the fee check mechanisms of the NotCoin contract. These mechanisms are designed to ensure that the fees for operations are sufficient, preventing underfunded transactions that could destabilize the network. Here is a closer look at how these functions are applied in fee checks:
check_amount_is_enough_to_transfer
This function uses get_compute_fee
, get_storage_fee
, and get_forward_fee
to calculate the total fee for transfer operations. The process involves the following steps:
- Calculate the computation fee for the transfer operation using
get_compute_fee
. - Calculate the storage fee for any new data that will be stored as a result of the transfer using
get_storage_fee
. - Calculate the message forwarding fee to ensure the transfer message can be delivered using
get_forward_fee
. - Sum these fees to determine the total cost of the transfer operation.
- Check if the amount provided by the sender is greater than or equal to the total fee. If not, the transaction is deemed underfunded and is rejected.
check_amount_is_enough_to_burn
This function uses get_compute_fee
and get_forward_fee
to calculate the total fee for burn operations. The steps include:
- Calculate the computation fee for the burn operation using
get_compute_fee
. - Calculate the message forwarding fee for the burn operation using
get_forward_fee
. - Sum these fees to determine the total cost of the burn operation.
- Verify that the sender has provided enough funds to cover the burn fee and the transaction fee. If the amount is insufficient, the transaction is rejected.
Advanced Considerations and Optimization Techniques
While the built-in functions provide a robust framework for managing Gas fees, developers must also consider advanced scenarios and optimization techniques to further refine their smart contracts. Here are some considerations:
Dynamic Gas Pricing
The Gas prices on the TON network can fluctuate based on network congestion and demand. Contracts can be designed to adapt to these changes by incorporating logic that fetches the current Gas prices and adjusts the fees accordingly.
Batch Operations
To optimize Gas usage, developers can batch operations together. For example, instead of making multiple transfers in separate transactions, a contract can be designed to handle multiple transfers in a single transaction, reducing the overall Gas consumption.
State Channels
State channels are a layer 2 solution that allows for off-chain transactions, which can significantly reduce the need for on-chain computation and storage, thereby lowering Gas costs.
Prepaid Contracts
Contracts can be designed to hold a certain amount of funds in advance to cover Gas fees for future operations. This can be particularly useful for contracts that perform frequent or predictable operations.
Conclusion
By interpreting the built-in functions in the TON NotCoin contract, we have explored the nuances of TON’s Gas handling mechanism. Mastering the use of these built-in functions is of paramount importance for developers aiming to optimize contract performance and control execution costs on the TON blockchain.
The practical application of these functions in fee checks ensures the stability and efficiency of the network by preventing underfunded transactions and encouraging responsible use of network resources. In the actual development process, the judicious use of these functions can guarantee the smooth operation and optimal resource utilization of smart contracts on the TON platform.
As the TON ecosystem evolves, developers must stay abreast of best practices and emerging techniques for Gas management. By doing so, they can create more efficient, cost-effective, and user-friendly smart contracts that will drive the adoption and growth of the TON blockchain.