Implementing staking rewards

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.

Thanks

1 Like

Emmm, maybe you can record the time when user deposits asset, and then uses this timestamp to calculate the reward amount.

1 Like

Hi @lotte1,

Welcome to the community :wave:

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.