怎么更方便的测试自己程序的gas消耗?

写些程序来测试我们的操作将消耗多少gas费用。

本地调试怎么计算gas消费?


yarn add -D hardhat-gas-reporter

import "hardhat-gas-reporter"

const config: Config = {
  ...,
  networks: {
    hardhat: {
      chainId: 31337
    },
    ...
  },
  gasReporter: {
    enabled: true
  }
};

测试操作gas消耗


pragma solidity ^0.8.24

contract TestGas {
	uint a;
	uint b;
	uint c;
	
	function test1() public {
		a++;
	}

	function test2() public {
		a++;
		b++;
	}
	
	function test3() public {
		a++;
		b++;
		c++;
	}
	
	function test4() public {
		c = a + b;
	}
	
	function test5() public {
		test4();
		b = a + c;
	}
	
}

Test Gas