Solid smart contract basic practice

1. realize your first smart contract

1) Official documentation of solidity: https://solidity-cn.readthedocs.io/zh/develop/
2) Online compiler: https://remix.ethereum.org/
Note: it is recommended that you download remix locally so that you can save the code for future use.
3) Solidity is a high-level language for smart contracts that runs on the Ethereum virtual machine (EVM).

2. gas saving tools pure and view

  • As long as there are functions with pure and view modifiers, calling functions will not consume gas. Functions without pure and view decoration, such as the following change, will consume gas.
    1)pure: fixed input and output. It can be called freely because it only "views" the status of the blockchain without changing it.
    2)view: it can be called freely because it only "views" the status of the blockchain without changing it
  • pure: do not read or modify variables on the block, and use the CPU resources of the machine to calculate our functions. So it is easy to understand that it does not consume any resources.
  • View: but since view wants to read the values on the blockchain, why not consume gas??
    In fact, it is very simple, because as a whole node, all information will be saved synchronously and locally. If we want to view the resources on the blockchain, we can also directly query the data on a whole node.
    I don't need nodes all over the world to know. All go to deal with this matter at the same time.
    I also do not need to record the information calling this function on the blockchain.
    So view still does not consume gas.
    --------
    Copyright notice: This article is the original article of CSDN blogger "knowledge only link", which follows the CC 4.0 BY-SA copyright agreement. Please attach the original source link and this notice for reprint.
    Original link: https://blog.csdn.net/weishixianglian/article/details/84034794

3. true / false bool value

  • &&: true&&true = true
  • ||: true||false = true
  • !: True is false, false is true

4. integer properties and operations

Binary computing

pragma soldiity ^0.4.0;
contract math{
    int numa = 100;//int256 == int
    int numb = 200;//int256 == int
    uint8 numc = 2;
    function add(uint a,uint b) returns(uint){
        return a+b;
    }
    function jian(uint a,uint b) returns(uint){
        return a-b;
    }
    function cheng(uint a,uint b) returns(uint){
        return a*b;
    }
    function chu(uint a,uint b) returns(uint){
        return a/b;
    }
    function yu(uint a,uint b) returns(uint){
        return a%b;
    }
    function pingfang(uint a,uint b) returns(uint){
        return a**b;
    }
}

5. bottom layer calculation

pragma solidity 0.4.0;
contract WeiComputes{
    uint8 num1 = 3;
    uint8 num2 = 4;
    function yu() public returns(uint){
        return num1&num2;
    }
    function huo() public returns(uint){
        return num1|num2;
    }
    function fei() public returns(uint){
        return ~num1;
    }
    function yihuo() public returns(uint){
        return num1^num2;
    }
/*     function zuoyi() public returns(uint){
        return num1<<1;
    }
     function youyi() public returns(uint){
        return num1>>1;
    }*/
}

6. dangerous integer overflow and exception handling

7. integer literal

Assign the operation result to the variable to enhance the operation precision.

8. fixed length byte array

Keywords are: bytes1,bytes2,bytes3,..., bytes32 (increment by step 1)
byte stands for bytes1

pragma solidity ^0.4.0;
contract BytesArray{
    bytes1 public num1 = 0x7a;
    bytes2 public num2 = 0X7a68;
    bytes3  public num3 = 0X7a687a;
    function getLength() returns(uint){
        return num1.length;
    }
     function getLength2() returns(uint){
        return num2.length;
    }
     function getLength3() returns(uint){
        return num3.length;
    }
}

9. fixed length byte array depth

pragma solidity ^0.4.0;
contract BytesArray{
    bytes1 public num1 = 0x7a;
    bytes2 public num2 = 0X7a68;
    bytes3  public num3 = 0X7a687a;
    bytes1 public a = 0x7a;
    bytes1 public b = 0x68;
    function getLength() returns(uint){
        return num1.length;
    }
     function getLength2() returns(uint){
        return num2.length;
    }
     function getLength3()public returns(uint){
        return num3.length;
    }
    function weiyu() public returns(bytes1){
        return a&b;
    }
     function weihuo() public returns(bytes1){
        return a|b;
    }
     function weiyihuo() public returns(bytes1){
        return a^b;
    }
     function zuoyi() public returns(bytes1){
        return a<<1;
    }
     function youyi() public returns(bytes1){
        return a>>1;
    }
    function changeContent() public{
        num3 = 2;
    }
}

Array length and contents cannot be changed

10. dynamic byte array

Array length and memory can be changed

pragma solidity^0.4.0;
contract DynamcByte{
    bytes1 name = new bytes1(2);
    function InitName(){
        name[0] = 0x7a;
        name[1] = 0x68;
    }
    function getLength()view returns(uint){
        return name.length;
    }
    function changeName(){
        name[0] =0x88;
        
    }
    function changeLength(){
        name.length = 5;
    }
    function pushtext(){
        name.push(0x99);//push is added after the array;  
    }
}

Tags: Blockchain

Posted by Daisatske on Thu, 02 Jun 2022 05:27:45 +0530