0

This is the error i am getting while testing my contract with hardhat pls help me to get this

PS E:\vs code files> npx hardhat test
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
× Help us improve Hardhat with anonymous crash reports & basic usage data? (Y/n) · y


    1) Deployment should assign the total supply of the token


  0 passing (4s)
  1 failing

  1) Token Contract
       Deployment should assign the total supply of the token:
     Error: missing argument: passed to contract (count=1, expectedCount=2, code=MISSING_ARGUMENT, version=contracts/5.6.2)
      at Logger.makeError (node_modules\@ethersproject\logger\src.ts\index.ts:261:28)
      at Logger.throwError (node_modules\@ethersproject\logger\src.ts\index.ts:273:20)
      at Logger.checkArgumentCount (node_modules\@ethersproject\logger\src.ts\index.ts:332:18)
      at E:\vs code files\node_modules\@ethersproject\contracts\src.ts\index.ts:187:12
      at step (node_modules\@ethersproject\contracts\lib\index.js:48:23)
      at Object.next (node_modules\@ethersproject\contracts\lib\index.js:29:53)
      at E:\vs code files\node_modules\@ethersproject\contracts\lib\index.js:23:71
      at new Promise (<anonymous>)
      at __awaiter (node_modules\@ethersproject\contracts\lib\index.js:19:12)
      at populateTransaction (node_modules\@ethersproject\contracts\lib\index.js:152:12)
//SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
contract Token{
    string public name = "HARDHAT TESTING";
    string public symbol = "HHH";
    uint256 public totalsupply = 10000;
    mapping(address=>uint256) balances;
    address owner;
    constructor()
    {
        balances[msg.sender] = totalsupply;
        owner = msg.sender;
    }
    function balanceof(address _to,uint256 amount) external {
        require(balances[msg.sender] > amount,"not enought tokens");
        balances[msg.sender] -=amount;
        balances[_to] +=amount;
    }
    function showbalance(address account) external view returns(uint256)
    {
        return balances[account];
    }
}

this is my contract

const {expect} = require("chai");

describe("Token Contract",function()
{
    it("Deployment should assign the total supply of the token",async function()
    {
        const [owners] = await ethers.getSigners();  // represent an ethereum account
        console.log("Signers Object",owners);
        const Token = await ethers.getContractFactory("Token");  // instance created
        const deploy_token  = await Token.deploy(); // deploy the contract statement
        const ownerbalance = await deploy_token.balanceof(owners.address);  //calling balanceof function of the contract
        console.log(owners.address);
        expect(await ownerbalance.showbalance(owners).to.equal(ownerbalance));
    });
});

My token.js file

Thanks in advance

2 Answers 2

0

balanceof(address _to,uint256 amount) must receive 2 args, but you pass only 1

await deploy_token.balanceof(owners.address)

P.S. Looks like you use wrong code inside balanceOf function. Balance of must show user baalance, but there is a code for transfer function.

0

Fix Your Contract Function Name: In your Solidity contract, rename the function balanceof to transfer.

Run Your Test Correctly: In your test code, you're using the function balanceof in a way that it's not supposed to be used. Instead, use showbalance to check the balance of the owner's address.

Fix the expect Line: In the test, you wrote the expect line incorrectly. You should be comparing the initial balance to '10000'.

Test:

describe("Token Contract", function () { it("Deployment should assign the total supply of the token", async function () { const [owner] = await ethers.getSigners(); const Token = await ethers.getContractFactory("Token"); const deployed_token = await Token.deploy(); await deployed_token.deployed();

const initialOwnerBalance = await deployed_token.showbalance(owner.address);

// This line checks if the owner's initial balance is 10000.
expect(initialOwnerBalance.toString()).to.equal('10000');

}); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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