The documentation is skimpy on details and it took me forever to figure out the correct way with the help of your links, anyways, here is what works for me:
Tested on Geth : 1.6.7 -stable , web3.js :0.20.2 Parity/v1.7.2 & Kovan testnet.
I'll be using the minimal token as example :
pragma solidity ^0.4.0;
contract minimalToken {
mapping (address => uint256) public balanceOf;
function minimalToken(uint256 initialSupply) {
balanceOf[msg.sender] = initialSupply;
}
function transfer(address _to, uint256 _value) {
require(balanceOf[msg.sender] >= _value);
require(balanceOf[_to] + _value >= balanceOf[_to]);
balanceOf[msg.sender] -= _value;
balanceOf[_to] += _value;
}
}
I'll be focusing on the transfer method which takes 2 arguments,( address, amount) to transfer:
var contract = web3.eth.contract(contractABI).at("0x8caaa1f263ff14d0276ff1a1a6ed15c51159d6e0");
var receiverAddress = '0x00Ce6C92856A657979E7728005DBc9acD002Eb09';
var callData = contract.transfer.getData(receiverAddress, 2000);
var gasEstimate = web3.eth.estimateGas({
from: web3.eth.coinbase,
to: "0x8caaa1f263ff14d0276ff1a1a6ed15c51159d6e0",
data: callData
});
var gasPrice = web3.eth.gasPrice;
console.log('gas Price: ' + gasPrice);
console.log('Estimated Transaction gas: ' + gasEstimate);
// gas Price: 21000000000
// Estimated Transaction gas: 34207
One thing that Threw me off, was that I kept on getting transaction errors due to me not including the from: field on the transaction.
Hope this helps.
Try using .getData()
.
.getData()
returns the encoded parameters of a function in order to send the transaction manually. You can then stick this in web3.eth.estimateGas()
(the one on web3.eth
, not on a given method.) to simulate sending the transaction.
Here's an untested example, but hopefully it can help you on your way:
var contractData = contractObject.new.getData(someparam, another, {data: contractBytecode});
var estimate = web3.eth.estimateGas({data: contractData})
References:
web3.eth.estimateGas()
An example of using .getData() (It's the fourth option.)
Best Answer
CallMsg
is defined in ./interfaces.go ingeth
s code at root directory:You can get the data with the help of the output of function
transaction.AsMessage()
, defined incore/types/transaction.go
:However you have to have a
types.Transaction
object already madeOf course you can create a struct
CallMsg
yourself and fill the corresponding fields: