jensb89 / anchor-earnings

Calculates and displays anchor protocol earnings and yields for a given wallet address
https://anchor-earnings.herokuapp.com
8 stars 4 forks source link

Accounting for aUST transfers: maybe handle multiple wallets? #1

Closed wtanksleyjr closed 2 years ago

wtanksleyjr commented 2 years ago

Although handling aUST transfers is hard, I note that you're actually doing it correctly: specifically, if the user transferred from an old account to a new one, they'll be credited correctly for all of the interest on both, but only if you add the two accounts together.

Since I suspect most users won't transfer aUST to someone else's account (personally, I did it after I'd bought a Ledger Nano), it's very likely that users with this problem will want to track all of their accounts. So the way you're doing it actually works for me; in my case, my original account (with almost no aUST left) shows almost all of my actual interest earned (because that's where I bought almost all of the aUST), and my second account shows almost nothing (because I made only minor deposits there). As an accidentally nice result, this means I can get an almost-right answer by simply viewing my original account, but in general I can get as exact as I want by adding the two amounts together (and realistically, I'll have to do that in the future).

This in turn suggests an "easy fix" (no doubt it's harder than it sounds) of allowing multiple wallets to be added.

jensb89 commented 2 years ago

Thanks for the input. Multiple wallet support is actually a good idea, but I think the problem of the wrong yield could also be dealt with in another way.

Let me try to explain:

The code that calculates the single yield value at the top of the webpage is the following:

for d in deposits:
        r = d["Out"]/d["In"]
        aUstAmount += d["In"]
        # Yield
        y = (currentaUstRate - r) * d["In"] if d["Out"]!=0 else 0 #todo: calculate right rate for aUST transfers

What is done here: Each time a user deposits UST, the UST amount is burned and an amount of aUST is minted. From this we can calculate an exact rate aUST to UST by diving the two (r in the above code). Also from the Anchor contract we can query the exact aUST rate at this very moment. The yield is then simply: (the rate right now minus the rate at the time of the deposit) times the aUST amount. (for redemption the same way, just with a - sign)

For the aUST transfer we don't have these information, which is why I set the yield from that to 0 for now and which is also why you still see it in your old account.

The proper way to handle it in my opinion would be: y = (currentRate - aUSTRateAtTransferTime) * aUSTAmountOfTransfer This means, at the time of the transfer the yield would be 0 and from there it would constantly grow.

The big problem: There is no historical information of the exact aUST rate, at least I couldn't find a way to query it from the contract or the Terra API :(

What we could do:

Thus, the only problem actually is to get the aUST to UST rate at the time of the aUST transfer. I don't really like both of these options, but for starters I could try to get the value from the historical data that is alraeady used to calculate the plot in the webapp. This data is also from flipsidecrypto but only has 1 datapoint per day, so it is not that accurate as of now...

Would be much easier if Anchor would allow us to query historical rates ...

jensb89 commented 2 years ago

Update: For the plot in the bottom of the page I already tried using the outlined approach. After debugging it turned out I was just using a wrong sign comparison. Now with the latest commit ed0d05b512056026f2c275f0c4817d7074eb5fa2 the plot should be right and reflect the actual yield.

The "Total Yield" value needs some more work as outlined above.

wtanksleyjr commented 2 years ago

The plot is incredible, it looks exactly right for both my accounts and matches the plots shown by the older database-app (um, I forget its name). And it also meets my assumption that adding the two plots together gives the same value as adding the two accounts numbers together.

Looks like you've got a very possible fix for those numbers, then. Will that work? I definitely admit it's a more user-friendly fix than just requiring people to enter all of the accounts they ever transferred aUST from :) . Although the latter actually IS a valid use case, since anyone who got a ledger AFTER aping into Terra (like me) will have two accounts.

I really like how those graphs are drawn, it's easy to find the day-by-day data points.

-Wm

On Wed, Dec 29, 2021 at 2:35 AM Jens Brauer @.***> wrote:

Update: For the plot in the bottom of the page I already tried using the outlined approach. After debugging it turned out I was just using a wrong sign comparison. Now with the latest commit ed0d05b https://github.com/jensb89/anchor-earnings/commit/ed0d05b512056026f2c275f0c4817d7074eb5fa2 the plot should be right and reflect the actual yield.

The "Total Yield" value needs some more work as outlined above.

— Reply to this email directly, view it on GitHub https://github.com/jensb89/anchor-earnings/issues/1#issuecomment-1002527897, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJ7H6KAUUBUG5RP3F6LCL3UTLQA7ANCNFSM5K4SQHFQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

jensb89 commented 2 years ago

Thx, glad you like it :) I pushed a fix now to use the same calculation for both the plot and the yield value. This works for now, I still left a warning as the yield is not exact anymore because the aUST rate is just estimated from the next closest datapoint (midnight next day), so it is off by a few hours. So, it can still be improved in the future (maybe we will get some new API endpoints at some time)

Yes, I agree, the multiple wallets would still be a useful feature, but would need some more time ;)

Have a good start in the new year tomorrow! :) Jens

omer411 commented 2 years ago

I am trying to figure out how to run the code locally on my home computer. I am a newbie to python... so if you could please provide instructions on how to run the code locally (a youtube video would be nice). what ever help you can provide will be appreciated.

Thanks.

jensb89 commented 2 years ago

@omer411 i don't have a YouTube video, but getting it to run locally is still straightforward:

  1. install python (I used 3.10)
  2. create a virtual environment ( so you don't mess with your local python installation). There should be enough videos and tutorials out there.
  3. do "pip install -r requirements.txt". That installs all needed packages into your virtual environment
  4. simply call "export FLASK_DEBUG=1 && flask run" and open a Webbrowser for the shown url on the console.

Hope that helps :)

omer411 commented 2 years ago

thanks will give it a try