So, I am trying to call a mint function which is present in my ERC20 token contract, which is a standard open Zeppelin Contract, From another Contract.
I am using Interface to call the mint function, from my staking smart contract. But it keeps getting reverted.
I tried the Remix debug as well but, I am not able to find the reason.
My code is as mentioned below:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
interface Token
{
function transfer(address _To, uint256 _Amount) external returns (bool);
function balanceOf(address account) external view returns(uint);
function transferFrom(address from, address to, uint256 amount) external returns (bool);
function mint(address to, uint256 amount) external returns(bool);
}
contract Staking
{
Token ELONToken;
// 2 months = (60*24*60*60)
uint256 public StakeDuration = 1;
// 10% intreset foer the Stakers.
uint8 IntrestRate = 10;
uint256 TotalStakers;
// Structure to hold details regarding the stakers.
struct StakerInfo
{
uint256 startTS;
uint256 endTS;
uint256 amount;
uint256 claimed;
}
//mapping which maps to the structure inorder to have the details of each staker.
mapping(address => StakerInfo) public StakerInfos;
//mapping to hold addresses of stakers.
mapping(address => bool) public StakedAddress;
constructor(Token ERC20_add)
{
require(address(ERC20_add)!= address(0),"Enter Valid Contract address");
ELONToken = ERC20_add;
TotalStakers = 0;
}
// Function to check balance
function CheckBalance(address account) external view returns(uint256)
{
return(ELONToken.balanceOf(account));
}
// Function to call the Mint from ERC20
function MintTokens(address To, uint256 _amount) public returns(bool)
{
ELONToken.mint(To, _amount);
return(true);
}
//Function to Stake Tokens in the smart Contract.
function StakeTokens(uint256 _Stakeamount) external payable
{
require(_Stakeamount > 0, "Stake amount cannot be Zero");
require(StakedAddress[msg.sender] == false,"You have already Staked");
require(ELONToken.balanceOf(msg.sender) >= _Stakeamount, "Insufficient Tokens");
ELONToken.transferFrom(msg.sender, address(this), _Stakeamount);
unchecked
{
++TotalStakers;
}
StakedAddress[msg.sender] = true;
StakerInfos[msg.sender] = StakerInfo({
startTS: block.timestamp,
endTS: block.timestamp + StakeDuration,
amount: _Stakeamount,
claimed: 0
});
}
function ClaimRewards(uint256 ClaimAmount) external returns (bool)
{
require(StakedAddress[msg.sender] == true,"You have not Staked any Tokens");
require(StakerInfos[msg.sender].endTS < block.timestamp, "Stake Time is not over yet");
require(ClaimAmount <= StakerInfos[msg.sender].amount,"Not enough Tokens to Claim");
uint256 Mintamount = ClaimAmount*IntrestRate/100;
uint256 totalReward = ClaimAmount + Mintamount;
//MintTokens(Mintamount);
StakerInfos[msg.sender].claimed += totalReward;
ELONToken.transfer(msg.sender, totalReward);
return true;
}
}
If you take a look at the interface I have mentioned some functions which I want to use from the Open-Zeppelin ERC20. But when I try to use the Mint function as you can see in the above code, it reverts.
The error I get is as follows:
The transaction has been reverted to the initial state. Note: The called function should be payable if you send value and the value you send should be less than your current balance. Debug the transaction to get more information.
I am not trying to send any ether to it so, no sense in making it payable, I am minting new tokens to its address.
Thank you if you took the time to go through this and help me out, I really appreciate it!!!