Getting Started with Solidity: My First Smart Contract

Ethereum is a global, decentralized financial and new application platform. In Ethereum, we can control assets through smart contract code and build applications that can be accessed anywhere in the world. Today we will write a simple smart contract code by ourselves and explore the mysteries of smart contracts.

【Smart Contract】

A smart contract (English: Smart contract ) is a computer protocol designed to communicate, verify, or enforce contracts in an informational manner . Smart contracts allow for trusted transactions without third parties, which are traceable and irreversible.

The purpose of smart contracts is to provide a method of security superior to traditional contracts and to reduce other transaction costs associated with contracts .

[EVM virtual machine]

The Ethereum Virtual Machine is the executor of the smart contract code. After the smart contract is compiled into a binary file, it is deployed on the blockchain. The user triggers the execution of the smart contract by calling the interface of the smart contract. EVM executes the code of the smart contract and modifies the data (state) on the current blockchain.

In other words, the executor used to execute smart contracts on Ethereum can execute smart contracts with it.

【Solidity language】

Solidity is the language for Ethereum Virtual Machine (EVM) smart contracts. I think Solidity is a necessary skill for playing blockchain projects: most blockchain projects are open source, if you can understand the code, it can help you avoid many money-losing projects.

Remix is ​​a smart contract development tool officially recommended by Ethereum. It is suitable for novices. It can quickly deploy and test smart contracts in the browser. You don't need to install any programs locally.

URL: remix.ethereum.org

【Create my first smart contract】

Entering remix, we can see that there are three buttons on the leftmost menu, which correspond to file (where to write code), compile (to run code), and deploy (to deploy to the chain). We click the Create New File button to create a blank solidity contract.

 After clicking "New", fill in a piece of smart contract code in the editing window on the right

pragma solidity ^0.4.17;

contract Faucet {

    function withdraw(uint amount) public {

        require(amount <= 1000000000000000000);

        msg.sender.transfer(amount);

    }

    function() public payable{}

}

This is a smart contract that simulates the work of a faucet. That is, the process of users receiving test coins through the faucet on the test network of Ethereum. Let's explain each line of this contract in detail:

first row

pragma solidity ^0.4.17;

All Solidity source code must be marked with a "version pragma" — indicating the version of the Solidity compiler. In order to avoid future new compilers that may break your code.

The current version is Solidity 0.4.17

second line

contract Faucet { }

contract contract

Faucet contract name (customizable)

The content contained in { } is the basic module of a contract.

The code of Solidity is wrapped in the contract. A contract is the basic module of the Ethereum application. All variables and functions belong to a contract, which is the starting point of all your applications.

The third row

function withdraw(uint amount) public {

function function

withdraw(uint amount) defines a function name named withdraw, and the parameter is the amount of unit (unsigned integer type)

public This function is publicly visible and can be called from outside

{ } is the content of the function

fourth line

require(amount <= 1000000000000000000);

require Conditional judgment statement, if the statement is true (True), it will be executed, if the statement is false (False), it will not be executed.

amount <= 1000000000000000000 The amount here represents the amount of ether currency, and the unit is wei. Like Bitcoin, Ether is not infinitely divisible, and the smallest unit of Ether is Wei.

The fifth line

msg.sender.transfer(amount);

msg.sender indicates the address of the current contract call

transfer(amount) indicates the amount of transfer

sixth line

function() public payable{}

This is a fallback function (Fallback Function)

A contract can have an anonymous function. This function cannot have parameters and cannot return any value. A contract call will be performed if no other function matches the given function identifier, or if no data is provided at all.

Additionally, whenever a contract receives plain Ether with no data, a fallback function is executed. Additionally, the fallback function must be marked payable in order to receive Ether. Without such a function, contracts cannot receive Ether through regular transactions.

In layman's terms, we do not need to transfer ETH to deploy this faucet contract, but only need to spend gas. Without a fallback function, contracts cannot receive Ether through regular transactions.

【Compile and deploy code】

Click on the compiled page, select the version, and click Compile. Or on the page of editing code, press ctrl+S to compile the code.

 After compiling, click the "Deploy" button on the left menu to enter the deployment page. By default, Remix will use the JS virtual machine to simulate the Ethereum chain and run smart contracts, similar to running a test chain in a browser. And remix will allocate several test accounts to you, each with 100 ETH (test tokens). You click Deploy (yellow button) to deploy the written contract.

After the deployment is successful, you will see a contract named faucet below, and in the red box at the bottom is the deployed contract address. Once the smart contract is successfully deployed on the chain, it cannot be tampered with. If you modify and redeploy, it's another contract.

This is a simple piece of smart contract code, isn’t it interesting? I hope this article is helpful to you, and let us unlock more smart contract technologies together.

Author: Solo Clover

Twitter: https://twitter.com/ZhiyuanQi

references:

@0xAA_Science  "Introduction to Solidity Minimalist" GitHub - AmazingAng/WTFSolidity: I am re-learning solidity recently, to consolidate the details, and also write an "Introduction to Solidity Minimalist" for beginners to use (programmers can find another tutorial), 1-3 lectures are updated every week.

Guess you like

Origin blog.csdn.net/weixin_48088680/article/details/125219055