Test cases failing due to custom modifier

Viewed 66

I am new to solidity and trying to code it on my own before using open Zepplin plugins.

Here is the contract

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

   contract LeagueWinners {

struct Winner {
    bool exists;
    bool claimed;
    uint256 reward;
}

mapping(address=>Winner) public winners;
mapping (address => bool) private AuthAccounts;


modifier onlyAuthAccounts() {
    require(AuthAccounts[msg.sender], "Auth: caller is not the authorized");
    _;
}

constructor () {
    AuthAccounts[_addr_1] = true;
    AuthAccounts[_addr_2] = true;
}


function addWinner(address _address, uint256 _amount ) public {
       Winner storage winner = winners[_address];
       winner.exists = true;
       winner.reward = _amount;
   }

}

I know we have the Ownable plugin from openzepplin. but just trying with my own modifier as I want 2 users to add winners.

The contract works well. but I am facing issues in writing test cases.

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

describe("LeagueWinners", function () {
  
  before(async () => {
    LeagueWinners = await ethers.getContractFactory("LeagueWinners");
    leagueWiners = await LeagueWinners.deploy();
    await leagueWiners.deployed();
    [owner] = await ethers.getSigners();
  });

  it("Claim Tokens to be deployed and verify owner", async function () {
    expect(await leagueWiners.owner()).to.equal(owner.address);
  });

  it("Add Winner", async function () {
    winner = await leagueWiners
      .connect(owner)
      .addWinner(
        "_addr",
        "50000000000000000000"
      );
  });
});

Add winner is getting failed, not sure how to pass the AuthAccounts. Any guidance will be great help

Error

Error: VM Exception while processing transaction: reverted with reason string 'Auth: caller is not the authorized'
2 Answers

You don't pass any addresses to the constructor here:

constructor () {
    AuthAccounts[_addr_1] = true;
    AuthAccounts[_addr_2] = true;
}

You need to add the _addr_1 and _addr_2 into the constructor like this:

constructor (address _addr_1, address _addr_2) {
        AuthAccounts[_addr_1] = true;
        AuthAccounts[_addr_2] = true;
    }

The compiler shouldn't let you compile this contract so I'm not sure how it lets you deploy it.

Then in your test you need to pass the arguments like this:

before(async () => {
    LeagueWinners = await ethers.getContractFactory("LeagueWinners");
    leagueWiners = await LeagueWinners.deploy(addr1, addr2); // HERE
    await leagueWiners.deployed();
    [owner] = await ethers.getSigners();
  });

After spending a few days, made a solution in this way.

constructor () {
    AuthAccounts[msg.sender] = true;
    AuthAccounts[_addr_1] = true;
    AuthAccounts[_addr_2] = true;
}

As i was hardcoding other addresses, it was not possible to get signer info in the test cases. Passing msg.sender in AuthAccounts helped to achieve that and test cases got passed.

Let me know if there are any other work arounds.

Related