The Nautical Chart of TON Smart Contracts: In-Depth Analysis and Function Interpretation stdlib_ext

  • King
  • 更新于 2024-10-19 11:48
  • 阅读 259

Before delving into the stdlib_ext library for TON smart contracts, it is essential to understand the unique charm of TON, a decentralized blockchain

Before delving into the stdlib_ext library for TON smart contracts, it is essential to understand the unique charm of TON, a decentralized blockchain platform. TON resembles a vast digital ocean, where smart contracts are the vessels navigating its waters. The stdlib_ext library serves as their compass and map, guiding them safely through the turbulent digital world.

{-
    stdlib_ext.fc

    Additional functions that are not present in stdlib.fc
-}

(int) mod(int x, int y) asm "MOD";
(int) divc(int x, int y) asm "DIVC";
slice preload_bits_offset(slice s, int offset, int len) asm "SDSUBSTR";
int builder_null?(builder b) asm "ISNULL";
(slice, slice) load_slice_ref(slice s) asm "LDREFRTOS";
forall X -> int to_int(X x) asm "NOP";
(slice, int) load_bool(slice s) asm(-> 1 0) "1 LDI";
(builder) store_bool(builder b, int v) asm(v b) "1 STI";
slice addr_none() asm "<b b{00} s, b> <s PUSHSLICE";
cell empty_cell() asm "<b b> PUSHREF";
slice empty_slice() asm "<b b> <s PUSHSLICE";
forall X -> tuple tset(tuple t, int k, X x) asm(t x k) "SETINDEXVAR";
forall X -> (tuple, ()) ~tset(tuple t, int k, X x) asm(t x k) "SETINDEXVAR";
forall X -> (tuple, X) tpop(tuple t) asm "TPOP";
int tlen(tuple t) asm "TLEN";
int tlenq(tuple t) asm "QTLEN";
slice preload_slice_offset(slice s, int offset, int len) asm "SDSUBSTR";
(slice, slice) load_slice(slice s, int len) asm( -> 1 0) "LDSLICEX";
forall X -> tuple _unsafe_tuple(X x) asm "NOP";
(slice) as_slice(builder b) asm "ENDC CTOS";
(slice, int) load_time(slice s) asm "64 LDU SWAP";
(builder) store_time(builder b, int t) inline asm(t b) "64 STU";
(slice, int) load_uint8(slice s) asm "8 LDU SWAP";
(builder) store_uint8(builder b, int t) inline asm(t b) "8 STU";
(slice, int) load_uint16(slice s) asm "16 LDU SWAP";
(builder) store_uint16(builder b, int t) inline asm(t b) "16 STU";
(slice, int) load_uint32(slice s) asm "32 LDU SWAP";
(builder) store_uint32(builder b, int t) inline asm(t b) "32 STU";
(slice, int) load_uint64(slice s) asm "64 LDU SWAP";
(builder) store_uint64(builder b, int t) inline asm(t b) "64 STU";
(slice, int) load_uint128(slice s) asm "128 LDU SWAP";
(builder) store_uint128(builder b, int t) inline asm(t b) "128 STU";
(slice, int) load_uint256(slice s) asm "256 LDU SWAP";
(builder) store_uint256(builder b, int t) inline asm(t b) "256 STU";
(int) equal_cells(cell a, cell b) asm "HASHCU SWAP HASHCU EQUAL"; 
(slice, int) load_opcode(slice s) asm "32 LDU SWAP";
(builder) store_opcode(builder b, int t) inline asm(t b) "32 STU";
(slice, int) load_query_id(slice s) asm "64 LDU SWAP";
(builder) store_query_id(builder b, int t) inline asm(t b) "64 STU";
forall X -> X ~strdump_safe_not_pretty(slice value) impure asm "STRDUMP";
forall X -> X ~dump_safe(X value) impure asm "s0 DUMP";
(builder) begin_message(int _opcode) asm "ctx GETGLOB 4 INDEX SWAP NEWC 32 STU 64 STU";
(slice, int) load_status(slice s) asm "8 LDU SWAP";
(builder) store_status(builder b, int t) inline asm(t b) "8 STU";
(int) negate(int _n) asm "NEGATE";
(int) sub1(int _n) asm "DEC";
(int) add1(int _n) asm "INC";
(int) sgn(int _n) asm "SGN";
(int, ()) ~sub1(int _n) asm "DEC";
(int, ()) ~add1(int _n) asm "INC";
(int, ()) ~negate(int _n) asm "NEGATE";
(int, ()) ~sgn(int _n) asm "SGN";

() set_lib(cell code, int mode) impure asm "SETLIBCODE";
() change_lib(int code_hash, int mode) impure asm "CHANGELIB";
forall X -> int   is_null(X x) asm "ISNULL";
forall X -> int   is_int(X x) asm "<{ TRY:<{ 0 PUSHINT ADD DROP -1 PUSHINT }>CATCH<{ 2DROP 0 PUSHINT }> }>CONT 1 1 CALLXARGS";
forall X -> int   is_cell(X x) asm "<{ TRY:<{ CTOS DROP -1 PUSHINT }>CATCH<{ 2DROP 0 PUSHINT }> }>CONT 1 1 CALLXARGS";
forall X -> int   is_slice(X x) asm "<{ TRY:<{ SBITS DROP -1 PUSHINT }>CATCH<{ 2DROP 0 PUSHINT }> }>CONT 1 1 CALLXARGS";
forall X -> int   is_tuple(X x) asm "ISTUPLE";
int               is_nan(int _n) asm "CHKNAN";
forall X -> cell  cast_to_cell(X x) asm "NOP";
forall X -> slice cast_to_slice(X x) asm "NOP";
forall X -> int   cast_to_int(X x) asm "NOP";
forall X -> tuple cast_to_tuple(X x) asm "NOP";

;; TVM Upgrade 2023.07
(cell) my_code() asm "MYCODE";
(tuple) get_values() asm "INCOMINGVALUE";
int storage_fees() asm "STORAGEFEES";
(tuple) get_prev_block_info() asm "PREVBLOCKSINFOTUPLE";
(tuple) get_last_mc_block() asm "PREVMCBLOCKS";
(tuple) get_prev_key_block() asm "PREVKEYBLOCK";
(int) get_global_id() asm "GLOBALID";
(int) get_gas_consumed() asm "GASCONSUMED";
(int) sha256_single(slice _t0) asm "1 PUSHINT HASHEXT_SHA256";
(int) sha256_double(slice _t0, slice _t1) asm "2 PUSHINT HASHEXT_SHA256";
(int) sha256_triple(slice _t0, slice _t1, slice _t2) asm "3 PUSHINT HASHEXT_SHA256";
(int) sha512_single(slice _t0) asm "1 PUSHINT HASHEXT_SHA512";
(int) sha512_double(slice _t0, slice _t1) asm "2 PUSHINT HASHEXT_SHA512";
(int) sha512_triple(slice _t0, slice _t1, slice _t2) asm "3 PUSHINT HASHEXT_SHA512";
(int) blake2b_single(slice _t0) asm "1 PUSHINT HASHEXT_BLAKE2B";
(int) blake2b_double(slice _t0, slice _t1) asm "2 PUSHINT HASHEXT_BLAKE2B";
(int) blake2b_triple(slice _t0, slice _t1, slice _t2) asm "3 PUSHINT HASHEXT_BLAKE2B";
(int) keccack256_single(slice _t0) asm "1 PUSHINT HASHEXT_KECCAK256";
(int) keccack256_double(slice _t0, slice _t1) asm "2 PUSHINT HASHEXT_KECCAK256";
(int) keccack256_triple(slice _t0, slice _t1, slice _t2) asm "3 PUSHINT HASHEXT_KECCAK256";
(int) keccack512_single(slice _t0) asm "1 PUSHINT HASHEXT_KECCAK512";
(int) keccack512_double(slice _t0, slice _t1) asm "2 PUSHINT HASHEXT_KECCAK512";
(int) keccack512_triple(slice _t0, slice _t1, slice _t2) asm "3 PUSHINT HASHEXT_KECCAK512";
(int, int, int, int) ecrecover(int hash, int v, int r, int s) asm "ECRECOVER NULLSWAPIFNOT NULLSWAPIFNOT2";
int send_message(cell msg, int mode) impure asm "SENDMSG";

int get_compute_fee(int workchain, int gas_used) asm(gas_used workchain) "GETGASFEE";
int get_storage_fee(int workchain, int seconds, int bits, int cells) asm(cells bits seconds workchain) "GETSTORAGEFEE";
int get_forward_fee(int workchain, int bits, int cells) asm(cells bits workchain) "GETFORWARDFEE";
int get_precompiled_gas_consumption() asm "GETPRECOMPILEDGAS";
int get_simple_compute_fee(int workchain, int gas_used) asm(gas_used workchain) "GETGASFEESIMPLE";
int get_simple_forward_fee(int workchain, int bits, int cells) asm(cells bits workchain) "GETFORWARDFEESIMPLE";
int get_original_fwd_fee(int workchain, int fwd_fee) asm(fwd_fee workchain) "GETORIGINALFWDFEE";

int my_storage_due() asm "DUEPAYMENT";
tuple get_fee_cofigs() asm "UNPACKEDCONFIGTUPLE";

(slice, ()) ~skip_maybe_ref(slice s) asm "SKIPOPTREF";

int cell_level(cell c) asm "CLEVEL";
int cell_level_mask(cell c) asm "CLEVELMASK";

builder store_addr_none(builder b) asm "b{00} STSLICECONST";
int is_bool(int x) asm "CHKBOOL";

builder store_zeroes(builder b, int x) asm "STZEROES";
builder store_ones(builder b, int x) asm "STONES";

cell preload_first_ref(slice s) asm "0 PLDREFIDX";
cell preload_second_ref(slice s) asm "1 PLDREFIDX";
cell preload_third_ref(slice s) asm "2 PLDREFIDX";
cell preload_fourth_ref(slice s) asm "3 PLDREFIDX";

int get_ton_balance() asm "BALANCE FIRST";

((int, int) -> ()) get_c2() asm "c2 PUSH";
() set_c2(((int, int) -> ()) c) impure asm "c2 POP";
() set_actions(cell actions) impure asm "c5 POP";
() clean_actions() impure asm "<b b> PUSHREF c5 POP";

Mathematical Operations and Logical Functions: The Cornerstones of Wisdom

In the sea of contract development, mathematical operations and logical functions form the solid foundation that supports the entire structure. The modulus operation searches for the islands of remainders in the numerical ocean; the division operation divides the numerical realm with precision. Operations like negation, decrement, and increment are like the sailors on board, skillfully maneuvering the sails of numbers. The sign and NaN checking functions are the keen observers, constantly vigilant to ensure that the contract’s logic stays on course.

Slice and Builder Operations: The Art of Data

In the world of TON, data is the colorful coral reef, and slice and builder operations are the agile divers exploring the depths of the data underworld. Slice loading is like the diver’s oxygen tank, enabling contracts to delve deep into the data. Builder checking and conversion are the discoveries they bring from the depths, transforming the secrets of builders into the miracles of slices.

Tuple Operations: The Power of Composition

Tuples, as composite data types in TON smart contracts, are like sturdy vessels woven from diverse materials. Operations such as setting tuple elements, popping tuple elements, and getting tuple length are like the craftsmen on board, meticulously crafting and maintaining this ship of data, ensuring it sails stably through the digital ocean.

Type Conversion: The Wisdom of Adaptability

In the journey of contract development, type conversion is the versatile tool on deck, capable of adapting to various environments. Converting to integers, type conversions, these functions are like experienced captains who always find the best route, leading contracts through the complex seas of type conversion.

Boolean Operations: The Lighthouse of Logic

Boolean operations are like the lighthouse guiding ships to safe passage. Loading and storing booleans, checking booleans, these operations ensure that the logical ship of the contract does not lose its way in the ever-changing digital ocean.

Time and State Operations: The Helmsman of Time

Time and state operations are like the helmsman steering the ship of time. Loading and storing timestamps, loading and storing status codes, these operations allow contracts to precisely grasp time and adjust states, ensuring each voyage reaches its destination on time.

Hashing and Signature Operations: The Guardians of Security

Hashing and signature operations are the armor protecting the security of contracts. Comparing cells, signature recovery, these operations ensure that every transaction in the digital ocean is safe, guarding against potential pirates and thieves.

Message and Library Operations: The Bridges of Communication

Message and library operations are like the bridges connecting different ships. Starting new messages, setting and changing library codes, these operations facilitate smooth communication between contracts, ensuring the协同运作 of the entire TON network.

Fee and Storage Operations: The Stewards of Resources

Fee and storage operations are like the stewards managing the ship’s resources. Calculating fees, getting storage fees and configurations, these operations ensure that each voyage in the digital ocean efficiently utilizes resources, preventing waste.

Other Operations: The Multifaceted Tools

Other operations in the stdlib_ext library are like the multifaceted tools, whether it’s type checking, reference and cell operations, or storing specific values or patterns, they are powerful assistants for contracts exploring and adventuring in the digital ocean.

Conclusion: The Navigator’s Guide

The stdlib_ext library, as the compass and map for TON smart contracts, provides developers with a powerful toolkit, enabling contracts to perform more complex and efficient operations. Through these extended functions, developers can easily handle mathematical operations, data slicing, tuple operations, type conversions, message sending, fee calculations, and more, greatly enhancing the functionality and flexibility of contracts. For developers looking to build advanced applications on the TON platform, the stdlib_ext library is an invaluable resource. By gaining a deep understanding and making good use of these extended functions, developers can create richer and more powerful smart contract applications.

点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
King
King
0x56af...a0dd
擅长Rust/Solidity/FunC/Move开发