01-Solidity8.0新特性

本文介绍了Solidity 8.0的主要新特性,包括安全数学运算、自定义错误处理、函数外部调用、引入文件别名以及Create2部署方式。这些改进旨在提高智能合约的安全性和开发效率,为区块链开发者提供了更强大的工具和更好的编程体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Solidity8.0

01-Solidity8.0新特性


在这里插入图片描述


前言

区块链越来越吃香了,开始做笔记


一、Solidity是什么?

在区块链上运行的一种脚本语言!

二、新特性

1.安全数学

代码如下(示例):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

contract SafeMath{
    function Underflow() public pure returns (uint){
        uint x =0;
        x--;
        return x;
    }
    function uncheckedUnderflow() public pure returns (uint){
        uint x =0;
        unchecked{x--;}
        return x;
    }
}

2.自定义错误

代码如下(示例):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

error Unauthorized(address caller);

contract customer {
   address payable owner = payable(msg.sender);

   function withdraw() public {
       if (msg.sender != owner){
           revert Unauthorized(msg.sender);
       }
       owner.transfer(address(this).balance);
   }
}

3.函数在合约之外使用

代码如下(示例):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

function add(uint256 a) pure returns (uint256) {
    return a + 7;
}

contract customer {
   function test() external view returns(uint256){
       return add(8);
   }
}

4.引入文件更改别名

代码如下(示例):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

import {test, helper as help} from "./Common.sol";

5.Create2

代码如下(示例):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

contract TestContract {
    address public owner;
    uint public foo;

    constructor(address _owner, uint _foo) payable {
        owner = _owner;
        foo = _foo;
    }

    function getBalance() public view returns (uint) {
        return address(this).balance;
    }
}
contract Factory {
    function deploy2(address _owner,uint _foo,bytes32 _salt) public payable returns (address) {
        return address(new TestContract{salt: _salt}(_owner, _foo));
    }
    
	event Deployed(address addr, uint salt);
    function getBytecode(address _owner, uint _foo) public pure returns (bytes memory) {
        bytes memory bytecode = type(TestContract).creationCode;
        return abi.encodePacked(bytecode, abi.encode(_owner, _foo));
    }
    
    function getAddress(bytes memory bytecode, uint _salt) public view returns (address) {
        bytes32 hash = keccak256(
            abi.encodePacked(bytes1(0xff), address(this), _salt, keccak256(bytecode))
        );
        return address(uint160(uint(hash)));
    }
    
    function deploy(bytes memory bytecode, uint _salt) public payable {
        address addr;
        assembly {
            addr := create2( callvalue(), add(bytecode, 0x20), mload(bytecode), _salt )
            if iszero(extcodesize(addr)) {
                revert(0, 0)
            }
        }
        emit Deployed(addr, _salt);
    }
}

总结

日拱一卒。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值