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

边栏推荐
- 6月刊 | AntDB数据库参与编写《数据库发展研究报告》 亮相信创产业榜单
- Solution of digital supply chain centralized purchase platform in mechanical equipment industry: optimize resource allocation and realize cost reduction and efficiency increase
- Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
- How to use the low code platform of the Internet of things for personal settings?
- Lake Shore continuous flow cryostat transmission line
- 【6.24-7.1】写作社区精彩技术博文回顾
- How to operate technology related we media well?
- 微服务大行其道的今天,Service Mesh是怎样一种存在?
- 白盒加密技术浅理解
- 11. Users, groups, and permissions (1)
猜你喜欢

pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】

Lumiprobe phosphide hexaethylene phosphide specification

苹果产品在日本全面涨价,iPhone13涨19%

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

Lake Shore - crx-em-hf low temperature probe station

Clean up system cache and free memory under Linux

见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约

一次SQL优化,数据库查询速度提升 60 倍

Today, with the popularity of micro services, how does service mesh exist?
![[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](/img/5c/b0030fd5fbc07eb94013f2699c2a04.png)
[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
随机推荐
MySQL common graphics management tools | dark horse programmers
VBA simple macro programming of Excel
Summary of cases of players' disconnection and reconnection in Huawei online battle service
【直播预约】数据库OBCP认证全面升级公开课
使用环信提供的uni-app Demo,快速实现一对一单聊
How to redraw the header of CListCtrl in MFC
Go语言高级
水产行业智能供应链管理平台解决方案:支撑企业供应链数字化,提升企业管理效益
Graduation summary
Write it down once Net travel management background CPU Explosion Analysis
赋能「新型中国企业」,SAP Process Automation 落地中国
pickle.load报错【AttributeError: Can‘t get attribute ‘Vocabulary‘ on <module ‘__main__‘】
小红书上的爱情买卖
见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
论文阅读【Discriminative Latent Semantic Graph for Video Captioning】
Chaos engineering platform chaosblade box new heavy release
白盒加密技术浅理解
241. Different Ways to Add Parentheses
Yyds dry inventory ravendb start client API (III)
Solution: you can ping others, but others can't ping me