A problem in my transfer function when I define it view

Hi, when I don’t define the transfer function as a view, it gives the this error: Overriding function changes state mutability from “view” to “nonpayable”.And when I define it view,it gives the this error:Function cannot be declared as view because this expression (potentially) modifies the state..Thank you for your help.
<>
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./IERC20.sol";
import "./safeMath.sol";
import "./context.sol";
import "./ownable.sol";

contract ERC20 is Context, Ownable , IERC20
{
mapping(address => uint256) _balances;
mapping(address => mapping (address => uint256)) _allowances;

uint private _totalSupply;
uint private _decimalse;
string private _name;
string private _symbol;

constructor(string memory name_,string memory symbol_,uint totalSupply_,uint decimalse_)
{
    _name=name_;
    _symbol=symbol_;
    _totalSupply=totalSupply_;
    _decimalse=decimalse_;
}

function name() public view virtual returns(string memory)
{
    return _name;
}
function symbol() public view virtual returns(string memory)
{
    return _symbol;
}
function decimals() public view virtual returns(uint)
{
    return _decimalse;
}
function totalSupply() public view virtual override returns(uint)
{
    return _totalSupply;
}
function balanceOf(address account) public view override returns(uint)
{
    return _balances[account];
}

function _transfer(address from,address to,uint256 amount) internal virtual
{
    require(from != address(0),"ERC20:transfer from the zero address!");
    require(to != address(0),"ERC20:transfer to the zero address!");
    
    uint256 fromBalance=_balances[from];
    require(fromBalance>=amount,"ERC20:transfer amount exceeds balance!");

    _balances[from]=_balances[from]-amount;
    _balances[to]=_balances[to]+amount;

    emit Transer (from,to,amount);
} 

function _approve(address owner,address spender,uint256 amount) internal virtual
{
    require(owner != address(0),"ERC20:approve owner the zero address!");
    require(spender != address(0),"ERC20:approve spender the zero address!");
    
    _allowances[owner][spender]=amount;

    emit Approval (owner,spender,amount);
}

function transfer(address to,uint256 amount) public virtual override returns(bool)
{
    address from =_msgSender();
    _transfer(from,to,amount);

    return true;
}

function allowance (address owner,address spender) public view virtual override returns(uint256)
{
    return _allowances[owner][spender];
}

function approve (address spender,uint256 amount) public view virtual override returns(bool)
{
   address owner=_msgSender();
   _approve(owner,spender,amount);
   return true;
}

function transferFrom (address from,address to,uint256 amount) public view virtual override returns(bool)
{
    address spender=_msgSender();
    require(amount <= _allowances[from][spender]);
    _allowances[from][spender]=_allowances[from][spender]-amount;

    _transfer(from,to,amount);

    return true;
}

}
</>

It is generally not clear why you inherit from IERC20 and then override all of its contents with pretty much identical contents. For sure, it is not clear why you redefine all the state variables.

With regards to the actual problem described in your question, it seems that you only need to get rid of the view restriction in function approve and in function transferFrom.

Thank you for your help. :pray:

1 Like