加油!区块链编程语言Solidity学习(一)

599 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

和我一起卷起来!

做中国的V神!

Immutable(只可以再构造函数里修改)

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.13;

contract Immutable {

   // coding convention to uppercase constant variables

   address public immutable MY_ADDRESS;

   uint public immutable MY_UINT;

    constructor(uint _myUint) {

       MY_ADDRESS = msg.sender;

       MY_UINT = _myUint;

   }

}

Ether and wei

Similar to how one dollar is equal to 100 cent, one ether is equal to 1018 wei.

1 wei is equal to 1

删除map中的值:用键删

function remove(address _addr) public { // Reset the value to the default value.

delete myMap[_addr]; }//这里是将那个索引处的值设置为默认值,不是把那个值删除!

二维度数组

mapping(address => mapping(uint => bool)) public nested;

指定长度或者是动态长度

uint[] public arr;

uint[] public arr2 = [1, 2, 3];

// Fixed sized array, all elements initialize to 0

uint[10] public myFixedSizeArr;

arr.push()/pop()/length

\


function examples() external {

// create array in memory, only fixed size can be created

uint[] memory a = new uint[](5);

}

\

数组右移动删除法

contract ArrayRemoveByShifting {

 // [1, 2, 3] -- remove(1) --> [1, 3, 3] --> [1, 3] 

// [1, 2, 3, 4, 5, 6] -- remove(2) --> [1, 2, 4, 5, 6, 6] --> [1, 2, 4, 5, 6] 

// [1, 2, 3, 4, 5, 6] -- remove(0) --> [2, 3, 4, 5, 6, 6] --> [2, 3, 4, 5, 6] 

// [1] -- remove(0) --> [1] --> [] 

uint[] public arr;

 function remove(uint _index) public { 

require(_index < arr.length, "index out of bound");

 for (uint i = _index

i < arr.length - 1; i++) 

{ arr[i] = arr[i + 1];

 } 

arr.pop();

 }

\

构造函数的两种构造方法:

todos.push(Todo(_text, false)); 

// key value mapping

todos.push(Todo({text: _text, completed: false}));

错误处理

Image.png

import的使用方法

// import {symbol1 as alias, symbol2} from "filename"import {Unauthorized, add as func, Point} from "./Foo.sol";//从一个地方分别抽取不同的方法

import http://...

ABI

adi其实就是函数选择器加上参数,转换为16进制编码 Image.png

Hashing with Keccak256(这个我不是很懂)

keccak256 computes the Keccak-256 hash of the input.

Some use cases are:

-   Creating a deterministic unique ID from a input
-   Commit-Reveal scheme
-   Compact cryptographic signature (by signing the hash instead of a larger input)
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.13;
contract HashFunction {

    function hash(

        string memory _text,

        uint _num,

        address _addr

    ) public pure returns (bytes32) {

        return keccak256(abi.encodePacked(_text, _num, _addr));

    }
    // Example of hash collision

    // Hash collision can occur when you pass more than one dynamic data type

    // to abi.encodePacked. In such case, you should use abi.encode instead.

    function collision(string memory _text, string memory _anotherText)

        public

        pure

        returns (bytes32)

    {

        // encodePacked(AAA, BBB) -> AAABBB

        // encodePacked(AA, ABBB) -> AAABBB

        return keccak256(abi.encodePacked(_text, _anotherText));

    }

}
contract GuessTheMagicWord {

    bytes32 public answer =

        0x60298f78cc0b47170ba79c10aa3851d7648bd96f2f8e46a19dbc777c36fb0c00;

    // Magic word is "Solidity"

    function guess(string memory _word) public view returns (bool) {

        return keccak256(abi.encodePacked(_word)) == answer;

    }
}

链下签名:solidity-by-example.org/signature/

这个搞不大来👆