Created
April 22, 2023 08:54
-
-
Save grasshaussoftware/a6f21de80d43f7799e56023f8f73d976 to your computer and use it in GitHub Desktop.
Cannacoin Prime Smart Contract ZETA
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.7; | |
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | |
import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; | |
contract CannacoinPrime is ERC20, ReentrancyGuard { | |
uint256 public constant TOTAL_SUPPLY_CAP = 1_000_000_000_000 * 10 ** 18; | |
address public immutable AVAX_ADDRESS; | |
address public immutable owner; | |
uint256 public mintingFees; | |
uint256 public transferFees; | |
constructor(address _avaxAddress) ERC20("Cannacoin Prime", "PRIME") { | |
AVAX_ADDRESS = _avaxAddress; | |
owner = msg.sender; | |
} | |
function mint() external nonReentrant { | |
require(msg.value == 0, "AVAX only"); | |
uint256 currentSupply = totalSupply(); | |
require(currentSupply < TOTAL_SUPPLY_CAP, "Total supply cap reached"); | |
uint256 mintingFee = 10000000000000000; // 0.00001 AVAX | |
uint256 mintAmount = (1 * 10 ** decimals()) - mintingFee; | |
require(IERC20(AVAX_ADDRESS).balanceOf(msg.sender) >= mintingFee, "Insufficient AVAX balance"); | |
require(currentSupply + mintAmount <= TOTAL_SUPPLY_CAP, "Minting would exceed total supply cap"); | |
IERC20(AVAX_ADDRESS).transferFrom(msg.sender, address(this), mintingFee); | |
_mint(msg.sender, mintAmount); | |
mintingFees += mintingFee; | |
} | |
function transfer(address recipient, uint256 amount) public override nonReentrant returns (bool) { | |
require(balanceOf(_msgSender()) >= amount, "Insufficient balance"); | |
uint256 transferFee = amount / 100; | |
uint256 transferAmount = amount - transferFee; | |
require(balanceOf(recipient) + transferAmount >= balanceOf(recipient), "Transfer amount would overflow"); | |
_transfer(_msgSender(), recipient, transferAmount); | |
if (transferFee > 0) { | |
require(balanceOf(address(this)) + transferFee >= balanceOf(address(this)), "Transfer fee would overflow"); | |
_transfer(_msgSender(), address(this), transferFee); | |
transferFees += transferFee; | |
} | |
return true; | |
} | |
function transferFrom(address sender, address recipient, uint256 amount) public override nonReentrant returns (bool) { | |
require(balanceOf(sender) >= amount, "Insufficient balance"); | |
require(allowance(sender, _msgSender()) >= amount, "Insufficient allowance"); | |
uint256 transferFee = amount / 100; | |
uint256 transferAmount = amount - transferFee; | |
require(balanceOf(recipient) + transferAmount >= balanceOf(recipient), "Transfer amount would overflow"); | |
_transfer(sender, recipient, transferAmount); | |
if (transferFee > 0) { | |
require(balanceOf(address(this)) + transferFee >= balanceOf(address(this)), "Transfer fee would overflow"); | |
_transfer(sender, address(this), transferFee); | |
transferFees += transferFee; | |
} | |
_approve(sender, _msgSender(), allowance(sender, _msgSender()) - amount + transferAmount); | |
return true; | |
} | |
function withdrawFees() external nonReentrant { | |
require(msg.sender == owner, "Only the owner can withdraw fees"); | |
uint256 totalFees = mintingFees + transferFees; | |
require(totalFees > 0, "No fees to withdraw"); | |
// Prevent integer overflow | |
require(mintingFees + transferFees >= mintingFees && mintingFees + transferFees >= transferFees, "Total fees integer overflow"); | |
mintingFees = 0; | |
transferFees = 0; | |
// Prevent integer underflow | |
require(IERC20(AVAX_ADDRESS).balanceOf(address(this)) + totalFees >= IERC20(AVAX_ADDRESS).balanceOf(address(this)), "Underflow in contract balance"); | |
IERC20(AVAX_ADDRESS).transfer(msg.sender, totalFees); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment