Ethereum tokens Development Calculation How much gas is required to invoke a contract method

Source: Internet
Author: User
Blockchain Enthusiast (qq:53016353)
Preparatory work
1, three accounts, Eth.account[0] is the default account, the mining proceeds will be entered into this account


> eth.getbalance (eth.accounts[0])
736031150000000000000
> eth.getbalance (eth.accounts[1])
500050000000000000
> eth.getbalance (eth.accounts[2])
500050000000000000
Gas required for ordinary exchanges
> Eth.estimategas ({from:eth.accounts[1], to:eth.accounts[2], value:50000000000000})
21001
> Eth.gasprice
20000000000
As above, show this account[1] = account[2] Trading needs 21001 gas, the current gasprice is 20000000000, the following to verify
Unlock account 1, send the deal, and open the mining package


> 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"
After the deal is packaged


> eth.getbalance (eth.accounts[1])
499580000000000000
> eth.getbalance (eth.accounts[2])
500100000000000000
by cost = gas * Gasprice, (account 1 reduced assets-account 2 increased assets)/Gasprice = gas consumed, i.e. the following formula should be established


(500050000000000000-499580000000000000)-(500100000000000000-500050000000000000) = 21001 * 20000000000
However, the careful classmate should find, this formula does not succeed, 21001 this number how to see how awkward, if minus 1 this formula succeeds ... Go on
View the details of this transaction


> Eth.gettransactionreceipt ("0x33b58084a35e99245b9c931204a0d161b9d00f9fae5ffb307aff29f200e5cd30")
{
Blockhash: "0x8e411163367bc42a70ecc230d05dd2038afe0dccfab29c8a718a57bdbea0b2fa",
blocknumber:134,
Contractaddress:null,
cumulativegasused:21000,
From: "0x27c649b7c4f66cfaedb99d6b38527db4deda6f41",
gasused:21000,
Logs: [],
Logsbloom: " 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000 ",
Root: "0x2008f134f3328e48d4d05919666a5924767b00b286cf1ff27b7956654d5b6482",
To: "0x49fbd70ca9f90972806c375a111d08950d203f96",
Transactionhash: "0x33b58084a35e99245b9c931204a0d161b9d00f9fae5ffb307aff29f200e5cd30",
transactionindex:0
}
gasused:21000 that's the right thing to do, so why Eth.estimategas () calculates 1 more results. This is for a reason, if the calculated value and gasused equal, then the transaction may be a failure, but if the gasused is less than the calculated value, then you can tell that the transaction is successful






The gas required to invoke the Contract method


The gas required for a regular transfer exchange is 21000 fixed, but the gas required to invoke the contract method is not necessarily, the more resources (computational, memory, etc.) that are consumed in the summary, the more gas is required. Prepare a simple contract first.


pragma solidity ^0.4.8;
Contract Test {
UINT public num;

function Setnum (uint newnum) {
num = Newnum;
}
}
Deployed to the private chain, the process is no longer demonstrated, and the last contract instance is testinstance. Take a look at the asset information of Eth.accounts[1] and eth.accounts[2], which is calculated later


> eth.getbalance (eth.accounts[1])
499580000000000000
> eth.getbalance (eth.accounts[2])
500100000000000000
Calculates the gas required by the calling contract method Setnum ()


> TestInstance.setNum.estimateGas (4, {from:eth.accounts[1]})
41645
Start calling


> TestInstance.setNum.sendTransaction (4, {from:eth.accounts[1]})
I0318 07:21:31.344279 internal/ethapi/api.go:1143] Tx ( 0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab) to: 0x03a4fb357f8c38694ab536d09003076033442f9e
"0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab"
Open the mining and let the deal be packaged, and then check to see if the gasused matches the numbers calculated above.


> Eth.gettransactionreceipt (' 0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab ')
{
Blockhash: "0x494f5f6fc0c156f105ffe3e4e1aa886c60f916a5998d44a03916b3f2cc733b8a",
blocknumber:139,
Contractaddress:null,
cumulativegasused:41644,
From: "0x27c649b7c4f66cfaedb99d6b38527db4deda6f41",
gasused:41644,
Logs: [],
Logsbloom: " 0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000 ",
Root: "0x857063e074cc3195ee2f3962438f3f6c31a759cfae461448e8726a5fa069d1ae",
To: "0x03a4fb357f8c38694ab536d09003076033442f9e",
Transactionhash: "0x3fad05f17f7904e08dcb9257ad28f85f29bd54c4729784fa39a9df88e3fcffab",
transactionindex:0
}
You can see that gasused:41644 is 1 less than calculated, the reason above has been said, here do not repeat.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.