NethermindEth / StarknetByExample

A collection of examples of Cairo smart contracts for Starknet.
https://starknet-by-example.voyager.online/
MIT License
101 stars 77 forks source link

Feature: Staking Contract #192

Closed julio4 closed 1 month ago

julio4 commented 1 month ago

Description

In this example, we want to create a smart contract that allows users to deposit a IERC20 stakingToken and earn rewards in the form of a IERC20 rewardToken. The contract will track user balances, and participants will be able to deposit and withdraw tokens at any time.

Rewards will be distributed based on the rewardRate, rewardPerToken, and finishAt variables, which are set during the contract's deployment. Users will be able to claim their rewards at any time.

Criteria:

Resources:

ODHack

To be eligible for additional rewards, be sure to review and follow the ODHack Common Guidelines and Contributing Guidelines. Be sure to join the telegram group and introduce yourself.

hudem1 commented 1 month ago

Hi @julio4,

I would like to work on this issue if possible !

I have been learning and experimenting with cairo and starknet as I have been more and more interested in those techs, and would love to contribute to the community and gain some hands-on experience by taking a shot at addressing this issue :)

Oshioke-Salaki commented 1 month ago

Please I would love to handle this issue

julio4 commented 1 month ago

I assigned @hudem1, good luck!

Oshioke-Salaki commented 1 month ago

@julio4 if there are more issues I would love to it.

the-first-elder commented 1 month ago

I can do this

hudem1 commented 1 month ago

@julio4, I think the rewardRate cannot be set in the constructor of the contract because we need to make sure the contract has indeed the reward amount (used for computing the rewardRate) in its rewardToken balance. And as we are in the constructor, the contract cannot have beforehand received reward tokens as we didn't know its address before. So, should we do like https://solidity-by-example.org/defi/staking-rewards/, where the owner can add rewards and duration at anytime ? or maybe i could do something similar to the example but make sure the owner can set the rewards amount and duration only once ? (as you initially wanted the reward amount and duration to be set in the constructor)

julio4 commented 1 month ago

@julio4, I think the rewardRate cannot be set in the constructor of the contract because we need to make sure the contract has indeed the reward amount (used for computing the rewardRate) in its rewardToken balance. And as we are in the constructor, the contract cannot have beforehand received reward tokens as we didn't know its address before. So, should we do like https://solidity-by-example.org/defi/staking-rewards/, where the owner can add rewards and duration at anytime ? or maybe i could do something similar to the example but make sure the owner can set the rewards amount and duration only once ? (as you initially wanted the reward amount and duration to be set in the constructor)

To allow anyone to join and exit at any time while receiving their portion of the reward, we need to handle the "out of reward" scenario. It's a bit complex, because we need to ensure that everyone who deposited before the "out of reward" time can still withdraw their share at any time.

An alternative, simpler approach is to set a fixed reward with an end date. The reward rate will determine the distribution of shares, but users can only claim their rewards after the end date, based on: userShares / totalShares.

Feel free to adjust this if needed, but please ensure the mechanism is clearly explained!