区块链开发(四)Geth 基本命令2

来源:互联网 发布:马云在贵州大数据 编辑:程序博客网 时间:2024/05/16 06:25

Ether币的基本单位

Ether币最小的单位是Wei,也是命令行默认的单位, 然后每1000个进一个单位,依次是

  • kwei (1000 Wei)
  • mwei (1000 KWei)
  • gwei (1000 mwei)
  • szabo (1000 gwei)
  • finney (1000 szabo)
  • ether (1000 finney)

简单地说就是就是1 以太币 = 1000000000000000000 Wei (这就是上一站章中为何我们转移0.01个以太币,结果却显示很长的原因)

如何进行ether 和 Wei之间的转换

Ether–> Wei:web3.toWei

> web3.toWei(1)"1000000000000000000"> web3.toWei(1.234567)"1234567000000000000">

Wei –> Ether: web3.fromWei

> web3.fromWei(10000000000000000)"0.01"> web3.fromWei(1000000000000000000)"1">

一个以太币各单位之间的转换工具

http://ether.fund/tool/converter

使用很简单,输入各种单位,就可以自动得到各种转换结果,例如输入0.01ether 可以得到多少Wei, 多少finney等。

开始挖矿 & 停止挖矿

> miner.start() //开始挖矿true> miner.stop() //停止挖矿true>

部署合约

注意合约部署的时候,以太坊的私有链必须处在挖矿进行的状态,否则合约部署将不会生效

  • 我们在命令行中,首先unlock(eth.accounts[0]),因为部署合约需要消耗gas,也就是以太币。而之前说过由于保护机制,不解锁账户,是不会允许任何以太币流出的。
> personal.unlockAccount(acc0)Unlock account 0x95a171d45c7551474f3479bf006e2a9a3852bbd8Passphrase:true>

然后我们复制黏贴下面代码到geth 命令行中。

var a_demotypesContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"f","outputs":[{"name":"b","type":"uint256"}],"payable":false,"type":"function"}]);var a_demotypes = a_demotypesContract.new(   {     from: web3.eth.accounts[0],      data: '0x6060604052341561000c57fe5b5b60ab8061001b6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063b3de648b14603a575bfe5b3415604157fe5b60556004808035906020019091905050606b565b6040518082815260200191505060405180910390f35b600060006008830290508091505b509190505600a165627a7a7230582010decdc0b0a43b565814fe904eae2544665457d6353c7d906fc2c43c81c867e40029',      gas: '4700000'   }, function (e, contract){    console.log(e, contract);    if (typeof contract.address !== 'undefined') {         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);    } })
结果如下图:
产生智能合约地址:

0x2863dd94267e99c839700db62677a43205a6e64b


此时输入合约部署的实例a_demotypes, 可以看到a_demotypes的详情。

> a_demotypes{  abi: [{      constant: false,      inputs: [{...}],      name: "f",      outputs: [{...}],      payable: false,      type: "function"  }],  address: "0x2863dd94267e99c839700db62677a43205a6e64b",  transactionHash: "0x267e5b9be58b76c35480f5144ce1ceeb8072323cdb94489605a7b36fa94ff56b",  allEvents: function(),  f: function()}>

也可以调用a_demotypes的方法f, 输入任何数字,会返回8*n,如输入100,返回800,输入125,返回1000

> a_demotypes.f.call(100)800> a_demotypes.f.call(125)1000>

2017-12-1写于深圳




阅读全文
0 0
原创粉丝点击