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 basics of solidity

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the basic knowledge of solidity". In daily operation, I believe many people have doubts about the basic knowledge of solidity. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts about "what is the basic knowledge of solidity?" Next, please follow the editor to study!

Frame

Just like HelloWorld in other languages, the framework for writing smart contracts using solidity is as follows:

Contract Helloworld {

……

}

Version instruction

The first line of each smart contract file specifies the solidity compiler version number in the following format:

Pragma solidity ^ version number

Chestnut:

Pragma solidity ^ 0.4.19

State variable

The state variable is permanently stored in the contract. That is, they are written into the etheric block chain. It can be identified by storage.

Corresponding to state variables, variables on a single node that do not need synchronization can be identified by memory, indicating that the variable is stored in memory and does not need to be written to the block chain.

Integer number

Integers within a solidity can be identified using uint. Uint defaults to uint256, which represents a 256-bit unsigned integer, corresponding to uint8,uint16,uint32, etc.

Operator

The operators in solidity are similar to other languages, +, -, *, /, and so on.

Structural body

The structures in solidity are similar to other languages:

Struct Person {

String name

Bool sexual

……

}

One thing to note here is that variables of the same type are best put together to save space:

Struct Person {

String name

String hairColor

Bool sexual

Uint8 age

……

}

Type conversion

Uint a

Uint8 b

Uint8 c

/ / c = a roomb; / / the compiler does not give it, because the an and b types are different

C=uint8 (a) + b

Array

Solidity supports static and dynamic arrays:

Uint [2] a

Uint [] b

You can also support structural body arrays:

Person [] people

Common array

You can define public arrays, and Solidity automatically creates getter methods. The syntax is as follows:

Person [] public people

Other contracts can read data from this array (but not write data), so this is a useful mode for preserving public data in contracts.

Add a new element to the end of the function

People.push (Person ("eddie", true))

Function

Similar to JS:

Function example (string _ a _ menuint _ b) returns (string) {

String s

Return s

}

Habit:

The parameters all start with _.

Function modifier

Public: can be called both inside and outside the contract.

Private: can be called within the contract.

Internal: callable within contracts and inheriting contracts.

External: can be called outside the contract.

Chestnut:

Contract example {

Function _ aaa (uint _ a) private {

}

}

Habit:

When the function is private, the function name starts with _.

Modifier: indicates the modifier body

Chestnut:

Contract example {

Modifier aboveMinEth () {

Require (msg.value > = 0.001 ether); / / require: conditional judgment statement, otherwise the function body will not be executed.

_; / / continue executing the function body.

}

Function aaa (uint _ a) public aboveMinEth {

……

}

}

View: does not change any values or write anything, only read operations at most.

Pure: there is no read or write operation, only the output value is determined based on the input value.

Contract example {

Uint axi9

Function add1 (uint _ b) public view returns (uint) {

Return axioms

}

Function add2 (uint _ bmenuint _ c) public pure returns (uint) {

Return _ baked roomc

}

}

Event

Event is a mechanism of communication between contract and block chain.

Contract example {

Uint axi9

Event addsomething (uint _ xreint _ yjinuint result); / / create the event

Function add2 (uint _ bmenuint _ c) public pure returns (uint) {

Addsomething (_ brec) / / notify app of the triggered event

Return _ baked roomc

}

}

At this point, the study of "what are the basics of solidity" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report