revo-market / make-crypto-mobile-hackathon

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.
1 stars 1 forks source link

feat(contract): deposit LP tokens into farm #22

Closed cajubelt closed 2 years ago

cajubelt commented 2 years ago

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 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.

fixes #6