Can someone help me identify what's wrong with my code?

I’m using the openzeppelin ERC20 and IERC20 contracts from the github and I’m trying to build a contract with a built in autoburn and auto stop, I’ve found some stuff and worked this much out on my own, however I can’t seem to figure out why I’m getting the ParserError.

I’m using Remix and the error I’m getting is the following:

ParserError: Expected primary expression. --> MYT.sol:23:9: | 23 | override return super.transferFrom(from, to, _partialBurn(amount)); | ^^^^^^^^

My code I have is this:

pragma solidity ^0.8.0;

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/IERC20.sol";


contract MyToken is ERC20 {

    uint256 private _minimumSupply = 10000000 * (10 ** 18);

    constructor () public ERC20("MyToken", "MYT", 18) {
        _mint(msg.sender, 500000000000000 * (10 ** uint256(decimals())));
    }

    function transfer(address to, uint256 amount) public returns (bool) {
        return super.transfer(to, _partialBurn(amount));
    }

    function transferFrom(address from, address to, uint256 amount) public returns (bool) {
        override return super.transferFrom(from, to, _partialBurn(amount));
    }

    function _partialBurn(uint256 amount) internal returns (uint256) {
        override uint256 burnAmount = _calculateBurnAmount(amount);

        if (burnAmount > 0) {
            _burn(msg.sender, burnAmount);
        }

        return amount.sub(burnAmount);
    }

    function _calculateBurnAmount(uint256 amount) internal view returns (uint256) {
        uint256 burnAmount = 0;

        
        if (totalSupply() > _minimumSupply) {
            burnAmount = amount.div(1000);
            uint256 availableBurn = totalSupply().sub(_minimumSupply);
            if (burnAmount > availableBurn) {
                burnAmount = availableBurn;
            }
        }

        return burnAmount;
    }
}

Remove override keyword and add it like this:

function transferFrom(address from, address to, uint256 amount) public override returns (bool) {
         return super.transferFrom(from, to, _partialBurn(amount));
  }

Same for other functions to. Try it out

2 Likes

Hi, I think there are some errors in your code.

1).

constructor () public ERC20("MyToken", "MYT", 18) {
        _mint(msg.sender, 500000000000000 * (10 ** uint256(decimals())));
    }

It seems like you are use the latest version of the OpenZeppelin Contract, so for the ERC20 contract, it only has two parameters in the constructor, the decimals of the contract is 18 by default.

2).

function transferFrom(address from, address to, uint256 amount) public returns (bool) {
    override return super.transferFrom(from, to, _partialBurn(amount));
}

There is a keyword override, and it means when you use it, you want to change a function behavior that inherits from a base contract, and the keyword override should be in the function header. But it seems like you do not change any thing, so I am not sure why you want to use this keyword.

3).

function _partialBurn(uint256 amount) internal returns (uint256) {
        override uint256 burnAmount = _calculateBurnAmount(amount);

        if (burnAmount > 0) {
            _burn(msg.sender, burnAmount);
        }

        return amount.sub(burnAmount);
    }

There is not a function named _partialBurn in the base contract ERC20, so you can not use override for this function.

4).
And you are trying to use sub and div to do calculation, but they are not valid, in the compiler version 0.8.x, arithmetic operations will revert on underflow and overflow automatically, so you can just use “-” to replace sub, for more breaking changes for 0.8.x, you can have a look at the ducumentation: Solidity v0.8.0 Breaking Changes — Solidity 0.8.5 documentation

1 Like

Alright, I’m no longer throwing errors. One more thing would be helpful from someone much more experienced than I, can you tell me what the code pasted below will effectively do? I know what it “should” do, but if someone else can effectively read it and knows what it does then I’m golden and didn’t screw this thing up.

}

function _calculateBurnAmount(uint256 amount) internal view returns (uint256) {
    uint256 burnAmount = 0;

    // burn amount calculations
    if (totalSupply() > _minimumSupply) {
        burnAmount = amount / 1000;
        uint256 availableBurn = totalSupply() -(_minimumSupply);
        if (burnAmount > availableBurn) {
            burnAmount = availableBurn;
        }
    }

    return burnAmount;
}