当前位置:网站首页>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 .

边栏推荐
- Transform + ASM data
- How to redraw the header of CListCtrl in MFC
- 华为游戏初始化init失败,返回错误码907135000
- Altair HyperWorks 2022 software installation package and installation tutorial
- 【Go ~ 0到1 】 第四天 6月30 defer,结构体,方法
- 华为联机对战服务玩家掉线重连案例总结
- 【pytorch记录】自动混合精度训练 torch.cuda.amp
- Specification of lumiprobe reactive dye indocyanine green
- 6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
- 【To .NET】C#集合类源码解析
猜你喜欢

Summary of the core steps in the life cycle of beans

华为游戏初始化init失败,返回错误码907135000

Lake Shore—CRX-EM-HF 型低温探针台

Lumiprobe 自由基分析丨H2DCFDA说明书

Altair HyperWorks 2022 software installation package and installation tutorial

More information about M91 fast hall measuring instrument

【直播预约】数据库OBCP认证全面升级公开课

Example explanation: move graph explorer to jupyterlab

Lumiprobe 活性染料丨吲哚菁绿说明书

B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
随机推荐
The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables
云服务器ECS夏日省钱秘籍,这次@老用户快来领走
Implement a Prometheus exporter
宝,运维100+服务器很头疼怎么办?用行云管家!
论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
VBA simple macro programming of Excel
Taiaisu M source code construction, peak store app premium consignment source code sharing
Boost the development of digital economy and consolidate the base of digital talents - the digital talent competition was successfully held in Kunming
The former 4A executives engaged in agent operation and won an IPO
6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
Dom4J解析XML、Xpath检索XML
Docker deploy mysql8.0
Go language self-study series | go language data type
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
Stanford, salesforce|maskvit: masked vision pre training for video prediction
组队学习! 14天鸿蒙设备开发“学练考”实战营限时免费加入!
Lake Shore continuous flow cryostat transmission line
MFC中如何重绘CListCtrl的表头
Mipi interface, DVP interface and CSI interface of camera [easy to understand]
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决