Skip to content

Instantly share code, notes, and snippets.

@jsh1n
Created January 17, 2019 07:50
Show Gist options
  • Save jsh1n/0fa92304fe412477c133f09cda9c4c98 to your computer and use it in GitHub Desktop.
Save jsh1n/0fa92304fe412477c133f09cda9c4c98 to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.24;
contract ERC20Interface {
function totalSupply() public constant returns (uint);
function balanceOf(address tokenOwner) public constant returns (uint balance);
function allowance(address tokenOwner, address spender) public constant returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
contract TokenERC20 {
string public name;
string public symbol;
uint8 public decimals = 18;
uint256 public totalSupply;
mapping (address => uint256) public balances;
mapping (address => mapping (address => uint256)) public allowed;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
constructor (
uint8 initialSupply,
string tokenName,
string tokenSymbol
) public {
totalSupply = initialSupply;
balances[msg.sender] = initialSupply;
name = tokenName;
symbol = tokenSymbol;
}
function totalSupply() public view returns (uint256) {
return (totalSupply);
}
function baranceOf(address _owner) public view returns (uint256) {
return (balances[_owner]);
}
function _transfer(address _from, address _to, uint _value) internal {
require(_to != 0x0);
require(balances[_from] >= _value);
require(balances[_to] + _value > balances[_to]);
uint previousBalances = balances[_from] + balances[_to];
balances[_from] -= _value;
balances[_to] += _value;
emit Transfer(_from, _to, _value);
assert(balances[_from] + balances[_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 <= allowed[_from][msg.sender]);
allowed[_from][msg.sender] -= _value;
_transfer(_from, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) public
returns (bool success) {
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) public view returns (uint256) {
return (allowed[_owner][_spender]);
}
}
contract MyPreciousCoin is TokenERC20 {
uint swapRate = 100; // coin/wei
uint limit = 100000; // =1000wei
constructor () TokenERC20(0, "MyPreciousCoin", "MPC") public {}
function saleToken() public payable {
require(msg.value>0);
require(totalSupply<limit);
uint toSale;
uint toReturn; // wei
toSale = msg.value*swapRate;
//※ 
if(toSale>(limit-totalSupply)){
toReturn = (toSale-(limit-totalSupply))/swapRate;
toSale = limit-totalSupply;
}
else{
toReturn = 0;
}
totalSupply = totalSupply + toSale;
balances[msg.sender] = balances[msg.sender] + toSale;
if(toReturn>0){
msg.sender.transfer(toReturn);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment