Created
May 2, 2020 21:39
-
-
Save lmichailian/c6b35a96a47b9432bd360186f7a9f1b5 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.17+commit.d19bba13.js&optimize=false&gist=
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.5.0; | |
contract RockerCoin { | |
string public name; | |
string public symbol; | |
uint8 public decimals; | |
uint256 public totalSupply; | |
mapping(address => uint256) public balanceOf; | |
mapping(address => mapping(address => uint256)) public allowance; | |
event Transfer(address indexed _from, address indexed _to, uint256 _value); | |
event Approval(address indexed _owner, address indexed _spender, uint256 _value); | |
constructor() public { | |
name = "RockerCoin"; | |
symbol = "RKC"; | |
decimals = 18; | |
totalSupply = 1000000 * (uint256(10) ** decimals); | |
balanceOf[msg.sender] = totalSupply; | |
} | |
function transfer(address _to, uint256 _value) public returns (bool success) { | |
require(balanceOf[msg.sender] >= _value, 'This account has not enough funds'); | |
balanceOf[msg.sender] -= _value; | |
balanceOf[_to] += _value; | |
emit Transfer(msg.sender, _to, _value); | |
return true; | |
} | |
function approve(address _spender, uint256 _value) public returns (bool success) { | |
allowance[msg.sender][_spender] = _value; | |
emit Approval(msg.sender, _spender, _value); | |
return true; | |
} | |
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { | |
require(balanceOf[_from] >= _value, 'Hasn\'t tokens' ); | |
require(allowance[_from][msg.sender] >= _value, 'No allowance'); | |
balanceOf[_from] -= _value; | |
balanceOf[_to] += _value; | |
allowance[_from][msg.sender] -= _value; | |
emit Transfer(_from, _to, _value); | |
return true; | |
} | |
} |
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.5.0; | |
interface RockerCoin { | |
function decimals() external view returns(uint8); | |
function balanceOf(address _address) external view returns(uint256); | |
function transfer(address _from, uint256 _value) external returns(bool success); | |
} | |
contract TokenSale { | |
address owner; | |
uint256 price; | |
RockerCoin rockerCoinContract; | |
uint256 tokenSold; | |
event Sold(address buyer, uint256 amount); | |
constructor(uint256 _price, address _addressContract) public { | |
owner = msg.sender; | |
price = _price; | |
rockerCoinContract = RockerCoin(_addressContract); | |
} | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
if (a == 0) { | |
return 0; | |
} | |
uint256 c = a * b; | |
require(c / a == b); | |
return c; | |
} | |
function buy(uint256 _numTokens) public payable { | |
// chequeo si por la cantidad de tokens que me pide, me esta pagando el valor que vale | |
require(msg.value == mul(price, _numTokens)); | |
// scalo a 18 decimales la cantidad de tokens requeridos | |
uint256 scaledAmount = mul(_numTokens, uint256(10) ** rockerCoinContract.decimals()); | |
// checkeo si existen los tokens suficientes para vender | |
require(rockerCoinContract.balanceOf(address(this)) >= scaledAmount); | |
tokenSold += _numTokens; | |
require(rockerCoinContract.transfer(msg.sender, scaledAmount)); | |
emit Sold(msg.sender, _numTokens); | |
} | |
function endSold() public { | |
require(msg.sender == owner); | |
require(rockerCoinContract.transfer(owner, rockerCoinContract.balanceOf(address(this)))); | |
msg.sender.transfer(address(this).balance); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment