Web3 - developing the first smart contract

Learn to write the first smart contract, the link below is the integrated development environment

https://github.com/smartcontractkit/full-blockchain-solidity-course-js#lesson-2-welcome-to-remix-simple-storage

insert image description here

Click "Remix" to come to Remix IDE, this is where we want to write code, integrated development environment. is where smart contracts are written and interacted with. Click "Accept".
insert image description here

It has many functions and helps us view and interact with smart contracts, although we will gradually move away from "Remix" and use the local development environment. But Remix is ​​very helpful for learning smart contract basics.

To develop the first contract, create a new "SimpleStorage.sol" file under "contracts". The .sol tells the compiler that this is a Solidity file. Solidity is the main programming language for smart contracts. In any Solidity smart contract, you first All you need is the used version of Solidity. Because the update frequency is very high, the version number is agreed by adding the version number to be used through "pragma solidity",

Add "SPDX-License-Identifier" at the top of the code. If there is no one, the compiler will give a warning. This will define the license and code sharing rules. As shown in the figure, MIT is used, and MIT is one of the least restrictive licenses. At this
time You can compile it, go to the compilation interface, click on the picture below,
insert image description here
no smart contract, start to define now

// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;  // 或者 ^0.8.7  或者 >=0.8.7

// 没有智能合约现在开始定义,输入“contract” ,contract是Solidity的关键字,告诉编译器后面的代码是来定义智能合约的。给智能合约取名名字叫“SimpleStorage”
contract SimpleStorage {
    
    

}

At this time, we can see the green checkmark by pressing Ctrl+S to save. As shown below
insert image description here

If not, enter the compilation interface, click compile, and then you can see it. A green checkmark indicates that the code compiled successfully without errors.

solidity basic data types

The four most basic data types are: boolean, uint, int and address;

  • boolean defines true or false.
  • uint is an unsigned integer, which means that this number is not positive or negative, but can only be positive.
  • int can represent positive or negative numbers.
  • address represents the address, as seen in Metamask.
bytes是一种更底层的数据类型。
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;  // 或者 ^0.8.7  或者 >=0.8.7

// 没有智能合约现在开始定义,输入“contract” ,contract是Solidity的关键字,告诉编译器后面的代码是来定义智能合约的。给智能合约取名名字叫“SimpleStorage”
contract SimpleStorage {
    
    
    // 创建一个变量,叫做“hasFavoriteNumber”
    bool hasFavoriteNumber = false; // 布尔类型数据
    uint8 favoriteNumber = 123;  // uint 比较特殊,我们可以决定给它分配的空间。uint8表示分配了8个bit ,最大uint256 ,
    int256 hasFavoriteInt = -54;
   // address myAddress = 0x106225d232323a23323592898;
    bytes32 favoriteByte = "1212";
    string hasFavoriteText= "123";
}

function

Like javaScript, it is represented by the "function" keyword,


    uint256 favoriteNum; // 定义变量
    // 接收uint256的参数,参数名是“_favoriteNum” .定义为public函数
    function funName(uint256 _favoriteNum) public {
    
      
          favoriteNum = _favoriteNum; // 一定要带上“;”符号`
    }

In order to see its actual running results, we deployed the contract on a test environment. Deploy it to the local network, compile before deploying,
deploy and send the transaction area.
insert image description here

Each account has 100 ether. These accounts are similar to the accounts in Metamask, the difference is that these are the ether currency in the test.
You can set the gas limit, select the contract to deploy, and
click the "deploy" button to deploy the contract. The running result is shown in the figure below.

insert image description here
Deployed Contracts is the smart contract deployment address. Click on the drop-down on the right to see familiar keywords: status, transaction hash, from, to, etc. Deploying a contract is actually sending a transaction. When we do anything on the blockchain to modify any state, we are sending a transaction. Deploying a contract modifies the blockchain so that the contract can be owned on the blockchain.
Enter 123 test after the function name "funName", as shown in the figure below, you can see that the balance of the account is also a little less. This is because when we call the contract, some gas is used.
insert image description here

Guess you like

Origin blog.csdn.net/weixin_43506403/article/details/127880864