I am trying to develop an ERC20 token that has a marketing fee for both buy and sell. The marketing fee must be calculated and paid in USDC which will be paired with my ERC20 token on the UniSwap V2 dex.
I am not sure how to implement the fee calculation and transfer logic. I think I know how to calculate the fee in my erc20 token (see my sample code below) but not sure how to do the same with the paired USDC token.
//SPDX-License-Identifier: MIT
pragma solidity 0.8.15;
import "./ERC20/ERC20.sol";
import "./access/Ownable.sol";
import "./uniswap/IUniswapV2Router02.sol";
contract MyxyzToken is ERC20, Ownable {
IUniswapV2Router02 public uniswapV2Router;
uint256 private buyMarketingFee;
uint256 private sellMarketingFee;
constructor() ERC20("MyxyzToken", "Myx") {
uint256 totalsupply = 100_000_000 (10*18);
uniswapV2Router = IUniswapV2Router02(
0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
);
address uniswapV2Pair = address(0x0); //placeholder pair address
address marketingWallet = address(0x0); //placeholder marketing wallet address
buyMarketingFee = 2;
sellMarketingFee = 2;
_mint(owner(), total_supply);
}
function _transfer(address from, address to, uint256 amount) internal override {
require(from != address(0), "zero address");
require(to != address(0), "zero address");
uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "transfer amount exceeds balance");
//buy
if (uniswapV2Pair == from) {
if (buyMarketingFee > 0) {
// this buy fee works for erc20 token but not for the USDC (other token with which it is paired)
uint256 fee = (amount * buyMarketingFee) / 100;
_balances[marketingWallet] = _balances[marketingWallet] + fee;
}
}
//sell
else if (uniswapV2Pair == to) {
if (sellMarketingFee > 0) {
// this sell fee works for erc20 token but not for the USDC (other token with which it is paired)
uint256 fee = (amount * sellMarketingFee) / 100;
_balances[marketingWallet] = _balances[marketingWallet] + fee;
}
}
uint256 amountReceived = amount - fee;
unchecked {
_balances[from] = fromBalance - amount;
_balances[to] += amountReceived;
}
}
}
I don't know if what I am doing is the right approach. Later I might have to add more type of fees.