ERC20 token buy and sell fee on the paired token

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(
        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");

      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;
      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.

hey @sanjay33
if you want to charge fee directly in USDC then you can't without your own dex.

@FreezyEx Thank you for prompt reply.
I need this logic because we want to avoid the contract sell of the erc20 token collected as fee.
Is this true for any token paired with our token (for ex, ETH, USDT, DAI)?

Yes you can't control anything apart your own token