Created
February 8, 2023 09:47
-
-
Save puneetkaura/3e59b60c2c51dfa64519d9b851f908a8 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.17+commit.8df45f5f.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: GPL-3.0 | |
pragma solidity >=0.7.0 <0.9.0; | |
import "hardhat/console.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/extensions/ERC20Capped.sol"; | |
// Simple tokens sale. Build an ERC20 with the above features that sells tokens at a conversion rate of 10,000 tokens to 1 ethereum. The total supply should be 22 million tokens. | |
contract SimpleTokenSaleERC20 is ERC20, ERC20Capped { | |
uint private constant CAP_AMOUNT = 22_000_000; | |
uint public constant TOKENS_PER_ETH = 10_000; | |
constructor() ERC20("GLD", "GLD") ERC20Capped(CAP_AMOUNT) { | |
_mint(address(this), CAP_AMOUNT); | |
} | |
event TokenPurchased(address from, uint amount); | |
event TokenSold(address from, uint amount); | |
function _mint(address to, uint256 amount) internal override(ERC20, ERC20Capped){ | |
super._mint(to, amount); | |
} | |
function buyToken() external payable { | |
uint tokenAmount = (msg.value/10**18) * TOKENS_PER_ETH; | |
require(tokenAmount > 0, "Funds not enough for secure atleast 1 GLD Token"); | |
_transfer(address(this), msg.sender, tokenAmount); | |
emit TokenPurchased(msg.sender, tokenAmount); | |
} | |
function sellToken(uint tokenAmount) external returns(bool){ | |
uint ethAmount = (tokenAmount * 10**18)/TOKENS_PER_ETH ; | |
console.log(ethAmount, address(this).balance); | |
(bool success, ) = msg.sender.call{value:ethAmount}(""); | |
transfer(address(this), tokenAmount); | |
emit TokenSold(msg.sender, tokenAmount); | |
return success; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment