// 評估要花多少gas,通常會將此數字乘以1.2
web3.eth.estimateGas({data: '0x' + <bytecode>})
// 估計部署合約要花多少gas
web3.eth.estimateGas({data: '0x6060604052341561000f57600080fd5b5b60ab8061001e6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa114603d575b600080fd5b3415604757600080fd5b605b60048080359060200190919050506071565b6040518082815260200191505060405180910390f35b60006007820290505b9190505600a165627a7a72305820668369d5ec98bbd9e537eb377a779e58f87d27b4cb70f247366fafb14ceff1170029'})
98392
// 部署合約並產生合約的實例
var browser_ballot_sol_test = browser_ballot_sol_testContract.new(
{
// 用哪個地址去部署合約
from: web3.eth.accounts[0],
// 合約程式碼的bytecode
data: '0x6060604052341561000f57600080fd5b5b60ab8061001e6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa114603d575b600080fd5b3415604757600080fd5b605b60048080359060200190919050506071565b6040518082815260200191505060405180910390f35b60006007820290505b9190505600a165627a7a72305820668369d5ec98bbd9e537eb377a779e58f87d27b4cb70f247366fafb14ceff1170029',
// 要花的gas
gas: '4700000'
}, function (e, contract){
// 當成功部署合約 顯示合約地址和部署合約的交易
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
Contract mined! address: 0x2ff5f980d80cb37a5cd92678157ed49b06ef324f transactionHash: 0x0b5ba1bbdc28dddaa6b0b0207e9c72576c8c10b9db9b49b680791a33a4ab0bc7
eth.getTransaction("0x0b5ba1bbdc28dddaa6b0b0207e9c72576c8c10b9db9b49b680791a33a4ab0bc7")
{
blockHash: "0xd139f77cba988b25ae7f10c12138b28e6630808259b86f290540bf7d80cbd10d",
blockNumber: 858,
from: "0x4560eb5a01478cbfa1390a4134cd752d99746266",
gas: 4700000,
gasPrice: 0,
hash: "0x0b5ba1bbdc28dddaa6b0b0207e9c72576c8c10b9db9b49b680791a33a4ab0bc7",
input: "0x6060604052341561000f57600080fd5b5b60ab8061001e6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa114603d575b600080fd5b3415604757600080fd5b605b60048080359060200190919050506071565b6040518082815260200191505060405180910390f35b60006007820290505b9190505600a165627a7a72305820668369d5ec98bbd9e537eb377a779e58f87d27b4cb70f247366fafb14ceff1170029",
nonce: 1,
r: "0x47f9359d20cded498ef3719390e4fbfe654f6438fbec71c397b29c70e8224f0d",
s: "0x4cf4a110964044afd7de6bc3d1905f90583013d9948ed37736ebdc99b894c63d",
to: null,
transactionIndex: 0,
v: "0xa95",
value: 0
}
// 從js程式其他地方建立此合約地址的實例(意圖查看或改變此合約地址的state)
var contract = web3.eth.contract(<ABI>).at(<contract address>)
var contract = web3.eth.contract([{"constant":true,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"payable":false,"type":"function"}]).at("0x2ff5f980d80cb37a5cd92678157ed49b06ef324f")
// 與合約互動
browser_ballot_sol_test.multiply(7)
49 // 與合約互動的結果,呼叫合約的constant方法 不會在鏈上產生交易,不用等挖礦,也沒有改變state(不是合約的成員變數),純粹回傳某個值
contract.multiply(15)
105