当前位置:网站首页>Solidity - 合约结构 - 错误(error)- ^0.8.4版本新增
Solidity - 合约结构 - 错误(error)- ^0.8.4版本新增
2022-07-01 18:43:00 【ling1998】
我们知道,合约结构包含 状态变量、函数、函数修改器(modifier)、事件(event)、结构体(struct)和枚举类型(enum)。
在^0.8.4版本,合约结构增加了错误(error),为应对失败时,错误可以在revert 中使用。与错误字符串相比,error花费更少的gas(即更便宜),并且允许编码额外的数据,还可以使用natspec注释形式。
举个栗子,代码如下:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @dev 没有足够的金额
/// @param _balance表示当前合约账户余额
/// @param _amount 转账金额
error NotEnoughFunds(uint _balance,uint _amount);
contract TestError {
/// @dev 事件 - 记录转账信息
event Tansfer(address _from, address _to, uint _amount);
/// @dev 当合约账户余额小于转账金额时,使用字符串 revert
function transfer(address _to, uint _amount) external {
if (address(this).balance < _amount) {
revert("no enough funds");
}
emit Tansfer(msg.sender, _to, _amount);
}
/// @dev 当合约账户余额小于转账金额时,使用error revert
function transferWithError(address _to, uint _amount) external {
if (address(this).balance < _amount) {
revert NotEnoughFunds(address(this).balance, _amount);
}
emit Tansfer(msg.sender, _to, _amount);
}
}error同struct一样,可以写在合约里,也可以写在合约外面,以上代码写在了合约外面。同时使用了natspec注释形式。
使用Remix中JavaScript VM部署合约,执行合约中两个函数(一个使用error结构,另一个使用错误字符串),验证失败时花费的gas:
(1)执行transfer函数(使用错误字符串)
执行后可以看到错误信息提示“no enough funds”,花费的gas为22513

(2)执行transferWithError函数(使用error结构)
执行后可以看到错误中输出的参数值,感觉与前端的console.log一样,可以当作调试信息,花费的gas为22497,比上面少了16个gas

若部署到测试网,如 Popsten,执行函数会直接先提示(如下图所示),若强制进行交易,则与上面结果相同。

边栏推荐
- Lake Shore—CRX-EM-HF 型低温探针台
- Games202 operation 0 - environment building process & solving problems encountered
- AI training speed breaks Moore's law; Song shuran's team won the RSS 2022 Best Paper Award
- [live broadcast appointment] database obcp certification comprehensive upgrade open class
- Huawei game failed to initialize init with error code 907135000
- linux下清理系统缓存并释放内存
- Docker deploy mysql8.0
- js找出数字在数组中下一个相邻的元素
- SuperVariMag 超导磁体系统 — SVM 系列
- Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
猜你喜欢

Li Kou daily question - Day 32 -589 N × Preorder traversal of tree

Openai video pre training (VPT): action learning based on watching unmarked online videos

June issue | antdb database participated in the preparation of the "Database Development Research Report" and appeared on the list of information technology and entrepreneurship industries

Docker deploy mysql8.0

Lumiprobe phosphide hexaethylene phosphide specification

Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference

混沌工程平台 ChaosBlade-Box 新版重磅发布

Today, with the popularity of micro services, how does service mesh exist?

CDGA|从事通信行业,那你应该考个数据管理证书

Leetcode-160 intersecting linked list
随机推荐
Bao, que se passe - t - il si le serveur 100 + O & M a mal à la tête? Utilisez le majordome xingyun!
学习笔记-JDBC连接数据库操作的步骤
Leetcode-21 combines two ordered linked lists
Viewing the whole ecology of Tiktok from a macro perspective
Graduation summary
Getting started with kubernetes command (namespaces, pods)
从零开始学 MySQL —数据库和数据表操作
Docker deploy mysql8.0
Qfile read / write file operation in QT
AppGallery Connect场景化开发实战—图片存储分享
Cdga | if you are engaged in the communication industry, you should get a data management certificate
Lake Shore—CRX-EM-HF 型低温探针台
[quick application] win7 system cannot run and debug projects using Huawei ide
Go language self-study series | go language data type
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
ES6数组方法find()、findIndex()的总结「建议收藏」
毕业季 | 华为专家亲授面试秘诀:如何拿到大厂高薪offer?
How to use the low code platform of the Internet of things for personal settings?
前4A高管搞代运营,拿下一个IPO
【pytorch记录】自动混合精度训练 torch.cuda.amp