Solidity:0.8新特性

1. 安全數(shù)學(xué)(safe math)

在之前的版本中沒有安全數(shù)學(xué),數(shù)值計算式有溢出的,在Solidity0.8版本中引入了安全數(shù)學(xué)新特性。

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;


// safe math
contract SafeMath {
    // 默認(rèn)的帶有安全數(shù)學(xué)檢驗的方法
    function testUnderflow() public pure returns (uint) {
        uint x = 0;  // revert
        x--;
        return x;
    }

    // 不帶安全數(shù)學(xué)檢驗的方法
    function testUncheckedUnderflow() public pure returns (uint) {
        uint x = 0;
        unchecked {x--;} // 不使用安全數(shù)學(xué),得到uint256最大值
        return x;
    }
}

2. 自定義錯誤(custom errors)

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8;

contract VendingMachine {
    address payable owner = payable(msg.sender);
    // 自定義錯誤
    error Unauthorized(address caller);

    function withdraw() public {
        if (msg.sender != owner){
            // revert("error");
            // 使用自定義錯誤
            revert Unauthorized(msg.sender); //23613
        }
        owner.transfer(address(this).balance);
    }

}

revert信息:

Error provided by the contract:
Unauthorized
Parameters:
{
 "caller": {
  "value": "0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2"
 }
}

3. 合約外函數(shù) (fuctions outside contract)

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8;


// 在合約外定義函數(shù)
function helper(uint _x) pure  returns (uint) {
    return _x * 2;
}

contract Testhelper {
    // 在合約內(nèi)調(diào)用合約外函數(shù)
    function test(uint _x) external pure returns (uint) {
        return helper(_x);
    }
}

4. 導(dǎo)入別名

import {symbol as alias} from "filename";

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8;

import {helper as helper2} from "./1.3 function_out_contract.sol";


function helper(uint _x) pure returns (uint) {
    return _x * 3;
}

contract Import {
    function double(uint _x) external  pure returns (uint) {
        // 使用別名,調(diào)用引入的helper
        return helper2(_x);
    }
    function triple(uint _x) external  pure returns (uint) {
        // 調(diào)用自己的helper
        return helper(_x);
    }
}

5. 加鹽合約創(chuàng)建(salted contract creations):create2

  • 在之前的create合約創(chuàng)建方法中,合約地址是通過msg.sendernounce進(jìn)行計算的,無法提前得到部署后合約的地址。
  • 新添加的create2合約創(chuàng)建方法,合約地址通過鹽(salt)進(jìn)行計算,這樣就可以提前獲得部署后的合約地址。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

contract D {
    uint public x;
    constructor(uint a) {
        x = a;
    }
}

contract create2 {
    function getBytes32(uint _salt) external pure returns (bytes32) {
        return bytes32(_salt);
    }

    function getAddress(bytes32 salt, uint arg) external view returns (address) {
        address addr = address(uint160(uint(keccak256((abi.encodePacked(
            bytes1(0xff),
            address(this),
            salt,
            keccak256(abi.encodePacked(
                type(D).creationCode,
                arg
            ))
        ))))));
        return addr;
    }

    address public deployedAddr;

    function createdSalted(bytes32 salt,uint arg) public {
        D d = new D{salt:salt}(arg);
        deployedAddr = address(d);
    }
}
  1. 調(diào)用getBytes32方法獲取鹽的bytes32格式值。

輸入:111
輸出:0x000000000000000000000000000000000000000000000000000000000000006f

  1. 調(diào)用getAddress方法獲取部署后的合約地址。

輸入:0x000000000000000000000000000000000000000000000000000000000000006f,222
輸出:0x4443e89035aeED9Cf521a0341aF3B72e8b80C038

  1. 調(diào)用createdSalted方法部署D合約。

輸入:0x000000000000000000000000000000000000000000000000000000000000006f,222

  1. 查詢deployedAddr。

輸出:0x4443e89035aeED9Cf521a0341aF3B72e8b80C038
輸出結(jié)果和提前獲得的合約地址相同。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容