Staking Contract in Zilliqa
This article explains how to implement the staking contract released by Zilliqa team in their community contracts. There are two stake holders in this contract i.e, stakers and admin. Stakers can deposit a ZRC-2 token of their choice and earn the same or another ZRC-2 token. Rewards could potentially be in multiple tokens and is available for claim at the end of every reward cycle which is set to be 1 day or 2,500 blocks. Reward amount and the tokens that users earn are decided by the admin at the contract deployment time. The admin also specifies the duration of the reward campaign (e.g., 60 days).
For example, the admin would specify that users staking a certain Token A will earn Token B and Token C for a total of 10 reward cycles and the total reward amount for each token is 1,000. I.e., for each reward cycle, 100 Token B and 100 Token C are to be disbursed among the stakers during that cycle. Reward earned by a given staker for a given reward cycle is proportional to his stake at the end of the cycle. Stakers need to lock their tokens for a specified number of cycles to earn rewards. In other words, once a staker has deposited funds, he cannot take his funds out for 7 cycles. In case, a staker wants to take his funds within 7 days, he has to pay a penalty of 10% to the system. 10% is on the staked tokens being withdrawn. These tokens can be claimed by the admin at any point of time.
Now let’s see how we can deploy this contract and deploy contract.
The contract address of the token to be staked and blocks per cycle needs to be passed while deploying the contract by the contract owner. Scilla IDE can be used to deploy the contract. After the contract has been deployed, there are a couple of other transitions that help us set the contract address.
One of those transitions is update_token_rewards which helps us to initialize the rewards for the stakers. Token address of the token which will be given as reward and amount of total rewards per cycle needs to be provided while calling the transition.
Now the amount of rewards to be distributed needs to be sent in the contract and then stakers can come and stake their tokens in the contract. Proper allowance should be given and then deposit transition can be called to do so.