以太坊(四):创建和部署智能合约

上一章已经完成以太坊开发环境的搭建:以太坊(三)从零开始搭建智能合约开发环境

要在区块链上创建一个合约,我们使用的方法是把以太坊虚拟机代码作为数据给空地址发送交易,所以我们需要一个有余额并激活的账户。

账号激活和转账请参考以太坊(三)从零开始搭建智能合约开发环境

编写智能合约代码

现在我们来开始编写第一个智能合约代码,solidity代码如下:

pragma solidity ^0.4.18;
contract hello {
    string greeting;
    
    function hello(string _greeting) public {
        greeting = _greeting;
    }

    function say() constant public returns (string) {
        return greeting;
    }
}

把这段代码写(拷贝)到Browser-Solidity,如果没有错误,点击Details获取部署代码,如:

以太坊(四):创建和部署智能合约_第1张图片
image

在弹出的对话框中找到WEB3DEPLOY部分,点拷贝,粘贴到编辑器后,修改初始化字符串为hello world。

以太坊(四):创建和部署智能合约_第2张图片
image

部署智能合约

Browser-Solidity生成的代码,拷贝到命令编辑器里:

var _greeting = "Hello World" ;
var helloContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"say","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
var hello = helloContract.new(
   _greeting,
   {
     from: web3.eth.accounts[1], 
     data: '0x6060604052341561000f57600080fd5b6040516102b83803806102b8833981016040528080518201919050508060009080519060200190610041929190610048565b50506100ed565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061008957805160ff19168380011785556100b7565b828001600101855582156100b7579182015b828111156100b657825182559160200191906001019061009b565b5b5090506100c491906100c8565b5090565b6100ea91905b808211156100e65760008160009055506001016100ce565b5090565b90565b6101bc806100fc6000396000f300606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063954ab4b214610046575b600080fd5b341561005157600080fd5b6100596100d4565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009957808201518184015260208101905061007e565b50505050905090810190601f1680156100c65780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100dc61017c565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101725780601f1061014757610100808354040283529160200191610172565b820191906000526020600020905b81548152906001019060200180831161015557829003601f168201915b5050505050905090565b6020604051908101604052806000815250905600a165627a7a723058204a5577bb3ad30e02f7a3bdd90eedcc682700d67fc8ed6604d38bb739c0655df90029', 
     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: 0xe368f8f0ac578a372921f278e8afc4217d238c09 transactionHash: 0x82d6e26f9c8d5ac04d56b51e45e319ef0ab8b103fe3630f7d086644efc84edf4

说明合约已经部署成功。
这时,可以查看账户余额:

 eth.getBalance(eth.accounts[1])

会发现余额变少了。

运行智能合约

> hello.say()
"Hello World"

输出Hello World,我们第一个合约Hello World,成功运行了。

你可能感兴趣的:(以太坊(四):创建和部署智能合约)