Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@GrandSchtroumpf
Created July 18, 2019 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GrandSchtroumpf/8e99e05903fa620fe87c99cdae063b3d to your computer and use it in GitHub Desktop.
Save GrandSchtroumpf/8e99e05903fa620fe87c99cdae063b3d to your computer and use it in GitHub Desktop.
Base code for meetup
pragma solidity >=0.5.0 <0.6.0;
import "https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/math/SafeMath.sol";
contract ERC20 {
using SafeMath for uint256;
uint256 private _totalSupply;
mapping(address => uint256) private _balances;
event Transfer(address indexed from, address indexed to, uint256 amount);
constructor(uint256 amount) public {
_totalSupply = amount;
_balances[msg.sender] = amount;
}
function transfer(address to, uint256 amount) external returns (bool) {
require(_balances[msg.sender] >= amount);
_balances[msg.sender] = _balances[msg.sender].sub(amount); // Remove the amount from msg.sender balance
_balances[to] = _balances[to].add(amount);// Add the amount to "to"
emit Transfer(msg.sender, to, amount);
return true;
}
function totalSupply() external view returns (uint256) {
return _totalSupply;
}
function balanceOf(address who) external view returns (uint256) {
return _balances[who];
}
}
pragma solidity >=0.5.0 <0.6.0;
import './ERC20.sol';
import 'https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/math/SafeMath.sol';
// 1000000000000000000
contract Marketplace {
using SafeMath for uint256;
ERC20 private token;
uint256 private price;
address payable private wallet;
constructor(ERC20 _token, uint256 _price, address payable _wallet) public {
require(_price > 0);
require(address(_token) != address(0));
require(_wallet != address(0));
token = _token;
price = _price;
wallet = _wallet;
}
function buyToken() payable public {
require(msg.value > 0);
require(token.balanceOf(address(this)) > 0);
require(msg.value.mod(price) == 0);
uint256 tokenAmount = msg.value.div(price);
require(token.balanceOf(address(this)) >= tokenAmount, 'You do not have enough token bro');
token.transfer(msg.sender, tokenAmount);
wallet.transfer(msg.value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment