Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic features of Solidity

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "what are the basic characteristics of Solidity". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the basic characteristics of Solidity".

Intelligent contract code structure

Any programming language has its own standard code structure, which is used to express how code is organized and written in a code file, and so does Solidity.

In this section, we will use a simple contract example to understand the code structure of a smart contract. The intelligent contract writing phase will be carried out from the basic features, advanced features, design patterns and programming strategies of Solidity, which will lead readers to understand Solidity and master its application, so as to better carry out intelligent contract development.

Unlike java, constructors do not support overloading and can only specify one constructor.

Function

Function is used to read and write state variables. Changes to the variables will be included in the transaction and will not take effect until confirmed by the block chain network. After taking effect, the changes will be permanently saved in the blockchain ledger.

The function signature defines the function name, input and output parameters, access modifiers, and custom modifiers.

Function setState (uint value) public onlyAdmin

The function can also return multiple return values:

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.

Event

Events are similar to logs and are logged to the block chain, and clients can subscribe to these events through web3.

Define event

Event SetState (uint value)

Tectonic event

Emit SetState (value)

Here are a few points to note:

The name of the event can be specified at will, not necessarily with the function name, but it is recommended to hook the two to clearly express what happened.

When you construct an event, you can also not write emit, but because the event is highly related to the function name and parameter, it is easy to write the event as a function call, so it is not recommended.

It is worth noting that the underscore "_" defined in the modifier indicates the call to the function and refers to the function that the developer modifies with the modifier. In this case, the meaning of the setState function call is expressed.

The operation of intelligent contract

After understanding the structure of the above intelligent contract example, you can run the contract directly. There are many ways to run the contract, and you can take any one of them:

Method 1: you can use the FISCO BCOS console to deploy the contract

Method 2: use online ide WEBASE-front provided by FISCO BCOS open source project WeBASE to run

Method 3: deploy and run the contract through online ide remix

In this example, remix is used as a running example.

Compile

First, type the code into remix's file ide and compile it through the compile button. After success, a green check appears on the button:

SetState

After the contract is deployed, let's call setState (4). After a successful execution, a transaction receipt is generated, which contains the execution information of the transaction.

GetState

After calling getState, you can directly see that the resulting value is 4, which is exactly the value we passed in setState earlier:

Fixed length bytes series

Solidity provides types from bytes1 to bytes32, which are fixed-length byte arrays.

Users can read the contents of a fixed-length bytes.

Here is a key detail, Solidity uses large-end coding, high address is stored in the small end of the integer. For example, b [0] is the low address side, it stores the high end of the integer, so the value is 0; take b [31] is 1.

Note that when you convert string to bytes, the data content itself is not copied, as shown above, the str and b variables both point to the same string abc.

Address

Address represents the account address, which is generated indirectly by the private key and is a 20-byte data. Similarly, it can also be converted to bytes20.

Array

If the array is a state variable, operations such as push are supported:

Struct

Solidity allows developers to customize structural objects. The structure can be stored as a state variable or as a local variable in the function.

Global variable

Msg.sender is included in the constructor of the sample contract code. It belongs to a global variable. In intelligent 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 and so on.

The more commonly used global variable is the msg variable, which represents the calling context. The common global variables are as follows:

Msg.sender: the direct caller of the contract.

Because it is a direct caller, when you are under the call chain of user A-> contract 1-> contract 2, if you use msg.sender in contract 2, you will 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 entire call chain.

Msg.calldata: contains complete call information, including function identification, parameters, and so on. The first 4 bytes of calldata are the function identifiers, which are the same as msg.sig.

The first 4 bytes of msg.sig:msg.calldata, used to identify the function.

Block.number: indicates the current height of the block.

Now: represents the current timestamp. It can also be expressed as block.timestamp.

Thank you for your reading, the above is the content of "what are the basic characteristics of Solidity". After the study of this article, I believe you have a deeper understanding of what the basic characteristics of Solidity are, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report