0

How would I go about implementing a staking contract similar to pancakeswap farms or synthetix staking rewards but with a reward rate which can be changed in the midst of staking duration? Where users gain rewards based on previous rate until the change and new rate after the change.

1
  • How to make a better exchange than existing exchanges that combines staking, and other features is just too broad for a questions and answer site.
    – Ismael
    Commented Feb 9, 2022 at 6:22

1 Answer 1

1

You could have a struct (timestamp, reward rate) and an array of those structs. Then when users calculate their rewards they'll go find the reward rate at the time of their deposit, multiply that by time until either next timestamp or present time, and continue along those lines.

Some example code:

uint256 mostRecentIndex;

struct rewardRate   {
   uint256 timestamp;
   uint256 rewardPerSecond; //Multiplied by 10**18
}

struct user   {
   uint256 rewardsEarned;
   uint256 amountStaked;
   uint256 timestampDeposited;
   uint256 currentRewardsIndex;
}

mapping(uint256 => rewardRate) rewards;

function claimRewards   {
   uint256 currentIndex = user.currentRewardsIndex;
   uint256 previousTimestamp = user.timestampDeposited;
   uint256 totalRewards;

   //Add rewards for past indices if necessary
   for (currentIndex; currentIndex < mostRecentIndex; currentIndex++)   {
      uint256 nextTimestamp = rewardRate[currentIndex+1].timestamp;
      totalRewards += (nextTimestamp - previousTimestamp) * rewardRate[currentIndex].rewardPerSecond;
      previousTimestamp = nextTimestamp;
   }

   //Add rewards for current index
   totalRewards += (block.timestamp - previousTimestamp) * rewardRate[currentIndex];
   user.rewardsEarned += totalRewards;
   user.timestampDeposited = block.timestamp;
   user.currentRewardsIndex = currentIndex;
}
2
  • Know of any existing contracts or protocols with similar mechanic?
    – Ithihas
    Commented Feb 7, 2022 at 13:59
  • I don't, sorry. I've added a bit of code to the main answer to get you started.
    – Meriadoc
    Commented Feb 7, 2022 at 18:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.