Skip to content

Instantly share code, notes, and snippets.

@MasterGordon
Created May 17, 2023 17:41
Show Gist options
  • Save MasterGordon/b34ce4c87804a76a126da129fd76ebd3 to your computer and use it in GitHub Desktop.
Save MasterGordon/b34ce4c87804a76a126da129fd76ebd3 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract TaxableToken is ERC20 {
using SafeMath for uint256;
uint256 private constant TOKEN_DECIMALS = 18;
uint256 private constant MAX_SUPPLY = 1000000 * (10**TOKEN_DECIMALS); // Maximum supply of 1,000,000 tokens
uint256 private constant BUY_TAX_RATE = 2; // 2% tax on buys
uint256 private constant SELL_TAX_RATE = 4; // 4% tax on sells
address private taxWallet; // Address to receive the tax amount
address private uniswapV2Pair; // Address of the Uniswap V2 pair
address private owner; // Address of the contract owner
constructor(address _taxWallet) ERC20("Taxable Token", "TT") {
taxWallet = _taxWallet;
owner = msg.sender;
_mint(msg.sender, MAX_SUPPLY);
}
function _transfer(
address sender,
address recipient,
uint256 amount
) internal override {
uint256 taxAmount = 0;
if (sender != address(0) && recipient != address(0)) {
// Exclude transfers from/to the zero address
if (msg.sender == uniswapV2Pair) {
// Tax on sell
taxAmount = amount.mul(SELL_TAX_RATE).div(100);
} else {
// Tax on buy
taxAmount = amount.mul(BUY_TAX_RATE).div(100);
}
// Subtract the tax amount from the transfer amount
uint256 transferAmount = amount.sub(taxAmount);
super._transfer(sender, recipient, transferAmount);
// Transfer the tax amount to the tax wallet
super._transfer(sender, taxWallet, taxAmount);
} else {
super._transfer(sender, recipient, amount);
}
}
modifier onlyOwner() {
require(msg.sender == owner, "Only the contract owner can call this function");
_;
}
function setUniswapPair(address _uniswapV2Pair) external onlyOwner {
require(_uniswapV2Pair != address(0), "Invalid Uniswap V2 pair address");
uniswapV2Pair = _uniswapV2Pair;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment