Before we start writing our token and then the smart contract system, let's take a quick overview of the important elements of smart contracts for FreeTON.
In the previous article Hello Word, a smart contract for TON (FreeTON), we looked at the very beginning of working with FreeTON and HelloWorld smart contracts.
The structure of this article:
Number types int and uint, pure-functions and event (events): let's write a calculator on the blockchain;
Structures and arrays: smart contracts as a database;
Hash tables: mapping;
Public keys and require.
: tvm.accept(); -.
int ( ) uint ( ). , : int8, int16, uint128. int uint - int256 uint256. - .
- . calc, 3 uint-: 2 - , . calc pure, , , -, , .
, - , - .
pragma ton-solidity >= 0.35.0;
pragma AbiHeader expire;
contract HelloCalc {
event UnknownOperator(uint op);
function calc(uint a, uint b, uint op) public pure returns (uint) {
tvm.accept();
if(op == 1) {
return a + b;
} else if(op == 2) {
return a - b;
} else if(op == 3) {
return a * b;
} else if(op == 4) {
return a / b;
} else if(op == 5) {
return a % b;
} else if(op == 6) {
return a ** b;
}else {
emit UnknownOperator(op);
return 0;
}
}
}
(event) - , , , , , 0 , , .
( ) -, , , -. , - blockchain, - blockchain . - blockchain.
pragma ton-solidity >= 0.35.0;
pragma AbiHeader expire;
contract HelloUser {
event NewUser(uint id);
struct User {
uint weight;
uint balance;
}
User[] users;
function AddUser(uint w, uint b) public {
tvm.accept();
users.push(User(w, b));
emit NewUser(users.length - 1);
}
function GetUser(uint id) public view returns (uint w, uint b) {
tvm.accept();
w = users[id].weight;
b = users[id].balance;
}
}
User , 2 uint: . , , , . push, - NewUser.
GetUser: returns, . pure view.
-: mapping
, (User) ( , ) . mapping. , , , , . , , .
mapping :
contract HelloToken {
event TokenCreated(uint owner, uint tid);
struct Token {
string name;
string symbol;
}
Token[] tokens;
mapping (uint => uint) accounts;
function NewToken() public {
tvm.accept();
tokens.push(Token("", ""));
accounts[tokens.length-1] = msg.pubkey();
emit TokenCreated(msg.pubkey(), tokens.length-1);
}
function GetTokenOwner(uint tid) public view returns (uint) {
tvm.accept();
return accounts[tid];
}
function GetTokenInfo(uint tid) public view returns (string _name, string _symbol) {
tvm.accept();
_name = tokens[tid].name;
_symbol = tokens[tid].symbol;
}
function SetTokenInfo(uint tid, string _name, string _symbol) public {
require(msg.pubkey() == accounts[tid], 101);
tvm.accept();
tokens[tid].name = _name;
tokens[tid].symbol = _symbol;
}
}
require
- , , - , ( ). : , , value TON Crystal . . - API msg.pubkey(). NewToken() accounts[tokens.length-1] = msg.pubkey(); ID accounts , , tokens. SetTokenInfo() , , . tid accounts . .
Aspects were described here for readers studying the topic of FreeTON smart contracts "from scratch", and in the next articles we will start creating our own token.