Skip to main content

Getting started

Referenced repository: https://docs.ton.dev/86757ecb2/p/950f8a-write-smart-contract-in-solidity

You will need:

  • A PC or laptop with a basic set of developer tools
  • (Recommended: Ubuntu 18.04) Linux
  • Windows
  • MacOS
  • A Solidity to TVM assembly compiler
  • (Recommended) build from sources
  • download as a binary
  • run in a docker container
  • Contract code in Solidity
  • Use Wallet.sol below
  • Use your own code
  • Take one of the samples from the samples repository
  • Utilities to link and deploy contract to the blockchain
  • build from sources
  • download as a binary
  • run in a docker container
  • OS: Ubuntu 18.04 is the easiest to run.
  • tip: running Ubuntu in VM works fine. Check out this install guide.
  • Build Solidity compiler from the source (4-6 minutes)
  • checkout from github (a few seconds);
  • install dependencies as per README (1-2 minutes)
  • build the compiler from source per README (~3-5 minutes)
  • Contract source code:
  • Use Wallet.sol below
  • Command line tools, either of:
  • download as a part of a binary package
  • build tvm_linker and tonos-cli from sources

Install the Compiler

Install EverX Solidity Compiler from the open source repository.

git clone git@github.com:tonlabs/TON-Solidity-Compiler.git
cd compiler
sh ./scripts/install_deps.sh
mkdir build
cd build
cmake .. -DUSE_CVC4=OFF -DUSE_Z3=OFF -DTESTS=OFF -DCMAKE_BUILD_TYPE=Debug
make -j8

Get the contract source code

pragma solidity >= 0.6.0;

/// @title Simple wallet
/// @author EverX
contract Wallet {
// Modifier that allows function to accept external call only if it was signed
// with contract owner's public key.
modifier checkOwnerAndAccept {
// Check that inbound message was signed with owner's public key.
// Runtime function that obtains sender's public key.
require(msg.pubkey() == tvm.pubkey(), 100);

// Runtime function that allows contract to process inbound messages spending
// its own resources (it's necessary if contract should process all inbound messages,
// not only those that carry value with them).
tvm.accept();
_;
}

/*
* Public functions
*/

/// @dev Contract constructor.
constructor() public checkOwnerAndAccept { }

/// @dev Allows to transfer grams to the destination account.
/// @param dest Transfer target address.
/// @param value Nanograms value to transfer.
/// @param bounce Flag that enables bounce message in case of target contract error.
function sendTransaction(address payable dest, uint128 value, bool bounce) public view checkOwnerAndAccept {
// Runtime function that allows to make a transfer with arbitrary settings.
dest.transfer(value, bounce, 3);
}

// Function to receive plain transfers.
receive() external payable {
}
}

Compile

Compile the contract code to TVM assembler with the Solidity Compiler.

<PATH_TO>/TON-Solidity-Compiler/compiler/build/solc/solc Wallet.sol

The compiler produces Wallet.code and Wallet.abi.json to be used in the following steps.

Assemble and link with a standard library into TVM bytecode:

<PATH_TO>/tvm_linker compile Wallet.code --lib <path-to>/TON-Solidity-Compiler/lib/stdlib_sol.tvm

Binary code of your contract is recorded into<WalletAddress>.tvcfile, where<WalletAddress>is a temporary address of the contract.

Compile

Compile the contract code to TVM assembler with the Solidity Compiler.

<PATH_TO>/TON-Solidity-Compiler/compiler/build/solc/solc Wallet.sol

The compiler produces Wallet.code and Wallet.abi.json to be used in the following steps.

Assemble and link with a standard library into TVM bytecode:

<PATH_TO>/tvm_linker compile Wallet.code --lib <path-to>/TON-Solidity-Compiler/lib/stdlib_sol.tvm

Binary code of your contract is recorded into WalletAddress.tvc file, where WalletAddress is a temporary address of the contract.

Deploy

Let's deploy the contract to TON Labs development blockchain atnet.ton.dev.

1) Make sure tonos-cli is in $PATH:

export PATH=$PATH:<PATH_TO>/tonos-cli

tonos-cli config --url net.ton.dev

2) Generate address, keys and seed phrase for your contract:

tonos-cli genaddr <WalletAddress>.tvc Wallet.abi.json --genkey Wallet.keys.json

Address of your contract in the blockchain is located after Raw address:

IMPORTANT: Save this value - you will need it to deploy your contract and to work with it. We will refer to it as "YourAddress" below. Seed phrase is also printed to stdout. Key pair will be generated and saved to the file Wallet.keys.json.

Note that you will need to send some coins to the address before the actual deployment. TON deploy is fee-based, so your new contract will be charged for this.

3) Get some [test] coins to your account. Options are:

  • ask a friend to sponsor your contract deployment;
  • transfer some currency from your wallet account;
  • ask in developer chats.

4) Check the state of the pre-deployed contract. It should be Uninit:

tonos-cli account <YourAddress>

5) Deploy your contract to the selected network (TON Labs devnet in the example) with the following command:

tonos-cli deploy --abi Wallet.abi.json --sign Wallet.keys.json <contract>.tvc {<constructor_arguments>}

If either of --abi or --sign options is omitted in parameters, it must be specified in the config file. See below.

6) Check the contract state again. This time, it is should be active.

7) Call the function of your contract:

tonos-cli call '<YourAddress>' sendTransaction '{"dest":"DestAddress", "value":1000000000, "bounce":true}' --abi Wallet.abi.json --sign Wallet.keys.json

Further Steps

Now your contract is up and running! You can: