Fundamentos de la programaci贸n de contratos inteligentes TON (FreeTON)

Antes de comenzar a escribir nuestro token y luego el sistema de contrato inteligente, echemos un vistazo r谩pido a los elementos importantes de los contratos inteligentes para FreeTON.





En el art铆culo anterior Hello Word, un contrato inteligente para TON (FreeTON), analizamos el comienzo de trabajar con los contratos inteligentes de FreeTON y HelloWorld.





La estructura de este art铆culo:





  1. Tipos de n煤meros int y uint, funciones puras y eventos (eventos): escribamos una calculadora en la cadena de bloques;





  2. Estructuras y matrices: contratos inteligentes como base de datos;





  3. Tablas hash: mapeo;





  4. Llaves p煤blicas y 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 . .





Aqu铆 se describieron aspectos para los lectores que estudian el tema de los contratos inteligentes de FreeTON "desde cero", y en los pr贸ximos art铆culos comenzaremos a crear nuestro propio token.








All Articles