You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/develop/howto/fees-low-level.md
+3-2Lines changed: 3 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ This section describes instructions and manuals for interacting with TON at a lo
7
7
:::caution
8
8
Here you will find the **raw formulas** for calculating commissions and fees on TON.
9
9
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**.
11
11
:::
12
12
13
13
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
Copy file name to clipboardExpand all lines: docs/develop/smart-contracts/fee-calculation.md
+33-19Lines changed: 33 additions & 19 deletions
Original file line number
Diff line number
Diff line change
@@ -1,21 +1,15 @@
1
1
# Fees Calculation
2
2
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.
8
4
9
5
This document describes how to calculate fees in FunC contracts using the new TVM opcodes.
10
6
11
7
:::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
-
14
8
For a comprehensive list of TVM opcodes, including those mentioned below, check the [TVM instruction page](/learn/tvm-instructions/instructions).
15
9
:::
16
10
17
11
:::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.
19
13
:::
20
14
21
15
## Storage Fee
@@ -52,19 +46,16 @@ int calculate_storage_fee(int balance, int msg_value, int workchain, int seconds
52
46
53
47
This way, you can check if the contract has enough balance to be stored for the `seconds` time before the next message arrives.
54
48
55
-
## Gas Fee (Computation Cost)
49
+
## Computation Fee
56
50
57
51
### Overview
58
52
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:
61
54
62
-
- Otherwise (in most cases), use the `GETGASFEE` opcode with the following parameters:
@@ -172,7 +171,22 @@ If even `GETORIGINALFWDFEE` can't be used, there is one more option. **It is the
172
171
| cells | Number of cells |
173
172
| mode | Message mode |
174
173
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
+
```
176
190
177
191
## See Also
178
192
-[Stablecoin contract with fees calculation](https://github.com/ton-blockchain/stablecoin-contract)
0 commit comments