Skip to content

Commit 303e92b

Browse files
authoredMar 19, 2025
Merge pull request #1559 from ton-blockchain/tolk-v0.10
Tolk v0.10: preparing for serialization; types `intN`, `bytesN`, `coins`
2 parents 020dbc8 + 6ba2355 commit 303e92b

File tree

244 files changed

+2123
-864
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

244 files changed

+2123
-864
lines changed
 

‎crypto/smartcont/tolk-stdlib/common.tolk

+63-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Standard library for Tolk (LGPL licence).
22
// It contains common functions that are available out of the box, the user doesn't have to import anything.
33
// More specific functions are required to be imported explicitly, like "@stdlib/tvm-dicts".
4-
tolk 0.9
4+
tolk 0.10
55

66
/**
77
Tuple manipulation primitives.
@@ -54,6 +54,13 @@ fun tupleLast<T>(self: tuple): T
5454
Mathematical primitives.
5555
*/
5656

57+
/// Converts a constant floating-point string to nanotoncoins.
58+
/// Example: `ton("0.05")` is equal to 50000000.
59+
/// Note, that `ton()` requires a constant string; `ton(some_var)` is an error
60+
@pure
61+
fun ton(floatString: slice): coins
62+
builtin;
63+
5764
/// Computes the minimum of two integers.
5865
@pure
5966
fun min(x: int, y: int): int
@@ -187,6 +194,42 @@ fun commitContractDataAndActions(): void
187194
Signature checks, hashing, cryptography.
188195
*/
189196

197+
/// Compile-time function that calculates crc32 of a constant string.
198+
/// Example: `const op = stringCrc32("some_str")` = 4013618352 = 0xEF3AF4B0
199+
/// Note: stringCrc32(slice_var) does not work! It accepts a constant string and works at compile-time.
200+
@pure
201+
fun stringCrc32(constString: slice): int
202+
builtin;
203+
204+
/// Compile-time function that calculates crc16 (XMODEM) of a constant string.
205+
/// Example: `const op = stringCrc16("some_str")` = 53407 = 0xD09F
206+
/// Note: stringCrc16(slice_var) does not work! It accepts a constant string and works at compile-time.
207+
@pure
208+
fun stringCrc16(constString: slice): int
209+
builtin;
210+
211+
/// Compile-time function that calculates sha256 of a constant string and returns 256-bit integer.
212+
/// Example: `const hash = stringSha256("some_crypto_key")`
213+
/// Note: it's a compile-time function, `stringSha256(slice_var)` does not work.
214+
/// Use `sliceBitsHash` or `sliceHash` (declared below) to hash a slice without/with its refs at runtime.
215+
@pure
216+
fun stringSha256(constString: slice): int
217+
builtin;
218+
219+
/// Compile-time function that calculates sha256 of a constant string and takes the first 32 bits.
220+
/// Example: `const minihash = stringSha256_32("some_crypto_key")`
221+
/// Note: stringSha256_32(slice_var) does not work! It accepts a constant string and works at compile-time.
222+
@pure
223+
fun stringSha256_32(constString: slice): int
224+
builtin;
225+
226+
/// Compile-time function that takes N-chars ascii string and interprets it as a number in base 256.
227+
/// Example: `const value = stringToBase256("AB")` = 16706 (65*256 + 66)
228+
/// Note: stringToBase256(slice_var) does not work! It accepts a constant string and works at compile-time.
229+
@pure
230+
fun stringToBase256(constString: slice): int
231+
builtin;
232+
190233
/// Computes the representation hash of a `cell` [c] and returns it as a 256-bit unsigned integer `x`.
191234
/// Useful for signing and checking signatures of arbitrary entities represented by a tree of cells.
192235
@pure
@@ -203,7 +246,7 @@ fun sliceHash(s: slice): int
203246
/// Computes sha256 of the data bits of `slice` [s]. If the bit length of `s` is not divisible by eight,
204247
/// throws a cell underflow exception. The hash value is returned as a 256-bit unsigned integer `x`.
205248
@pure
206-
fun stringHash(s: slice): int
249+
fun sliceBitsHash(s: slice): int
207250
asm "SHA256U";
208251

209252
/// Checks the Ed25519-`signature` of a `hash` (a 256-bit unsigned integer, usually computed as the hash of some data)
@@ -333,6 +376,22 @@ fun debugDumpStack(): void
333376
When you _preload_ some data, you just get the result without mutating the slice.
334377
*/
335378

379+
/// Compile-time function that converts a constant string to TL-encoded MsgAddressInt (TVM slice).
380+
/// Example: stringAddressToSlice("EQCRDM9h4k3UJdOePPuyX40mCgA4vxge5Dc5vjBR8djbEKC5")
381+
/// Example: stringAddressToSlice("0:527964d55cfa6eb731f4bfc07e9d025098097ef8505519e853986279bd8400d8")
382+
/// Note: it's a compile-time function, `stringAddressToSlice(slice_var)` does not work.
383+
/// Use `parseStandardAddress` to decode a slice at runtime into workchain and hash.
384+
@pure
385+
fun stringAddressToSlice(constStringAddress: slice): slice
386+
builtin;
387+
388+
/// Compile-time function that converts a constant hex-encoded string to N/2 bytes.
389+
/// Example: `const v = stringHexToSlice("abcdef")` = slice with 3 bytes `[ 0xAB, 0xCD, 0xEF ]`
390+
/// Note: stringHexToSlice(slice_var) does not work! It accepts a constant string and works at compile-time.
391+
@pure
392+
fun stringHexToSlice(constStringBytesHex: slice): slice
393+
builtin;
394+
336395
/// Converts a `cell` [c] into a `slice`. Notice that [c] must be either an ordinary cell,
337396
/// or an exotic cell (see [TVM.pdf](https://ton-blockchain.github.io/docs/tvm.pdf), 3.1.2)
338397
/// which is automatically loaded to yield an ordinary cell `c'`, converted into a `slice` afterwards.
@@ -386,7 +445,7 @@ fun preloadBits(self: slice, len: int): slice
386445

387446
/// Loads serialized amount of Toncoins (any unsigned integer up to `2^120 - 1`).
388447
@pure
389-
fun loadCoins(mutate self: slice): int
448+
fun loadCoins(mutate self: slice): coins
390449
asm( -> 1 0) "LDGRAMS";
391450

392451
/// Loads bool (-1 or 0) from a slice
@@ -485,7 +544,7 @@ fun storeSlice(mutate self: builder, s: slice): self
485544

486545
/// Stores amount of Toncoins into a builder.
487546
@pure
488-
fun storeCoins(mutate self: builder, x: int): self
547+
fun storeCoins(mutate self: builder, x: coins): self
489548
asm "STGRAMS";
490549

491550
/// Stores bool (-1 or 0) into a builder.

‎crypto/smartcont/tolk-stdlib/gas-payments.tolk

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// A part of standard library for Tolk
2-
tolk 0.9
2+
tolk 0.10
33

44
/**
55
Gas and payment related primitives.

0 commit comments

Comments
 (0)