So I am building a contract which allows users to deposit my ERC20 token into it and for doing so I want them to be rewarded every 24 hours with a 5% reward based on their holdings.
So I have added a ‘stake’ function to my contract which works perfectly.
function stake(uint256 amount) public {
staker[msg.sender].amountStaked = staker[msg.sender].amountStaked.add(amount);
stakeToken.transferFrom(msg.sender, address(this), amount);
}
Is it possible to reward ‘stakers’ every 24 hours based on their total ‘staked’? Maybe some sort of function which updates their rewards if they stake again or withdraw.
You could create a function to distribute rewards and then automate calling this function every 24 hours. You could also use OpenZeppelin Defender Autotasks to automate calling a function. (https://docs.openzeppelin.com/defender/autotasks).
Alternatively you could create a function to distribute rewards that rewards the account calling it if there are rewards to distribute, to incentivize your community.