Build the next generation of Defi accessibility with Celo mobile-first blockchain! Create new solutions on top of the Celo Ledger - DeFi, NFTs, Smart Contracts, Developer tooling and more.
Also includes refactor of how balances are tracked (introduces "farm points" concept)
Farm points are an abstraction that allows for gas-efficient balance tracking.
If we just mapped user addresses to LP tokens, then every time we reinvest farming rewards into the liquidity pool, we'd need to update every user's balance of LP tokens. This would create gas costs that scale linearly with the number of users investing in the farm.
Conceptually, all we need to track is a user's share of the total balance held by the farm. So if we know how much they put into farm bot, and how much farm bot was worth at that time, we should be able to grow their share appropriately as the total assets held by farm bot increase.
The way I did that is with "Farm Points" (FP for short). Initially, farm points can be bought 1:1 for LP tokens. However, as Farm Bot's assets grow from harvesting rewards, the ratio of LP tokens held by farm bot to the LP tokens invested increases. Over time, a farm point will be worth much more than 1 LP token.
The FP:LP ratio will be tracked with 3 fields:
fpBalances: maps user address to farm points they hold
lpTotalBalance: tracks the total number of LP tokens farm bot holds
fpTotalBalance: tracks the total number of FP points held by users
The fields will be updated as follows:
on deposit, user "purchases" farm points at the current rate (1:1 if lpTotalBalance is 0, otherwise fpTotalBalance / lpTotalBalance). all 3 fields are updated.
on withdrawal, user "cashes out" at the current rate (calculated same as above). all 3 fields updated.
on reinvest, lpTotalBalance increases by whatever farm bot is able to purchase with rewards it has harvested. Only lpTotalBalance is updated (it increases)
Deposit/withdrawal only affect the fpBalances entry for one user, and reinvest doesn't affect the fpBalances field at all. This makes it much more gas efficient than updating the LP balance of every user each time rewards are harvested.
Notice that the only step where the fields don't grow together is reinvest. This is how the farm points will grow in value over time.
Eventually we can issue FPs as ERC20 tokens, creating a marketplace of shares in farm bot. For now, a mapping will suffice.
Also includes refactor of how balances are tracked (introduces "farm points" concept)
Farm points are an abstraction that allows for gas-efficient balance tracking.
If we just mapped user addresses to LP tokens, then every time we reinvest farming rewards into the liquidity pool, we'd need to update every user's balance of LP tokens. This would create gas costs that scale linearly with the number of users investing in the farm.
Conceptually, all we need to track is a user's share of the total balance held by the farm. So if we know how much they put into farm bot, and how much farm bot was worth at that time, we should be able to grow their share appropriately as the total assets held by farm bot increase.
The way I did that is with "Farm Points" (FP for short). Initially, farm points can be bought 1:1 for LP tokens. However, as Farm Bot's assets grow from harvesting rewards, the ratio of LP tokens held by farm bot to the LP tokens invested increases. Over time, a farm point will be worth much more than 1 LP token.
The FP:LP ratio will be tracked with 3 fields:
The fields will be updated as follows:
Deposit/withdrawal only affect the
fpBalances
entry for one user, and reinvest doesn't affect thefpBalances
field at all. This makes it much more gas efficient than updating the LP balance of every user each time rewards are harvested.Notice that the only step where the fields don't grow together is
reinvest
. This is how the farm points will grow in value over time.Eventually we can issue FPs as ERC20 tokens, creating a marketplace of shares in farm bot. For now, a mapping will suffice.
fixes #6