Solid basic syntax structure - personal notes

Ethereum has many high-level languages that can be used to write smart contracts, and each language is inspired by another widely used language. The most popular one is called Solidity, which is based on JavaScript.  

  Its syntax is close to Javascript and is an object-oriented language.

 Remix - Ethereum IDEhttps://remix.ethereum.org/

  Need excellent network speed

catalogue

Four visibility / access rights for solidity

Three modifiers of solidity

Function of solidity

Boolean type

And, or

Usual operator

Bitwise Operators

pragma solidity ^0.4.0;

It can be compatible with versions within 0.4.0-0.5.0

pragma solidity ^0.4.0;

contract helloworld {
    string myword = "helloworld";
    
    function show() public view returns(string){
        return myword;
    }
}

 

Four visibility / access rights for solidity

public: anyone can call this function, including users of DApp.
private: only the contract itself can call this function (in another function).
internal: only this contract and all contracts arising therefrom can be called a contract.
external: this function can only be called externally, but not internally.

Three modifiers of solidity

view: it can be called freely because it only "views" the status of the blockchain without changing it
pure: it can also be called freely without reading or writing to the blockchain
payable: often used to send tokens to contract addresses.

Function of solidity

pragma solidity ^0.4.0;

contract helloworld {
    function stringtest(string inputstr) public view returns(string){
        return inputstr;
    }
}

Solidness is statically typed, so you always need to define the type of variable before assigning a value to it. A list of all types can be found in the documentation for Solidity.
Finally, we hope to query this value from our contract by calling the function. This function getIt will be called:

contract HelloWorld {
  string firstTest = "jonson";
     //Add function:
     function getIt() returns(string) {
      return firstTest;
  }
}

Boolean type

pragma solidity ^0.4.0;

contract helloworld {
    bool boola=true; //Declare a Boolean value with only one equal sign
    function booltesta() public view returns(bool){
        return boola;
    }
    
    function booltestb(int a,int b) public view returns(bool){
        return a==b;
    }
}

 

 

And, or

pragma solidity ^0.4.0;

contract helloworld {
    function andtestTT() public view returns(bool){
        return true&&true;
    }
    function andtesTF() public view returns(bool){
        return true&&false;
    }
    function andtestFF() public view  returns(bool){
        return false&&false;
    }
    function ortestTT() public view returns(bool){
        return true||true;
    }
    function ortesTF() public view  returns(bool){
        return true||false;
    }
    function ortestFF() public view  returns(bool){
        return false||false;
    }
}

Usual operator

 

pragma solidity ^0.4.0;

contract helloworld {
     function jiatest(int a,int b) public view  returns(int){
        return a+b;
    }
    function jiantest(int a,int b)  public view returns(int){
        return a-b;
    }
    function chengtest(int a,int b) public view  returns(int){
        return a*b;
    }
    function chutest(int a,int b)  public view returns(int){
        return a/b;
    }
    function quyutest(int a,int b)  public view returns(int){
        return a%b;
    }
    function mitest(uint a,uint b)  public view returns(uint){
        return a**b; //uint is required here. If int256 is written directly, an error will be reported
    }
}

  uint8, can only store 8bit data

 

Bitwise Operators

1. & after the operands are converted into binary, each operator performs and operation (1 out of 1)
2. | after the operands are converted into binary, each operator performs or operation (1 out of 1)
3. ~ after the operands are converted to binary, each operator performs the inverse operation (directly opposite)
4. ^ after the operands are converted into binary, each performs XOR operation (take 1 for different operands)
5. < < after the operand is converted to binary, each operator moves x bits to the left
6. > > after the operand is converted to binary, each operation moves x bits to the right

 

pragma solidity ^0.4.0;

contract helloworld {
    function Wyutest(uint8 a,uint8 b)  public view returns(uint8){
        return a&b;
    }
    function Whuotest(uint8 a,uint8 b)  public view returns(uint8){
        return a|b;
    }
    function Wfantest(uint8 a)  public view returns(uint8){
        return ~a;//Reverse
    }
    function Wyihuotest(uint8 a,uint8 b)  public view returns(uint8){
        return a^b;//a. b XOR
    }
    function zuoyitest(uint8 a,uint8 b)  public view returns(uint8){
        return a<<b;//a shift left b
    }
    function youyitest(uint8 a,uint8 b)  public view returns(uint8){
        return a>>b;//a shift right b
    }
}

Tags: Javascript

Posted on Sun, 05 Dec 2021 07:00:36 -0500 by Fruct0se