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 is the Solidity contract structure?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "what is the structure of Solidity contract". 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 the structure of Solidity contract is.

Solidity contract structure mainly includes: state variable, local variable, constructor and destructor.

1. What is a smart contract?

The program running on the ethernet block chain is usually called intelligent contract (Smart Contract), so the program of writing block chain is usually renamed and smart contract is written.

2. A complete example of a contract: pragma solidity ^ 0.4.23 X contract Counter {uint count = 0; address owner; constructor () public {owner = msg.sender;} function increment () public {uint step = 10; if (owner = = msg.sender) {count = count + step }} function getCount () constant public returns (uint) {return count;} function kill () public {if (owner = = msg.sender) {selfdestruct (owner);} 3, version statement

Pragma solidity represents the solidity version statement, 0.4.23 represents the solidity version, ^ indicates upward compatibility, ^ 0.4.23 means that the solidity version in the 0.4.23-0.5.0 (excluding 0.5.0) version can be compiled on this contract code, 0.4.24,0.4.25 and so on can be used to fix some bug existing in the previous solidity.

4. Declaration of contract

Contract is the keyword of the contract statement, Counter is the contract name, and contract Counter is the declaration of a Counter contract.

Contract is equivalent to class,Counter in other languages, and contract Counter is equivalent to class Counter.

5. State variable

Uint count = 0

Address owner

Count and owner are state variables, and the state variables in the contract are equivalent to the attribute variables in the class.

6. Constructor (Contructor)

Constructor () public can also be written as function Counter (). The function name is the same as the contract name, and this function is the constructor of the contract. When the contract is deployed, the constructor will first call the constructor to initialize the relevant data, and the constructor will only be executed once during the entire life cycle.

7. Member function

Function increment () public

Function getCount () constant returns (uint)

The above two are member functions of the Counter contract. Member functions are called methods and behaviors in iOS. Contract instances can call member functions to handle related operations. When the increments function is called, the state variable count is incremented by step. When getCount () is called, you get the value of the state variable count.

8. Local variable function increment () public {uint step = 10; if (owner = = msg.sender) {count = count + step;}}

The step declared in the increment method is a local variable. A local variable is used only in the {} content closest to it.

9. Destructor (selfdestruct)

The destructor corresponds to the constructor, which initializes the data and destroys the data. In a counter contract, when we manually call the kill function, we call selfdestruct (owner) to destroy the current contract.

Thank you for your reading, the above is the content of "what is the structure of Solidity contract". After the study of this article, I believe you have a deeper understanding of what the structure of Solidity contract is, 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