Skip to content

Instantly share code, notes, and snippets.

@Bryanmankind
Created May 14, 2024 13:43
Show Gist options
  • Save Bryanmankind/6f8158f716b64752c853b7053d6c91ca to your computer and use it in GitHub Desktop.
Save Bryanmankind/6f8158f716b64752c853b7053d6c91ca 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.25+commit.b61c2a91.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
import {IERC20} from "@openzeppelin/contracts/interfaces/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract PMT is ERC20 {
constructor() ERC20("PERMIT", "PMT") {
// Mint initial supply to contract creator
_mint(msg.sender, 1000000 * 10**decimals());
}
}
contract StarterPreSale {
// Error handeling
error invalidAccount();
error invalidPrice();
error fundsTooLow();
error tokensSoldOut();
// address of owner to deploy contract;
address payable public owner;
// payment token to contract
IERC20 public USDC;
// contract native token
IERC20 public PMT;
uint256 public preSaleStartTime;
uint256 public endpreSale;
uint256 public soldTokens;
uint256 public amountRaisedUSDC;
uint256 public amountRaisedEth;
bool public preSaleSatus;
uint256 public preSaleSupply = 200000 * 10** 18; // PMT presale tokens
uint256 public costOfToken = 0.000002059 ether; // 1 PMT cost for one token in ether
uint256 public tokenPerUSDC = 0.006 * 10** 6; // 1 token per $1 current price of eth
uint256 public minimumUSDC = 200 * 10** 6; // minimum amount to get to tokens in ustd
uint256 public minimumEth = 0.001724 ether; // minimum amount to get to tokens in eth
modifier onlyOwner() {
require (msg.sender == owner, "You are not the owner");
_;
}
event BuyToken (address indexed user, uint256 indexed amount);
event PriceUpdated(uint256 newPrice);
constructor (address _tokenAddress, address paymentAdd) {
owner = payable (msg.sender);
PMT = IERC20(_tokenAddress);
USDC = IERC20(paymentAdd);
preSaleStartTime = block.timestamp;
endpreSale = preSaleStartTime + 30 days;
preSaleSatus = true;
}
// change the cost of the token
function tokenCost (uint256 _price) external onlyOwner {
if (_price <= 0) {
revert invalidPrice();
}
costOfToken = _price;
emit PriceUpdated(_price);
}
// Change the cost per USDC
function tokenPriceUsdc (uint256 _price) external onlyOwner {
if (_price <= 0) {
revert invalidPrice();
}
tokenPerUSDC = _price;
emit PriceUpdated(_price);
}
// Change the minimum dollar
function tokenPriceminimumDollar (uint256 _price) external onlyOwner {
if (_price <= 0) {
revert invalidPrice();
}
minimumUSDC = _price;
emit PriceUpdated(_price);
}
// Change the minimum Eth
function tokenPriceminimumEth (uint256 _price) external onlyOwner {
if (_price <= 0) {
revert invalidPrice();
}
minimumEth = _price;
emit PriceUpdated(_price);
}
function buyTokenInEth(uint256 token) external payable returns (bool) {
// Calculate the amount of tokens to be purchased
if (msg.sender == address(0)) {
revert invalidAccount();
}
if (soldTokens > preSaleSupply) {
revert tokensSoldOut();
}
uint256 amount = costOfToken * token ;
require(amount < 0, "Invalid payment");
if (amount < minimumEth) {
revert fundsTooLow();
}
// Transfer the received ETH to the contract owner
if (amount > 0) {
owner.transfer(amount);
amountRaisedEth += amount;
}
// Transfer PMT tokens to the buyer's address
PMT.transfer(msg.sender, token);
// Update contract state variables
soldTokens += token;
amountRaisedEth += msg.value;
emit BuyToken(msg.sender, token);
return true;
}
function buyTokenWithUSDC (uint256 _usdcAmount, uint256 token) public payable returns (bool) {
if (msg.sender != address(0)) {
revert invalidAccount();
}
require(preSaleStartTime <= endpreSale, "presale is over");
if (soldTokens < preSaleSupply) {
revert tokensSoldOut();
}
if (_usdcAmount < minimumUSDC) {
revert fundsTooLow();
}
// transfer USDC tokens from the sender to the contract
if (_usdcAmount > 0) {
USDC.transferFrom(msg.sender, address(this), _usdcAmount);
amountRaisedUSDC += _usdcAmount;
}
// Transfer PMT tokens to the buyer's address
PMT.transfer(msg.sender, token);
// Update contract state variables
soldTokens += token;
emit BuyToken(msg.sender, token);
return true;
}
function withdrawEth() public onlyOwner {
address payable _owner = payable(msg.sender);
uint256 balance = address(this).balance;
bool sent = _owner.send(balance);
require(sent, "Failed to withdraw tokens");
}
function withdrawUSDC() public onlyOwner {
address _owner = msg.sender;
uint256 amount = USDC.balanceOf(address(this));
bool sent = USDC.transfer(_owner, amount);
require(sent, "Failed to withdraw tokens");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment