操作脚本
1 | mkdir three |
genesis.json的内容如下:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17{
"config": {
"chainId": 10,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x8000000",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00",
"alloc": {}
}
创建区块链1
geth init ./genesis.json --datadir "./chain"
连接区块链,创建远程连接1
2
3
4
5
6
7
8
9
10geth \
--identity "yudao.me etherum" \
--rpcaddr 0.0.0.0 \
--rpc \
--rpcport 8545 \
--maxpeers 2 \
--rpcapi "db,eth,net,web3,debug" \
--networkid 100 \
--datadir "./chain" \
--nodiscover
另起终端1
2
3
4
5
6geth attach ipc://home/guoqiang/blockchain/three/chain/geth.ipc
web3.personal.newAccount("123456")
personal.unlockAccount(eth.accounts[0], "123456", 15000)
miner.start(1)
acc0 = web3.eth.accounts[0]
web3.eth.getBalance(acc0)
通过https://www.bejson.com,压缩转义,将../two/build/contracts目录下的Hello_yudao_me.json中abi部分进行压缩后,如下代码1
2
3yudao_abi=JSON.parse('[{\"constant\":true,\"inputs\":[{\"name\":\"name\",\"type\":\"string\"}],\"name\":\"print\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"say\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"}]')
yudao_bytecode="0x6060604052341561000f57600080fd5b6102488061001e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806311114af114610051578063954ab4b214610127575b600080fd5b341561005c57600080fd5b6100ac600480803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919050506101b5565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100ec5780820151818401526020810190506100d1565b50505050905090810190601f1680156101195780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561013257600080fd5b61013a6101c5565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561017a57808201518184015260208101905061015f565b50505050905090810190601f1680156101a75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6101bd610208565b819050919050565b6101cd610208565b6040805190810160405280600e81526020017f48656c6c6f206d73686b2e746f70000000000000000000000000000000000000815250905090565b6020604051908101604052806000815250905600a165627a7a7230582020589f9fd38871fc4097c360c32d1ec234f8f0c8b865bdc68937053b842dffd30029"
web3.eth.estimateGas({data: yudao_bytecode})
另终端部署合约,记得解锁账号1
cd two, truffle migrate --reset
返回之前终端1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16yudao_Contract = web3.eth.contract(yudao_abi);
yudao_hello = yudao_Contract.new({from:acc0, data:yudao_bytecode, gas:300000}, function(e, contract){
if(!e) {
if(!contract.address) {
console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");
} else {
console.log("Contract mined! Address: " + contract.address);
console.log(contract);
}
}
});
yudao_hello.say()
yudao_hello.print("Hello https://yudao.me")