I wish to know if the function given below correctly transfers rewards from the contract caller to the required account or not. Is there anything to consider before calling this function from some source?
I’m assuming that I need to approve an amount that the contract can spend from. Can this be done once at the time of contract deployment? If so, how can I ensure that every address with the DISBURSER_ROLE
gets approved, i.e. during role assignment?
Code to reproduce
function disburseReward(address to, uint256 rewardWei)
external
onlyRole(DISBURSER_ROLE)
returns (bool)
{
if (msg.sender.balance < gasleft()) revert("native balance < gas");
if (token.balanceOf(msg.sender) < rewardWei)
revert("token balance < reward");
if (!token.transferFrom(msg.sender, to, rewardWei))
revert("Token transfer failed.");
emit RewardClaimed(msg.sender, to, rewardWei);
return true;
}
Environment
Hardhat 2.8.3