"Hello World" TON smart-contract for 15 minutes
Project ManagerЭта статья доступна на русском языке
The objective of a tutorial: understand how faster to deploy smart contracts at the TON for the very beginners.
What to do: to prepare necessary components, deploy the smart-contract, where there is a function hello_world. Call this function from outside and we will get 1234567890
This tutorial is intended for OS Ubuntu 18.04/20.04 (Server и Desktop). For other OS this tutorial can be useless.
upd: If the guide looks scary, you can deploy a smart contract in 2 minutes via https://deployer.tonsc.org , but it's better to read the guide to understand the essence.
The smart-contract will deploy in testnet
For deploying at mainnet: in p.19 testnet.ton.sh change to the ton.sh, p.20 you can skip, p.21 and p.22 change the testnet-global.config.json to global.config.json
Let’s go!
1. Connect to the server by SSH or open Terminal, then:
sudo -i {if the system asked for a password - type it, then enter}
2. If you haven’t installed mytonctrl yet - install (if your PC /server isn’t powerful, it can take ~15 min, so have a cup of tea/coffee after starting the installation):
wget https://raw.githubusercontent.com/igroman787/mytonctrl/master/scripts/install.sh sudo bash install.sh -m lite
3. Compile executable file func:
cd /usr/bin/ton/ && make func
4. Create the required environment variable:
export FIFTPATH=/usr/src/ton/crypto/fift/lib:/usr/src/ton/crypto/smartcont
5. Download global configuration file for mainnet and testnet:
wget https://newton-blockchain.github.io/global.config.json wget https://newton-blockchain.github.io/testnet-global.config.json
6. Make generate.fif file, to which be referred .sh file from p.9:
cd /usr/src/ton/crypto/smartcont && nano generate.fif
7. Insert code:
#!/usr/bin/fift -s "TonUtil.fif" include "Asm.fif" include 5 :$1..n $1 parse-workchain-id =: wc // set workchain id from command line argument $2 parse-int =: subwallet-id $3 "/root/sc" replace-if-null =: file-base $4 "/root/sc.fif" replace-if-null =: fif-code fif-code include <b 0 32 u, subwallet-id 32 u, file-base +".pk" load-generate-keypair constant wallet_pk B, b> null <b b{0011} s, 3 roll ref, rot ref, swap dict, b> dup ."StateInit: " <s csr. cr dup hashu wc swap 2dup 2constant wallet_addr ."new wallet address = " 2dup .addr cr 2dup file-base +".addr" save-address-verbose ."Non-bounceable address (for init): " 2dup 7 .Addr cr ."Bounceable address (for later access): " 6 .Addr cr <b subwallet-id 32 u, -1 32 i, 0 32 u, b> dup ."signing message: " <s csr. cr dup hashu wallet_pk ed25519_sign_uint rot <b b{1000100} s, wallet_addr addr, b{000010} s, swap <s s, b{0} s, swap B, swap <s s, b> dup ."External message for initialization is " <s csr. cr 2 boc+>B dup Bx. cr file-base +".boc" tuck B>file ."(Saved wallet creating query to file " type .")" cr
8. Exit with saving:
{ctrl+x -> y -> enter}
upd: more detailed analysis of the code from p.7
9. Create .sh file, which permits compile file of future smart-contract (.fc) to .fif file. On-base .fif - generate .addr, .pk, .boc files and get the address of smart-contract:
cd /usr/bin/ton/ && nano compile.sh
10. Insert code:
#!/bin/bash fcDir=$1 workchainId=$2 subwalletId=$3 if [ -z "${workchainId}" ] then workchainId=0 fi if [ -z "${subwalletId}" ] then subwalletId=0 fi funcPath="/usr/bin/ton/crypto/func" fiftPath="/usr/bin/ton/crypto/fift" fiftLibPath="/usr/src/ton/crypto/fift/lib" smartcontPath="/usr/src/ton/crypto/smartcont" stdlibPath="${smartcontPath}/stdlib.fc" fcNames=$(ls ${fcDir}/*.fc) fcPath=${fcNames[0]} fcFullName=$(basename $fcPath) fcShortName=${fcFullName:0:-3} fifPath="${fcDir}/${fcShortName}.fif" toFifCmd="${funcPath} -SPA ${stdlibPath} ${fcPath} -o ${fifPath}" ${toFifCmd} toGenerateCmd="${fiftPath} -I ${fiftLibPath}:${smartcontPath} -s generate.fif ${workchainId} ${subwalletId} ${fcDir}/${fcShortName} ${fifPath}" generateResult=$(${toGenerateCmd}) rawAddr=${generateResult#*new wallet address = } rawAddr=${rawAddr%(Saving address to file*} nonBounceableAddr=${generateResult#*Non-bounceable address (for init): } nonBounceableAddr=${nonBounceableAddr%Bounceable*} bounceableAddr=${generateResult#*Bounceable address (for later access): } bounceableAddr=${bounceableAddr%signing message:*} echo -e "\nRaw address: ${rawAddr}\nNon-bounceable address: ${nonBounceableAddr}\nBounceable address: ${bounceableAddr}"
11. Exit wth saving:
{ctrl+x -> y -> enter}
12. Allow the compile.sh to be executed:
chmod +x compile.sh
13. Create a directory with our project and go to it:
mkdir /srv/HelloWorldSC && cd /srv/HelloWorldSC
14. Use as the base smart-contract code of the wallet wallet3-code.fc. Copy it to the current directory like sc.fc (sc - smart-contract):
cp /usr/src/ton/crypto/smartcont/wallet3-code.fc ./sc.fc
15. Open sc.fc with a text editor:
nano sc.fc
16. Add at the end of the file our function hello_world:
int hello_world() method_id { return 1234567890; }
17. Exit with saving:
{ctrl+x -> y -> enter}
18. Run compile.sh. First argument - directory with the project, where is located .fc file. Second argument - workchain_id, in our case it's 0 (if you are going to work with elector smart-contract in future, then point workchain_id -1):
/usr/bin/ton/compile.sh /srv/HelloWorldSC 0
19. Copy the address of smart-contract (it will be shown in console like Bounceable address), and transfer on it 1 TON. You can check your TON transfer on the link (replace "SMARTCONTRACT_ADDRESS"):
https://testnet.ton.sh/address/SMARTCONTRACT_ADDRESS
20. The simplest option is to register a wallet in testnet with the link https://tonwallet.me?testnet=true
Ask on telegram channel https://t.me/tondev to transfer a little of TON (5 will be enough), or text me (@ProjectManageRR), if I am not busy, I’ll send you.
21. Activate our smart-contract (roughly speaking - connect the smart-contract code to the wallet address):
/usr/bin/ton/lite-client/lite-client -C /usr/bin/ton/testnet-global.config.json -c "sendfile /srv/HelloWorldSC/sc.boc"
22. Call the hello_world function of our smart-contract, and get in response 1234567890:
/usr/bin/ton/lite-client/lite-client -C /usr/bin/ton/testnet-global.config.json -c "runmethod SMARTCONTRACT_ADDRESS hello_world"
23. If you get in the response "..result: [ 1234567890 ].." - Congratulations! You successfully have deployed a smart-contract in TON network! 🥳🥳
Bonus:
Type "ls" in the console and you will see these files:
sc.addr - a file with a smart-contract address
sc.boc -a file that we will send to activate a smart-contract
sc.fc - a file with a smart-contract code at programming language FunC
sc.fif - a compilation of sc.fc in Fift smart-contract code
sc.pk - a private key to access a smart-contract (for example, to transfer coins from it)
Usually, you need to save .addr and .pk files to a secure location for having access to your address.
Later I will consider the code line by line used in this tutorial smart-contract (sc.fc) and add here a link instead of this message 😉
If something doesn’t work at this tutorial, please contact @ProjectManageRR
I will be so thankful for your likes: EQAoB3Myz9p5DMjdAytez_d4FcWcXEnqRL7Mk11jS6PW0_R1
👍