在以太坊钱包中发行自己的数字货币

以太坊钱包自带合约开发功能,这个应该是最简单的智能合约开发方式。

不过首次启动以太坊钱包后需要同步测试网络的block,这个比较费时间。虽然以太坊钱包列出了除主网外的3个测试网络,但只有Rinkeby可以用(也可能是我没有找Ropsten和Solo网络的正确启动方法)。

Rinkeby测试网络需要需要先获得测试币,获得测试币的网站连接是: https://faucet.rinkeby.io/

获得测试币的方法是:把自己的地址发到Google+或者facebook(需要)上的一篇文章里,然后把这篇文章的地址输入上面网址,然后选择测试币额度,系统就会自动赠送。

拿到测试币后,就可以开始发行自己的数字货币了。以太坊上的数字货币也是一种智能合约。参考官方的文档:

https://www.ethereum.org/token#the-code

因为编译器更新了,官方文档中的智能合约代码无法正常编译,下面是我修改后可以编译的(今天是2018.1.12,也可能后面编译器升级后又无法编译了):


pragma solidity ^0.4.17;

contract MyToken {
    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;
    
    string public name;
	string public symbol;
	uint8 public decimals;
    
    event Transfer(address indexed from, address indexed to, uint256 value);
        
    /* Initializes contract with initial supply tokens to the creator of the contract */
    function MyToken(uint256 initialSupply, string tokenName, string tokenSymbol, uint8 decimalUnits) public {
        balanceOf[msg.sender] = initialSupply;              // Give the creator all initial tokens
        name = tokenName;                                   // Set the name for display purposes
        symbol = tokenSymbol;                               // Set the symbol for display purposes
        decimals = decimalUnits;                            // Amount of decimals for display purposes
    }
        
    function transfer(address _to, uint256 _value) public {
        /* Check if sender has balance and for overflows */
        require(balanceOf[msg.sender] >= _value && balanceOf[_to] + _value >= balanceOf[_to]);

        /* Add and subtract new balances */
        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        
        /* Notify anyone listening that this transfer took place */
        Transfer(msg.sender, _to, _value);        
    }
}
    




注意官方文档关于return和throw的区别说明:

To stop a contract execution mid execution you can either return or throw The former will cost less gas but it can be more headache as any changes you did to the contract so far will be kept. In the other hand, 'throw' will cancel all contract execution, revert any changes that transaction could have made and the sender will lose all ether he sent for gas. But since the Wallet can detect that a contract will throw, it always shows an alert, therefore preventing any ether to be spent at all.


你可能感兴趣的:(以太坊,区块链)