I have been trying to create a smart contract/Token, will deploy this on the Binance Smart Chain test net. I followed some documentation and started with this. I am getting into this function issue. Function is declared as Read only. Here is the source code

The function is changing the state of the Owner Address, what is the other option to declare it as read only


// SPDX-License-Identifier: GPL-3.0

pragma solidity ^0.8.2;

//import "Context.sol";
//import "IBEP20.sol";
//import "SafeMath.sol";
//import "Ownable.sol";

contract SampleTaken {
    
    
    mapping(address => uint) public balances;
    
    uint public totalSupply = 1000000 * 10 ** 18;
    string public name ="Sample Token";
    string public symbol ="KJA";
    uint public decimals = 18;
    
    /** Events aailable for the Contract**/
    
    event Transfer(address indexed _from, address indexed _to, uint256 _value);
    
    constructor(){
        balances[msg.sender] = totalSupply;
    }
    
    function balanceOf(address _ownerAddress) public view returns (uint){
        return balances[_ownerAddress];
    }
    
    function transfer(address _toAddress, uint _noOfTokens) public view returns (bool){
    require(balanceOf(msg.sender) >= _noOfTokens, "Total balance is less than the number of Tokens asked for !!!");
    balances[_toAddress] +=_noOfTokens;
    balances[msg.sender] -= _noOfTokens;
    emit Transfer(msg.sender,_toAddress, _noOfTokens);
    return true;
    }
    
    function transferFrom(address _from, address _to, uint _value) public returns (bool){
     require(balanceOf(_from) >= _value, "Balance is less than the number of Tokens asked for !!!");
    // require(allowance[_from][msg.sender] >= _value, "Allowance too low");
     balances[_to] += _value;
     balances[_from] -= _value;   
     
     emit Transfer (_from, _to, _value);
     return true;
     
     }    
}

Any help is much appreciated.

Regards

2

There are 2 answers

2
Petr Hejda On

Your transfer() function is declared as a view function.

Functions can be declared view in which case they promise not to modify the state.

Source: Solidity docs

But these lines (within the transfer() function) modify the state:

balances[_toAddress] +=_noOfTokens;
balances[msg.sender] -= _noOfTokens;
emit Transfer(msg.sender,_toAddress, _noOfTokens);

If you want your function to modify the state, it cannot be a view (nor a pure) function - and you need to remove the view modifier:

function transfer(address _toAddress, uint _noOfTokens) public returns (bool){
0
Codemaker2015 On

Remove view or pure keyword from the function declaration and keep only public returns

Change

function transfer(address _toAddress, uint _noOfTokens) public view returns (bool)

to

function transfer(address _toAddress, uint _noOfTokens) public returns (bool)

Changed method:

function transfer(address _toAddress, uint _noOfTokens) public returns (bool){
    require(balanceOf(msg.sender) >= _noOfTokens, "Total balance is less than the number of Tokens asked for !!!");
    balances[_toAddress] +=_noOfTokens;
    balances[msg.sender] -= _noOfTokens;
    emit Transfer(msg.sender,_toAddress, _noOfTokens);
    return true;
    }