Created
October 26, 2021 10:35
-
-
Save omeganoob/2c267940a1909c79544824d9507447a3 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.8.9+commit.e5eed63a.js&optimize=false&runs=200&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
// SPDX-License-Identifier: MIT | |
pragma solidity >= 0.8.0 < 0.9.0; | |
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | |
contract regimenToken is ERC20 { | |
constructor(uint256 initialSupply) ERC20("Gold", "GLD") { | |
_mint(msg.sender, initialSupply); | |
} | |
} | |
contract Payment { | |
event Bought(uint256 amount); | |
event Sold(uint256 amount); | |
ERC20 public token; | |
// Total tokens(regiment) of the account | |
constructor(uint256 initialSupply) { | |
token = new regimenToken(initialSupply); | |
} | |
function buy() payable public { | |
uint256 amountTobuy = msg.value; | |
uint256 dexBalance = token.balanceOf(address(this)); | |
require(amountTobuy > 0, "You need to send some ether"); | |
require(amountTobuy <= dexBalance, "Not enough tokens in the reserve"); | |
token.transfer(msg.sender, amountTobuy); | |
emit Bought(amountTobuy); | |
} | |
function sell(uint256 amount) public { | |
require(amount > 0, "You need to sell at least some tokens"); | |
uint256 allowance = token.allowance(msg.sender, address(this)); | |
require(allowance >= amount, "Check the token allowance"); | |
token.transferFrom(msg.sender, address(this), amount); | |
payable(msg.sender).transfer(amount); | |
emit Sold(amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment