本文节选自电子书Netkiller Blockchain 手札
Netkiller Blockchain 手札
Mr. Neo Chan, 陈景峯 (BG7NYT)
中国广东省深圳市龙华新区民治街道溪山美地 518131 +86 13113668890 <netkiller@msn.com>
文档始创于 2018-02-10
版权 © 2018 Netkiller(Neo Chan). All rights reserved.
网站:http://www.netkiller.cn |
---|
QQ:13721218 请注明 “读者” |
QQ 群:128659835 请注明 “读者” |
内容摘要
这一部关于区块链开发及运维的电子书.
为什么会写区块链电子书? 因为 2018 年是区块链年, 区块链是一个风口, 前几个风口我都错过了. 例如 web2.0, 云, 大数据等等, 都从身旁擦肩而过. 所以我要抓住这次.
这本电子书是否会出版 (纸质图书)? 不会, 因为互联网技术更迭太快, 纸质书籍的内容无法实时更新, 一本书动辄百元, 很快就成为垃圾, 你会发现目前市面的上区块链书籍至少是一年前写的, 内容已经过时, 很多例子无法正确运行. 所以我不会出版, 电子书的内容会追逐技术发展, 及时跟进软件版本的升级, 做到内容最新, 至少是主流.
这本电子书与其他区块链书籍有什么不同? 市面上大部分区块链书籍都是用 2/3 去讲区块链原理, 只要不到 1/3 的干货, 干货不够理论来凑, 通篇将理论或是大谈特谈区块链行业, 这些内容更多是头脑风暴, 展望区块链, 均无法落地实施. 本书与那些书籍完全不同, 不讲理论和原理, 面向应用落地, 注重例子, 均是干货.
写作原则, 无法落地的项目作者绝对不会写. 凡是写入电子的内容均具备可操作, 可落地.
电子书更新频率? 每天都会有新内容加入, 更新频率最迟不会超过一周, 更新内容请关注 https://github.com/netkiller/netkiller.github.io/commits/master
本文采用碎片化写作, 原文会不定期更新, 请尽量阅读原文 http://www.netkiller.cn/blockchain/index.html
您的打赏是我的写作动力: http://www.netkiller.cn/blockchain/donations.html
接受 ETH 打赏: 0x3e827461Cc53ed7c75A29187CfF39629FCAE3661
14.11. 合约接收 ETH
首先你需要在智能合约中定义这个函数 function () payable public {}, 这时这个合约地址就可以接收 ETH 了.
测试方法, 向合约地址发送 ETH 即可.
14.11.1. 调用 selfdestruct(msg.sender); 取出合约中的 ETH
- pragma solidity ^0.4.24;
- contract NetkillerCashier {
- function () payable public {}
- function claim() public {
- selfdestruct(msg.sender);
- }
- }
- https://ropsten.etherscan.io/tx/0x6504df0e18416c3c319f1f11f84ffa40a752b47c257faee58a7ef2c8ef78cc45
- Contract 0x0896827f5e3d2683763321bdf780bde1824f6137
- TRANSFER 0.03 Ether from 0x0896827f5e3d2683763321bdf780bde1824f6137 to 0x22c57f0537414fd95b9f0f08f1e51d8b96f14029
- SELF-DESTRUCT Contract 0x0896827f5e3d2683763321bdf780bde1824f6137
查看 Code https://ropsten.etherscan.io/address/0x0896827f5e3d2683763321bdf780bde1824f6137#code 显示
Contract SelfDestruct called at TxHash 0x6504df0e18416c3c319f1f11f84ffa40a752b47c257faee58a7ef2c8ef78cc45
14.11.2. 自动退款合约
本合约只收取 1 ETH 多余 ETH 将退给用户
- pragma solidity ^0.4.24;
- // Author: netkiller@msn.com
- // Website: http://www.netkiller.cn
- contract Refund {
- address owner = 0x0;
- uint256 ticket = 1 ether;
- constructor() public payable {
- owner = msg.sender;
- }
- function () public payable {
- require(msg.value>= ticket);
- if (msg.value> ticket) {
- uint refundFee = msg.value - ticket;
- msg.sender.transfer(refundFee);
- }
- }
- }
14.11.3. 收款合约自动转账
合约收到 ETH 后自动转到 owner 账号中.
- pragma solidity ^0.4.24;
- contract NetkillerCashier {
- address public owner;
- constructor() public payable {
- owner = msg.sender;
- }
- function () payable public {
- owner.transfer(msg.value);
- }
- }
14.11.4. 指定账号提取 ETH
- pragma solidity ^0.4.24;
- contract NetkillerCashier {
- address public owner;
- uint public amount;
- modifier onlyOwner {
- require(msg.sender == owner);
- _;
- }
- constructor() public {
- owner = msg.sender;
- }
- function () public payable {
- amount += msg.value;
- }
- function transferOwnership(address newOwner) onlyOwner public {
- if (newOwner != address(0)) {
- owner = newOwner;
- }
- }
- function withdraw() onlyOwner public {
- msg.sender.transfer(amount);
- amount = 0;
- }
- }
function transferOwnership(address newOwner) 可以修改指定账号提取 ETH
function withdraw() 提取 ETH 的函数
- https://ropsten.etherscan.io/tx/0xadad8c4cd7649d825fb8c362e97f80c4821b07c97d423050289986bd75703b78
- Contract 0xb31fb5297340a06e1af3e21c1780b7001db6890a
- TRANSFER 0.05 Ether from 0xb31fb5297340a06e1af3e21c1780b7001db6890a to 0x22c57f0537414fd95b9f0f08f1e51d8b96f14029
来源: https://www.qcloud.com/developer/article/1164025