主页 > 苹果下载imtoken教程 > 以太币的基本单位
以太币的基本单位
以太币的基本单位
以太币的最小单位是wei,也是命令行默认的单位,然后每1000输入一个单位,后面是
kwei (1000 维)
mwei (1000 千瓦)
gwei (1000 毫瓦)
萨博 (1000 gwei)
芬尼 (1000 szabo)
以太币(1000 芬尼)
简单来说就是1 eth = 1000 * 1000 * 1000 * 1000 * 1000 * 1000 Wei = 1000 ^ 6 Wei(这就是为什么我们在使用geth命令行转账0.01个以太币时,结果显示很长的原因)
以太和Wei如何转换
Ether–> Wei:web3.toWei
> web3.toWei(1)
"1000000000000000000"
> web3.toWei(1.3423423)
"1342342300000000000"
> web3.toWei(0.00034)
"340000000000000"
>
Wei –> Ether: web3.fromWei
> web3.fromWei(10000000000000000)
"0.01"
> web3.fromWei(1000000000000000000)
"1"
>
以太单位之间的转换工具
使用非常简单,输入各种单位,就可以自动得到各种换算结果,比如输入0.01ether就可以得到多少魏,多少芬尼等。
开始挖矿和停止挖矿
> miner.start() //开始挖矿
true
> miner.stop() //停止挖矿
true
>
部署合约
注意部署合约时,以太坊私链必须处于挖矿状态,否则合约部署不会生效
在命令行中以太坊停止挖矿,首先unlock(eth.accounts[0]),因为部署合约需要消耗gas,也就是以太坊。 之前说过,由于保护机制,如果账户没有解锁,是不允许以太币流出的。
> personal.unlockAccount(acc0)
Unlock account 0xbcf5b841303bc08026ce2d3b8f83498ffe42c12f
Passphrase:
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);
}
})
结果如下:
稍等片刻,你会发现合约已经部署到挖矿挖出的区块中,回车表示成功
此时进入合约部署的实例a_demotypes,可以看到a_demotypes的详细信息。
> a_demotypes
{
abi: [{
constant: false,
inputs: [{...}],
name: "f",
outputs: [{...}],
payable: false,
type: "function"
}],
address: "0x54ed7a5f5a63ddada3bfe83b3e632adabaa5fc2f",
transactionHash: "0x69cde62bcd6458e14f40497f4840f422911d63f5dea2b3a9833e6810db64a1c9",
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
参考: