Calculating APY for every user in a shared environment

15 Views Asked by At

It is actually a real-world programming problem that I'm looking for a mathematical answer to solve it:
There is a lending pool that pays interest to user-supplied cryptocurrencies, this interest rate we know as APY is calculated dynamically based on the capital efficiency of each pool (e.g. number of users that borrow from that pool and amount that is being borrowed. So, it does not apply the normal APY formula).
If we provide X units of -for example- ShitCoin or SHC to DiarrheaPool, we get an equivalent amount of SHC-LP which can be later used for redeeming the original asset (there is a 1:1 co-relation between them). Interest is allocated to each participant in the pool by adding directly to their SHC-LP tokens over time. For example, if a user provides 100 SHC and gets 100 SHC-LP, after some time that he earns 1 extra SHC-LP as interest, it accumulates with the current balance and his SHC-LP holding would be 101 (which is equivalent to 101 SHC). We are not aware of that interest amount in advance, but we can get SHC-LP balance of the user at any given time.
Now let's dive into the interesting part! A passionate user takes the responsibility of investing on behalf of his crypto-illiterate friends to share this unique smelly opportunity with them! Unfortunately, he can't create a single wallet for each and every of his genius friends, so he wants to manage all assets in his own wallet. His friends may send him any amount of SHC to invest at any time and may need to withdraw their SHC -in addition to their interest- at any time as well. He needs to somehow keeps track of interest allocated to each person, which depends on the time and amount of initial supply, but is not easily separable. Can anybody give him a hint?!