# Errors in the contract

**URL:** <https://forum.openzeppelin.com/t/errors-in-the-contract/38739>\
**Category:** Smart Contracts\
**Tags:** erc20\
**Created:** [December 6, 2023, 9:41pm UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739 "2023-12-06T21:41:02Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan\_Mirga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/jan_mirga/32/18792_2.png) [@Jan\_Mirga](https://forum.openzeppelin.com/u/Jan_Mirga)\
**Post date:** [December 6, 2023, 9:41pm UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/1 "2023-12-06T21:41:02Z")

</div>

I have several errors in the code, if someone can take a look at it, I will be very grateful

```auto
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol";

contract CustomToken is ERC20, ERC20Burnable, ReentrancyGuard, Ownable {
    // Wallet address for marketing
    address public marketingWallet;

    // Uniswap router and pair addresses
    address public uniswapV2RouterAddress;
    IUniswapV2Router02 public uniswapV2Router;
    address public uniswapV2Pair;

    // Tax rate for transactions and total supply
    uint256 public taxRate = 4;
    uint256 public constant TOTAL_SUPPLY = 450000000000000 * 10**18;
    uint256 public minEthBeforeLiquidity = 0.05 ether;

    // Constructor to initialize the token
    constructor(
        string memory name,
        string memory symbol,
        address _marketingWallet,
        address _uniswapV2RouterAddress
    ) ERC20(name, symbol) {
        marketingWallet = _marketingWallet;
        uniswapV2RouterAddress = _uniswapV2RouterAddress;

        // Initialize Uniswap router and create a pair
        uniswapV2Router = IUniswapV2Router02(uniswapV2RouterAddress);
        uniswapV2Pair = IUniswapV2Factory(uniswapV2Router.factory()).createPair(address(this), uniswapV2Router.WETH());

        // Mint the total supply to the sender
        _mint(msg.sender, TOTAL_SUPPLY);
    }

    // Override the transfer function to include tax and distribution
    function _transfer(address sender, address recipient, uint256 amount) internal override nonReentrant {
        uint256 taxAmount = (amount * taxRate) / 100;
        uint256 marketingAmount = taxAmount / 2; // 50% of the tax (2% of the transaction)
        uint256 liquidityAmount = taxAmount / 2; // 50% of the tax (2% of the transaction)

        // Transfer tax to marketing wallet
        super._transfer(sender, marketingWallet, marketingAmount);

        // Add liquidity if the contract balance is above the minimum
        if (address(this).balance >= minEthBeforeLiquidity) {
            addLiquidity(liquidityAmount);
        }

        // Transfer the remaining amount to the recipient
        uint256 sendAmount = amount - taxAmount;
        super._transfer(sender, recipient, sendAmount);
    }

    // Function to add liquidity to Uniswap
    function addLiquidity(uint256 tokenAmount) internal onlyOwner {
        uint256 half = tokenAmount / 2;
        uint256 otherHalf = tokenAmount - half;

        uint256 balanceBeforeSwap = address(this).balance;
        swapTokensForEth(half);
        uint256 ethReceived = address(this).balance - balanceBeforeSwap;

        // Add liquidity to Uniswap
        uniswapV2Router.addLiquidityETH{value: ethReceived}(address(this), otherHalf, 0, 0, address(this), block.timestamp);
    }

    // Function to swap tokens for ETH
    function swapTokensForEth(uint256 tokenAmount) internal {
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = uniswapV2Router.WETH();

        // Approve the router to spend tokens
        _approve(address(this), uniswapV2RouterAddress, tokenAmount);

        // Perform the swap
        uniswapV2Router.swapExactTokensForETHSupportingFeeOnTransferTokens(tokenAmount, 0, path, address(this), block.timestamp);
    }
}

```

---

<div class="post-metadata">

**Author:** ![Jan\_Mirga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/jan_mirga/32/18792_2.png) [@Jan\_Mirga](https://forum.openzeppelin.com/u/Jan_Mirga)\
**Post date:** [December 6, 2023, 10:14pm UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/2 "2023-12-06T22:14:32Z")

</div>

2% will go to liquidation, half of which (i.e. 1% of the total transaction) will be converted to ETH. These funds will accumulate until they reach 0.05 Ether to minimize the transaction fees associated with frequently moving smaller amounts into the liquidity pool.

I want to ask, will this function automatically send to liquidity?

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [December 7, 2023, 11:01am UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/3 "2023-12-07T11:01:05Z")

</div>

> [@Jan\_Mirga](#):
>
> I have several errors in the code

You'll need to do a little more than that. Please describe the error in details, including the steps required for reproducing it, as well as any other information which might be useful for addressing the problem.

---

<div class="post-metadata">

**Author:** ![Jan\_Mirga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/jan_mirga/32/18792_2.png) [@Jan\_Mirga](https://forum.openzeppelin.com/u/Jan_Mirga)\
**Post date:** [December 7, 2023, 5:33pm UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/4 "2023-12-07T17:33:12Z")

</div>

I have these errors in the code

```auto
TypeError: Trying to override non-virtual function. Did you forget to add "virtual"?
   --> @openzeppelin/contracts/token/ERC20/ERC20.sol:171:5:
    |
171 | function _transfer(address from, address to, uint256 value) internal {
    | ^ (Relevant source part starts here and spans across multiple lines).
Note: Overriding function is here:
  --> geniius/genius.sol:44:5:
   |
44 | function _transfer(address sender, address recipient, uint256 amount) internal override nonReentrant {
   | ^ (Relevant source part starts here and spans across multiple lines).

```

next :

```auto
TypeError: Trying to override non-virtual function. Did you forget to add "virtual"?
   --> @openzeppelin/contracts/token/ERC20/ERC20.sol:171:5:
    |
171 | function _transfer(address from, address to, uint256 value) internal {
    | ^ (Relevant source part starts here and spans across multiple lines).
Note: Overriding function is here:
  --> geniius/genius.sol:44:5:
   |
44 | function _transfer(address sender, address recipient, uint256 amount) internal override nonReentrant {
   | ^ (Relevant source part starts here and spans across multiple lines).

```

next :

```auto
TypeError: No arguments passed to the base constructor. Specify the arguments or mark "CustomToken" as abstract.
  --> geniius/genius.sol:11:1:
   |
11 | contract CustomToken is ERC20, ERC20Burnable, ReentrancyGuard, Ownable {
   | ^ (Relevant source part starts here and spans across multiple lines).
Note: Base constructor parameters:
  --> @openzeppelin/contracts/access/Ownable.sol:38:16:
   |
38 | constructor(address initialOwner) {
   | ^^^^^^^^^^^^^^^^^^^^^^

```

I don't know if the library needs to be updated.  
I will be very grateful if someone can help me

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [December 7, 2023, 7:56pm UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/5 "2023-12-07T19:56:09Z")

</div>

> [@Jan\_Mirga](#):
>
> ```auto
> TypeError: Trying to override non-virtual function. Did you forget to add "virtual"?
> 
> ```

The first part in this error-message tells you that you are... trying to override a non-virtual function.  
This means that the reason for the error can be exactly one of the following two:

1. The function in the inherited contract should be declared `virtual`
2. The function in the inheriting contract should not be declared `override`

Since you did not implement the inherited contract (ERC20), you cannot apply the 1st option.  
Since you did implement the inheriting contract (CustomToken), you can apply the 2nd option.

Unfortunately, the compiler has no knowledge regarding which contract you implemented and which contract you took from someone else (OZ in this case), hence the second part in the error-message - `Did you forget to add "virtual"` - actually hints to you that you should apply the 1st option.

---

<div class="post-metadata">

**Author:** ![Jan\_Mirga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/jan_mirga/32/18792_2.png) [@Jan\_Mirga](https://forum.openzeppelin.com/u/Jan_Mirga)\
**Post date:** [December 7, 2023, 9:12pm UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/6 "2023-12-07T21:12:07Z")

</div>

> [@Jan\_Mirga](#):
>
> 2% will go to liquidation, half of which (i.e. 1% of the total transaction) will be converted to ETH. These funds will accumulate until they reach 0.05 Ether to minimize the transaction fees associated with frequently moving smaller amounts into the liquidity pool.
> 
> I want to ask, will this function automatically send to liquidity?

Thank you, I will try to fix it, and as for the previous question, can you advise me?

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [December 7, 2023, 9:28pm UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/7 "2023-12-07T21:28:51Z")

</div>

Why don't you test it on a local network (for example, using Truffle or HardHat)? You'll need to implement a mock for each one of the 3rd-party contracts used in your code (uniswap, etc).

---

<div class="post-metadata">

**Author:** ![Jan\_Mirga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/jan_mirga/32/18792_2.png) [@Jan\_Mirga](https://forum.openzeppelin.com/u/Jan_Mirga)\
**Post date:** [December 8, 2023, 7:01am UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/8 "2023-12-08T07:01:18Z")

</div>

I don't know what to do when I remove the override  
it gives me this error::

```auto
TypeError: Overriding function is missing "override" specifier.
  --> geniius/genius.sol:44:1:
   |
44 | function _transfer(address sender, address recipient, uint256 amount) internal nonReentrant {
   | ^ (Relevant source part starts here and spans across multiple lines).
Note: Overridden function is here:
   --> @openzeppelin/contracts/token/ERC20/ERC20.sol:171:5:
    |
171 | function _transfer(address from, address to, uint256 value) internal {
    | ^ (Relevant source part starts here and spans across multiple lines).

```

What should I do to change the transfer function?

or edit in ERC20 openzeppelin, but I shouldn't do that?

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [December 8, 2023, 7:17am UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/9 "2023-12-08T07:17:13Z")

</div>

Yeah, my previous description was wrong:

> [@barakman](#):
>
> This means that the reason for the error can be exactly one of the following two:
> 
> 1. The function in the inherited contract should be declared `virtual`
> 2. The function in the inheriting contract should not be declared `override`

What it REALLY means, is exactly one of the following two:

1. The function in the inherited contract should be declared `virtual`
2. The function in the inherited contract CANNOT be overridden in the inheriting contract

In other words, since OZ did not declare this function `virtual` in their contract, you CANNOT override this function in your contract.

As far as I remember, in OZ v5, you should only override function `_update`.

---

<div class="post-metadata">

**Author:** ![Jan\_Mirga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/jan_mirga/32/18792_2.png) [@Jan\_Mirga](https://forum.openzeppelin.com/u/Jan_Mirga)\
**Post date:** [December 8, 2023, 7:33am UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/10 "2023-12-08T07:33:04Z")

</div>

like this

```auto
// Modify the _update function to include your custom logic for transaction fees
function _update(address from, address to, uint256 value) internal virtual override {
    if (from == address(0)) {
        // Logic for minting tokens (when from == address(0))
        super._update(from, to, value);
    } else if (to == address(0)) {
        // Logic for burning tokens (when to == address(0))
        super._update(from, to, value);
    } else {
        // Calculate the fee for normal transactions
        uint256 taxAmount = (value * taxRate) / 100;
        uint256 marketingAmount = (value * 2) / 100; // 2% for marketing
        uint256 liquidityAmount = (value * 2) / 100; // 2% for liquidity

        // Transfer the fee to the marketing wallet
        if (marketingAmount > 0) {
            super._update(from, marketingWallet, marketingAmount);
        }

        // Add liquidity if needed
        if (address(this).balance >= minEthBeforeLiquidity && liquidityAmount > 0) {
            addLiquidity(liquidityAmount);
        }

        // Transfer the remaining amount to the recipient
        uint256 sendAmount = value - taxAmount;
        super._update(from, to, sendAmount);
    }
}

```

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [December 8, 2023, 7:55am UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/11 "2023-12-08T07:55:15Z")

</div>

> [@Jan\_Mirga](#):
>
> ```auto
> uint256 sendAmount = value - taxAmount;
> 
> ```

I'm pretty sure that this should be:

```auto
uint256 sendAmount = value - (taxAmount + marketingAmount + liquidityAmount);

```

---

<div class="post-metadata">

**Author:** ![Jan\_Mirga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/jan_mirga/32/18792_2.png) [@Jan\_Mirga](https://forum.openzeppelin.com/u/Jan_Mirga)\
**Post date:** [December 8, 2023, 8:05am UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/12 "2023-12-08T08:05:33Z")

</div>

thank you bro  
I want to ask you again, should I test the contract through Holesky

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [December 8, 2023, 8:20am UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/13 "2023-12-08T08:20:07Z")

</div>

Never heard of `Holesky`; I'm familiar with Truffle, HardHat and Forge.  
You should definitely test it though, regardless of what testing tool you use, **prior** to deploying it.

---

<div class="post-metadata">

**Author:** ![Jan\_Mirga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/jan_mirga/32/18792_2.png) [@Jan\_Mirga](https://forum.openzeppelin.com/u/Jan_Mirga)\
**Post date:** [December 8, 2023, 11:26am UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/14 "2023-12-08T11:26:52Z")

</div>

deploying the contract still fails

 ![Snímka obrazovky 2023-12-08 122614](https://us1.discourse-cdn.com/flex022/uploads/zeppelin/original/3X/d/1/d1ecf5d4afbedcf94628d127ae9f688efb2eed9c.png)

---

<div class="post-metadata">

**Author:** ![barakman](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/barakman/32/855_2.png) [@barakman](https://forum.openzeppelin.com/u/barakman)\
**Post date:** [December 8, 2023, 12:11pm UTC](https://forum.openzeppelin.com/t/errors-in-the-contract/38739/15 "2023-12-08T12:11:23Z")

</div>

> [@Jan\_Mirga](#):
>
> deploying the contract still fails

This means that one of the lines in the constructor reverts the transaction.

In order to find out which line it is, remove them one by one starting from the last one, and deploy the contract each time until the transaction is successfully executed.
