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

边栏推荐
- Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?
- Lumiprobe cell imaging study PKH26 cell membrane labeling kit
- transform + asm资料
- 白盒加密技术浅理解
- Lake Shore低温恒温器的氦气传输线
- sql查询去重统计的方法总结
- indexof和includes的区别
- Lake Shore - crx-em-hf low temperature probe station
- 混沌工程平台 ChaosBlade-Box 新版重磅发布
- 【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
猜你喜欢

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

MySQL common graphics management tools | dark horse programmers

XML语法、约束

Openai video pre training (VPT): action learning based on watching unmarked online videos
使用环信提供的uni-app Demo,快速实现一对一单聊

【森城市】GIS数据漫谈(一)

Chinese and English instructions human soluble advanced glycation end products receptor (sRAGE) ELISA Kit

生鲜行业B2B电商平台解决方案,提高企业交易流程标准化和透明度

论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】

宝,运维100+服务器很头疼怎么办?用行云管家!
随机推荐
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
[6.24-7.1] review of wonderful technical blog posts in the writing community
【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
More information about M91 fast hall measuring instrument
市值蒸发740亿,这位大佬转身杀入预制菜
一次SQL优化,数据库查询速度提升 60 倍
Go语言高级
MFC中如何重绘CListCtrl的表头
Graduation summary
The market value evaporated by 74billion yuan, and the big man turned and entered the prefabricated vegetables
nacos启动失败问题解决与总结
Superoptimag superconducting magnet system - SOM, Som2 series
智慧防疫系统为建筑工地复工复产提供安全保障
中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
Lake shore optimag superconducting magnet system om series
Specification of lumiprobe reactive dye indocyanine green
Stanford, salesforce|maskvit: masked vision pre training for video prediction
Clean up system cache and free memory under Linux
Getting started with kubernetes command (namespaces, pods)
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency