Override approve in OpenZeppelin Contracts version 2.x

I’m trying to override function approve. In that way, I want to avoid that function to be called by anyone. So, I’m doing:

function approve(address, uint256) public override {
                revert("Approve cannot be called");
            }

And I’m getting this:
ParserError: Expected '{' but got reserved keyword 'override' function approve(address, uint256) public override { ^------^

1 Like

Hi @pedromtelho,

Welcome to the community :wave:

I am curious as why you want to prevent the use of approve. This would mean that your token may not be usable in applications/services that use standard ERC20 functions. It also means users would need to use increaseAllowance to be able to use the ERC20 token in a contract: Example on how to use ERC20 token in another contract


For Solidity 0.5 (assuming you are using OpenZeppelin Contracts 2.x rather than OpenZeppelin Contracts 3.x) you don’t need to use the keyword override.

override was introduced in Solidity 0.6; see: https://docs.soliditylang.org/en/v0.6.0/060-breaking-changes.html

// contracts/SimpleToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.0;

import "@openzeppelin/contracts/GSN/Context.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";

/**
 * @title SimpleToken
 * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` functions.
 */
contract SimpleToken is Context, ERC20, ERC20Detailed {

    /**
     * @dev Constructor that gives _msgSender() all of existing tokens.
     */
    constructor () public ERC20Detailed("SimpleToken", "SIM", 18) {
        _mint(_msgSender(), 10000 * (10 ** uint256(decimals())));
    }

    function approve(address spender, uint256 amount) public returns (bool) {
        revert("SimpleToken: Approve cannot be called");
    }
}
1 Like

I thought that approve function is only used if I want to use transferFrom, increaseAllowance or decreaseAllowance. Is there any need if I want only using transfer and buyTokens functions?

Thank you!!

1 Like

Hi @pedromtelho,

ERC20 is a list of balances for addresses. If you don’t have approve and transferFrom then you can’t use your token with other contracts as those contracts won’t know when tokens are transferred to them.

I recommend reading the EIP: https://eips.ethereum.org/EIPS/eip-20

1 Like

Hmm interesting, thanks so much for your support!!

I’m going to read it!

1 Like