How Auto Compound Staking Pools Works ?

Ahmet Öznar
4 min readJan 2, 2022

Greetings to All 👋🏻
In this article, I will explain the logic of AutoCompound Staking pool which we meet with them as frequently at some famous De-Fi s such as PancakeSwap. Frankly, this subject made curious me a lot and I did searched on web a bit about on this subject but I couldn’t reach the result like that expect. I was thinking that there might be many people who has same feelings as me that’s why I decided to publish an article about on this subject

Without further ado, I’ll first explain how it works and then share the code with you.

I made a function scheme of smart contracts to explain it more easier and to be more understandable article. I hope this will help you a bit, No worry if it not helps to you, I will share the codes with you.

To realize AutoCompound we will need 2 different smart contracts and 1 Interface. One of these smart contracts will be AutoVault and the other will be MasterChef.
Users will interact with AutoVault, not with MasterChef directly. And the AutoVault smart contract will interact with the MasterChef contract. So the AutoVault contract will be a staker on MasterChef. The MasterChef contract will weight the AutoVault contract based on the total staked value and the duration of the pool and distribute the reward accordingly.

I wanted to improve the standard AutoVault contract a bit more and store every deposit of users as a struct. And that’s how I made every deposit unique. And if the user withdraws within 3 days after depositing, Smart contract applies a penalty.

Let’s take a closer look at smart contracts;

- AutoVault

Below I will write the most basic functions found on the contract and its explanation.

Earn();
This function works in Deposit and Harvest function. When user wants to stake, it sends tokens to AutoVault contract, MasterChef contract sends accumulated pending reward to AutoVault contract and Earn() function stakes all tokens in AutoVault contract to MasterChef. And the user is subject to a reward for staking the Pending reward here as well. S/He will be able to claim this reward with the harvest() function.

deposit(amountToStake);
This function creates a new deposit pool for the user, passes the tokens from the user to the AutoVault contract, processes the data into the contract, and runs the Earn() function.

withdraw(poolId, amountToWithdraw);
It processes the data into the contract, checks whether the Deposit is subject to penalties, and if the token value in the AutoVault contract is lower than the desired withdrawal value; Requests enough tokens from MasterChef to complete the desired value. It sends tokens to the user and updates the data.

harvest();
Users are subject to rewards for helping stake the reward from the AutoVault contract back into MasterChef. They can claim this reward with this function.

PancakeSwap says:
This bounty is given as a reward for providing a service to other users.
Whenever you successfully claim the bounty, you’re also helping out by activating the Auto CAKE Pool’s compounding function for everyone.

withdrawAll();
It checks whether all the deposits of the user are subject to penalties and deducts the penalty from the total staked value of the user and sends the user all deposited value + harvest reward to the user in a single function.

- MasterChef

stakeTokens(amountToStake); ← [AutoVault][Deposit()]
Receives tokens from the AutoVault contract, updates the data and stores them in the contract.

withdraw(amountToWithdraw); ← [AutoVault][Withdraw()]
It evaluates the withdrawal request from the AutoVault contract and sends the harvest reward + the desired value to the AutoVault contract.

To make the code more understandable, I put comment lines next to the code as in the example. Here is the sample code snippet for deposit() function.

You can directly deploy the codes via remix. Codes are not audited, shouldn’t use at production.

Codes can be find at:
https://github.com/ahmetoznar/AutoVault-SmartContract

Hope this article will help to someone.

--

--