当前位置:网站首页>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,执行函数会直接先提示(如下图所示),若强制进行交易,则与上面结果相同。

边栏推荐
- [quick application] there are many words in the text component. How to solve the problem that the div style next to it will be stretched
- 【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
- Specification of lumiprobe reactive dye indocyanine green
- The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
- How to operate technology related we media well?
- Yyds dry inventory ravendb start client API (III)
- Prices of Apple products rose across the board in Japan, with iphone13 up 19%
- Lean thinking: source, pillar, landing. I understand it after reading this article
- 助力数字经济发展,夯实数字人才底座—数字人才大赛在昆成功举办
- kubernetes命令入门(namespaces,pods)
猜你喜欢

6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单

Viewing technological changes through Huawei Corps (VI): smart highway

Lake Shore M91快速霍尔测量仪

2. Create your own NFT collections and publish a Web3 application to show them start and run your local environment

Dlib+Opencv库实现疲劳检测

Huawei cloud experts explain the new features of gaussdb (for MySQL)

docker 部署mysql8.0

Docker deploy mysql8.0

Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration

华为云专家详解GaussDB(for MySQL)新特性
随机推荐
kubernetes命令入门(namespaces,pods)
1. "Create your own NFT collections and publish a Web3 application to show them." what is NFT
实现一个Prometheus exporter
PriorityQueue的用法和底层实现原理
The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables
js找出数字在数组中下一个相邻的元素
Lefse analysis
How to realize the bottom layer of read-write lock in go question bank 16
Viewing the whole ecology of Tiktok from a macro perspective
More information about M91 fast hall measuring instrument
有关 M91 快速霍尔测量仪的更多信息
Netease games, radical going to sea
Go语言自学系列 | go语言数据类型
机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效
记一次 .NET 差旅管理后台 CPU 爆高分析
Li Kou daily question - Day 32 -589 N × Preorder traversal of tree
PostgreSQL varchar[] 数组类型操作
Boost the development of digital economy and consolidate the base of digital talents - the digital talent competition was successfully held in Kunming
C端梦难做,科大讯飞靠什么撑起10亿用户目标?
一次SQL优化,数据库查询速度提升 60 倍