【问题标题】:Test cases failing due to custom modifier由于自定义修饰符导致测试用例失败
【发布时间】:2022-06-10 22:27:38
【问题描述】:

我是 Solidity 新手,在使用开放式 Zepplin 插件之前尝试自己编写代码。

这是合同

// 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;
   }

}

我知道我们有来自 openzepplin 的 Ownable 插件。但只是尝试使用我自己的修饰符,因为我希望 2 个用户添加获胜者。

合同运作良好。但是我在编写测试用例时遇到了问题。

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"
      );
  });
});

添加获胜者失败,不确定如何通过 AuthAccounts。任何指导都会有很大帮助

错误

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

【问题讨论】:

    标签: solidity hardhat


    【解决方案1】:

    这里没有将任何地址传递给构造函数:

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

    您需要像这样将 _addr_1 和 _addr_2 添加到构造函数中:

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

    编译器不应该让你编译这个合约,所以我不确定它是如何让你部署它的。

    然后在您的测试中,您需要像这样传递参数:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-28
      • 1970-01-01
      相关资源
      最近更新 更多