# Fixed Rate Rewards

- This is the more complicated of the two reward types
- A fixed amount of rewards is paid out per gem staked. How much exactly? That depends on two factors:
- The fixed reward curve
- Farmer tenure (how long has the farmer been staking for)

- The reward curve can be configured using the following parameters:
- Base rate (required)
- Tier 1 rate + tenure (optional)
- Tier 2 rate + tenure (optional)
- Tier 3 rate + tenure (optional)

- Thus you effectivelly have a stepped reward curve with up to 4 increments. Visually it can be represented like this:

- Note: additional tiers are optional - if you don't need them, you can keep it simple and only specify the base rate
- If you do specify tiers, there is no restriction on what the rates are. They can easily be:
- Increasing: 1 tokens/gem/s > 2 > 3 > 4
- Decreasing: 4 > 3 > 2 > 1
- Random: 1 > 4 > 2 > 3
- Cutting off: 1 > 2 > 3 > 0
- With a gap: 1 > 0 > 2 > 3

- Basically you have full creative freedom here :)
- Example:
- You specify the following schedule:
- Base rate: 1 tokens/gem/s
- Tier 1:
- Rate: 2 tokens/gem/s
- Required tenure: 10 sec

- Tier 2:
- Rate: 3 tokens/gem/s
- Required tenure: 30 sec

- Tier 3: skipped (null value passed)

- Two famers show up - one stakes 5 gems for 60 sec, another 10 for 20 sec
- The first farmer gets: 5 gems * (1 * 10 + 2 * 20 + 3 * 30) = 700 reward tokens total
- The second farmer gets: 10 gems * (1 * 10 + 2 * 10) = 300 reward tokens total
- Taking the last example above, if all the gems staked by farmer1 have rarity of 1, and all the gems staked by farmer2 have rarity of 2, then final reward amounts are 700 and 600, respectively

- To fund the above two farmers you need 1000 reward tokens total
- What if you deposited 1500?
- Another 500 tokens would still be available for other stakers to bid for

- What if you deposited only 800?
- This is a tricky situation. TLDR: the 2nd farmer would not be able to begin staking :(
- It's tricky because you have a choice here - either you choose to guarantee certain rewards to early farmers -- or you run the risk of not being able to pay all the farmers as more join
- The approach taked in Gem Farm is one that sides with farmers - when a farmer shows up to stake and a fixed rate schedule is selected, the appropriate amount of tokens is "reserved" for them
- This means unless they unstake - they are guaranteed to receive the payout as time passes

- Calculating reserve amount
- The reserve amount is the total remaining duration * reward schedule
- Example:
- Using the same reward schedule as above, if you funded the reward for a total duration of 100 sec, the reserve amount per gem would be: (1 * 10) + (2 * 20) + (3 * 70) = 260 tokens/gem/s
- This means if a farmer showed up to stake 10 gems, you must have at least 2600 tokens still available (not reserved) in the funding vault, or they would be denied staking
- Thus the farm manager needs to be careful with what reward duration they set. If they set a duration of 100 sec and only fund 26000 tokens, they can only support up to 100 gems staked on the above schedule
- Note: this assumes everyone stakes from second 0 (unlikely). If a farmer showed up to stake 50% into the reward duration, you'd only need 130 reward tokens to fund them
- Note2: if the token amounts look large to you don't worry there is a "denominator" parameter which lets you adjust down the rate
- eg with a denominator of 10 you only need 26 tokens to fund a farmer for 100 sec on the above schedule

- Another complication that comes with fixed rate staking is rolling - what happens to the farmer after they finish one schedule and begin another?
- With variable rewards it's easy - they just get the going rate
- With fixed rewards there again is a choice to make - either you say "well, new schedule, start from base rate again" -- or you honor their tenure and give them a beter rate
- Gem Farm again sides with the farmer here - assuming they didn't unstake, we take into account the total duration they've been staked for when calculating the reward on the new curve
- Example:
- Same schedule as above funded for 100s
- It was run once, our farmer staked for the entire period, and didn't unstake
- Same schedule is funded for another 100s
- What rate should our farmer get? They're getting Tier 2 (the highest) straight away. On eg second 5 of the new schedule their staking duration is NOT 5, but rather 100+5, and thus they fall into Tier 2 based on required tenure

- There is another nasty dynamic with fixed rate staking - if you ever introduce more than one fixed rate reward in a row, you will have to continuously "crank" the staking program to move farmers over
- What this means:
- Farmer comes and stakes with you when your fixed reward is funded for duration of 100 sec
- The first rewrd expires, and you introduce another fixed reward for another 100 sec
- Unfortunately, unless the farmer's struct is refreshed, there is no way for them to "enroll" in the second reward. They would complete the first one and just sit there, not accruing any rewards
- That refresh can be done by the farmer themselves if they remember to (or you remind them to)
- Or it can be done by you
- In fact it can be done by anyone. The instruction to refresh the farmer doesn't require any signers (since it's not doing anything sensitive)

- This is a little painful because if you have 10000 farmers and introduce 2 consecutive fixed rate rewards you now need to do 10000 refreshes
- I say "a little" because I'm pretty sure you can write a script to do it in one go, in probably less than a second
- I haven't had time to do this - but if you're a budding Solana dev and want to do this in exchange for some mentorship hmu :)

Last modified 10mo ago