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:
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
Solidity
files. - 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 Solidity
language better, in addition to the components that have been activated by default, we also need to activate the Solidity Compiler
components, that is, the Solidity
language compiler.
- First, click the plug-in browser icon to look for the plug-in.
- Then, click the Activate button at the back.
At this point, there will be an additional icon in the column on the left side of the interface, which is Solidity compiler
.
After installing the
Solidity Compiler
components, 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 transactions
Components page, selectJavascript VM
as 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!";
}
}
- 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, SPDX
the software open source protocol is standardized and standardized to facilitate the selection of open source software developers. Specific reference: SPDX license identifiers
- pragma
In smart contracts, pragma
flags are used to prevent future versions of the compiler from introducing some incompatible features that prevent previously written source code from compiling.
In solidity
languages, version differences with major changes are generally distinguished by 0.x.0
or 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 npm
to the version rule in .
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 compiler
button, select the auto compile
option, and then click the Compile button to compile.
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 Deploy
button.
After the contract is published successfully, you can find a published contract below it, and click the button helloWorld
to call the function in the published contract.
After the call, the following output can be printed in the log, indicating that our contract has been successfully executed.
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.