header-langage
简体中文
English
Tiếng Việt
Scan to Download the APP
Partnered With:

Simple Introduction to Solidity|Lecture 11: Constructors and Decorators

Read this article in 5 Minutes
Use the example of contract authority control to introduce the constructor and unique decorator in solidity language.
Original title: "Introduction to Solidity Minimalist: 11.  Constructors and Decorators"
Original author: 0xAA  


I am re-learning solidity recently, to consolidate the details, and also write a "Solidity minimalist introduction" for beginners to use (programmers can find it elsewhere Tutorials), 1-3 lectures are updated every week.


All codes and tutorials are open source on github: github.com/AmazingAng/WTFSolidity


In this talk, we will use the example of contract authority control (Ownable) to introduce the constructor (constructor) in solidity language ) and unique modifiers.


Constructor


The constructor (constructor) is a special function, each contract can define one, and automatically run once when the contract is deployed. It can be used to initialize some parameters of the contract, such as initializing the owner address of the contract:


< /p>


Modifier


< /p>

A modifier (modifier) is a solidity-specific syntax, similar to a decorator in object-oriented programming, which declares the characteristics of a function and reduces code redundancy. It's like Iron Man's smart armor, and functions that wear it will have certain behaviors. The main usage scenario of modifier is to check before running functions, such as addresses, variables, balances, etc.


  Iron Man's modifier


We define a modifier called onlyOwner:



Represents a function with onlyOwner modifier It can only be called by the owner address, such as the following example:



We defined a changeOwner function, running it can change the owner of the contract, but due to the existence of the onlyOwner modifier, only the original owner can call it, others call it It will report an error. This is also the most commonly used method of controlling smart contract permissions.


Ownable standard implementation of OppenZeppelin: https://github.com/OpenZeppelin/openzeppelin-contracts /blob/master/contracts/access/Ownable.sol  


Summary


In this talk, we introduced the constructors and modifiers in solidity, and gave a control contract authority Ownable contract.


Original link


Welcome to join the official BlockBeats community:

Telegram Subscription Group: https://t.me/theblockbeats

Telegram Discussion Group: https://t.me/BlockBeats_App

Official Twitter Account: https://twitter.com/BlockBeatsAsia

举报 Correction/Report
Choose Library
Add Library
Cancel
Finish
Add Library
Visible to myself only
Public
Save
Correction/Report
Submit