Antes de começarmos a escrever nosso token e, em seguida, o sistema de contrato inteligente, vamos dar uma rápida visão geral dos elementos importantes dos contratos inteligentes para FreeTON.
No artigo anterior Hello Word, um contrato inteligente para a TON (FreeTON), vimos o início do trabalho com os contratos inteligentes FreeTON e HelloWorld.
A estrutura deste artigo:
Tipos de números int e uint, funções puras e evento (eventos): vamos escrever uma calculadora no blockchain;
Estruturas e matrizes: contratos inteligentes como um banco de dados;
Tabelas de hash: mapeamento;
Chaves públicas e requerem.
: 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 . .
Aspectos foram descritos aqui para leitores que estudam o tópico de contratos inteligentes FreeTON "do zero", e nos próximos artigos começaremos a criar nosso próprio token.