Skip to content

Commit 389d67c

Browse files
committed
Apply some updates on text
1 parent 6532aa1 commit 389d67c

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

docs/develop/howto/fees-low-level.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ This section describes instructions and manuals for interacting with TON at a lo
77
:::caution
88
Here you will find the **raw formulas** for calculating commissions and fees on TON.
99

10-
However, most of them are **already implemented through opcodes**! So, you **should use these opcodes instead of manual calculations**. After reading this section, check the [fees calculation](/develop/smart-contracts/fee-calculation) to learn how to calculate fees in FunC contracts using the new TVM opcodes.
10+
However, most of them are **already implemented through opcodes**! So, you **use them instead of manual calculations**.
1111
:::
1212

1313
This document provides a general idea of transaction fees on TON and particularly computation fees for the FunC code. There is also a [detailed specification in the TVM whitepaper](https://ton.org/tvm.pdf).
@@ -312,4 +312,5 @@ For educational purposes [example of the old one](https://explorer.toncoin.org/c
312312
## See Also
313313

314314
* [TON Fees overview](/develop/smart-contracts/fees)
315-
* [Transactions and Phases](/learn/tvm-instructions/tvm-overview#transactions-and-phases)
315+
* [Transactions and Phases](/learn/tvm-instructions/tvm-overview#transactions-and-phases)
316+
* [Fees calculation](/develop/smart-contracts/fee-calculation)

docs/develop/smart-contracts/fee-calculation.md

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
# Fees Calculation
22

3-
:::caution
4-
Read [theoretical `storage fee` explanation](/develop/howto/fees-low-level#storage-fee) before proceeding.
5-
:::
6-
7-
When your contract starts processing an incoming message, you should check the amount of TONs attached to the message to ensure they are enough to cover the fees. To do this, you need to calculate (or predict) the fee for the current transaction.
3+
When your contract starts processing an incoming message, you should check the amount of TONs attached to the message to ensure they are enough to cover [all types of fees](/develop/smart-contracts/fees#elements-of-transaction-fee). To do this, you need to calculate (or predict) the fee for the current transaction.
84

95
This document describes how to calculate fees in FunC contracts using the new TVM opcodes.
106

117
:::info More information on opcodes
12-
If you don't know what a FunC opcode is, check the [function doc page](/develop/func/functions).
13-
148
For a comprehensive list of TVM opcodes, including those mentioned below, check the [TVM instruction page](/learn/tvm-instructions/instructions).
159
:::
1610

1711
:::info
18-
All functions with the opcodes described below are presented in the [stdlib](/ton-blockchain/ton/blob/master/crypto/smartcont/stdlib.fc) library.
12+
Most functions with the opcodes described below are presented in the [stdlib](https://github.com/ton-blockchain/ton/blob/master/crypto/smartcont/stdlib.fc) library. If your contract use previous version of stdlib.fc, you can add functions with required opcodes manually.
1913
:::
2014

2115
## Storage Fee
@@ -52,19 +46,16 @@ int calculate_storage_fee(int balance, int msg_value, int workchain, int seconds
5246

5347
This way, you can check if the contract has enough balance to be stored for the `seconds` time before the next message arrives.
5448

55-
## Gas Fee (Computation Cost)
49+
## Computation Fee
5650

5751
### Overview
5852

59-
Generally, there are two cases of gas fee processing:
60-
- **If it's impossible to predict gas usage** (similar to the third case of [forward fee calculation](/develop/smart-contracts/fee-calculation#forward-fee), which you should read if you want to use this opcode), there is the `GASCONSUMED` opcode. Use it with `SENDMSG` as described below in the forward fee calculation section. **Please, do not use `GASCONSUMED` unless necessary**.
53+
In most cases use the `GETGASFEE` opcode with the following parameters:
6154

62-
- Otherwise (in most cases), use the `GETGASFEE` opcode with the following parameters:
63-
64-
| Param | Description |
65-
|:-----------|:-------------------------------------------------|
66-
| `gas_used` | Gas amount, calculated in tests and hardcoded |
67-
| `is_mc` | True if the source or destination is in the masterchain |
55+
| Param | Description |
56+
|:-----------|:--------------------------------------------------------|
57+
| `gas_used` | Gas amount, calculated in tests and hardcoded |
58+
| `is_mc` | True if the source or destination is in the masterchain |
6859

6960
### Calculation Flow
7061

@@ -124,6 +115,15 @@ const transferTx = findTransactionRequired(sendResult.transactions, {
124115
success: true
125116
});
126117

118+
let computedGeneric: (transaction: Transaction) => TransactionComputeVm;
119+
computedGeneric = (transaction) => {
120+
if(transaction.description.type !== "generic")
121+
throw("Expected generic transactionaction");
122+
if(transaction.description.computePhase.type !== "vm")
123+
throw("Compute phase expected")
124+
return transaction.description.computePhase;
125+
}
126+
127127
let printTxGasStats: (name: string, trans: Transaction) => bigint;
128128
printTxGasStats = (name, transaction) => {
129129
const txComputed = computedGeneric(transaction);
@@ -133,7 +133,6 @@ printTxGasStats = (name, transaction) => {
133133
}
134134

135135
send_gas_fee = printTxGasStats("Jetton transfer", transferTx);
136-
send_gas_fee = computeGasFee(gasPrices, 9255n);
137136
```
138137

139138
## Forward Fee
@@ -172,7 +171,22 @@ If even `GETORIGINALFWDFEE` can't be used, there is one more option. **It is the
172171
| cells | Number of cells |
173172
| mode | Message mode |
174173

175-
It creates an output action and returns a fee for creating a message.
174+
It creates an output action and returns a fee for creating a message. However, it uses an unpredictable amount of gas, which can't be calculated using formulas, so how can it be calculated? Use `GASCONSUMED`:
175+
176+
```func
177+
int send_message(cell msg, int mode) impure asm "SENDMSG";
178+
int gas_consumed() asm "GASCONSUMED";
179+
;; ... some code ...
180+
181+
() calculate_forward_fee(cell msg, int mode) inline {
182+
int gas_before = gas_consumed();
183+
int forward_fee = send_message(msg, mode);
184+
int gas_usage = gas_consumed() - gas_before;
185+
186+
;; forward fee -- fee value
187+
;; gas_usage -- amount of gas, used to send msg
188+
}
189+
```
176190

177191
## See Also
178192
- [Stablecoin contract with fees calculation](https://github.com/ton-blockchain/stablecoin-contract)

0 commit comments

Comments
 (0)