1.如果compile 0.4.23,找不到对应的版本,可以先选auto compile。

2.callData就是console里面的input数据

3、消耗gas多的代码:
3.1.assert() 多、
require和revert少
assert(condition)
revert(string reason)
require(condition, string reason)
require(i<10, "非常长的报错信息提示浪费gas")
所以优化用error(0.8^)
error MyError(address caller, uint i);
function testCustomError(uint _i) public view {
if(_i >10) {
revert MyError(msg.sender, _i);
}
}
3.2.storage状态变量修改
3.3.while、for循环越多
3.4.函数mutability的pure、view消耗少
4.函数returns中,可以返回reference type类型嘛?
可以return (value1, value2, value3)

5.节约gas技巧
https://www.bilibili.com/video/BV1Pi4y1U7Cu/?spm_id_from=333.788
.start - 50908 gas
1.use calldata - 49163 gas
2.load state variables to memory - 48952 gas // 函数声明memory引用类型,代替状态storage
3.short circuit - 48634 gas // &&
4.loop increments - 48226 gas // ++i
5.cache array length - 48191 gas // 缓存数组长度
6.load array elements to memory - 48029 gas // for中多次用到的 num[i],缓存起来

本文介绍了如何通过避免assert和revert,减少存储修改,优化循环结构,选择view函数以及利用内存存储等技巧,来降低以太坊智能合约的gas消耗。还推荐了查看节省Gas的具体实例教程。
4947

被折叠的 条评论
为什么被折叠?



