ESSAY / NOTE
计算以太坊中 发送交易/调用合约方法 需要消耗多少gas
准备工作
1、三个账户,eth.account[0]为默认账户,挖矿所得的奖励都会进入到这个账户> eth.getBalance(eth.accounts[0])
736031150000000000000
> eth.getBalance(eth.accounts[1])
500050000000000000
> eth.getBalance(eth.accounts[2])
500050000000000000
普通交易所需的gas
> eth.estimateGas({from:eth.accounts[1], to: eth.accounts[2], value:50000000000000})
21001
> eth.gasPrice
20000000000
> eth.sendTransaction({from:eth.accounts[1], to: eth.accounts[2], value:50000000000000})
I0318 00:24:21.360815 internal/ethapi/api.go:1143] Tx(0x33b58084a35e99245b9c931204a0d161b9d00f9fae5ffb307aff29f200e5cd30) to: 0x49fbd70ca9f90972806c375a111d08950d203f96
"0x33b58084a35e99245b9c931204a0d161b9d00f9fae5ffb307aff29f200e5cd30"
> eth.getBalance(eth.accounts[1])
499580000000000000
> eth.getBalance(eth.accounts[2])
500100000000000000
(500050000000000000 - 499580000000000000) - (500100000000000000 - 500050000000000000) = 21001 * 20000000000
> eth.getTransactionReceipt("0x33b58084a35e99245b9c931204a0d161b9d00f9fae5ffb307aff29f200e5cd30")
{
blockHash: "0x8e411163367bc42a70ecc230d05dd2038afe0dccfab29c8a718a57bdbea0b2fa",
blockNumber: 134,
contractAddress: null,
cumulativeGasUsed: 21000,
from: "0x27c649b7c4f66cfaedb99d6b38527db4deda6f41",
gasUsed: 21000,
logs: [],
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
root: "0x2008f134f3328e48d4d05919666a5924767b00b286cf1ff27b7956654d5b6482",
to: "0x49fbd70ca9f90972806c375a111d08950d203f96",
transactionHash: "0x33b58084a35e99245b9c931204a0d161b9d00f9fae5ffb307aff29f200e5cd30",
transactionIndex: 0
}
调用合约方法所需要的gas
普通的转账交易所需要的gas是固定的21000,但是调用合约方法所需要的gas并不一定,总结来说占用的资源(计算量、内存等)越多,那么所需要的gas也就越多。先准备一个最简单的合约 pragma solidity ^0.4.8;
contract Test {
uint public num;
function setNum(uint newNum) {
num = newNum;
}
}
> eth.getBalance(eth.accounts[1])
499580000000000000
> eth.getBalance(eth.accounts[2])
500100000000000000
> testInstance.setNum.estimateGas(4, {from: eth.accounts[1]})
41645
> testInstance.setNum.sendTransaction(4, {from: eth.accounts[1]})
I0318 07:21:31.344279 internal/ethapi/api.go:1143] Tx(0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab) to: 0x03a4fb357f8c38694ab536d09003076033442f9e
"0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab"
> eth.getTransactionReceipt('0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab')
{
blockHash: "0x494f5f6fc0c156f105ffe3e4e1aa886c60f916a5998d44a03916b3f2cc733b8a",
blockNumber: 139,
contractAddress: null,
cumulativeGasUsed: 41644,
from: "0x27c649b7c4f66cfaedb99d6b38527db4deda6f41",
gasUsed: 41644,
logs: [],
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
root: "0x857063e074cc3195ee2f3962438f3f6c31a759cfae461448e8726a5fa069d1ae",
to: "0x03a4fb357f8c38694ab536d09003076033442f9e",
transactionHash: "0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab",
transactionIndex: 0
}