当前位置:网站首页>Solidity - contract structure - error - ^0.8.4 NEW
Solidity - contract structure - error - ^0.8.4 NEW
2022-07-01 19:25:00 【ling1998】
We know , The contract structure includes State variables 、 function 、 Function modifier (modifier)、 event (event)、 Structure (struct) And enumeration types (enum).
stay ^0.8.4 edition , The contract structure has increased error (error), In response to failure , Mistakes can be made in revert Use in . Compared with the error string ,error It costs less gas( That is, cheaper ), And allow encoding of additional data , You can also use natspec Annotation form .
Take a chestnut , The code is as follows :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
/// @dev Not enough money
/// @param _balance Indicates the current contract account balance
/// @param _amount Transfer amount
error NotEnoughFunds(uint _balance,uint _amount);
contract TestError {
/// @dev event - Record transfer information
event Tansfer(address _from, address _to, uint _amount);
/// @dev When the balance of the contract account is less than the transfer amount , Use string revert
function transfer(address _to, uint _amount) external {
if (address(this).balance < _amount) {
revert("no enough funds");
}
emit Tansfer(msg.sender, _to, _amount);
}
/// @dev When the balance of the contract account is less than the transfer amount , Use 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 Same as struct equally , It can be written in the contract , It can also be written outside the contract , The above code is written outside the contract . At the same time natspec Annotation form .
Use Remix in JavaScript VM Deployment contract , Execute two functions in the contract ( A use error structure , The other uses the wrong string ), Cost when validation fails gas:
(1) perform transfer function ( Use the wrong string )
After execution, you can see the error message prompt “no enough funds”, Spent gas by 22513
(2) perform transferWithError function ( Use error structure )
After execution, you can see the parameter value output in the error , Feeling and front-end console.log equally , It can be used as debugging information , Spent gas by 22497, Less than above 16 individual gas
If deployed to the test network , Such as Popsten, Executing the function will directly prompt ( As shown in the figure below ), If the transaction is forced , The result is the same as the above .
边栏推荐
- 实现一个Prometheus exporter
- web开发常用的开源框架的开源协议整理
- CDGA|从事通信行业,那你应该考个数据管理证书
- The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
- More information about M91 fast hall measuring instrument
- XML语法、约束
- Openai video pre training (VPT): action learning based on watching unmarked online videos
- 物联网平台thingsboard搭建学习记录
- 市值蒸发740亿,这位大佬转身杀入预制菜
- 案例分享:QinQ基本组网配置
猜你喜欢
Games202 operation 0 - environment building process & solving problems encountered
Helium transmission line of lake shore cryostat
论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables
Today, with the popularity of micro services, how does service mesh exist?
How to use the low code platform of the Internet of things for personal settings?
Improve yolov5 with gsconv+slim neck to maximize performance!
论文阅读【Discriminative Latent Semantic Graph for Video Captioning】
Lake Shore低温恒温器的氦气传输线
华为游戏初始化init失败,返回错误码907135000
随机推荐
Solution: you can ping others, but others can't ping me
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
How to operate technology related we media well?
Solidity - 算术运算的截断模式(unchecked)与检查模式(checked)- 0.8.0新特性
【快应用】Win7系统使用华为IDE无法运行和调试项目
Netease games, radical going to sea
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
MySQL common graphics management tools | dark horse programmers
SuperOptiMag 超导磁体系统 — SOM、SOM2 系列
Huawei cloud experts explain the new features of gaussdb (for MySQL)
Love business in Little Red Book
MySQL常用图形管理工具 | 黑马程序员
sql查询去重统计的方法总结
XML语法、约束
Cache problems after app release
网易游戏,激进出海
Bao, que se passe - t - il si le serveur 100 + O & M a mal à la tête? Utilisez le majordome xingyun!
Stanford, salesforce|maskvit: masked vision pre training for video prediction
Cdga | if you are engaged in the communication industry, you should get a data management certificate
Lake shore M91 fast hall measuring instrument