Sol2TVM Compiler

**Solidity compiler for Free TON** – a program which translates a text, written in the Solidity programming language, into TVM bytecode for TON blockchain.

## Installation

### Ubuntu Linux

```
git clone git@github.com:tonlabs/TON-Solidity-Compiler.git
cd TON-Solidity-Compiler/compiler
sh ./scripts/install_deps.sh
mkdir build
cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
cmake --build . -- -j8

```

Installation takes about 10 minutes.

### Windows 10

Install Visual Studio, Git bash, cmake. Run Visual Studio Developer Command Prompt:

```
git clone https://github.com/tonlabs/TON-Solidity-Compiler
cd TON-Solidity-Compiler\compiler
cmake -P scripts\install_deps.cmake
mkdir build
cd build
cmake ..
cmake --build . --config Release -j 8

```

## Compile

### Compile a smart contract using the solc utility

```
solc MyContract.sol
```

As a result, we get a tvm. file (a compiled code in assembler) and a .abi.json file (interface for accessing contract functions)

Then, using the tvm\_linker utility, we convert it to bytecode:

```
tvm_linker compile MyContract.code --lib <path to>/stdlib_sol.tvm --abi-json MyContract.abi.json
```

Binary code of the contract is recorded into <MyContractAddress> .tvc file, where <MyContractAddress> is the address of the contract. The address is a hash of the initial data and contract code. The contract is ready for deploy

**Note:** you can test your contracts locally without even running a node:

```
tvm_linker test <MyContractAddress> --abi-json <MyContract.abi.json> --abi-method <"myMethod"> --abi-params '{"parameter":"value"}'
```

### Compile a smart contract using TON Dev CLI (a command-line utility from TON Labs Dev)

```
tondev sol [options] [files...]
```

The following parameters are valid:

`-l, --client-languages <languages>` a client code generation for languages: js, rs (use a comma to separate different languages)

`-L, --client-level <client-level>` a client code level: «run» for launch only, «deploy» both for launch and deployment.

Example:

```
tondev sol hello.sol -l js -L deploy
```

The `-l js` parameter is used to generate a client supporting JavaScript code for the compiled contract.

`-L deploy` is used to include the `imageBase64` field into the generated client JavaScript code of a contract.

`L deploy` with the `js` flag creates a .js file (contractPackage.js) with the wrapper class of a contract. This wrapper contains methods for deploying and invoking a contract in a convenient format. Using the `deploy()` method, a message to the constructor to create a new contract is generated and sent. For more details, see, the description of the TON Solidity API methods. For each ABI function, two methods from the same class are implemented; one of them is called as the function itself, the other is `--function_nameLocal`. The class has a static package variable that stores two fields: abi and imageBase64.

Usage examples:

```
const hello = new HelloContract(client);
await hello.deploy();
console.log(hello.address, hello.keys);
const response = await hello.sayHello();

```

```
const existingKeys = { secret: ..., public: ...};
const existingAddress = '...';
const hello = new HelloContract(client, existingAddress, existingKeys);
const response = await hello.sayHello();

```

Example of the .js wrapper class from a contract.

## Links

1. Solidity compiler repository for the TON
2. Example of writing a TON smart contract. Solidity
3. Compile a smart contract in Solidity
4. Tonlabs Solidity Compiler (Youtube)]
5. Samples of smart contracts for the TON in Solidity

be:Sol2TVM Compiler
de:Sol2TVM Compiler
el:Sol2TVM Compiler
es:Sol2TVM Compiler
fr:Sol2TVM Compiler
ru:Sol2TVM Compiler
tr:Sol2TVM Compiler
uk:Компілятор Solidity