Skip to content

Commit 0e83352

Browse files
authored
Merge branch 'main' into smart-contract-review
2 parents dcd682c + 649e3e6 commit 0e83352

File tree

15 files changed

+350
-41
lines changed

15 files changed

+350
-41
lines changed

docs.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@
255255
"standard/wallets/how-it-works",
256256
"standard/wallets/mnemonics",
257257
"standard/wallets/comparison",
258+
"standard/wallets/performance",
258259
"standard/wallets/interact",
259260
"standard/wallets/history",
260261
"standard/wallets/v4",
@@ -512,6 +513,7 @@
512513
"tvm/gas",
513514
"tvm/initialization",
514515
"tvm/exit-codes",
516+
"tvm/get-method",
515517
"tvm/changelog"
516518
]
517519
},

extra.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ thead>tr>th {
6565
html.dark thead>tr>th {
6666
background: rgb(var(--background-dark));
6767
}
68+
69+
/* Tabular numbers for better alignment in tables */
70+
table {
71+
font-variant-numeric: tabular-nums;
72+
}

foundations/actions/send.mdx

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,57 @@
22
title: "Send message"
33
---
44

5-
import { Stub } from '/snippets/stub.jsx';
5+
Sending a message is the most frequent action that a smart contract performs during the [action phase](/foundations/phases#action-phase). It can get into the [out action list](https://github.com/ton-blockchain/ton/blob/5c0349110bb03dd3a241689f2ab334ae1a554ffb/crypto/block/block.tlb#L411) by:
66

7-
<Stub
8-
issue="1077"
9-
/>
7+
- The [`SENDRAWMSG`](/tvm/instructions#fb00-sendrawms) instruction.
8+
- The [`SENDMSG`](/tvm/instructions#fb08-sendmsg) instruction.
9+
- The [`POPCTR`](/tvm/instructions#ed5-popctr) instruction.
10+
11+
The sending message action consists of:
12+
13+
- an 8-bit bitmask [`mode`](/foundations/messages/modes) specifying the way of sending;
14+
- a cell containing the message to send, which is serialized as `MessageRelaxed` type.
15+
16+
## Message normalization
17+
18+
During the process of sending, the original message is normalized. In the final message that is sent to the destination address, the following changes are applied to the fields of the [`CommonMsgInfoRelaxed`](https://github.com/ton-blockchain/ton/blob/5c0349110bb03dd3a241689f2ab334ae1a554ffb/crypto/block/block.tlb#L135-L140) type:
19+
20+
- `ihr_disabled` is set to `true`;
21+
- `bounced` is set to `false`;
22+
- `fwd_fee` is set to actual forward fee dedicated to the validators of the destination address shard;
23+
- `src` address is set to address of the sender smart contract;
24+
- `created_at` is set to the current Unix timestamp;
25+
- `created_lt` is set to the logical time of that action.
26+
27+
As a result, when composing a message cell, it is acceptable to:
28+
29+
- fill the `ihr_disabled`, `bounced`, `fwd_fee`, `created_at`, and `created_lt` fields with any values;
30+
- fill the `src` field with [`addr_none`](/foundations/addresses/overview#external-addresses).
31+
32+
If the message cell does not fit into the maximum allowed size (1023 bits) after the normalization, the process is repeated with different ways to pack it:
33+
34+
- with `init` packed in a separate cell, referred from the root cell;
35+
- with `init` and `body` packed in separate cells.
36+
37+
If after the second attempt the message is still too large, the exception is thrown in the action phase. This can lead to rejecting the whole action phase, sending a bounce message, or skipping this action. The exact behavior depends on the `mode` bitmask of the action.
38+
39+
## Serialization
40+
41+
```tlb
42+
action_send_msg#0ec3c86d mode:(## 8)
43+
out_msg:^(MessageRelaxed Any) = OutAction;
44+
45+
out_list_node$_ prev:^Cell action:OutAction = OutListNode;
46+
47+
message$_ {X:Type} info:CommonMsgInfoRelaxed
48+
init:(Maybe (Either StateInit ^StateInit))
49+
body:(Either X ^X) = MessageRelaxed X;
50+
51+
int_msg_info$0 ihr_disabled:Bool bounce:Bool bounced:Bool
52+
src:MsgAddress dest:MsgAddressInt
53+
value:CurrencyCollection extra_flags:(VarUInteger 16) fwd_fee:Grams
54+
created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
55+
56+
ext_out_msg_info$11 src:MsgAddress dest:MsgAddressExt
57+
created_lt:uint64 created_at:uint32 = CommonMsgInfoRelaxed;
58+
```

foundations/whitepapers/tblkch.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ where $[A]$ equals one when condition $A$ is true, and zero otherwise.
6161

6262
- Finally, $r$ references to other cells follow. Each reference is normally represented by 32 bytes containing the $\text{Sha256}$ hash of the referenced cell, computed as explained below in [1.1.4](#1-1-4-the-sha256-hash-of-a-cell).
6363

64-
In this way, the standard representation $\text{CellRepr}(c)$ of a cell $c$ with $l$ data bits and $r$ references is $2+\lfloor l/8\rfloor+\lceil l/8\rceil+32r$ bytes long.
64+
In this way, the standard representation $\text{CellRepr}(c)$ of a cell $c$ with $l$ data bits and $r$ references is $2+\lceil l/8\rceil+32r$ bytes long.
6565

6666
### 1.1.4. The Sha256 hash of a cell
6767

from-ethereum.mdx

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,90 +3,92 @@ title: "Coming from Ethereum"
33
---
44

55
import { Aside } from "/snippets/aside.jsx";
6+
import { Image } from '/snippets/image.jsx';
67

78
Learn how to develop and build on TON coming from the Ethereum (EVM) ecosystem.
89

910
## Execution model
1011

1112
### Asynchronous blockchain
1213

13-
One of the biggest stepping stones to learn TON development is the asynchronous execution model. Messages sent by one contract take time to arrive at another, meaning that the resulting transactions for processing incoming messages will occur after the current transaction terminates.
14+
A fundamental aspect of TON development is the asynchronous execution model. Messages sent by one contract take time to arrive at another, so the resulting transactions for processing incoming messages occur after the current transaction terminates.
1415

15-
So, compared to Ethereum, where you can have multiple processed messages and state changes on different contracts in the same atomic transaction, a TON transaction represents a state change only for one account and single message processing. That means that a signed, included-in-block unit is called a "transaction" in both chains; however, having the differences in execution models, it has a different impact.
16-
17-
Here is a table for comparison:
16+
Compared to Ethereum, where multiple messages and state changes on different contracts can be processed within the same atomic transaction, a TON transaction represents a state change only for one account and only for a processing of a single message. Even though in both blockchains a signed included-in-block unit is called a "transaction", one transaction on Ethereum usually corresponds to several transactions on TON, that are processed over a span of several blocks.
1817

1918
| Action description | Ethereum | TON |
2019
| :--------------------------------------------------------------------------------------------- | -------------------------------------- | -------------------------------- |
2120
| Single message processing with state change on one contract | Message call or "internal transaction" | Transaction |
2221
| Number of state changes and messages on different accounts produced from initial contract call | Transaction | Chain of transactions or "trace" |
2322

24-
Let's explore a practical example, liquidity withdrawal on DEX.
23+
Consider a practical example: liquidity withdrawal on a DEX.
2524

26-
On Ethereum, it will appear as a single atomic transaction with multiple contract calls inside it. You can see that this transaction has a single hash and is included in one block:
25+
- On Ethereum, it appears as a single atomic transaction with multiple contract calls inside it. This transaction has a single hash and is included in one block.
2726

28-
![ETH burn](/resources/images/from-ethereum/burn-tx-eth.png)
27+
<Image
28+
src="/resources/images/from-ethereum/burn-tx-eth-light.png"
29+
darkSrc="/resources/images/from-ethereum/burn-tx-eth-dark.png"
30+
alt="ETH burn"
31+
/>
2932

30-
The same operation on TON will differ, consisting of a sequence of more than 10 transactions. Each arrow on this image represents a distinct finalized transaction, with its own hash, inclusion block, and all the other properties:
33+
- The same operation on TON consists of a sequence of more than 10 transactions. Each arrow on this image represents a distinct finalized transaction, with its own hash, inclusion block, and all the other properties:
3134

32-
![TON burn](/resources/images/from-ethereum/burn-tx-ton.png)
35+
<Image
36+
src="/resources/images/from-ethereum/burn-tx-ton-light.png"
37+
darkSrc="/resources/images/from-ethereum/burn-tx-ton-dark.png"
38+
alt="TON burn"
39+
/>
3340

34-
If you want to execute a _huge_ transaction on Ethereum (or any other EVM-based blockchain), you will have certain limitations: [EVM call-depth](https://ethereum.org/developers/docs/evm/#evm-instructions) of 1024 nested calls and [block gas limit](https://ethereum.org/developers/docs/blocks/#block-size). With TON's asynchronous execution model, you can have a trace (chain of transactions) of whatever length you want, as long as you have fees to continue it. For example, [trace that resulted from this message](https://tonviewer.com/transaction/e887503f7dac857be80487e3ed0774db962379d1c153e6df7b9b5313c657ab94) consisted of more than 1.5+ million transactions, lasting more than 4000 blocks until its end!
41+
Executing a large transaction on Ethereum or any other EVM-based blockchain comes with certain limitations: [call depth](https://ethereum.org/developers/docs/evm/#evm-instructions) of 1,024 nested calls and the [block gas limit](https://ethereum.org/developers/docs/blocks/#block-size). With TON's asynchronous execution model, a trace — a chain of transactions — can have any length, as long as there are enough fees to continue it. For example, the [trace](https://tonviewer.com/transaction/e887503f7dac857be80487e3ed0774db962379d1c153e6df7b9b5313c657ab94) resulting from this message consisted of more than 1.5 million transactions, lasting more than 4,000 blocks until completion.
3542

3643
### On-chain get methods
3744

38-
Another radical difference between the two chains is that TON has get methods, which allow data to be retrieved from the contracts without paying any fees. In TON, you _can't_ synchronously retrieve data from another contract - you can't call a get method from another contract during the transaction.
45+
Another difference is in the [get methods](/tvm/get-method). Both Ethereum and TON support them, allowing data to be retrieved from contracts without paying fees. However, in TON, get methods cannot be called on-chain: a contract cannot synchronously retrieve data from another contract during a transaction. This is a consequence of TON's asynchronous model: by the moment transaction that called a get method would start its execution, data might already change.
3946

40-
If you wonder how we can make any DeFi protocol or complicated on-chain system work with these limitations, read an article about the on-chain [Request-Response pattern](/contract-dev/carry-value).
47+
To understand how any DeFi protocol or complex on-chain system works with these limitations, read about the [carry-value pattern](/contract-dev/carry-value).
4148

4249
### Account model
4350

44-
There are two types of accounts in Ethereum: externally owned accounts (EOA in short) and contract accounts. EOAs are human-controlled entities, each represented by a private-public key pair. They are used to sign transactions and each has its own balance; they are commonly called "wallets" by the community.
51+
In Ethereum, there are two types of accounts: externally owned accounts (EOA), and contract accounts. EOAs are human-controlled entities, each represented by a private-public key pair. They sign transactions and each has its own balance; the community often refers to them as "wallets".
4552

46-
In TON, there is no such separation; every valid address represents an on-chain account, each with its own state and balance, that could be changed through transactions (read more about accounts [here](/foundations/addresses/overview)). This means that "wallets" in TON are smart contracts that operate under the same rules as any other contract on the blockchain.
53+
In TON, there is no such separation. Every valid address represents an on-chain [account](/foundations/addresses/overview), each with its own state and balance, that could be changed through transactions. This means that "wallets" in TON are smart contracts that operate under the same rules as any other contract on the blockchain.
4754

48-
The TON wallet smart contract uses regular asymmetric cryptography to control message signing, ensuring that the user experience remains essentially unchanged. You can examine [Wallet Standard](/standard/wallets/comparison) implementation code and how it changed through ecosystem development.
55+
The [TON wallet](/standard/wallets/comparison) smart contract works as a proxy: handles an external message, checks message is sent by the wallet's owner using regular [public-key cryptography](https://en.wikipedia.org/wiki/Public-key_cryptography), and sends an internal message somewhere further in the network.
4956

5057
### Limited contract storage
5158

52-
In Ethereum, you can store as much data as you want in a single contract. Unbounded maps and arrays are considered a standard practice, and you will probably see them in most of the contract examples. This is not the case with TON - every smart contract on TON blockchain has a storage size upper limit. That means that you can't implement ERC20-like fungible tokens in the same way as in an EVM chain, by using a single map inside one contract.
59+
In Ethereum, it's possible to store any amount of data in a single contract. Unbounded maps and arrays are considered standard practice. TON sets a limit to the amount of data a contract can store. This means that ERC-20-like fungible tokens cannot be implemented in the same way as in an EVM chain, using a single map within a single contract.
5360

54-
[The limit](/foundations/config) is 65536 unique cells per message or contract storage. Each cell is up to 1,023 bits, which sets a hard upper limit.
61+
[The limit](/foundations/config) for contract storage is 65,536 unique cells contract storage, where a cell [stores up to](/foundations/serialization/cells) 1,023 bits. Messages are constrained by two size limits: 8,192 cells or 2<sup>21</sup> bits among them, whichever is smaller.
5562

5663
<Aside
5764
type="danger"
5865
>
59-
Every map that is expected to grow more than **1000** values is dangerous! Note that in the TVM map, key access is asymptotically logarithmic, meaning that it will continuously cost you more gas to find keys as the map grows.
66+
Every map that is expected to grow beyond 1,000 values is dangerous. In the TVM map, key access is asymptotically logarithmic, meaning that gas consumption continuously increases to find keys as the map grows.
6067
</Aside>
6168

62-
Instead, you should use [sharding](/foundations/shards).
63-
64-
You can read more about TON architecture design choices that have led to such differences [here](/standard/overview).
69+
Instead, [sharding](/contract-dev/contract-sharding) should be used.
6570

6671
## Ecosystem
6772

6873
### Tooling
6974

70-
<Aside>
71-
This section will primarily focus on Typescript tooling since historically this language has been better adopted and well-established in TON.
72-
</Aside>
73-
74-
Recommended programming language for smart contract development in TON is [Tolk](/languages/tolk). However, there are other established languages in the ecosystem; you can read more about them [here](/languages/tact).
75+
The recommended programming language for smart contract development in TON is [Tolk](/languages/tolk). Other established languages are also used in the ecosystem; more information about them is available [here](/languages/tact).
7576

76-
Here is the ecosystem overview table.
77+
For off-chain software, Typescript is the most adopted language in TON. Most of the [tooling](/contract-dev/blueprint/overview), bindings and [SDKs](/ecosystem/sdks) are implemented in Typescript.
7778

78-
| Use case | Popular Ethereum tool | TON counterpart |
79+
| Use case | Ethereum tool | TON counterpart |
7980
| :------------------------------------ | --------------------- | -------------------------------------------------------------------------------------------------------------- |
8081
| Blockchain interaction | Ethers, Web3.js, Viem | [`@ton/ton`](https://www.npmjs.com/package/@ton/ton), [Asset-sdk](https://github.com/ton-community/assets-sdk) |
8182
| Wallet connection protocol | Walletconnect, Wagmi | [TonConnect](https://github.com/ton-connect) |
8283
| Dev environment framework / scripting | Hardhat, Truffle | [Blueprint](https://github.com/ton-org/blueprint) |
8384
| Simulation engine | Revm & Reth | [Sandbox](https://github.com/ton-org/sandbox) |
8485

85-
Another essential library to know about is [`@ton/core`](https://www.npmjs.com/package/@ton/core). This library handles low-level blockchain primitives (de-)serialization; you will probably need it together with any RPC-client or contract interaction library.
86+
For low-level manipulation of TON-specific data structures, there is [`@ton/core`](https://www.npmjs.com/package/@ton/core). Another library with wrappers for most important contracts and HTTP APIs is [`@ton/ton`](https://www.npmjs.com/package/@ton/ton).
8687

8788
### Services
8889

89-
Most web3 developers also have their favorite set of products and services for easier on-chain development. This table showcases some of the use cases that existing TON services can cover.
90+
Web3 developers often rely on specific products and services for on-chain development.
91+
The following table showcases some use cases that existing TON services support.
9092

9193
| Use case | Ethereum service | TON service |
9294
| --------------------------------------- | ---------------- | -------------------------------------------------------------------- |
@@ -96,15 +98,11 @@ Most web3 developers also have their favorite set of products and services for e
9698
| IDE | Remix IDE | [Web IDE](https://ide.ton.org/) |
9799
| Asm playground and compilation explorer | EVM.Codes | [TxTracer](https://txtracer.ton.org/) |
98100

99-
If you are looking for commercial RPC and node providers, check our [Providers overview](/ecosystem/api/overview) section.
100-
101101
### Standards
102102

103-
This section showcases a match between some of the Ethereum standards and proposals (ERC and EIP) and their counterpart or similar proposals in TON (TEP).
103+
The table maps Ethereum standards and proposals, including ERC and EIP, to their TON counterparts, referred to as TEP.
104104

105-
<Aside>
106-
Due to significant differences in execution models, most of the standards in TON differ significantly in semantics and general approach compared to their Ethereum analogs.
107-
</Aside>
105+
Due to significant differences in execution models, most of the standards in TON differ significantly in semantics and general approach compared to their Ethereum analogs.
108106

109107
| Description | Ethereum standard | TON Standard (TEP) |
110108
| --------------------------------------- | --------------------------------------- | ------------------------------------------------------------------ |
464 KB
Loading
463 KB
Loading
-487 KB
Binary file not shown.
359 KB
Loading
348 KB
Loading

0 commit comments

Comments
 (0)