如何写合同为玩家设定奖励并让他们领取奖励?

how to write a contract to set a reward for players and they claim the reward?

我想创建一个合约来给玩家奖励并且他们领取奖励。

看来一切正常!

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

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract Rewards is Ownable {
    IERC20 public rewardsToken;

    mapping(address => uint) public rewards;

    constructor(address _rewardsToken) {
        rewardsToken = IERC20(_rewardsToken);
    }

    function setReward(address account,uint256 amount)  public onlyOwner  {
        rewards[account] = amount;
    }

    function claimReward() public{
        uint256 reward = rewards[msg.sender];
        rewards[msg.sender] = 0;
        rewardsToken.transfer(msg.sender, reward);
    }
}

但我不知道为什么当我领取奖励时没有任何反应,而且我有这个错误。

如何将代币发送到我的合约?

rewardsToken.transfer(msg.sender, reward);

此代码段失败,因为 Rewards 合约地址没有足够的代币余额。

解决方案:至少将reward数量的代币发送到Rewards合约地址,以便合约可以将它们转移给用户。