部署智能合約的步驟為:
- 啟動一個以太坊節(jié)點 (例如geth或者testrpc)。
- 使用solc編譯智能合約。 => 獲得二進(jìn)制代碼丹禀。
- 將編譯好的合約部署到網(wǎng)絡(luò)。(這一步會消耗以太幣,還需要使用你的節(jié)點的默認(rèn)地址或者指定地址來給合約簽名双泪。) => 獲得合約的區(qū)塊鏈地址和ABI(合約接口的JSON表示持搜,包括變量,事件和可以調(diào)用的方法)焙矛。(譯注:作者在這里把ABI與合約接口弄混了葫盼。ABI是合約接口的二進(jìn)制表示。)
- 用web3.js提供的JavaScript API來調(diào)用合約村斟。
Brew MacOS包管理器
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
安裝go的環(huán)境
brew install go
安裝 geth
npm install -g solc
Solidity以太坊智能合約語言
brew install solidity
geth console會啟動節(jié)點贫导,geth attach是通過rpc或者ipc和已經(jīng)啟動的節(jié)點進(jìn)行交互。所以二者的使用區(qū)別還是挺大的蟆盹,在geth console里面可以使用所有模塊的api孩灯,但是geth attach只能使用已經(jīng)打開的模塊的api,如果節(jié)點沒有打開rpc geth attach甚至都不能連接上節(jié)點
修改創(chuàng)世Genesis.json
1,啟動一個以太坊節(jié)點
1,geth --datadir "./" --nodiscover console
2,user1 = eth.accounts[0]
"0xa4aa2105cc3e6b6a83faaaf72782d1e9a68e90ca"
> eth.getBalance(user1)
1.156e+21
> user2 = eth.accounts[1]
"0x33481f6d7b7f4ff430397913af1eaf582f3f4391"
> eth.getBalance(user2)
4000000000000000000
2,智能合約代碼:
contract Multiply7 {
event Print(uint);
function multiply(uint input) returns (uint) {
Print(input * 7);
return input * 7;
}
}
3逾滥,獲得abiDefinition和bytecode,
通過https://remix.ethereum.org編譯智能合約或者solc編譯
我們先把合約代碼壓縮為一行.新建一個ssh session, 切換到geth用戶環(huán)境su - geth, 然后輸入:cat contracts/Token.sol | tr '\n' 空格'空格 '.
轉(zhuǎn)義成字符串
http://www.bejson.com
abi=[{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]
4峰档,看黑板:字節(jié)碼前加0x
bytecode="0x6060604052341561000f57600080fd5b60b18061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b60006007820290509190505600a165627a7a72305820e8baa72a4bd1ffe2e8fa51812e8d65fc7b145baf48382c4ae5039510fd35a6ff0029"
5,通過abiDefinition和bytecode實例化創(chuàng)建合約
var contract = eth.contract(abi);
var initializer = {from:web3.eth.accounts[0],data:bytecode,gas:300000};
personal.unlockAccount(user1,"jianghua")
var token = contract.new(initializer)
INFO [03-03|17:25:44] Submitted contract creation fullhash=0x7e1bd79681ee748b1057f123a9951652e67232c2624631a5531edb0bb629e88b contract=0x1BeEf17Dac85D369d41A9891276D58fC8F877A24
6寨昙,需要通過挖礦這一步驟讥巡,對合約地址進(jìn)行確認(rèn)
miner.start(4)
admin.sleepBlocks(2)
miner.stop()
到此為止,合約的布署已布署到了區(qū)塊鏈上舔哪。
7欢顷,通過合約地址,實例化自己的合約尸红,并進(jìn)行調(diào)用
mycontract = contract.at(token.address)
{
abi: [{
constant: false,
inputs: [{...}],
name: "multiply",
outputs: [{...}],
payable: false,
stateMutability: "nonpayable",
type: "function"
}],
address: "0x1beef17dac85d369d41a9891276d58fc8f877a24",
transactionHash: null,
allEvents: function(),
multiply: function()
}
8吱涉,到這里,合約的布署與調(diào)用完成
mycontract.multiply.call(2)
到了這一步外里,我們可以得到自己合約的返回值14
9怎爵,預(yù)估手續(xù)費
web3.eth.estimateGas({data:bytecode})
10,檢查合約是否部署成功
eth.getCode("0x1beef17dac85d369d41a9891276d58fc8f877a24")
"0x606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b60006007820290509190505600a165627a7a72305820e8baa72a4bd1ffe2e8fa51812e8d65fc7b145baf48382c4ae5039510fd35a6ff0029"
11盅蝗,驗證區(qū)塊的內(nèi)容
eth.getBlock(2550)
{
difficulty: 314416,
extraData: "0xd783010802846765746886676f312e31308664617277696e",
gasLimit: 4712388,
gasUsed: 99930,
hash: "0x57ca73b779ae13c542f0421fc81a272fc6ce938aab1951b412f8813b6dc5bc8e",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0xa4aa2105cc3e6b6a83faaaf72782d1e9a68e90ca",
mixHash: "0xecee4f13a617954c9bfe19e14bdf00c6b0d006d16e19e7474faa8de49eb74bf9",
nonce: "0x39f04fd757c3fab6",
number: 2550,
parentHash: "0xcdfb7e8f78f86e942acfe5a9a1f1ef3637a77cd96ebf35f28e6880e40e94fa0c",
receiptsRoot: "0xea304d377e8d7db719679ecfb3900e0b6c18469512ac3ba1c74c754e54f73d9e",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 830,
stateRoot: "0x69c10e0e14757623f279f8df6564659f1ac4cf25ab2ec559acedb964315804f7",
timestamp: 1520069708,
totalDifficulty: 589085710,
transactions: ["0x7e1bd79681ee748b1057f123a9951652e67232c2624631a5531edb0bb629e88b"],
transactionsRoot: "0xba26a1e628172e65320c9d9474148763c03bd16513fc63b425f781537d542aa9",
uncles: []
}
12鳖链,驗證交易信息
eth.getTransaction("0x7e1bd79681ee748b1057f123a9951652e67232c2624631a5531edb0bb629e88b")
{
blockHash: "0x57ca73b779ae13c542f0421fc81a272fc6ce938aab1951b412f8813b6dc5bc8e",
blockNumber: 2550,
from: "0xa4aa2105cc3e6b6a83faaaf72782d1e9a68e90ca",
gas: 300000,
gasPrice: 0,
hash: "0x7e1bd79681ee748b1057f123a9951652e67232c2624631a5531edb0bb629e88b",
input: "0x6060604052341561000f57600080fd5b60b18061001d6000396000f300606060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b3415604e57600080fd5b606260048080359060200190919050506078565b6040518082815260200191505060405180910390f35b60006007820290509190505600a165627a7a72305820e8baa72a4bd1ffe2e8fa51812e8d65fc7b145baf48382c4ae5039510fd35a6ff0029",
nonce: 4,
r: "0x4a3d67e18204891de590b634ef34cdff373a199f0640fa5d11b91b4788ad8290",
s: "0x45467daa1b578c05e5b2c64b564a41a306d7e1ecc21ce1b472290b719fecc77c",
to: null,
transactionIndex: 0,
v: "0xfe7",
value: 0
}
合約的布署與調(diào)用驗證無誤,大功告成墩莫!