如何将奖励令牌转移到测试案例中

发布于 2025-02-07 13:41:19 字数 2704 浏览 2 评论 0原文

我正在制定样本合同,该合同应为用户提供奖励。获胜者获得了代币的奖励。

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

IERC20 private rewardTokenAddr;


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

constructor (address _rewardTokenAddr) {
    rewardTokenAddr = IERC20(_rewardTokenAddr);
    AuthAccounts[msg.sender] = true;
    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;
   }


function claimPrize() public {
    Winner storage winner = winners[msg.sender];
    require(winner.exists, "Not a winner");
    require(!winner.claimed, "Winner already claimed");
    winner.claimed = true;
    rewardTokenAddr.safeTransfer(msg.sender, winner.tokenAmount);
}

}

当用户试图索取奖品时,测试用例正在失败。我认为合同中没有奖励令牌,因此失败了。

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

describe("LeagueWinners", function () {
  let rewardTokenAddr = "0xAddr.....";
  
  before(async () => {
    LeagueWinners = await ethers.getContractFactory("LeagueWinners");
    leagueWiners = await LeagueWinners.deploy(rewardTokenAddr);
    await leagueWiners.deployed();
    [owner, winner1, winner2, nonwinner] = 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(
        "winner1.address",
        "50000000000000000000"
      );
  });

  it("Confirm Winner Added with proper reward", async function () {
    winner = await leagueWiners.winners(winner1.address);
    expect(winner.reward).to.equal("50000000000000000000");
  });

  it("Non winner cannot claim", async function () {
    await expect(
      leagueWiners.connect(nonwinner).claimReward()).to.be.revertedWith("Not a winner");
  });

  it("Winner to claim", async function () {
    await leagueWiners.connect(winner1).claimPrize();
    winner = await leagueWiners.winners(winner1.address);
    expect(winner.claimed).to.equal(true);
  });

});

错误:

错误:处理交易时的VM异常:用理由字符串'地址恢复:致电非合同'

此行导致错误 等待loagewiners.connect(winner1).claimreward();

I am working on a sample contract which should provide a reward for the user. The winner gets the reward as Token.

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

IERC20 private rewardTokenAddr;


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

constructor (address _rewardTokenAddr) {
    rewardTokenAddr = IERC20(_rewardTokenAddr);
    AuthAccounts[msg.sender] = true;
    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;
   }


function claimPrize() public {
    Winner storage winner = winners[msg.sender];
    require(winner.exists, "Not a winner");
    require(!winner.claimed, "Winner already claimed");
    winner.claimed = true;
    rewardTokenAddr.safeTransfer(msg.sender, winner.tokenAmount);
}

}

Test cases is failing when the user is trying to claim the prize. I assume there is no reward token in the contract so its failing.

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

describe("LeagueWinners", function () {
  let rewardTokenAddr = "0xAddr.....";
  
  before(async () => {
    LeagueWinners = await ethers.getContractFactory("LeagueWinners");
    leagueWiners = await LeagueWinners.deploy(rewardTokenAddr);
    await leagueWiners.deployed();
    [owner, winner1, winner2, nonwinner] = 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(
        "winner1.address",
        "50000000000000000000"
      );
  });

  it("Confirm Winner Added with proper reward", async function () {
    winner = await leagueWiners.winners(winner1.address);
    expect(winner.reward).to.equal("50000000000000000000");
  });

  it("Non winner cannot claim", async function () {
    await expect(
      leagueWiners.connect(nonwinner).claimReward()).to.be.revertedWith("Not a winner");
  });

  it("Winner to claim", async function () {
    await leagueWiners.connect(winner1).claimPrize();
    winner = await leagueWiners.winners(winner1.address);
    expect(winner.claimed).to.equal(true);
  });

});

Error:

Error: VM Exception while processing transaction: reverted with reason string 'Address: call to non-contract'

This line is causing the error
await leagueWiners.connect(winner1).claimReward();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅笑依然 2025-02-14 13:41:19

从您提供的智能合约来源中,我看到函数的名称为siperprize() not sairsereward()

如果您更改为:

leagueWiners.connect(winner1).claimPrize();

From the smart contract source that you provided I see that the name of function is claimPrize() not claimReward()

Maybe can work if you change to:

leagueWiners.connect(winner1).claimPrize();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文