Override approve in OpenZeppelin Contracts version 2.x

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