PlatON Lectures: Online Open Class on PlatON Smart Contract (I)

On May 26, 2020, PlatON launched its first open class on smart contract. PlatON CTO James Qu shared the latest development on PlatON, deep interpretation of smart contract and the practice training of smart contract compiling and deployment, with core smart contract developers.



Deep Interpretation of Smart Contract - Compiling

Smart Contract (Head file)

Smart contract class is the key component of smart contract, and the method to execute the smart contract should be the member function of a class.

A class should be decorated by CONTRACT , must publicly inherit platon::Contract base class, and has init member function which will be executed in deployment once.

Smart contract method must be decorated by ACTION or CONST. This kind of member function can’t reload. When the function is decorated by ACTION, the on-chain data will be modified; when the member function is decorated by CONST, only attribute search is executed and the on-chain data is kept.

If the parameters and returned values of smart contract method are customization, then they need to add PLATON_SERIALIZE macro declaration serialization function in it’s definition; for other types, they need to add PLATON_SERIALIZE_DERIVED macro declaration serialization function.


Smart Contract (Implementation file)

The implementation of smart contract method should add ACTION or CONST the same as the head file, and apply the PLATON_DISPATCH macro declaration smart contract class and method.

The member variables of platon::StorageType can be stored persistently. There should be a n added in front of the first parameter of platon:StorageType template. The second parameter is the specific type of the true storage. The member functions need to get the specific samples from self() function to modify the member variables, and then execute the related sample functions.

The second parameter of platon:StorageType is a customization type, PLATON_SERIALIZE macro declaration serialization function should be added in such kind of definition type. If this kind of type inherits other types, PLATON_SERIALIZE_DERIVED macro declaration serialization function should be added.

Smart contract supports the initialization of class’s static data such as static variables, global variables, etc.


Smart Contract Permissions

Developers are recommended to set the permissions for the execution, burning, migration upgrade of smart contract, and other sensitive actions.

When executing the general smart contract, platon-caller is the trading initiator’s address; when executing the cross smart contract execution, platon-caller is the executed smart contract’s address.

Whether executing the general smart contract or the cross smart contract, platon_origin is the trading initiator’s address.

When executing the general smart contract or the cross smart contract, platon_address is the executed smart contract’s address; when executing the agent cross smart contract, platon_address is the address that execute the smart contract.

When deploying smart contract, if there is an address introduced, then set the smart contract owner as this address. Otherwise, set the smart contract as the trading initiator’s address.

When burning smart contract, check whether if the trading initiator is the smart contract owner. If yes, burn it, if no, exit the burning process.

When migrating smart contract, check whether if the trading initiator is the smart contract owner. If yes, migrate it, if no, exit the migrating process.


PlatON team provides a suite of authentication functions to simplify the difficulty of development

Apply set_owner to set the owner of smart contract. If the parameter is null, then set the smart contract initiator’s address as the owner. Apply “ init ” method for execution, and “ owner ” menthod to get the owner of smart contract.

The is_owner can check whether the introduced address is the owner of smart contact. If the parameter is null, then we will know whether the original trading initiator is the owner of smart contract.

The WhiteList class enables developer to add, delete and check whether if it’s in a white list.

A smart contract can support multiple white lists, and provides “ SysWhitelist ”, the system white list class, at the same time. Usually, SysWhitelist is OK to the great extent.


Smart Contract Creation

The smart contract will be initialized when being created. The initialization will be finished with init function which will be executed when being creating once. The init function can’t be reloaded, but can set the number and type of function parameters accordingly.


Smart Contract Burning

Smart contract can be self-destructed. Developers can realize the smart contract burning according to the embedded platon_destroy function PlatON provides. An address parameter of the smart contract will be necessary for transferring the balance of smart contract to this address. Keep in mind that smart contract burning is a high-risk action, please be cautious and set reasonable permissions of burning , and execute the embedded plaon-destroy smart contract burning after the permission verification gets approved.


Smart Contract Upgrade/Migratiom

The embedded function platon_migrate_contract will migrate the data in the previous smart contract to the new one, and upgrade the code logic. Notice that, smart contract migration is a high-risk action, please be cautious and set reasonable permissions of migration , and execute the embedded platon_migrate_contract to migarate the previous smart contract after the permission verification gets approved.

The return_address parameter is included in the new smart contract address, and the init-arg should be: magic number + RLP (code, RLP(hash(“init”), init_paras…)) .

The transfer_value refers to the transferred amount in the new smart contract address, gas_value refers to the estimated gas fee.