Hello everyone,
I am new to .sol and am trying to learn on the fly with the help of Ai explicitly as a learning tool.
My goal here was to make a lightweight, simple token optimized for gas. Whilst this token will be used for governance I have decided that it suits my needs better to have other external contracts control that function.
Note: I keep getting an error on REMIX IDE, for the 'onlyOwner' as a declaration error stating Identifier not found. Even though I have got - Ownable.sol ?!
Please review my code and feel free to point out any best practices or code I have missed or not understood correctly.
Cheers and thank you, Happy Holidays!
CC
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract EnterNAME is ERC20 {
// Transactional tax constants
uint256 private constant TAX_PERCENT = 10; // 0.1% in basis points (1/10000)
uint256 private constant BURN_PERCENT = 5; // 0.05% in basis points (1/10000)
address private constant BURN_ADDRESS = address(0xdead);
// Treasury address to receive tax funds
address public immutable treasury;
// Events for transparency
event Taxed(
address indexed from,
address indexed to,
uint256 amount,
uint256 taxAmount,
uint256 burnAmount
);
constructor(address _treasury) ERC20() {
require(
_treasury != address(),
"Treasury address cannot be zero"
);
treasury = _treasury;
// Mint total supply to the treasury address
uint256 totalSupply = 500_000_000 * 10**decimals();
_mint(treasury, totalSupply);
}
function _transfer(
address from,
address to,
uint256 amount
) internal override {
// Exclude tax for transfers to/from treasury or burn address
if (from == treasury || to == treasury || to == BURN_ADDRESS) {
super._transfer(from, to, amount);
return;
}
// Calculate tax and burn amounts
uint256 taxAmount = (amount * TAX_PERCENT + 9999) / 10000; // Round up
uint256 burnAmount = (amount * BURN_PERCENT + 9999) / 10000; // Round up
uint256 netAmount = amount - taxAmount;
// Ensure proper transfer amounts
require(netAmount + taxAmount == amount, "Math error");
// Perform taxed transfer
super._transfer(from, treasury, taxAmount - burnAmount); // Tax to treasury
super._transfer(from, BURN_ADDRESS, burnAmount); // Burn tokens
super._transfer(from, to, netAmount); // Send remaining tokens to recipient
emit Taxed(from, to, amount, taxAmount, burnAmount);
}
// Renounce ownership to make the contract immutable
function renounceOwnership() public override onlyOwner {
super.renounceOwnership();
}
}