Home > Web Front-end > JS Tutorial > Solidity Crash Course - Part Smart Contracts

Solidity Crash Course - Part Smart Contracts

DDD
Release: 2025-01-29 22:33:14
Original
856 people have browsed it

Solidity Crash Course - Part  Smart Contracts

Solidity Speed ​​Course -Part 2: Smart Contract

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 grammar


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>
Copy after login
-D license to specify the contract.

Pragma instructions
    -De your use of Solidity 0.8.0 or higher versions of the contract.
  1. The contract statement -
  2. defines the contract.
  3. Status variables -
  4. and
  5. The durable data on the blockchain storage blockchain. Construct function contract MyFirstContract {} -The state of initialization of the contract during deployment.
  6. Public function - and uint Functions to modify the state of the contract. string
  7. Solidity -Smart Contract
  8. Smart contract is a self -executed contract with predefined rules. After deployment, it interacts with users and other contracts. setNumber 2. Write a simple smart contract setString
  9. The following is an intelligent contract example that allows users to store and retrieve their favorite numbers.

Explanation Incident

-Inned to record contract interaction on the blockchain.

Public function

-Make users to store numbers.

View function

-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>
Copy after login

Sentence
    -record the event when updating the number.
  1. Deploy contracts
  2. setFavoriteNumber To deploy a contract, you can use Remix IDE
  3. or
  4. Hardhat . getFavoriteNumber Use Remix IDE to deploy
  5. Go to Remix IDE. emit Copy and paste the solidity contract and paste it into the new file.
Use a solidity compiler to compile contracts.

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

In this section, we introduced the basic Solidity contract grammar and created a simple smart contract.
  1. Please look forward to the third part!

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template