Flush, DoHardWork and sync
Last updated
Last updated
The key to understanding the Yelay protocol is to grasp the flush->DoHardWork->sync
cycle. This chapter aims to systematically explain the underlying process for handling deposits and withdraws in Yelay , enabling the developers to gain the insight into how the process works from the technical perscpective, so they understand how to achieve the goals of their product.
After the user makes a deposit into a smart vault the assets first enter a pending state, during which they are kept in the Yelay Master Wallet and tracked by Smart vault manager contract.
To be able calculate the distribution of the assets among the strategies of a particular smart vault, the so-called 'flush' of the vault must be initiated.
During the flush, pending assets tracking is transfered from the Smart vault manager contract to the Strategy registry contact. This contract keeps track of all the unflushed deposits and withdrawals for each strategy. When a vault is flushed it updates its calculations for each strategy that particular vault uses. Also, the flush index of the vault is increased by one, which is then used later in the sync process.
When withdrawing, SVTs that represent the pending withdrawal amount are burned. Consequently, the proportional amount of SSTs from the smart vault get transfered from the smart vault to the strategies.
Flush has to be called for each vault individually. The process flushes all pending deposits and withdrawals. The process can be initiated by any user, it is not conditioned by holding any kind of Yelay-related tokens, but the caller does pay the gas cost of the call. Protocol users don't have to flush the vaults themselves if they don't have a specific use case for that, as Yelay team ensures all vaults are flushed on time.
The key process of the Yelay protocol is the so-called 'DoHardWork' (DHW) function, called on the Strategy registry contract. Its main purpose is to manage the funds between the Master Wallet and individual strategies. On top of that it also calculates APY of the strategies, compound rewards and manages vault fees. This is all done based on the track records, provided by the Strategy registy contract. After that the actual DWH can be triggered on each strategy.
The DHW function can be called independently for each strategy.
Each strategy in Yelay is represented by a separate smart contract. There the logic for dealing with the underlying protocol is stored. Nevertheless, logic for the execution of the DHW is the same for all the strategies.
When the process begins, first some basic check are put in place to validate the input parameters to prevent malicious practices with slippages, like front-running.
Next, the yield of the strategy protocol is calculated, which is usually calculated on-chain, but can, in certain more complex scenarios, be calculated off-chain and provided as a prameter of the DHW call.
This is followed by compouding reward tokens. Available rewards are swapped into the underlying asset and deposited back into the protocol. Compounded deposits are separate from the users' deposits to keep track of the compound yield. Nevertheless, compound yield is distributed to the users, proportionally to their past deposits.
The platform fees are collected in the form of SSTs that get minted at that point. The amount of newly-minted shares is proportional to the yield that was generated by the strategy.
To optimize the number of transactions, this phase of the process calculates the 'strategy transaction amount'. First, it sums up the depositing amounts to the strategy, which are then subtracted by its withdrawal amounts. Consequently, in each DWH cycle, we either only make a single depositing transaction (positive strategy transaction amount) or withdrawing transaction (negative strategy transaction amount). If the amount is zero, no transaction takes place.
When strategy transaction amount is positive, the deposits from the Mater wallet are swapped into the underlying protocol asset and new SST are minted for that strategy.
In contrast, when strategy transaction amount is negative, the said amount of assets is redeemed from the protocol. Then the proportional amount of SSTs get burnt and the assets are transfered to the Master wallet contract.
TODO: The function can only be called by a set of whitelisted addresses, called 'DoHardWorkers'.
To conclude the cycle, the smart vault must claim the SSTs (for deposits)and assets (for withdrawals) from its strategies. This is called syncing. Any user can initiate a sync for any vault.
Only smart vaults that had all their strategies processed by the DHW, can be synced. That is checked by confirming that the DHW for every strategy in the vault was called with the latest flush index (assigned when flushing the vault).
When syncing the deposits of the smart vault, the appropriate amount of SSTs are claimed from the underlying strategies by the smart vault that is being synced.
At that stage all the fees that the vault owner has set up are collected. Those include deposit fees, performance fees and management fees. Fees are collected in the form of SVTs.
Deposit fees
Deposit fees are calculated based on the actual amount, users deposited into the vault. Example: if the sum of deposits in the latest cycle was 1000USDC and the deposit fee was set to 1%, 10USDC worth of SVTs are reserved for the owner.
Performance fees
Performance fees are calculated based on the total yield generated by all the strategies in the smart vault. Example: if a smart vault generated 10USDC of yield and the performance fee is set to 10%, 1USDC is reserved for the owner, by minting new SVTs to dilute the value accordingly.
Management fees
Management fees are calculated based on the total amount of assets, managed by the smart vault, looking at the annual level. Example: If there is 1,000,000USDC managed by the smart vault and the management fee is set to 1% and the last DHW was ran a day ago, 10,000USDC/365 (=27.3973) get reserved fot the owner, by minting new SVTs to dilute the value accordingly.
Based on the amount of SSTs and the fees that have to be collected, the right amount of SVTs is minted. It is important to note, that all the SVTs are still kept in the smart vault in this step and do not get distributed to the users yet.
When syncing the withdrawals, the smart vault that is being synced claims the appropriate ammount of assets from the Master wallet. That amount is then marked under the smart vault, but the users still have to claim their shares.