As mentioned in the previous article, most of the alliance chain platforms, including FISCO bcos, use solidness as the development language of smart contracts, so it is necessary to be familiar with solidness.
As a Turing complete programming language for blockchain platform design, solidness supports function call, modifier, overload, event, inheritance and other features, and has extensive influence and active community support in blockchain community. But for those who have just come into contact with blockchain, solid is a strange language.
The writing stage of smart contract will start from the basic features, advanced features, design patterns and programming strategies of solidness, which will help readers understand solidness and master its application, so as to better develop smart contract.
This article will focus on the basic features of solidness and take you to develop a basic smart contract.
Code structure of smart contract
Any programming language has its standard code structure, which is used to express how to organize and write code in a code file, so does solidness.
In this section, we will use a simple contract example to understand the code structure of smart contract.
The above program includes the following functions:
- Deploy contracts through constructors
- Set contract status through setValue function
- Query contract status through getValue function
The whole contract is mainly divided into the following parts:
- state variable– _ admin, _ These variables are stored permanently and can also be modified by functions
- Constructors-Used to deploy and initialize contracts
- event-Setstate, which is similar to log, records the occurrence of an event
- Modifier -Onlyadmin is used to coat the function
- function-Setstate, getstate, used to read and write state variables
The following will introduce the above components one by one.
The state variable is the bone marrow of the contract, which records the business information of the contract. Users can modify these state variables through functions, and these modifications will also be included in the transaction; after the transaction is confirmed by the blockchain network, the modification will take effect.
uint private _state;
State variables are declared as: [type] [access modifier – optional] [field name]
Constructor is used to initialize the contract, which allows the user to pass in some basic data and write it to the state variable.
In the above example, the_ Admin field, as the premise of other functions.
Unlike Java, constructors do not support overloading and can only specify one constructor.
Functions are used to read and write state variables. The modification of variables will be included in the transaction and will take effect after being confirmed by the blockchain network. After it takes effect, the modification will be permanently saved in the blockchain ledger.
Function signature defines function name, input and output parameters, access modifiers and user-defined modifiers.
function setState(uint value) public onlyAdmin；
The function can also return multiple return values:
In this contract, there is also a function with the view modifier. This view indicates that the function does not modify any state variables.
Similar to view, there is also the modifier pure, which indicates that the function is a pure function, even the state variables do not need to be read, and the operation of the function only depends on the parameters.
If you try to modify the state variable in the view function or access the state variable in the pure function, the compiler will report an error.
Events are similar to logs and will be recorded in the blockchain. The client can subscribe to these events through Web3.
event SetState(uint value);
Here are some points to note:
- The name of an event can be specified arbitrarily, not necessarily linked to the function name, but it is recommended to link the two so as to clearly express what happened
- When constructing an event, you may not write emit, but because the event and function are highly related in name and parameter, it is easy to write the event as a function call by mistake, so it is not recommended.
- Solid programming style should adopt certain specifications. About programming style, it is recommended to refer to
The modifier is a very important part of the contract. It is hung on the function declaration to provide some additional functions for the function, such as checking, cleaning and so on.
In this case, the modifier onlyadmin requires that before a function is called, it is necessary to check whether the caller of the function is the administrator (that is, the deployer of the contract) set during function deployment.
It is worth noting that the underline “﹣ defined in the modifier , which represents the call of a function and refers to the function modified by the developer with modifiers. In this case, it means to call the setstate function.
Operation of smart contract
After understanding the structure of the above smart contract example, you can start to run it directly. There are many ways to run the contract, and you can take one of them at will
- Method 1: you can use FISCO bcos console to deploy contracts. Please refer to
- Method 2: use the online ide webase front provided by the open source project of FISCO bcos
- Method 3: deploy and run the contract through online ide Remix. The address of remix is
In this case, remix is used as a running example.
First, type the code in the file ide of remix, and then use the compile button to compile. After success, a green check mark will appear on the button:
After the compilation is successful, the deployment phase can be started, and the contract instance will appear after the deployment is successful.
After the contract is deployed, let’s call setstate (4). After successful execution, a transaction receipt will be generated, which contains the execution information of the transaction.
Here, the user can see the transaction execution status, transaction executor, transaction input and output, transaction cost and logs.
In logs, we can see that the setstate event is thrown, and the parameter in it also records the value 4 passed in by the event.
If we execute it in another account, the call will fail because the onlyadmin modifier prevents the user from calling it.
After calling getstate, you can directly see that the value obtained is 4, which is exactly the value passed in by our previous setstate
Solid data type
In the previous example, we used uint and other data types. Due to the special design of solid type, here will also briefly introduce the data type of solid.
Solid provides a set of data types to represent integers, including signed and unsigned integers. Each type of integer can also be subdivided according to the length, and the specific subdivision types are as follows.
Fixed length bytes series
Solid provides types of bytes1 to bytes32, which are fixed length byte arrays.
Users can read fixed length bytes.
Also, the integer type can be converted to bytes.
Here’s a key detail. Solidness adopts big endings encoding, and stores the small endings of integers. For example, B  is the low address side, which stores the high end of the integer, so the value is 0; take B  as 1.
Variable length bytes
From the above, readers can understand the fixed length byte array. In addition, solid provides a variable length byte array: bytes. The usage is similar to array, which will be introduced later.
The essence of string provided by solidity is a string of UTF-8 encoded byte array, which is compatible with variable length bytes.
At present, solidity does not support string well, nor does it have the concept of character. Users can convert string to bytes.
It should be noted that when converting string to bytes, the data content itself will not be copied. As mentioned above, STR and B variables point to the same string ABC.
Address represents the account address, which is generated indirectly by the private key and is a 20 byte data. Again, it can be converted to bytes20.
Mapping is a very important data structure. It differs from the mapping in Java in the following aspects:
- It can’t iterate keys, because it only saves the hash of the key, not the key value. If you want to iterate, you can use the open source iteratable hash class library
- If a key is not saved in mapping, the corresponding value can be read normally, but the value is null (all bytes are 0). So it does not need to put, get and other operations, the user can directly operate it.
If the array is a state variable, operations such as push are supported
Arrays can also be used as local variables, but slightly different:
Solidity allows developers to customize structure objects. The structure can be stored as a state variable or as a local variable in a function.
Only common data types are introduced in this section. For a more complete list, please refer to the official website of solidity
The constructor of the sample contract code contains msg.sender . It is a global variable. In smart contracts, global variables or global methods can be used to obtain some basic information related to the current block and transaction, such as block height, block time, contract caller, etc.
The commonly used global variables are MSG variables, which represent the calling context. The common global variables are as follows:
msg.sender: the direct caller of a contract.
Because it is a direct caller, when it is in the user a > contract 1 > Contract 2 call chain, if it is used in contract 2 msg.sender And get the address of contract 1. If you want to get user a, you can use tx.origin .
- tx.origin: the “initiator” of the transaction, the starting point of the whole call chain.
- msg.calldata: contains complete call information, including function identification, parameters, etc. The first four bytes of calldata are the function identifier, which is the same as the msg.sig Same.
msg.sig： msg.calldata The first 4 bytes of, used to identify the function.
- block.number: indicates the height of the current block.
now: represents the current timestamp. It can also be used block.timestamp express.
Only some common global variables are listed here. Please refer to:
In this paper, a simple example contract is introduced to introduce the basic knowledge of developing smart contract with solidity. Readers can try to run the contract and feel the development of smart contract.
If you want to learn more about the smart contract example, you can recommend the official website example for readers to learn. You can also pay attention to the following series of articles in this topic:
In the example of the official website, there are many cases such as voting, bidding and micro payment channel. These cases are close to real life and are good learning materials.
Next issue notice
The code of FISCO bcos is completely open source and free of charge