Created
March 28, 2018 04:52
-
-
Save easychen/fe0bcf40ca8f489b3cabb0f11261e0e3 to your computer and use it in GitHub Desktop.
代币演示合约
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.16; | |
interface tokenRecipient { function receiveApproval (address _from, uint256 _value, address _token, bytes _extraData) external; } | |
contract FangTangCoin { | |
string public name; | |
string public symbol; | |
uint8 public decimals = 18; | |
uint256 public totalSupply; | |
address private creator; | |
mapping (address => uint256) public balanceOf; | |
mapping (address => mapping (address => uint256)) public allowance; | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
function FangTangCoin( | |
uint256 initialSupply, | |
string tokenName, | |
string tokenSymbol | |
) public { | |
totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount | |
balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens | |
name = tokenName; // Set the name for display purposes | |
symbol = tokenSymbol; // Set the symbol for display purposes | |
creator = msg.sender; | |
} | |
function _transfer(address _from, address _to, uint _value) internal { | |
require(_to != 0x0); | |
require(balanceOf[_from] >= _value); | |
require(balanceOf[_to] + _value > balanceOf[_to]); | |
uint previousBalances = balanceOf[_from] + balanceOf[_to]; | |
balanceOf[_from] -= _value; | |
balanceOf[_to] += _value; | |
emit Transfer(_from, _to, _value); | |
assert(balanceOf[_from] + balanceOf[_to] == previousBalances); | |
} | |
function transfer(address _to, uint256 _value) public { | |
_transfer(msg.sender, _to, _value); | |
} | |
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { | |
require(_value <= allowance[_from][msg.sender]); // Check allowance | |
allowance[_from][msg.sender] -= _value; | |
_transfer(_from, _to, _value); | |
return true; | |
} | |
function approve(address _spender, uint256 _value) public | |
returns (bool success) { | |
allowance[msg.sender][_spender] = _value; | |
return true; | |
} | |
function buyToken() public payable returns (bool success){ | |
uint256 weiAmount = msg.value; | |
uint256 tokens; | |
uint rate = 1000; | |
tokens = weiAmount * ( 10 ** uint256(decimals)/ 1000000000000000000 ) * rate ; | |
require(tokens > 0); | |
_transfer(creator , msg.sender, tokens); | |
return true; | |
} | |
function getETH() public | |
{ | |
require(address(this).balance > 0 && msg.sender == creator); | |
creator.transfer(address(this).balance); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment