Hardhat testing getting failed with "balanceOf" is not the function error

Hi there! I have been trying to test smart contracts with the help of hardhat. but everytime i get the error of 'balanceOf' is not the function. and i have tried testing other smart contract too but i start to have same kinda error with both smart contracts. could pleae anyone comeup with the idea of fixing this? truly appreciate.

please get used to providing code and examples when asking for help


hey thankyou for the reply. i am so sorry i am so new to this. did not realise it. and next time i will for sure will come up with pictures. btw for now i have uploaded the picture. thanks

Please copy paste the token.sol and token.js files if possible

sure i could. is there a way to paste it here? or do i have to make a link of it?
what would be the right way to send?
truly help would be appreciated.

you just copy paste it in here as a reply, but wrap it in 2 ticks which look like this: ``

`// SPDX-License-Identifier: GPL-3.0

pragma solidity >= 0.5.0 < 0.9.0;

contract Token{
string public name="hardhat token";
string public symbol="HHT";
uint public totalSupply = 10000;

address public owner;

mapping(address => uint256)balances;

constructor(){
    balances[msg.sender] = totalSupply;
    owner = msg.sender;
}
function transfer(address to, uint256 amount)external {
    require(balances[msg.sender] >=amount,"Not enough token");
    balances[msg.sender] -=amount; //balances[msg.sender]=balances[msg.sender]-amount;
    balances[to] += amount;
}
}`

`const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Token contract", function () {

let Token;
let hardhatToken;
let owner;
let addr1;
let addr2;
let addrs;

beforeEach(async function () {

Token = await ethers.getContractFactory("Token");
[owner, addr1, addr2, ...addrs] = await ethers.getSigners();
  hardhatToken = await Token.deploy();

});

describe("Deployment", function () {

it("Should set the right owner", async function () {
  
  expect(await hardhatToken.owner()).to.equal(owner.address);
});

it("Should assign the total supply of tokens to the owner", async function () {
  const ownerBalance = await hardhatToken.balanceOf(owner.address);
  expect(await hardhatToken.totalSupply()).to.equal(ownerBalance);
});

});

describe("Transactions", function () {
it("Should transfer tokens between accounts", async function () {

  await hardhatToken.transfer(addr1.address, 50);
  const addr1Balance = await hardhatToken.balanceOf(addr1.address);
  expect(addr1Balance).to.equal(50);

  
  await hardhatToken.connect(addr1).transfer(addr2.address, 50);
  const addr2Balance = await hardhatToken.balanceOf(addr2.address);
  expect(addr2Balance).to.equal(50);
});

it("Should fail if sender doesn’t have enough tokens", async function () {
  const initialOwnerBalance = await hardhatToken.balanceOf(owner.address);

 await expect(
    hardhatToken.connect(addr1).transfer(owner.address, 1)
  ).to.be.revertedWith("Not enough tokens");

    expect(await hardhatToken.balanceOf(owner.address)).to.equal(
    initialOwnerBalance
  );
});

it("Should update balances after transfers", async function () {
  const initialOwnerBalance = await hardhatToken.balanceOf(owner.address);
   await hardhatToken.transfer(addr1.address, 100);
  await hardhatToken.transfer(addr2.address, 50);

  const finalOwnerBalance = await hardhatToken.balanceOf(owner.address);
  expect(finalOwnerBalance).to.equal(initialOwnerBalance.sub(150));

  const addr1Balance = await hardhatToken.balanceOf(addr1.address);
  expect(addr1Balance).to.equal(100);

  const addr2Balance = await hardhatToken.balanceOf(addr2.address);
  expect(addr2Balance).to.equal(50);
});

});
});`

hey! thankyou vey much again. please check the code :slight_smile:

the formatting is not quite right :sweat_smile:

but if that is the whole code, then the problem is clearly that your Token contract does not have a balanceOf() function. So I assume that you were trying to mix code from two different sources/tutorials

1 Like

have figured it. thankyou very much tho.

1 Like

hey , i am facing the same issue , can you pls tell me how to resolve this issue

My contract does not have had that function. Trying to check if you have the same function implemented in the contract as well.