智能合约helloworld

2018-11-21  本文已影响0人  暗夜巡林

1.geth --datadir testNet --dev console 2>> test.log

启动环境

启动环境

2.准备账户:

所注册的账户

3.查看账户余额:

账户余额

4.编写合约代码:

pragma solidity ^0.4.18;

contract hello {

    string greeting;

    functionhello(string _greeting)public{

        greeting = _greeting;

    }

    functionsay()constantpublicreturns(string){

        return greeting;

    }

}

5.使用Remix - Solidity IDE获取部署代码

在Remix中调试

6.部署代码,并加入参数:

var _greeting ="hello" ;

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[0],

    data: '0x608060405234801561001057600080fd5b506040516102a83803806102a8833981018060405281019080805182019291905050508060009080519060200190610049929190610050565b50506100f5565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009157805160ff19168380011785556100bf565b828001600101855582156100bf579182015b828111156100be5782518255916020019190600101906100a3565b5b5090506100cc91906100d0565b5090565b6100f291905b808211156100ee5760008160009055506001016100d6565b5090565b90565b6101a4806101046000396000f300608060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063954ab4b214610046575b600080fd5b34801561005257600080fd5b5061005b6100d6565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561009b578082015181840152602081019050610080565b50505050905090810190601f1680156100c85780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561016e5780601f106101435761010080835404028352916020019161016e565b820191906000526020600020905b81548152906001019060200180831161015157829003601f168201915b50505050509050905600a165627a7a72305820c976d336839f62902d3e798ebbb4988339c19ca2b2f579e210dd546b3998c2330029',

    gas: '4700000'

  }, function (e, contract){

    console.log(e, contract);

    if (typeof contract.address !== 'undefined') {

        console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);

    }

})

7.粘贴代码到环境中

粘贴代码

8.解锁账户:

解锁成功

9.开始挖矿

开始挖矿

10.成功被挖到

挖到

11.运行函数

运行成功
上一篇下一篇

猜你喜欢

热点阅读