Introduction
Solidity is a high -level programming language that is used to write smart contracts on the Ethereum blockchain. It is affected by JavaScript, Python, and C. In this part of this speed course, we will introduce the basic grammar of the solidity contract and how to write smart contracts.
Solidity contract is a collection of code (functions) and data (status), and specific addresses staying on the Ethereum blockchain. Each Solidity contract begins with a version statement, which specifies the version of the compiler version. 1. The basic structure of the solidity contract
Code decomposition
SPDX-LICENSE-IDentifier
<code class="language-solidity">// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract MyFirstContract { // 状态变量 uint public myNumber; string public myString; // 构造函数 - 合约部署时仅运行一次 constructor(uint _myNumber, string memory _myString) { myNumber = _myNumber; myString = _myString; } // 更新数字的函数 function setNumber(uint _newNumber) public { myNumber = _newNumber; } // 更新字符串的函数 function setString(string memory _newString) public { myString = _newString; } }</code>
contract MyFirstContract {}
-The state of initialization of the contract during deployment. uint
Functions to modify the state of the contract. string
setNumber
2. Write a simple smart contract setString
Explanation Incident
-Inned to record contract interaction on the blockchain.Public function
-Make users to store numbers.-In retrieved storage numbers without modifying the blockchain.
<code class="language-solidity">// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract FavoriteNumber { uint private favoriteNumber; // 当数字更改时通知的事件 event NumberUpdated(uint newNumber); // 设置喜欢的数字的函数 function setFavoriteNumber(uint _number) public { favoriteNumber = _number; emit NumberUpdated(_number); } // 获取喜欢的数字的函数 function getFavoriteNumber() public view returns (uint) { return favoriteNumber; } }</code>
setFavoriteNumber
To deploy a contract, you can use Remix IDE getFavoriteNumber
Use Remix IDE to deploy emit
Copy and paste the solidity contract and paste it into the new file. Use the Injected Web3 Environment (Metamask) to deploy it. Use the deployed functions to interact with the contract.
If you need to deploy help, please inform! Conclusion
The above is the detailed content of Solidity Crash Course - Part Smart Contracts. For more information, please follow other related articles on the PHP Chinese website!