Solidity learning (1)

1.Solidity foundation

Friendly reminder: Every sentence is very important, and every point is a detail. Moreover, this tutorial is suitable for those who have a certain programming foundation to get started quickly.
Basic syntax, integer overflow and exception handling, byte arrays, dynamic byte arrays, strings, string and byte array conversions, to help you quickly get started with Solidity

Here is a piece of code directly

pragma solidity ^0.4.16;

contract Helloworld {
    string Myname = "张三";
    function getName() public view returns(string) {
        return  Myname;
    }
    function setName(string newname) public {
        Myname = newname;
    }
     function pureTest(string _name) public pure returns (string) {
        return _name;
    }
}

The editor used here is the remix web editor: click to enter the remix editor

Below we analyze this code

In java, we already have a lot of knowledge, so we only need to learn how it differs from other languages ​​when learning solidity

  1. The prama in the first line is the compilation control instruction of Solidity, then our line of code means that the source file is not allowed to be compiled by compilers below 0.4.16 and greater than or equal to 0.5.0. In short, allow The range is left closed and right open

    0.4.16<=Compiler version<0.5.0

    The compiler version in this interval can be compiled.

  2. The contract keyword, equivalent to the class in java, defines a contract

  3. View keyword, this is a keyword unique to Solidity, because solidity is ultimately used in the contract, so there will be related functions, and view is one of these functions, view can only be used to modify the method, and the modified method You cannot modify the state variable, which is the attribute of the class in java. Here, the state variable is Myname. Here we can see that the getName() method does not modify the value of Myname.

  4. To run this code, we first call the getName function. As a result, the following Redmix code running resultspicture shows the result of the operation. The operation is successful.
    Gas 1
    This picture shows the fuel consumption, remember this number

  5. Run the setName() method, this method needs to pass in a parameter, we pass in "Li Si" here, click run, and then run getName() to get the value again, the result is as follows,
    Insert picture description here
    Insert picture description here
    but we find that the gas fee has changed, which shows what?

  6. When we continue to click getName() to run it multiple times, the fuel rate remains unchanged, and when we run the setName() method again, the fuel rate will change.

  7. This shows that the method modified by the view will not consume gas, because it does not need to change the state variables, so the view keyword can also save gas.

  8. The pure keyword, the method modified by this keyword does not allow access to state variables, so there is no need to spend gas.

  9. bool,&& ,||, usage is exactly the same as in java, so I won't explain much here.

  10. The uint keyword represents non-negative integer type, the default is uint256, 256 means 256 bits,

8bit=1byte

  1. Method overloading is also supported in solidity
  2. Bit operators: Since solidity is used to deploy contracts, it has high memory requirements. In order to save memory, bit operators are very important

&: bitwise AND, which is 1 at the same time, get 1, and one or more is 0, which is 0
|: bitwise OR, which is 0 at the same time, get 0, and one or more is 1, which is 1
~: negate, 0 It becomes 1, and 1 becomes 0 (only a string of numbers is required)
^: bitwise XOR, the same is 0, the difference is 1
<<: shift left
. >>: shift right

Test all the above symbols, the results are not given here, if you are interested, you can run it yourself


pragma solidity ^0.4.16;
   
   contract BoolTest {
       uint8 a = 4;
       uint8 b = 2;
       
       function plus() public view returns(uint8){
           return a + b;
       }
       function minus() public view returns(uint8){
           return a - b;
       }
       function multiply() public view returns(uint8){
           return a * b;
       }
       function divide() public view returns(uint8){
           return a / b;
       }
       function power() public view returns(uint8){
           return a ** b;
       }
       
       function test1() public view returns(uint8){
           return a&b;
       }
       function test2() public view returns(uint8){
           return a|b;
       }
       function test3() public view returns(uint8){
           return ~b;
       }
       function test4() public view returns(uint8){
           return a^b;
       }
       function test5() public view returns(uint8){
           return a<<1;
       }
        function test6() public view returns(uint8){
           return a>>1;
       }
   }

2. Integer overflow and exception handling

  1. Next, we will introduce the very dangerous integer overflow problem and attach the code.
 function flow() public pure returns(uint8){
           uint8 mm = 255;
           return mm;
       }

we know. The 8-bit data range is 0~255, so this method will return a value of mm, which is 255, and the operation result is no problem. Insert picture description here
Next we modify the code, the modified code is below

function flow() public pure returns(uint8){
           uint8 mm = 255;
           mm++;
           return mm;
       }

After the code is modified, mm should become 256, but 256 has already exceeded the 8-bit value range, so an error will occur. Operation result: It
Insert picture description here
can be seen that the value of mm output directly becomes 0. Why is there such a result?

1 1 1 1 , 1 1 1 1

The above is our 8 bits. This is the binary representation of 255. When the number is increased by 1, the last bit is increased by 1. Friends who have studied binary know that, from back to front, full 2 ​​enters 1, and the last The result is:

1, 0 0 0 0, 0 0 0 0

Since we can only read 8 bits, the value after reading is

0 0 0 0, 0 0 0 0

This value is 0, so the 8-bit reading after m becomes 0.

To verify our conclusion, we modify the code again:

 function flow() public pure returns(uint8){
           uint8 mm = 255;
           mm++;
           mm++;
           return mm;
       }

Operation result: It
Insert picture description here
can be seen that when we add 1 to the previous value of mm, it becomes

0 0 0 0, 0 0 0 1

So the result of mm is 1, which can verify that the conclusion we just made is correct.

  1. Let's write another function and attach the code:
function erroTest() public pure returns(int){
            int a1 = 1;
            int a2 = 0;
            return a1/a2;
        }

Pay attention to the above function, it is obvious that we see an error in the above function, because 0 cannot be used as a divisor, then we can compile this code to pass, but when we call this function, we will see the following fields.
Insert picture description here
This is An exception has occurred.

Note: Solidity does not currently support decimal types

3. Byte array

We still give a piece of code

pragma solidity ^0.4.0;
 
 contract BytesArray {
     
     bytes1 public num1 = 0x7a;// 只有一个字节的数  二进制表示:0111 1010
     bytes2 public num2 = 0x7a68;// 有两个字节的数 二进制表示:0111 1010 0110 1000
     
     function getLength() public returns(uint256) {
          return num1.length;// length属性不可修改
     }
     
     function getLength2() public returns(uint256) {
          return num2.length;// length属性不可修改
     }
 }
  1. Although the byte array is the same as int in storage, it cannot directly perform addition, subtraction, multiplication, and division operations. But you can compare numbers (>,>=,==,<=,<), and you can also perform bitwise operations (&, |, ~, ^, <<, >>).
  2. When the attribute is added as public, a get method to obtain the attribute will be generated by default.
  3. Add the following code to verify the above statement, f respectively calculate the value of &, |, ~, ^, <<, >> after bit operation, and return.
 function and() public returns(bytes2 n1,bytes2 n2,bytes2 n3, bytes2 n4, bytes2 n5,bytes2 n6){
          return (num1&num2,num1|num2,~num1,num1^num2,num1<<1,num1>>1);
      }

Get the result: the
Insert picture description here
result is correct.

4. Dynamic byte array

Above code:

 pragma solidity ^0.4.0;
 
 contract BytesArray {
     
     bytes public name = new bytes(2);
     
     function initName() public {
         name[0] = 0x7a;
         name[1] = 0x68;
     }
     
     function getLength() public returns(uint256){
         return name.length;
     }
     
     function changeLength() public {
         name.length = 5;
     }
 }
  1. After running initName, check the value of the name attribute: the
    Insert picture description here
    result is no problem

  2. Run the getLength() method to view the length of the bytes array:

Insert picture description here
The length is 2, the result is correct

  1. Run the changeLength() method to modify the length of the bytes array: the
    Insert picture description here
    Insert picture description here
    length becomes 5

Summary: Dynamic byte arrays can modify the length of the array.

  1. name.push(0x99) adds an element of 0x99 to the name dynamic byte array.

4.string type

  1. The string operation is basically the same as that of java. The string balance bytes can be forcibly converted. This feature can be used to access the value at a specific position in the string.
    Code test
pragma solidity ^0.4.0;

contract StringTest {
    string public name = "zhangsan";
    
    function getLength() public returns(uint256){
        return bytes(name).length;
    }
    
    function changeName() public {
        bytes(name)[0] = 'L';
    }
    
    function getCName() public returns(bytes1){
        return bytes(name)[0];
    }
}
  1. Get name:
    Insert picture description here
  2. Get the length of 8 bytes:
    Insert picture description here
  3. Call getCName() to get the first letter:
    Insert picture description here
    6. Call changeName to modify the first letter and then get the name:
    Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51194902/article/details/112266013