Day02 実行ログ(2026-01 更新)
環境
- RPC:
http://127.0.0.1:8545(ローカル Hardhat node) - サンプルコントラクト:
contracts/GasTest.sol - ドライバースクリプト:
scripts/day02_gas.ts
実行手順
./node_modules/.bin/hardhat nodenpx hardhat compilenpx hardhat run scripts/day02_gas.ts --network localhostcurl -X POST ... eth_getTransactionReceiptでstoreTx を解析
Gas 計測結果
| 呼び出し | Tx Hash | Gas Used | Effective Gas Price (wei) | 手数料 (ETH) |
|———-|———|———|—————————|————–|
| store(123) | 0xba728419... | 43,516 | 970,068,939 | 4.221e-05 |
| add(10,20) | 0xcd44a299... | 21,860 | 849,162,102 | 1.856e-05 |
| testRewrite(123) | 0x5306f1d4... | 23,638 | 743,171,529 | 1.757e-05 |
add()は pure 関数のため、そのまま呼ぶとcallになってしまう。encodeFunctionDataで calldata を生成し、sendTransactionで Tx を作成してGasを取得。testRewrite()は同じ値を再書込みした結果、store()よりガスが少なくなり、Refund を確認できた。
RPC での Tx 解析例
curl -s -X POST http://127.0.0.1:8545 \
-H 'Content-Type: application/json' \
--data '{
"jsonrpc":"2.0",
"method":"eth_getTransactionReceipt",
"params":["0xba7284193ee601b6787921d03a78bf8ce1e1fe35359447754cbe1872750970be"],
"id":10
}' | jq '.result | {blockNumber, gasUsed, effectiveGasPrice, status}'
→ gasUsed: 0xa9fc, effectiveGasPrice: 0x39d213cb, status: 0x1
学び
pure/view関数であっても raw Tx を組めばガス計測が可能。SSTOREの初回書込みは高コストだが、同値再書込みは Refund が発生しガスが下がる。- RPC から取得した
gasUsed/effectiveGasPriceを 16進→10進変換すれば、手数料 (ETH) を精度高く算出できる。