Yield Farming on Layer 2

Edgeswap's yield farming adopts the deposit & mining approach. Specifically, after the liquidity provider (LP) tokens have enabled mining, such LP tokens can participate in the mining in Layer 2 immediately. In order to ensure the fairness and decentralization of mining, we adopt a reward calculation method similar to SushiSwap’s yield farming, which is constrained in the circuit.
We calculate the reward based on time and other factors through the formula below.
amount: the user's LP token balance in Layer 2
bonus_per_second: rewards to be issued per second
total_supply: total supply of LP tokens
t_1: end time of reward settlement (seconds)
t_2: start time of reward settlement (seconds)
With the above formula (1), the reward calculation is fair and effective only when the user’s LP tokens and bonus_per_second/total_supply remain unchanged from t_1 to t_2. However, when a Pair’s liquidity increases or decreases, total_supply will change, and the rewards of each user must be updated. In this scenario, lots of accounts need to be updated, which is unrealistic.
Given that bonus_per_second and total_supply are only related to the Pair account, while amount is related to each user account, formula (1) can be divided into two parts.
Formula (2) calculates the cumulative income of each LP token, which is only related to the Pair account. We assume that the time period does not change (generally, bonus_per_second_k does not change frequently). However, if it changes, we need to calculate the cumulative income first. The cumulative income of each LP token is recorded in the Pair account, which also records the start and end time of yield farming, the current rewards per second, and the current total amount of LP tokens. Every time liquidity is added or removed, the total_supply of LP tokens changes, and the cumulative income of each LP token need to be updated.
Formula (3) calculates the farming rewards in periods i and j. accu_i is the current cumulative income of each LP token, and accu_j is the cumulative income of each LP token that was calculated last time. Whenever the balance of a user's LP tokens changes, it is necessary to update the user's claimable income. This requires that while recording the balance under the user's account, the accu_j of last update must also be recorded. The accu_i is obtained from the Pair account. In order to ensure that the accu_i is up to date, the accu value of the Pair account need to be updated first. Therefore, when users deposit LP tokens, withdraw LP tokens to layer 1, add and remove liquidity, the accu value of the Pair account will be updated first, and then the user's claimable income will be calculated. Meanwhile, the farming rewards are first recorded under the LP token node of the Balance subtree, namely the token node where the rewards have not been claimed, which means that this part of income is invisible in the user's wallet. Users can claim such income through the ClaimBonus transaction.
In the specific implementation, formulas (2) and (3) have been modified.
Introducing FACTOR aims to accurately calculate the user's income. We set FACTOR as.
It should be noted that when calculating the cumulative income of each LP token, the settlement time is the time of the Layer 2 block, so the estimated income shown in the wallet may be different from the actual income received. However, users can receive that estimated amount by claiming the income again or claiming it after yield farming ends.