Ethereum Basics-Solidity language 15 minutes first experience

Ethereum Smart Contract Development Fundamentals-Solidity Language 15-minute first experience

Solidity is a language for writing Ethereum smart contracts. So, if you want to become a blockchain programmer, learning this language is the way to go. However, since I really couldn't find my favorite basic tutorials on the Chinese website, I decided to create a series of tutorials to guide you to learn the language quickly and efficiently.

Build the base environment

The integrated development environment (IDE) I use is Remix , and the interface looks like this:

image-20211226095050760

On the far left of the interface, I can choose the default components in the IDE:

  • The first component is the file browser component , where you can see all the Solidityfiles.
  • The second component is the smart contract operation publishing component .
  • The third component is the Plugin Browser component , where you can browse and add new plugins.

In order to learn the Soliditylanguage better, in addition to the components that have been activated by default, we also need to activate the Solidity Compilercomponents, that is, the Soliditylanguage compiler.

  • First, click the plug-in browser icon to look for the plug-in.
  • Then, click the Activate button at the back.

image-20211226132427460

At this point, there will be an additional icon in the column on the left side of the interface, which is Solidity compiler.

image-20211226132448901

After installing the Solidity Compilercomponents, the first step is to confirm whether the automatic compilation option is turned on normally.

Write the HelloWorld smart contract

congratulations! You now have the tools to start writing your first smart contract.

Note that in the Deploy and run transactionsComponents page, select Javascript VMas Actuator. This is an Ethereum network simulator provided by Remix. We can directly publish the contract to the simulator, and then directly interact with the smart contract here to verify the contract.

Create a new file, name it HelloWorldContract.sol, and you can see that the first smart contract looks like this:

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract HelloWorldContract {
	function helloWorld() external pure returns(string memory) {
		return "Hello World!";
	}
}
  1. SPDX-License-Identifiers

When software is open source, software developers usually write the open source agreement that the open source project complies with in the README.md file.

However, due to the accumulation of history, various open source protocols have appeared, which are complicated and difficult for software developers to choose. Therefore, SPDXthe software open source protocol is standardized and standardized to facilitate the selection of open source software developers. Specific reference: SPDX license identifiers

  1. pragma

In smart contracts, pragmaflags are used to prevent future versions of the compiler from introducing some incompatible features that prevent previously written source code from compiling.

In soliditylanguages, version differences with major changes are generally distinguished by 0.x.0or x.0.0. In the above example, the compiler version can only be selected equal to 0.7.0 or higher than 0.7.0, but not higher than 0.8.0 (exclusive). For this rule, please refer npmto the version rule in .

  1. contract

The above two fields must appear in the first two lines of each smart contract file.

contract HelloWorldContract {
	function helloWorld() external pure returns(string memory) {
		return "Hello World!";
	}
}

Compile the contract

Click the leftmost Solidity compilerbutton, select the auto compileoption, and then click the Compile button to compile.

image-20211226131532471

release contract

Click the button on the far left Deploy & run transactions, select the corresponding content according to the figure below, and finally click the Publish Deploybutton.

image-20211226131711135

After the contract is published successfully, you can find a published contract below it, and click the button helloWorldto call the function in the published contract.

image-20211231142239000

After the call, the following output can be printed in the log, indicating that our contract has been successfully executed.

image-20211231142402976

So far, we have successfully released a contract, and it's just a little taste. Does it feel like it has not landed and is relatively empty? No problem, the following chapters will revolve around two points.

Guess you like

Origin blog.csdn.net/u012331525/article/details/122255636