@ton/blueprint 0.23.0 → 0.25.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (29) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +13 -8
  3. package/dist/cli/constants.js +11 -3
  4. package/dist/cli/create.d.ts +0 -4
  5. package/dist/cli/create.js +3 -21
  6. package/dist/compile/CompilerConfig.d.ts +8 -1
  7. package/dist/compile/compile.d.ts +13 -3
  8. package/dist/compile/compile.js +26 -0
  9. package/dist/index.d.ts +1 -1
  10. package/dist/network/createNetworkProvider.d.ts +0 -1
  11. package/dist/network/createNetworkProvider.js +23 -34
  12. package/dist/network/send/MnemonicProvider.d.ts +1 -1
  13. package/dist/network/send/MnemonicProvider.js +2 -0
  14. package/dist/templates/func/common/contracts/imports/stdlib.fc.template +885 -0
  15. package/dist/templates/func/not-separated-common/contracts/imports/stdlib.fc.template +885 -0
  16. package/dist/templates/tolk/common/compilables/compile.ts.template +9 -0
  17. package/dist/templates/tolk/counter/contracts/contract.tolk.template +71 -0
  18. package/dist/templates/tolk/counter/scripts/deploy.ts.template +22 -0
  19. package/dist/templates/tolk/counter/scripts/increment.ts.template +38 -0
  20. package/dist/templates/tolk/counter/tests/spec.ts.template +82 -0
  21. package/dist/templates/tolk/counter/wrappers/wrapper.ts.template +67 -0
  22. package/dist/templates/tolk/empty/contracts/contract.tolk.template +4 -0
  23. package/dist/templates/tolk/empty/scripts/deploy.ts.template +14 -0
  24. package/dist/templates/tolk/empty/tests/spec.ts.template +40 -0
  25. package/dist/templates/tolk/empty/wrappers/wrapper.ts.template +30 -0
  26. package/dist/templates/tolk/not-separated-common/wrappers/compile.ts.template +9 -0
  27. package/package.json +10 -9
  28. package/dist/network/send/TonHubProvider.d.ts +0 -16
  29. package/dist/network/send/TonHubProvider.js +0 -110
@@ -0,0 +1,885 @@
1
+ stdlib.fc
2
+ ;; Standard library for funC
3
+ ;;
4
+
5
+ {-
6
+ This file is part of TON FunC Standard Library.
7
+
8
+ FunC Standard Library is free software: you can redistribute it and/or modify
9
+ it under the terms of the GNU Lesser General Public License as published by
10
+ the Free Software Foundation, either version 2 of the License, or
11
+ (at your option) any later version.
12
+
13
+ FunC Standard Library is distributed in the hope that it will be useful,
14
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
15
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
+ GNU Lesser General Public License for more details.
17
+
18
+ -}
19
+
20
+ {-
21
+ # Tuple manipulation primitives
22
+ The names and the types are mostly self-explaining.
23
+ See [polymorhism with forall](https://ton.org/docs/#/func/functions?id=polymorphism-with-forall)
24
+ for more info on the polymorphic functions.
25
+
26
+ Note that currently values of atomic type `tuple` can't be cast to composite tuple type (e.g. `[int, cell]`)
27
+ and vise versa.
28
+ -}
29
+
30
+ {-
31
+ # Lisp-style lists
32
+
33
+ Lists can be represented as nested 2-elements tuples.
34
+ Empty list is conventionally represented as TVM `null` value (it can be obtained by calling [null()]).
35
+ For example, tuple `(1, (2, (3, null)))` represents list `[1, 2, 3]`. Elements of a list can be of different types.
36
+ -}
37
+
38
+ ;;; Adds an element to the beginning of lisp-style list.
39
+ forall X -> tuple cons(X head, tuple tail) asm "CONS";
40
+
41
+ ;;; Extracts the head and the tail of lisp-style list.
42
+ forall X -> (X, tuple) uncons(tuple list) asm "UNCONS";
43
+
44
+ ;;; Extracts the tail and the head of lisp-style list.
45
+ forall X -> (tuple, X) list_next(tuple list) asm(-> 1 0) "UNCONS";
46
+
47
+ ;;; Returns the head of lisp-style list.
48
+ forall X -> X car(tuple list) asm "CAR";
49
+
50
+ ;;; Returns the tail of lisp-style list.
51
+ tuple cdr(tuple list) asm "CDR";
52
+
53
+ ;;; Creates tuple with zero elements.
54
+ tuple empty_tuple() asm "NIL";
55
+
56
+ ;;; Appends a value `x` to a `Tuple t = (x1, ..., xn)`, but only if the resulting `Tuple t' = (x1, ..., xn, x)`
57
+ ;;; is of length at most 255. Otherwise throws a type check exception.
58
+ forall X -> tuple tpush(tuple t, X value) asm "TPUSH";
59
+ forall X -> (tuple, ()) ~tpush(tuple t, X value) asm "TPUSH";
60
+
61
+ ;;; Creates a tuple of length one with given argument as element.
62
+ forall X -> [X] single(X x) asm "SINGLE";
63
+
64
+ ;;; Unpacks a tuple of length one
65
+ forall X -> X unsingle([X] t) asm "UNSINGLE";
66
+
67
+ ;;; Creates a tuple of length two with given arguments as elements.
68
+ forall X, Y -> [X, Y] pair(X x, Y y) asm "PAIR";
69
+
70
+ ;;; Unpacks a tuple of length two
71
+ forall X, Y -> (X, Y) unpair([X, Y] t) asm "UNPAIR";
72
+
73
+ ;;; Creates a tuple of length three with given arguments as elements.
74
+ forall X, Y, Z -> [X, Y, Z] triple(X x, Y y, Z z) asm "TRIPLE";
75
+
76
+ ;;; Unpacks a tuple of length three
77
+ forall X, Y, Z -> (X, Y, Z) untriple([X, Y, Z] t) asm "UNTRIPLE";
78
+
79
+ ;;; Creates a tuple of length four with given arguments as elements.
80
+ forall X, Y, Z, W -> [X, Y, Z, W] tuple4(X x, Y y, Z z, W w) asm "4 TUPLE";
81
+
82
+ ;;; Unpacks a tuple of length four
83
+ forall X, Y, Z, W -> (X, Y, Z, W) untuple4([X, Y, Z, W] t) asm "4 UNTUPLE";
84
+
85
+ ;;; Returns the first element of a tuple (with unknown element types).
86
+ forall X -> X first(tuple t) asm "FIRST";
87
+
88
+ ;;; Returns the second element of a tuple (with unknown element types).
89
+ forall X -> X second(tuple t) asm "SECOND";
90
+
91
+ ;;; Returns the third element of a tuple (with unknown element types).
92
+ forall X -> X third(tuple t) asm "THIRD";
93
+
94
+ ;;; Returns the fourth element of a tuple (with unknown element types).
95
+ forall X -> X fourth(tuple t) asm "3 INDEX";
96
+
97
+ ;;; Returns the first element of a pair tuple.
98
+ forall X, Y -> X pair_first([X, Y] p) asm "FIRST";
99
+
100
+ ;;; Returns the second element of a pair tuple.
101
+ forall X, Y -> Y pair_second([X, Y] p) asm "SECOND";
102
+
103
+ ;;; Returns the first element of a triple tuple.
104
+ forall X, Y, Z -> X triple_first([X, Y, Z] p) asm "FIRST";
105
+
106
+ ;;; Returns the second element of a triple tuple.
107
+ forall X, Y, Z -> Y triple_second([X, Y, Z] p) asm "SECOND";
108
+
109
+ ;;; Returns the third element of a triple tuple.
110
+ forall X, Y, Z -> Z triple_third([X, Y, Z] p) asm "THIRD";
111
+
112
+
113
+ ;;; Push null element (casted to given type)
114
+ ;;; By the TVM type `Null` FunC represents absence of a value of some atomic type.
115
+ ;;; So `null` can actually have any atomic type.
116
+ forall X -> X null() asm "PUSHNULL";
117
+
118
+ ;;; Moves a variable [x] to the top of the stack
119
+ forall X -> (X, ()) ~impure_touch(X x) impure asm "NOP";
120
+
121
+
122
+
123
+ ;;; Returns the current Unix time as an Integer
124
+ int now() asm "NOW";
125
+
126
+ ;;; Returns the internal address of the current smart contract as a Slice with a `MsgAddressInt`.
127
+ ;;; If necessary, it can be parsed further using primitives such as [parse_std_addr].
128
+ slice my_address() asm "MYADDR";
129
+
130
+ ;;; Returns the balance of the smart contract as a tuple consisting of an int
131
+ ;;; (balance in nanotoncoins) and a `cell`
132
+ ;;; (a dictionary with 32-bit keys representing the balance of "extra currencies")
133
+ ;;; at the start of Computation Phase.
134
+ ;;; Note that RAW primitives such as [send_raw_message] do not update this field.
135
+ [int, cell] get_balance() asm "BALANCE";
136
+
137
+ ;;; Returns the logical time of the current transaction.
138
+ int cur_lt() asm "LTIME";
139
+
140
+ ;;; Returns the starting logical time of the current block.
141
+ int block_lt() asm "BLOCKLT";
142
+
143
+ ;;; Computes the representation hash of a `cell` [c] and returns it as a 256-bit unsigned integer `x`.
144
+ ;;; Useful for signing and checking signatures of arbitrary entities represented by a tree of cells.
145
+ int cell_hash(cell c) asm "HASHCU";
146
+
147
+ ;;; Computes the hash of a `slice s` and returns it as a 256-bit unsigned integer `x`.
148
+ ;;; The result is the same as if an ordinary cell containing only data and references from `s` had been created
149
+ ;;; and its hash computed by [cell_hash].
150
+ int slice_hash(slice s) asm "HASHSU";
151
+
152
+ ;;; Computes sha256 of the data bits of `slice` [s]. If the bit length of `s` is not divisible by eight,
153
+ ;;; throws a cell underflow exception. The hash value is returned as a 256-bit unsigned integer `x`.
154
+ int string_hash(slice s) asm "SHA256U";
155
+
156
+ {-
157
+ # Signature checks
158
+ -}
159
+
160
+ ;;; Checks the Ed25519-`signature` of a `hash` (a 256-bit unsigned integer, usually computed as the hash of some data)
161
+ ;;; using [public_key] (also represented by a 256-bit unsigned integer).
162
+ ;;; The signature must contain at least 512 data bits; only the first 512 bits are used.
163
+ ;;; The result is `−1` if the signature is valid, `0` otherwise.
164
+ ;;; Note that `CHKSIGNU` creates a 256-bit slice with the hash and calls `CHKSIGNS`.
165
+ ;;; That is, if [hash] is computed as the hash of some data, these data are hashed twice,
166
+ ;;; the second hashing occurring inside `CHKSIGNS`.
167
+ int check_signature(int hash, slice signature, int public_key) asm "CHKSIGNU";
168
+
169
+ ;;; Checks whether [signature] is a valid Ed25519-signature of the data portion of `slice data` using `public_key`,
170
+ ;;; similarly to [check_signature].
171
+ ;;; If the bit length of [data] is not divisible by eight, throws a cell underflow exception.
172
+ ;;; The verification of Ed25519 signatures is the standard one,
173
+ ;;; with sha256 used to reduce [data] to the 256-bit number that is actually signed.
174
+ int check_data_signature(slice data, slice signature, int public_key) asm "CHKSIGNS";
175
+
176
+ {---
177
+ # Computation of boc size
178
+ The primitives below may be useful for computing storage fees of user-provided data.
179
+ -}
180
+
181
+ ;;; Returns `(x, y, z, -1)` or `(null, null, null, 0)`.
182
+ ;;; Recursively computes the count of distinct cells `x`, data bits `y`, and cell references `z`
183
+ ;;; in the DAG rooted at `cell` [c], effectively returning the total storage used by this DAG taking into account
184
+ ;;; the identification of equal cells.
185
+ ;;; The values of `x`, `y`, and `z` are computed by a depth-first traversal of this DAG,
186
+ ;;; with a hash table of visited cell hashes used to prevent visits of already-visited cells.
187
+ ;;; The total count of visited cells `x` cannot exceed non-negative [max_cells];
188
+ ;;; otherwise the computation is aborted before visiting the `(max_cells + 1)`-st cell and
189
+ ;;; a zero flag is returned to indicate failure. If [c] is `null`, returns `x = y = z = 0`.
190
+ (int, int, int) compute_data_size(cell c, int max_cells) impure asm "CDATASIZE";
191
+
192
+ ;;; Similar to [compute_data_size?], but accepting a `slice` [s] instead of a `cell`.
193
+ ;;; The returned value of `x` does not take into account the cell that contains the `slice` [s] itself;
194
+ ;;; however, the data bits and the cell references of [s] are accounted for in `y` and `z`.
195
+ (int, int, int) slice_compute_data_size(slice s, int max_cells) impure asm "SDATASIZE";
196
+
197
+ ;;; A non-quiet version of [compute_data_size?] that throws a cell overflow exception (`8`) on failure.
198
+ (int, int, int, int) compute_data_size?(cell c, int max_cells) asm "CDATASIZEQ NULLSWAPIFNOT2 NULLSWAPIFNOT";
199
+
200
+ ;;; A non-quiet version of [slice_compute_data_size?] that throws a cell overflow exception (8) on failure.
201
+ (int, int, int, int) slice_compute_data_size?(cell c, int max_cells) asm "SDATASIZEQ NULLSWAPIFNOT2 NULLSWAPIFNOT";
202
+
203
+ ;;; Throws an exception with exit_code excno if cond is not 0 (commented since implemented in compilator)
204
+ ;; () throw_if(int excno, int cond) impure asm "THROWARGIF";
205
+
206
+ {--
207
+ # Debug primitives
208
+ Only works for local TVM execution with debug level verbosity
209
+ -}
210
+ ;;; Dumps the stack (at most the top 255 values) and shows the total stack depth.
211
+ () dump_stack() impure asm "DUMPSTK";
212
+
213
+ {-
214
+ # Persistent storage save and load
215
+ -}
216
+
217
+ ;;; Returns the persistent contract storage cell. It can be parsed or modified with slice and builder primitives later.
218
+ cell get_data() asm "c4 PUSH";
219
+
220
+ ;;; Sets `cell` [c] as persistent contract data. You can update persistent contract storage with this primitive.
221
+ () set_data(cell c) impure asm "c4 POP";
222
+
223
+ {-
224
+ # Continuation primitives
225
+ -}
226
+ ;;; Usually `c3` has a continuation initialized by the whole code of the contract. It is used for function calls.
227
+ ;;; The primitive returns the current value of `c3`.
228
+ cont get_c3() impure asm "c3 PUSH";
229
+
230
+ ;;; Updates the current value of `c3`. Usually, it is used for updating smart contract code in run-time.
231
+ ;;; Note that after execution of this primitive the current code
232
+ ;;; (and the stack of recursive function calls) won't change,
233
+ ;;; but any other function call will use a function from the new code.
234
+ () set_c3(cont c) impure asm "c3 POP";
235
+
236
+ ;;; Transforms a `slice` [s] into a simple ordinary continuation `c`, with `c.code = s` and an empty stack and savelist.
237
+ cont bless(slice s) impure asm "BLESS";
238
+
239
+ {---
240
+ # Gas related primitives
241
+ -}
242
+
243
+ ;;; Sets current gas limit `gl` to its maximal allowed value `gm`, and resets the gas credit `gc` to zero,
244
+ ;;; decreasing the value of `gr` by `gc` in the process.
245
+ ;;; In other words, the current smart contract agrees to buy some gas to finish the current transaction.
246
+ ;;; This action is required to process external messages, which bring no value (hence no gas) with themselves.
247
+ ;;;
248
+ ;;; For more details check [accept_message effects](https://ton.org/docs/#/smart-contracts/accept).
249
+ () accept_message() impure asm "ACCEPT";
250
+
251
+ ;;; Sets current gas limit `gl` to the minimum of limit and `gm`, and resets the gas credit `gc` to zero.
252
+ ;;; If the gas consumed so far (including the present instruction) exceeds the resulting value of `gl`,
253
+ ;;; an (unhandled) out of gas exception is thrown before setting new gas limits.
254
+ ;;; Notice that [set_gas_limit] with an argument `limit ≥ 2^63 − 1` is equivalent to [accept_message].
255
+ () set_gas_limit(int limit) impure asm "SETGASLIMIT";
256
+
257
+ ;;; Commits the current state of registers `c4` (“persistent data”) and `c5` (“actions”)
258
+ ;;; so that the current execution is considered “successful” with the saved values even if an exception
259
+ ;;; in Computation Phase is thrown later.
260
+ () commit() impure asm "COMMIT";
261
+
262
+ ;;; Not implemented
263
+ ;;; Computes the amount of gas that can be bought for `amount` nanoTONs,
264
+ ;;; and sets `gl` accordingly in the same way as [set_gas_limit].
265
+ ;;() buy_gas(int amount) impure asm "BUYGAS";
266
+
267
+ ;;; Computes the minimum of two integers [x] and [y].
268
+ int min(int x, int y) asm "MIN";
269
+
270
+ ;;; Computes the maximum of two integers [x] and [y].
271
+ int max(int x, int y) asm "MAX";
272
+
273
+ ;;; Sorts two integers.
274
+ (int, int) minmax(int x, int y) asm "MINMAX";
275
+
276
+ ;;; Computes the absolute value of an integer [x].
277
+ int abs(int x) asm "ABS";
278
+
279
+ {-
280
+ # Slice primitives
281
+
282
+ It is said that a primitive _loads_ some data,
283
+ if it returns the data and the remainder of the slice
284
+ (so it can also be used as [modifying method](https://ton.org/docs/#/func/statements?id=modifying-methods)).
285
+
286
+ It is said that a primitive _preloads_ some data, if it returns only the data
287
+ (it can be used as [non-modifying method](https://ton.org/docs/#/func/statements?id=non-modifying-methods)).
288
+
289
+ Unless otherwise stated, loading and preloading primitives read the data from a prefix of the slice.
290
+ -}
291
+
292
+
293
+ ;;; Converts a `cell` [c] into a `slice`. Notice that [c] must be either an ordinary cell,
294
+ ;;; or an exotic cell (see [TVM.pdf](https://ton-blockchain.github.io/docs/tvm.pdf), 3.1.2)
295
+ ;;; which is automatically loaded to yield an ordinary cell `c'`, converted into a `slice` afterwards.
296
+ slice begin_parse(cell c) asm "CTOS";
297
+
298
+ ;;; Checks if [s] is empty. If not, throws an exception.
299
+ () end_parse(slice s) impure asm "ENDS";
300
+
301
+ ;;; Loads the first reference from the slice.
302
+ (slice, cell) load_ref(slice s) asm(-> 1 0) "LDREF";
303
+
304
+ ;;; Preloads the first reference from the slice.
305
+ cell preload_ref(slice s) asm "PLDREF";
306
+
307
+ {- Functions below are commented because are implemented on compilator level for optimisation -}
308
+
309
+ ;;; Loads a signed [len]-bit integer from a slice [s].
310
+ ;; (slice, int) ~load_int(slice s, int len) asm(s len -> 1 0) "LDIX";
311
+
312
+ ;;; Loads an unsigned [len]-bit integer from a slice [s].
313
+ ;; (slice, int) ~load_uint(slice s, int len) asm( -> 1 0) "LDUX";
314
+
315
+ ;;; Preloads a signed [len]-bit integer from a slice [s].
316
+ ;; int preload_int(slice s, int len) asm "PLDIX";
317
+
318
+ ;;; Preloads an unsigned [len]-bit integer from a slice [s].
319
+ ;; int preload_uint(slice s, int len) asm "PLDUX";
320
+
321
+ ;;; Loads the first `0 ≤ len ≤ 1023` bits from slice [s] into a separate `slice s''`.
322
+ ;; (slice, slice) load_bits(slice s, int len) asm(s len -> 1 0) "LDSLICEX";
323
+
324
+ ;;; Preloads the first `0 ≤ len ≤ 1023` bits from slice [s] into a separate `slice s''`.
325
+ ;; slice preload_bits(slice s, int len) asm "PLDSLICEX";
326
+
327
+ ;;; Loads serialized amount of TonCoins (any unsigned integer up to `2^128 - 1`).
328
+ (slice, int) load_grams(slice s) asm(-> 1 0) "LDGRAMS";
329
+ (slice, int) load_coins(slice s) asm(-> 1 0) "LDVARUINT16";
330
+
331
+ ;;; Returns all but the first `0 ≤ len ≤ 1023` bits of `slice` [s].
332
+ slice skip_bits(slice s, int len) asm "SDSKIPFIRST";
333
+ (slice, ()) ~skip_bits(slice s, int len) asm "SDSKIPFIRST";
334
+
335
+ ;;; Returns the first `0 ≤ len ≤ 1023` bits of `slice` [s].
336
+ slice first_bits(slice s, int len) asm "SDCUTFIRST";
337
+
338
+ ;;; Returns all but the last `0 ≤ len ≤ 1023` bits of `slice` [s].
339
+ slice skip_last_bits(slice s, int len) asm "SDSKIPLAST";
340
+ (slice, ()) ~skip_last_bits(slice s, int len) asm "SDSKIPLAST";
341
+
342
+ ;;; Returns the last `0 ≤ len ≤ 1023` bits of `slice` [s].
343
+ slice slice_last(slice s, int len) asm "SDCUTLAST";
344
+
345
+ ;;; Loads a dictionary `D` (HashMapE) from `slice` [s].
346
+ ;;; (returns `null` if `nothing` constructor is used).
347
+ (slice, cell) load_dict(slice s) asm(-> 1 0) "LDDICT";
348
+
349
+ ;;; Preloads a dictionary `D` from `slice` [s].
350
+ cell preload_dict(slice s) asm "PLDDICT";
351
+
352
+ ;;; Loads a dictionary as [load_dict], but returns only the remainder of the slice.
353
+ slice skip_dict(slice s) asm "SKIPDICT";
354
+ (slice, ()) ~skip_dict(slice s) asm "SKIPDICT";
355
+
356
+ ;;; Loads (Maybe ^Cell) from `slice` [s].
357
+ ;;; In other words loads 1 bit and if it is true
358
+ ;;; loads first ref and return it with slice remainder
359
+ ;;; otherwise returns `null` and slice remainder
360
+ (slice, cell) load_maybe_ref(slice s) asm(-> 1 0) "LDOPTREF";
361
+
362
+ ;;; Preloads (Maybe ^Cell) from `slice` [s].
363
+ cell preload_maybe_ref(slice s) asm "PLDOPTREF";
364
+
365
+
366
+ ;;; Returns the depth of `cell` [c].
367
+ ;;; If [c] has no references, then return `0`;
368
+ ;;; otherwise the returned value is one plus the maximum of depths of cells referred to from [c].
369
+ ;;; If [c] is a `null` instead of a cell, returns zero.
370
+ int cell_depth(cell c) asm "CDEPTH";
371
+
372
+
373
+ {-
374
+ # Slice size primitives
375
+ -}
376
+
377
+ ;;; Returns the number of references in `slice` [s].
378
+ int slice_refs(slice s) asm "SREFS";
379
+
380
+ ;;; Returns the number of data bits in `slice` [s].
381
+ int slice_bits(slice s) asm "SBITS";
382
+
383
+ ;;; Returns both the number of data bits and the number of references in `slice` [s].
384
+ (int, int) slice_bits_refs(slice s) asm "SBITREFS";
385
+
386
+ ;;; Checks whether a `slice` [s] is empty (i.e., contains no bits of data and no cell references).
387
+ int slice_empty?(slice s) asm "SEMPTY";
388
+
389
+ ;;; Checks whether `slice` [s] has no bits of data.
390
+ int slice_data_empty?(slice s) asm "SDEMPTY";
391
+
392
+ ;;; Checks whether `slice` [s] has no references.
393
+ int slice_refs_empty?(slice s) asm "SREMPTY";
394
+
395
+ ;;; Returns the depth of `slice` [s].
396
+ ;;; If [s] has no references, then returns `0`;
397
+ ;;; otherwise the returned value is one plus the maximum of depths of cells referred to from [s].
398
+ int slice_depth(slice s) asm "SDEPTH";
399
+
400
+ {-
401
+ # Builder size primitives
402
+ -}
403
+
404
+ ;;; Returns the number of cell references already stored in `builder` [b]
405
+ int builder_refs(builder b) asm "BREFS";
406
+
407
+ ;;; Returns the number of data bits already stored in `builder` [b].
408
+ int builder_bits(builder b) asm "BBITS";
409
+
410
+ ;;; Returns the depth of `builder` [b].
411
+ ;;; If no cell references are stored in [b], then returns 0;
412
+ ;;; otherwise the returned value is one plus the maximum of depths of cells referred to from [b].
413
+ int builder_depth(builder b) asm "BDEPTH";
414
+
415
+ {-
416
+ # Builder primitives
417
+ It is said that a primitive _stores_ a value `x` into a builder `b`
418
+ if it returns a modified version of the builder `b'` with the value `x` stored at the end of it.
419
+ It can be used as [non-modifying method](https://ton.org/docs/#/func/statements?id=non-modifying-methods).
420
+
421
+ All the primitives below first check whether there is enough space in the `builder`,
422
+ and only then check the range of the value being serialized.
423
+ -}
424
+
425
+ ;;; Creates a new empty `builder`.
426
+ builder begin_cell() asm "NEWC";
427
+
428
+ ;;; Converts a `builder` into an ordinary `cell`.
429
+ cell end_cell(builder b) asm "ENDC";
430
+
431
+ ;;; Stores a reference to `cell` [c] into `builder` [b].
432
+ builder store_ref(builder b, cell c) asm(c b) "STREF";
433
+
434
+ ;;; Stores an unsigned [len]-bit integer `x` into `b` for `0 ≤ len ≤ 256`.
435
+ ;; builder store_uint(builder b, int x, int len) asm(x b len) "STUX";
436
+
437
+ ;;; Stores a signed [len]-bit integer `x` into `b` for` 0 ≤ len ≤ 257`.
438
+ ;; builder store_int(builder b, int x, int len) asm(x b len) "STIX";
439
+
440
+
441
+ ;;; Stores `slice` [s] into `builder` [b]
442
+ builder store_slice(builder b, slice s) asm "STSLICER";
443
+
444
+ ;;; Stores (serializes) an integer [x] in the range `0..2^128 − 1` into `builder` [b].
445
+ ;;; The serialization of [x] consists of a 4-bit unsigned big-endian integer `l`,
446
+ ;;; which is the smallest integer `l ≥ 0`, such that `x < 2^8l`,
447
+ ;;; followed by an `8l`-bit unsigned big-endian representation of [x].
448
+ ;;; If [x] does not belong to the supported range, a range check exception is thrown.
449
+ ;;;
450
+ ;;; Store amounts of TonCoins to the builder as VarUInteger 16
451
+ builder store_grams(builder b, int x) asm "STGRAMS";
452
+ builder store_coins(builder b, int x) asm "STVARUINT16";
453
+
454
+ ;;; Stores dictionary `D` represented by `cell` [c] or `null` into `builder` [b].
455
+ ;;; In other words, stores a `1`-bit and a reference to [c] if [c] is not `null` and `0`-bit otherwise.
456
+ builder store_dict(builder b, cell c) asm(c b) "STDICT";
457
+
458
+ ;;; Stores (Maybe ^Cell) to builder:
459
+ ;;; if cell is null store 1 zero bit
460
+ ;;; otherwise store 1 true bit and ref to cell
461
+ builder store_maybe_ref(builder b, cell c) asm(c b) "STOPTREF";
462
+
463
+
464
+ {-
465
+ # Address manipulation primitives
466
+ The address manipulation primitives listed below serialize and deserialize values according to the following TL-B scheme:
467
+ ```TL-B
468
+ addr_none$00 = MsgAddressExt;
469
+ addr_extern$01 len:(## 8) external_address:(bits len)
470
+ = MsgAddressExt;
471
+ anycast_info$_ depth:(#<= 30) { depth >= 1 }
472
+ rewrite_pfx:(bits depth) = Anycast;
473
+ addr_std$10 anycast:(Maybe Anycast)
474
+ workchain_id:int8 address:bits256 = MsgAddressInt;
475
+ addr_var$11 anycast:(Maybe Anycast) addr_len:(## 9)
476
+ workchain_id:int32 address:(bits addr_len) = MsgAddressInt;
477
+ _ _:MsgAddressInt = MsgAddress;
478
+ _ _:MsgAddressExt = MsgAddress;
479
+
480
+ int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
481
+ src:MsgAddress dest:MsgAddressInt
482
+ value:CurrencyCollection ihr_fee:Grams fwd_fee:Grams
483
+ created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
484
+ ext_out_msg_info$11 src:MsgAddress dest:MsgAddressExt
485
+ created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
486
+ ```
487
+ A deserialized `MsgAddress` is represented by a tuple `t` as follows:
488
+
489
+ - `addr_none` is represented by `t = (0)`,
490
+ i.e., a tuple containing exactly one integer equal to zero.
491
+ - `addr_extern` is represented by `t = (1, s)`,
492
+ where slice `s` contains the field `external_address`. In other words, `
493
+ t` is a pair (a tuple consisting of two entries), containing an integer equal to one and slice `s`.
494
+ - `addr_std` is represented by `t = (2, u, x, s)`,
495
+ where `u` is either a `null` (if `anycast` is absent) or a slice `s'` containing `rewrite_pfx` (if anycast is present).
496
+ Next, integer `x` is the `workchain_id`, and slice `s` contains the address.
497
+ - `addr_var` is represented by `t = (3, u, x, s)`,
498
+ where `u`, `x`, and `s` have the same meaning as for `addr_std`.
499
+ -}
500
+
501
+ ;;; Loads from slice [s] the only prefix that is a valid `MsgAddress`,
502
+ ;;; and returns both this prefix `s'` and the remainder `s''` of [s] as slices.
503
+ (slice, slice) load_msg_addr(slice s) asm(-> 1 0) "LDMSGADDR";
504
+
505
+ ;;; Decomposes slice [s] containing a valid `MsgAddress` into a `tuple t` with separate fields of this `MsgAddress`.
506
+ ;;; If [s] is not a valid `MsgAddress`, a cell deserialization exception is thrown.
507
+ tuple parse_addr(slice s) asm "PARSEMSGADDR";
508
+
509
+ ;;; Parses slice [s] containing a valid `MsgAddressInt` (usually a `msg_addr_std`),
510
+ ;;; applies rewriting from the anycast (if present) to the same-length prefix of the address,
511
+ ;;; and returns both the workchain and the 256-bit address as integers.
512
+ ;;; If the address is not 256-bit, or if [s] is not a valid serialization of `MsgAddressInt`,
513
+ ;;; throws a cell deserialization exception.
514
+ (int, int) parse_std_addr(slice s) asm "REWRITESTDADDR";
515
+
516
+ ;;; A variant of [parse_std_addr] that returns the (rewritten) address as a slice [s],
517
+ ;;; even if it is not exactly 256 bit long (represented by a `msg_addr_var`).
518
+ (int, slice) parse_var_addr(slice s) asm "REWRITEVARADDR";
519
+
520
+ {-
521
+ # Dictionary primitives
522
+ -}
523
+
524
+
525
+ ;;; Sets the value associated with [key_len]-bit key signed index in dictionary [dict] to [value] (cell),
526
+ ;;; and returns the resulting dictionary.
527
+ cell idict_set_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETREF";
528
+ (cell, ()) ~idict_set_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETREF";
529
+
530
+ ;;; Sets the value associated with [key_len]-bit key unsigned index in dictionary [dict] to [value] (cell),
531
+ ;;; and returns the resulting dictionary.
532
+ cell udict_set_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTUSETREF";
533
+ (cell, ()) ~udict_set_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTUSETREF";
534
+
535
+ cell idict_get_ref(cell dict, int key_len, int index) asm(index dict key_len) "DICTIGETOPTREF";
536
+ (cell, int) idict_get_ref?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIGETREF" "NULLSWAPIFNOT";
537
+ (cell, int) udict_get_ref?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUGETREF" "NULLSWAPIFNOT";
538
+ (cell, cell) idict_set_get_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTISETGETOPTREF";
539
+ (cell, cell) udict_set_get_ref(cell dict, int key_len, int index, cell value) asm(value index dict key_len) "DICTUSETGETOPTREF";
540
+ (cell, int) idict_delete?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIDEL";
541
+ (cell, int) udict_delete?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUDEL";
542
+ (slice, int) idict_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIGET" "NULLSWAPIFNOT";
543
+ (slice, int) udict_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUGET" "NULLSWAPIFNOT";
544
+ (cell, slice, int) idict_delete_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIDELGET" "NULLSWAPIFNOT";
545
+ (cell, slice, int) udict_delete_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUDELGET" "NULLSWAPIFNOT";
546
+ (cell, (slice, int)) ~idict_delete_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTIDELGET" "NULLSWAPIFNOT";
547
+ (cell, (slice, int)) ~udict_delete_get?(cell dict, int key_len, int index) asm(index dict key_len) "DICTUDELGET" "NULLSWAPIFNOT";
548
+ cell udict_set(cell dict, int key_len, int index, slice value) asm(value index dict key_len) "DICTUSET";
549
+ (cell, ()) ~udict_set(cell dict, int key_len, int index, slice value) asm(value index dict key_len) "DICTUSET";
550
+ cell idict_set(cell dict, int key_len, int index, slice value) asm(value index dict key_len) "DICTISET";
551
+ (cell, ()) ~idict_set(cell dict, int key_len, int index, slice value) asm(value index dict key_len) "DICTISET";
552
+ cell dict_set(cell dict, int key_len, slice index, slice value) asm(value index dict key_len) "DICTSET";
553
+ (cell, ()) ~dict_set(cell dict, int key_len, slice index, slice value) asm(value index dict key_len) "DICTSET";
554
+ (cell, int) udict_add?(cell dict, int key_len, int index, slice value) asm(value index dict key_len) "DICTUADD";
555
+ (cell, int) udict_replace?(cell dict, int key_len, int index, slice value) asm(value index dict key_len) "DICTUREPLACE";
556
+ (cell, int) idict_add?(cell dict, int key_len, int index, slice value) asm(value index dict key_len) "DICTIADD";
557
+ (cell, int) idict_replace?(cell dict, int key_len, int index, slice value) asm(value index dict key_len) "DICTIREPLACE";
558
+ cell udict_set_builder(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTUSETB";
559
+ (cell, ()) ~udict_set_builder(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTUSETB";
560
+ cell idict_set_builder(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTISETB";
561
+ (cell, ()) ~idict_set_builder(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTISETB";
562
+ cell dict_set_builder(cell dict, int key_len, slice index, builder value) asm(value index dict key_len) "DICTSETB";
563
+ (cell, ()) ~dict_set_builder(cell dict, int key_len, slice index, builder value) asm(value index dict key_len) "DICTSETB";
564
+ (cell, int) udict_add_builder?(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTUADDB";
565
+ (cell, int) udict_replace_builder?(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTUREPLACEB";
566
+ (cell, int) idict_add_builder?(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTIADDB";
567
+ (cell, int) idict_replace_builder?(cell dict, int key_len, int index, builder value) asm(value index dict key_len) "DICTIREPLACEB";
568
+ (cell, int, slice, int) udict_delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTUREMMIN" "NULLSWAPIFNOT2";
569
+ (cell, (int, slice, int)) ~udict::delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTUREMMIN" "NULLSWAPIFNOT2";
570
+ (cell, int, slice, int) idict_delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTIREMMIN" "NULLSWAPIFNOT2";
571
+ (cell, (int, slice, int)) ~idict::delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTIREMMIN" "NULLSWAPIFNOT2";
572
+ (cell, slice, slice, int) dict_delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTREMMIN" "NULLSWAPIFNOT2";
573
+ (cell, (slice, slice, int)) ~dict::delete_get_min(cell dict, int key_len) asm(-> 0 2 1 3) "DICTREMMIN" "NULLSWAPIFNOT2";
574
+ (cell, int, slice, int) udict_delete_get_max(cell dict, int key_len) asm(-> 0 2 1 3) "DICTUREMMAX" "NULLSWAPIFNOT2";
575
+ (cell, (int, slice, int)) ~udict::delete_get_max(cell dict, int key_len) asm(-> 0 2 1 3) "DICTUREMMAX" "NULLSWAPIFNOT2";
576
+ (cell, int, slice, int) idict_delete_get_max(cell dict, int key_len) asm(-> 0 2 1 3) "DICTIREMMAX" "NULLSWAPIFNOT2";
577
+ (cell, (int, slice, int)) ~idict::delete_get_max(cell dict, int key_len) asm(-> 0 2 1 3) "DICTIREMMAX" "NULLSWAPIFNOT2";
578
+ (cell, slice, slice, int) dict_delete_get_max(cell dict, int key_len) asm(-> 0 2 1 3) "DICTREMMAX" "NULLSWAPIFNOT2";
579
+ (cell, (slice, slice, int)) ~dict::delete_get_max(cell dict, int key_len) asm(-> 0 2 1 3) "DICTREMMAX" "NULLSWAPIFNOT2";
580
+ (int, slice, int) udict_get_min?(cell dict, int key_len) asm (-> 1 0 2) "DICTUMIN" "NULLSWAPIFNOT2";
581
+ (int, slice, int) udict_get_max?(cell dict, int key_len) asm (-> 1 0 2) "DICTUMAX" "NULLSWAPIFNOT2";
582
+ (int, cell, int) udict_get_min_ref?(cell dict, int key_len) asm (-> 1 0 2) "DICTUMINREF" "NULLSWAPIFNOT2";
583
+ (int, cell, int) udict_get_max_ref?(cell dict, int key_len) asm (-> 1 0 2) "DICTUMAXREF" "NULLSWAPIFNOT2";
584
+ (int, slice, int) idict_get_min?(cell dict, int key_len) asm (-> 1 0 2) "DICTIMIN" "NULLSWAPIFNOT2";
585
+ (int, slice, int) idict_get_max?(cell dict, int key_len) asm (-> 1 0 2) "DICTIMAX" "NULLSWAPIFNOT2";
586
+ (int, cell, int) idict_get_min_ref?(cell dict, int key_len) asm (-> 1 0 2) "DICTIMINREF" "NULLSWAPIFNOT2";
587
+ (int, cell, int) idict_get_max_ref?(cell dict, int key_len) asm (-> 1 0 2) "DICTIMAXREF" "NULLSWAPIFNOT2";
588
+ (int, slice, int) udict_get_next?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTUGETNEXT" "NULLSWAPIFNOT2";
589
+ (int, slice, int) udict_get_nexteq?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTUGETNEXTEQ" "NULLSWAPIFNOT2";
590
+ (int, slice, int) udict_get_prev?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTUGETPREV" "NULLSWAPIFNOT2";
591
+ (int, slice, int) udict_get_preveq?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTUGETPREVEQ" "NULLSWAPIFNOT2";
592
+ (int, slice, int) idict_get_next?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTIGETNEXT" "NULLSWAPIFNOT2";
593
+ (int, slice, int) idict_get_nexteq?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTIGETNEXTEQ" "NULLSWAPIFNOT2";
594
+ (int, slice, int) idict_get_prev?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTIGETPREV" "NULLSWAPIFNOT2";
595
+ (int, slice, int) idict_get_preveq?(cell dict, int key_len, int pivot) asm(pivot dict key_len -> 1 0 2) "DICTIGETPREVEQ" "NULLSWAPIFNOT2";
596
+
597
+ ;;; Creates an empty dictionary, which is actually a null value. Equivalent to PUSHNULL
598
+ cell new_dict() asm "NEWDICT";
599
+ ;;; Checks whether a dictionary is empty. Equivalent to cell_null?.
600
+ int dict_empty?(cell c) asm "DICTEMPTY";
601
+
602
+
603
+ {- Prefix dictionary primitives -}
604
+ (slice, slice, slice, int) pfxdict_get?(cell dict, int key_len, slice key) asm(key dict key_len) "PFXDICTGETQ" "NULLSWAPIFNOT2";
605
+ (cell, int) pfxdict_set?(cell dict, int key_len, slice key, slice value) asm(value key dict key_len) "PFXDICTSET";
606
+ (cell, int) pfxdict_delete?(cell dict, int key_len, slice key) asm(key dict key_len) "PFXDICTDEL";
607
+
608
+ ;;; Returns the value of the global configuration parameter with integer index `i` as a `cell` or `null` value.
609
+ cell config_param(int x) asm "CONFIGOPTPARAM";
610
+ ;;; Checks whether c is a null. Note, that FunC also has polymorphic null? built-in.
611
+ int cell_null?(cell c) asm "ISNULL";
612
+
613
+ ;;; Creates an output action which would reserve exactly amount nanotoncoins (if mode = 0), at most amount nanotoncoins (if mode = 2), or all but amount nanotoncoins (if mode = 1 or mode = 3), from the remaining balance of the account. It is roughly equivalent to creating an outbound message carrying amount nanotoncoins (or b − amount nanotoncoins, where b is the remaining balance) to oneself, so that the subsequent output actions would not be able to spend more money than the remainder. Bit +2 in mode means that the external action does not fail if the specified amount cannot be reserved; instead, all remaining balance is reserved. Bit +8 in mode means `amount <- -amount` before performing any further actions. Bit +4 in mode means that amount is increased by the original balance of the current account (before the compute phase), including all extra currencies, before performing any other checks and actions. Currently, amount must be a non-negative integer, and mode must be in the range 0..15.
614
+ () raw_reserve(int amount, int mode) impure asm "RAWRESERVE";
615
+ ;;; Similar to raw_reserve, but also accepts a dictionary extra_amount (represented by a cell or null) with extra currencies. In this way currencies other than TonCoin can be reserved.
616
+ () raw_reserve_extra(int amount, cell extra_amount, int mode) impure asm "RAWRESERVEX";
617
+ ;;; Sends a raw message contained in msg, which should contain a correctly serialized object Message X, with the only exception that the source address is allowed to have dummy value addr_none (to be automatically replaced with the current smart contract address), and ihr_fee, fwd_fee, created_lt and created_at fields can have arbitrary values (to be rewritten with correct values during the action phase of the current transaction). Integer parameter mode contains the flags. Currently mode = 0 is used for ordinary messages; mode = 128 is used for messages that are to carry all the remaining balance of the current smart contract (instead of the value originally indicated in the message); mode = 64 is used for messages that carry all the remaining value of the inbound message in addition to the value initially indicated in the new message (if bit 0 is not set, the gas fees are deducted from this amount); mode' = mode + 1 means that the sender wants to pay transfer fees separately; mode' = mode + 2 means that any errors arising while processing this message during the action phase should be ignored. Finally, mode' = mode + 32 means that the current account must be destroyed if its resulting balance is zero. This flag is usually employed together with +128.
618
+ () send_raw_message(cell msg, int mode) impure asm "SENDRAWMSG";
619
+ ;;; Creates an output action that would change this smart contract code to that given by cell new_code. Notice that this change will take effect only after the successful termination of the current run of the smart contract
620
+ () set_code(cell new_code) impure asm "SETCODE";
621
+
622
+ ;;; Generates a new pseudo-random unsigned 256-bit integer x. The algorithm is as follows: if r is the old value of the random seed, considered as a 32-byte array (by constructing the big-endian representation of an unsigned 256-bit integer), then its sha512(r) is computed; the first 32 bytes of this hash are stored as the new value r' of the random seed, and the remaining 32 bytes are returned as the next random value x.
623
+ int random() impure asm "RANDU256";
624
+ ;;; Generates a new pseudo-random integer z in the range 0..range−1 (or range..−1, if range < 0). More precisely, an unsigned random value x is generated as in random; then z := x * range / 2^256 is computed.
625
+ int rand(int range) impure asm "RAND";
626
+ ;;; Returns the current random seed as an unsigned 256-bit Integer.
627
+ int get_seed() impure asm "RANDSEED";
628
+ ;;; Sets the random seed to unsigned 256-bit seed.
629
+ () set_seed(int x) impure asm "SETRAND";
630
+ ;;; Mixes unsigned 256-bit integer x into the random seed r by setting the random seed to sha256 of the concatenation of two 32-byte strings: the first with the big-endian representation of the old seed r, and the second with the big-endian representation of x.
631
+ () randomize(int x) impure asm "ADDRAND";
632
+ ;;; Equivalent to randomize(cur_lt());.
633
+ () randomize_lt() impure asm "LTIME" "ADDRAND";
634
+
635
+ ;;; Checks whether the data parts of two slices coinside
636
+ int equal_slices_bits(slice a, slice b) asm "SDEQ";
637
+ ;;; Checks whether b is a null. Note, that FunC also has polymorphic null? built-in.
638
+ int builder_null?(builder b) asm "ISNULL";
639
+ ;;; Concatenates two builders
640
+ builder store_builder(builder to, builder from) asm "STBR";
641
+
642
+ ;; CUSTOM:
643
+
644
+ ;; TVM UPGRADE 2023-07 https://docs.ton.org/learn/tvm-instructions/tvm-upgrade-2023-07
645
+ ;; In mainnet since 20 Dec 2023 https://t.me/tonblockchain/226
646
+
647
+ ;;; Retrieves code of smart-contract from c7
648
+ cell my_code() asm "MYCODE";
649
+
650
+ ;;; Creates an output action and returns a fee for creating a message. Mode has the same effect as in the case of SENDRAWMSG
651
+ int send_message(cell msg, int mode) impure asm "SENDMSG";
652
+
653
+ int gas_consumed() asm "GASCONSUMED";
654
+
655
+ ;; TVM V6 https://github.com/ton-blockchain/ton/blob/testnet/doc/GlobalVersions.md#version-6
656
+
657
+ int get_compute_fee(int workchain, int gas_used) asm(gas_used workchain) "GETGASFEE";
658
+ int get_storage_fee(int workchain, int seconds, int bits, int cells) asm(cells bits seconds workchain) "GETSTORAGEFEE";
659
+ int get_forward_fee(int workchain, int bits, int cells) asm(cells bits workchain) "GETFORWARDFEE";
660
+ int get_precompiled_gas_consumption() asm "GETPRECOMPILEDGAS";
661
+
662
+ int get_simple_compute_fee(int workchain, int gas_used) asm(gas_used workchain) "GETGASFEESIMPLE";
663
+ int get_simple_forward_fee(int workchain, int bits, int cells) asm(cells bits workchain) "GETFORWARDFEESIMPLE";
664
+ int get_original_fwd_fee(int workchain, int fwd_fee) asm(fwd_fee workchain) "GETORIGINALFWDFEE";
665
+ int my_storage_due() asm "DUEPAYMENT";
666
+
667
+ tuple get_fee_cofigs() asm "UNPACKEDCONFIGTUPLE";
668
+
669
+ ;; BASIC
670
+
671
+ const int TRUE = -1;
672
+ const int FALSE = 0;
673
+
674
+ const int MASTERCHAIN = -1;
675
+ const int BASECHAIN = 0;
676
+
677
+ ;;; skip (Maybe ^Cell) from `slice` [s].
678
+ (slice, ()) ~skip_maybe_ref(slice s) asm "SKIPOPTREF";
679
+
680
+ (slice, int) ~load_bool(slice s) inline {
681
+ return s.load_int(1);
682
+ }
683
+
684
+ builder store_bool(builder b, int value) inline {
685
+ return b.store_int(value, 1);
686
+ }
687
+
688
+ ;; ADDRESS NONE
689
+ ;; addr_none$00 = MsgAddressExt; https://github.com/ton-blockchain/ton/blob/8a9ff339927b22b72819c5125428b70c406da631/crypto/block/block.tlb#L100
690
+
691
+ builder store_address_none(builder b) inline {
692
+ return b.store_uint(0, 2);
693
+ }
694
+
695
+ slice address_none() asm "<b 0 2 u, b> <s PUSHSLICE";
696
+
697
+ int is_address_none(slice s) inline {
698
+ return s.preload_uint(2) == 0;
699
+ }
700
+
701
+ ;; MESSAGE
702
+
703
+ ;; The message header info is organized as follows:
704
+
705
+ ;; https://github.com/ton-blockchain/ton/blob/8a9ff339927b22b72819c5125428b70c406da631/crypto/block/block.tlb#L126
706
+ ;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
707
+ ;; src:MsgAddressInt dest:MsgAddressInt
708
+ ;; value:CurrencyCollection ihr_fee:Grams fwd_fee:Grams
709
+ ;; created_lt:uint64 created_at:uint32 = CommonMsgInfo;
710
+
711
+ ;; https://github.com/ton-blockchain/ton/blob/8a9ff339927b22b72819c5125428b70c406da631/crypto/block/block.tlb#L135
712
+ ;; int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
713
+ ;; src:MsgAddress dest:MsgAddressInt
714
+ ;; value:CurrencyCollection ihr_fee:Grams fwd_fee:Grams
715
+ ;; created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
716
+
717
+
718
+ ;; https://github.com/ton-blockchain/ton/blob/8a9ff339927b22b72819c5125428b70c406da631/crypto/block/block.tlb#L123C1-L124C33
719
+ ;; currencies$_ grams:Grams other:ExtraCurrencyCollection = CurrencyCollection;
720
+
721
+ ;; MSG FLAGS
722
+
723
+ const int BOUNCEABLE = 0x18; ;; 0b011000 tag - 0, ihr_disabled - 1, bounce - 1, bounced - 0, src = adr_none$00
724
+ const int NON_BOUNCEABLE = 0x10; ;; 0b010000 tag - 0, ihr_disabled - 1, bounce - 0, bounced - 0, src = adr_none$00
725
+
726
+ ;; store msg_flags and address none
727
+ builder store_msg_flags_and_address_none(builder b, int msg_flags) inline {
728
+ return b.store_uint(msg_flags, 6);
729
+ }
730
+
731
+ ;; load msg_flags only
732
+ (slice, int) ~load_msg_flags(slice s) inline {
733
+ return s.load_uint(4);
734
+ }
735
+ ;;; @param `msg_flags` - 4-bit
736
+ int is_bounced(int msg_flags) inline {
737
+ return msg_flags & 1 == 1;
738
+ }
739
+
740
+ (slice, ()) ~skip_bounced_prefix(slice s) inline {
741
+ return (s.skip_bits(32), ()); ;; skip 0xFFFFFFFF prefix
742
+ }
743
+
744
+ ;; after `grams:Grams` we have (1 + 4 + 4 + 64 + 32) zeroes - zeroed extracurrency, ihr_fee, fwd_fee, created_lt and created_at
745
+ const int MSG_INFO_REST_BITS = 1 + 4 + 4 + 64 + 32;
746
+
747
+ ;; MSG
748
+
749
+ ;; https://github.com/ton-blockchain/ton/blob/8a9ff339927b22b72819c5125428b70c406da631/crypto/block/block.tlb#L155
750
+ ;; message$_ {X:Type} info:CommonMsgInfo
751
+ ;; init:Maybe (Either StateInit ^StateInit)
752
+ ;; body:(Either X ^X) = Message X;
753
+ ;;
754
+ ;;message$_ {X:Type} info:CommonMsgInfoRelaxed
755
+ ;; init:(Maybe (Either StateInit ^StateInit))
756
+ ;; body:(Either X ^X) = MessageRelaxed X;
757
+ ;;
758
+ ;;_ (Message Any) = MessageAny;
759
+
760
+ ;; if have StateInit (always place StateInit in ref):
761
+ ;; 0b11 for `Maybe (Either StateInit ^StateInit)` and 0b1 or 0b0 for `body:(Either X ^X)`
762
+
763
+ const int MSG_WITH_STATE_INIT_AND_BODY_SIZE = MSG_INFO_REST_BITS + 1 + 1 + 1;
764
+ const int MSG_HAVE_STATE_INIT = 4;
765
+ const int MSG_STATE_INIT_IN_REF = 2;
766
+ const int MSG_BODY_IN_REF = 1;
767
+
768
+ ;; if no StateInit:
769
+ ;; 0b0 for `Maybe (Either StateInit ^StateInit)` and 0b1 or 0b0 for `body:(Either X ^X)`
770
+
771
+ const int MSG_ONLY_BODY_SIZE = MSG_INFO_REST_BITS + 1 + 1;
772
+
773
+ builder store_statinit_ref_and_body_ref(builder b, cell state_init, cell body) inline {
774
+ return b
775
+ .store_uint(MSG_HAVE_STATE_INIT + MSG_STATE_INIT_IN_REF + MSG_BODY_IN_REF, MSG_WITH_STATE_INIT_AND_BODY_SIZE)
776
+ .store_ref(state_init)
777
+ .store_ref(body);
778
+ }
779
+
780
+ builder store_only_body_ref(builder b, cell body) inline {
781
+ return b
782
+ .store_uint(MSG_BODY_IN_REF, MSG_ONLY_BODY_SIZE)
783
+ .store_ref(body);
784
+ }
785
+
786
+ builder store_prefix_only_body(builder b) inline {
787
+ return b
788
+ .store_uint(0, MSG_ONLY_BODY_SIZE);
789
+ }
790
+
791
+ ;; parse after sender_address
792
+ (slice, int) ~retrieve_fwd_fee(slice in_msg_full_slice) inline {
793
+ in_msg_full_slice~load_msg_addr(); ;; skip dst
794
+ in_msg_full_slice~load_coins(); ;; skip value
795
+ in_msg_full_slice~skip_dict(); ;; skip extracurrency collection
796
+ in_msg_full_slice~load_coins(); ;; skip ihr_fee
797
+ int fwd_fee = in_msg_full_slice~load_coins();
798
+ return (in_msg_full_slice, fwd_fee);
799
+ }
800
+
801
+ ;; MSG BODY
802
+
803
+ ;; According to the guideline, it is recommended to start the body of the internal message with uint32 op and uint64 query_id
804
+
805
+ const int MSG_OP_SIZE = 32;
806
+ const int MSG_QUERY_ID_SIZE = 64;
807
+
808
+ (slice, int) ~load_op(slice s) inline {
809
+ return s.load_uint(MSG_OP_SIZE);
810
+ }
811
+ (slice, ()) ~skip_op(slice s) inline {
812
+ return (s.skip_bits(MSG_OP_SIZE), ());
813
+ }
814
+ builder store_op(builder b, int op) inline {
815
+ return b.store_uint(op, MSG_OP_SIZE);
816
+ }
817
+
818
+ (slice, int) ~load_query_id(slice s) inline {
819
+ return s.load_uint(MSG_QUERY_ID_SIZE);
820
+ }
821
+ (slice, ()) ~skip_query_id(slice s) inline {
822
+ return (s.skip_bits(MSG_QUERY_ID_SIZE), ());
823
+ }
824
+ builder store_query_id(builder b, int query_id) inline {
825
+ return b.store_uint(query_id, MSG_QUERY_ID_SIZE);
826
+ }
827
+
828
+ (slice, (int, int)) ~load_op_and_query_id(slice s) inline {
829
+ int op = s~load_op();
830
+ int query_id = s~load_query_id();
831
+ return (s, (op, query_id));
832
+ }
833
+
834
+ ;; SEND MODES - https://docs.ton.org/tvm.pdf page 137, SENDRAWMSG
835
+
836
+ ;; For `send_raw_message` and `send_message`:
837
+
838
+ ;;; x = 0 is used for ordinary messages; the gas fees are deducted from the senging amount; action phaes should NOT be ignored.
839
+ const int SEND_MODE_REGULAR = 0;
840
+ ;;; +1 means that the sender wants to pay transfer fees separately.
841
+ const int SEND_MODE_PAY_FEES_SEPARATELY = 1;
842
+ ;;; + 2 means that any errors arising while processing this message during the action phase should be ignored.
843
+ const int SEND_MODE_IGNORE_ERRORS = 2;
844
+ ;;; + 32 means that the current account must be destroyed if its resulting balance is zero.
845
+ const int SEND_MODE_DESTROY = 32;
846
+ ;;; x = 64 is used for messages that carry all the remaining value of the inbound message in addition to the value initially indicated in the new message.
847
+ const int SEND_MODE_CARRY_ALL_REMAINING_MESSAGE_VALUE = 64;
848
+ ;;; x = 128 is used for messages that are to carry all the remaining balance of the current smart contract (instead of the value originally indicated in the message).
849
+ const int SEND_MODE_CARRY_ALL_BALANCE = 128;
850
+ ;;; in the case of action fail - bounce transaction. No effect if SEND_MODE_IGNORE_ERRORS (+2) is used. TVM UPGRADE 2023-07. https://docs.ton.org/learn/tvm-instructions/tvm-upgrade-2023-07#sending-messages
851
+ const int SEND_MODE_BOUNCE_ON_ACTION_FAIL = 16;
852
+
853
+ ;; Only for `send_message`:
854
+
855
+ ;;; do not create an action, only estimate fee. TVM UPGRADE 2023-07. https://docs.ton.org/learn/tvm-instructions/tvm-upgrade-2023-07#sending-messages
856
+ const int SEND_MODE_ESTIMATE_FEE_ONLY = 1024;
857
+
858
+ ;; Other modes affect the fee calculation as follows:
859
+ ;; +64 substitutes the entire balance of the incoming message as an outcoming value (slightly inaccurate, gas expenses that cannot be estimated before the computation is completed are not taken into account).
860
+ ;; +128 substitutes the value of the entire balance of the contract before the start of the computation phase (slightly inaccurate, since gas expenses that cannot be estimated before the completion of the computation phase are not taken into account).
861
+
862
+ ;; RESERVE MODES - https://docs.ton.org/tvm.pdf page 137, RAWRESERVE
863
+
864
+ ;;; Creates an output action which would reserve exactly x nanograms (if y = 0).
865
+ const int RESERVE_REGULAR = 0;
866
+ ;;; Creates an output action which would reserve at most x nanograms (if y = 2).
867
+ ;;; Bit +2 in y means that the external action does not fail if the specified amount cannot be reserved; instead, all remaining balance is reserved.
868
+ const int RESERVE_AT_MOST = 2;
869
+ ;;; in the case of action fail - bounce transaction. No effect if RESERVE_AT_MOST (+2) is used. TVM UPGRADE 2023-07. https://docs.ton.org/learn/tvm-instructions/tvm-upgrade-2023-07#sending-messages
870
+ const int RESERVE_BOUNCE_ON_ACTION_FAIL = 16;
871
+
872
+ ;; TOKEN METADATA
873
+ ;; https://github.com/ton-blockchain/TEPs/blob/master/text/0064-token-data-standard.md
874
+
875
+ ;; Key is sha256 hash of string. Value is data encoded as described in "Data serialization" paragraph.
876
+ ;; Snake format - must be prefixed with 0x00 byte
877
+ (cell, ()) ~set_token_snake_metadata_entry(cell content_dict, int key, slice value) impure {
878
+ content_dict~udict_set_ref(256, key, begin_cell().store_uint(0, 8).store_slice(value).end_cell());
879
+ return (content_dict, ());
880
+ }
881
+
882
+ ;; On-chain content layout The first byte is 0x00 and the rest is key/value dictionary.
883
+ cell create_token_onchain_metadata(cell content_dict) inline {
884
+ return begin_cell().store_uint(0, 8).store_dict(content_dict).end_cell();
885
+ }