0

Here are the errors i'm getting with the code all coming from balanceOf function:

TypeError: Data location must be "memory" or "calldata" for parameter in function, but none was given. --> contracts/Htoken.sol:39:24: | 39 | function balanceOf(address [msg.sender]) public override view returns (uint256) { | ^^^^^^^^^^^^^^^^^^^^

TypeError: Invalid array length, expected integer literal or constant expression. --> contracts/Htoken.sol:39:33: | 39 | function balanceOf(address [msg.sender]) public override view returns (uint256) { | ^^^^^^^^^^

THE CONTRACT CODE

contract NewToken {

string public constant name = "NewToken";
string public constant symbol = "NTKN";
uint8 public constant decimals = 18;
uint256 totalSupply_ = 10000000000000000000000000; 

mapping(address => uint256) public balances;
mapping(address => mapping (address => uint256)) allowed;

event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);

constructor() { balances[msg.sender] = totalSupply_; }

function totalSupply() public override view returns (uint256) {
return totalSupply_;
}

function balanceOf(address [msg.sender]) public override view returns (uint256) {
    return balances[msg.sender];
}

function transfer(address to, uint256 value) public override returns (bool) {
    require(value <= balances[msg.sender]);
    balances[msg.sender] = balances[msg.sender]-value;
    balances[to] = balances[to]+value;
    emit transfer(msg.sender, to, value);
    return true;
}

function approve(address spender, uint256 value) public override returns (bool) {
    allowed[msg.sender][spender] = value;
    emit Approval(msg.sender, spender, value);
    return true;
}

function allowance(address owner, address spender) public override view returns (uint) {
    return allowed[owner][spender];
}

function transferFrom(address from, address to, uint256 value) public override returns (bool) {
    require(value <= balances[from]);
    require(value <= allowed[from][msg.sender]);

    balances[from] = balances[from]-value;
    allowed[from][msg.sender] = allowed[from][msg.sender]+value;
    balances[to] = balances[to]+value;
    emit Transfer(from, to, value);
    return true;
}

}

1 Answer 1

0

I changed your smart contract implementation with this smart contract.

I put notes just so you'd know your errors.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// NOTE: Import IERC20 contract
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

// NOTE: You must to implement IERC20 for override ERC20 methods!
contract NewToken is IERC20 {

  string public constant name = "NewToken";
  string public constant symbol = "NTKN";
  uint8 public constant decimals = 18;
  uint256 totalSupply_ = 10000000000000000000000000; 

  mapping(address => uint256) public balances;
  mapping(address => mapping (address => uint256)) allowed;

  // NOTE: I removed the events because it already defined into IERC20
  
  constructor() { 
    balances[msg.sender] = totalSupply_;
  }

  function totalSupply() public override view returns (uint256) {
    return totalSupply_;
  }

  // NOTE: You must to change signature function adding address parameter
  function balanceOf(address _address) public override view returns(uint256) {
    return balances[_address];
  }

  function transfer(address to, uint256 value) public override returns (bool) {
      require(value <= balances[msg.sender]);
      balances[msg.sender] = balances[msg.sender]-value;
      balances[to] = balances[to]+value;
      // NOTE: The event is called  Transfer not 'transfer'
      emit Transfer(msg.sender, to, value);
      return true;
  }

  function approve(address spender, uint256 value) public override returns (bool) {
      allowed[msg.sender][spender] = value;
      emit Approval(msg.sender, spender, value);
      return true;
  }

  function allowance(address owner, address spender) public override view returns (uint) {
      return allowed[owner][spender];
  }

  function transferFrom(address from, address to, uint256 value) public override returns (bool) {
      require(value <= balances[from]);
      require(value <= allowed[from][msg.sender]);

      balances[from] = balances[from]-value;
      allowed[from][msg.sender] = allowed[from][msg.sender]+value;
      balances[to] = balances[to]+value;
      emit Transfer(from, to, value);
      return true;
  }
}
4
  • Thanks @Kerry99, you saved me a lots of time. i did define my IERC interface but forget adding to the contract as 'is IERC20' after adding that and moving the event to the interface changing the signature it work. Thanks again
    – Alfred H
    Apr 23 at 12:44
  • Hi @Kerry99 would you be up for a PM, i'm running into some issues i wish you could help me through my course of learning
    – Alfred H
    Jun 12 at 19:24
  • Hi, please write me on this Telegram username: @kerry99 and we discus together about this possibility.
    – Kerry99
    Jun 13 at 11:19
  • Thanks @Kerry99
    – Alfred H
    Jun 13 at 20:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.