精通以太坊笔记(二)

以太币的单位

以太坊的货币单位为ether

以太币可以被拆分为更小的单元称为wei

值(以wei为单位) 指数 通俗名称             标准名称
1 1 wei wei
1 000         10^{3} babbage         kilowei
1 000 000  10^{6} lovelace        megawei
1 000 000 000        10^{9} shannon gigwei
1 000 000 000 000 10^{12} szabo microether
1 000 000 000 000 000 10^{15} finney milliether
1 000 000 000 000 000 000 10^{18} ether ether
1 000 000 000 000 000 000 000 10^{21} grand kiloether
1 000 000 000 000 000 000 000 000 10^{24} megaether

第一个智能合约

//第一个水龙头智能合约
contract Faucet{
    //给任何请求的人ether
    function withdraw(uint withdraw_amount) public {
    //限制取出数量     10^17
    require(withdraw_amount <=100000000000000000);
    //发送他们请求的ether到他们的地址
    msg.sender.transfer(withdraw_amount);
    }
    //回退函数
    function ()public payable{ }
}

//第一个水龙头智能合约     

注释不会被包括在EVM的字节码中,被编辑器忽略。

contract Faucet{ 

实际合约的开始,声明一个contract对象,类似class。

 function withdraw(uint withdraw_amount) public {

声明函数 名字为withdraw 接收uint(无符号整型)名为 withdraw_amount的参数。函数被声明为公开函数,意味着它可以被其他合约调用。

require(withdraw_amount <=100000000000000000);

 使用Solidity内置函数require来判断条件即withdraw_amount是否小于等于10^17wei==0.1ether。如果条件判断不通过,则将导致合约执行停止并因异常而失败。

msg.sender.transfer(withdraw_amount);

扫描二维码关注公众号,回复: 14802094 查看本文章

 实际的提币行为。

                        msg对象是一个所有合约都可以访问的输入。

                        sender属性就是发起这个交易的发起方地址。

                        transfer函数是内置函数,用来转账。

function ()public payable{ }

回退函数,会在一些特殊情况下被调用,比如触发这个合约的交易没有指定调用哪一个具体函数,或者合约本身没有定义任何函数,再或者交易没有包括任何数据。合约可以有一个这样的默认函数(没有名字),通常也使用这个函数来接受以太币。定义包含public和payable属性,这意味这个合约可以接受以太币。 

猜你喜欢

转载自blog.csdn.net/qq_45372573/article/details/121235379