Search code examples
blockchainsmartcontractssolana

How to create a staking smart contract?


I'm a novice in a smartcontracts but want to create a simple stake contract in Solana. But can't understand when I need to update a reward counter per user? Because contracts interact with accounts for write, only when user paid for this. But in all staking protocols, we can see rewards count after staking. How this can work? And how I can change APY in a time?

I invented only one method - save counters in off chain program but think it's not right way


Solution

  • A lot of this is more down to design, but a few suggestions:

    But can't understand when I need to update a reward counter per user?

    You don't necessarily need to update all the time. In fact, many staking protocols keep track of the last time a user's rewards were paid out, but don't actually change the value until the user tries to withdraw. For frontends, you just need to do a dynamic calculation based on the user's position.

    You may find more interesting information by looking at the protocols' source code. For example, Orca's contract is based on token-swap from the SPL: https://github.com/solana-labs/solana-program-library/tree/master/token-swap

    Otherwise, there's more general-purpose clients and code that might be interesting at https://github.com/solana-labs/solana-program-library/tree/master/farms