Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch to compound v3 borrows #6495

Merged
merged 33 commits into from
Aug 28, 2024

Conversation

darvinrio
Copy link
Contributor

Thank you for contributing to Spellbook 🪄

Update!

Please build spells in the proper subproject directory. For more information, please see the main readme, which also links to a GH discussion with the option to ask questions.

Contribution type

Please check the type of contribution this pull request is for:

  • New spell(s)
  • Adding to existing spell lineage
  • Bug fix

Note: You can safely discard any section below which doesn't apply based on selection above


For adding to existing spell lineage

If you are adding to an existing spell lineage, please provide the following information:


Additional information

Please provide any additional information that might help us review your pull request:

  • [Any additional information]

Thank you for your contribution!

@dune-eng
Copy link

dune-eng commented Aug 4, 2024

Workflow run id 10237860105 approved.

@dune-eng
Copy link

dune-eng commented Aug 4, 2024

Workflow run id 10237859950 approved.

@dune-eng
Copy link

dune-eng commented Aug 4, 2024

Workflow run id 10237860113 approved.

@dune-eng
Copy link

dune-eng commented Aug 4, 2024

Workflow run id 10237861969 approved.

@dune-eng
Copy link

dune-eng commented Aug 4, 2024

Workflow run id 10237861865 approved.

@jeff-dude jeff-dude self-assigned this Aug 9, 2024
@jeff-dude jeff-dude added in review Assignee is currently reviewing the PR and removed ready-for-final-review labels Aug 9, 2024
@jeff-dude
Copy link
Member

thank you @darvinrio for the PR. can you help me out a bit more with some context here?

  • was data missing and this fills the gaps?
  • were there bugs in existing data?
  • both?
  • does this work towards an existing known bug, tracked somewhere?

@jeff-dude
Copy link
Member

@tomfutago friendly tag, as the one who helped build this lineage of spells. do you want to give a quick review?

@jeff-dude jeff-dude added the question Further information is requested label Aug 9, 2024
@darvinrio
Copy link
Contributor Author

thank you @darvinrio for the PR. can you help me out a bit more with some context here?

  • was data missing and this fills the gaps?
  • were there bugs in existing data?
  • both?
  • does this work towards an existing known bug, tracked somewhere?

@jeff-dude

  1. earlier version of supply side tables only had collateral supply and withdrawal
  2. the borrow side tables filtered out partial borrows and repays

I added base asset supply and withdrawal and partial borrows and repays.

for more context:
compound increases your supply if you repay more than your borrow.
similarly it increases your borrow if you withdraw more than your supply (if you have collateral left for that)

@jeff-dude jeff-dude added ready-for-final-review and removed question Further information is requested in review Assignee is currently reviewing the PR labels Aug 9, 2024
@tomfutago
Copy link
Contributor

hey @darvinrio @jeff-dude

I looked at the changes, admittedly not in massive depth, and here are my notes that hopefully will add some value:

  • original code for compound v3 borrow/supply macros was based on an existing query I found on a "many-stars" dash, which I can't unfortunately find now, so this is just to add some background story..
  • with changes in this PR (btw, thanks for looking into this @darvinrio!) there are a few of them that seem arbitrary, eg:
    when s.amount < t.amount+10 then 'supply' -- accuracy fix (don't understand +10 part, as in: why this specific value?)
    or
    s.evt_index+1 on join with evt_transfer - is +1 always the case?
  • I looked at check queries, but tbh I don't get what they're showing/proving. what would make me more comfortable was to see a query that takes 1 specifc compound v3 market and compare supply/borrow totals between spell in the current version with the one added in this PR - ideally market where totals are off when using current spells to showcase how this PR fixes that

@darvinrio
Copy link
Contributor Author

hey @tomfutago thanks for the review.
i ll get back with a detailed report on the choices made and the impact.
i am currently swamped with a bunch of other work.

thanks in ahead for your patience

cc: @jeff-dude

@jeff-dude
Copy link
Member

hey @tomfutago thanks for the review. i ll get back with a detailed report on the choices made and the impact. i am currently swamped with a bunch of other work.

thanks in ahead for your patience

cc: @jeff-dude

all good, no rush! thanks for collaborating.

@jeff-dude jeff-dude added WIP work in progress in review Assignee is currently reviewing the PR and removed ready-for-final-review labels Aug 13, 2024
@darvinrio
Copy link
Contributor Author

A slightly more detailed report :
@jeff-dude @tomfutago

This PR #6495 aims to

  1. Add supply and withdrawal of the Base Asset (the existing model only covers Collateral supply and withdrawal)
  2. Add both full and partial types of supply and withdraw
  3. Add partial borrow and repay (the existing model only covers full repayment and borrows).

I hope this clears the confusions. happy to clear any doubts that might arise.

In compound v3, the repay and supply were put together to prevent looping.
so, the USDC one borrows, can’t be supplied back, only repaid.
Similarly, borrowing and withdrawing were put together.

Since these two were put together, and there was no amount bound, a user can repay and supply in the same event, if the amount sent to the Comet contract is more than their outstanding debt.

Definition:

  1. Full Repay - is an action, where the entire base asset is used to pay back the loan.
    1. (100 USDC is sent via supply() and 100 USDC is used in debt repayment)
  2. Partial Repay - is an action, where a portion of the base asset is used to pay back the loan, and the remaining is supplied
    1. (100 USDC is sent via supply() and 60 USDC is used in debt repayment and 40 is supplied for lending)
  3. Full Borrow - is an action, where the entire base asset sent to the user is debt
    1. (100 USDC that the user receives from withdraw() is 100 USDC debt against collateral)
  4. Partial Borrow - is an action, where a portion of base asset sent to user is debt, the rest is the pre-existing supply of the user
    1. (100 USDC that the user receives from withdraw() is 60 USDC debt against collateral, and 40 USDC from their supply)

To find these amounts, we can track Supply, Withdraw and Transfer events.

  • Supply is emitted when base Asset is sent to the Comet - supply and repays
  • Withdraw is emitted when a base Asset is withdrawn from the Comet - borrows and withdraws
  • Transfer is emitted when there is a transfer of the Comet - supply and withdraws or transfers (out of scope)

Supply

Say a user has 100 USDC debt, and sends 150 USDC via supply() as repayment, the Comet contract would repay 100 USDC debt and log the remaining 50 USDC as supply.

In this case, the Supply event logs 100, while the Transfer event logs 50.

image

image

The Transfer event is always emitted immediately after the Supply event

The Transfer event is always emitted immediately after the Supply event

Withdrawals:

A user has supplied 50 USDC to the Comet, and now tries to withdrawal 110 USDC. Provided there are enough collaterals, the user withdraws 50 USDC from their supply, and borrows 60 USDC against their collateral.

In this case, the Withdraw event logs 110, while the Transfer event logs 50.

image

image

The Transfer event is always emitted immediately after the Withdraw event

Comparing Existing Models:

In the below queries, you can see that the new model accurately matches the existing models for full borrows and full repays, while also adding the partial borrows and partial repays.

Borrows Check:

https://dune.com/queries/3996425

image

https://dune.com/queries/3996456

image

Repays Check:

https://dune.com/queries/3996448

image

https://dune.com/queries/3996453

image

The arbitrary 10

This was a threshold added (back in 2022) to filter out minuscule repayments, borrows, supply and withdrawals.

The aim was to exclude actions that have for example 1/1e6 USDC repaid, while taking the count of such actions.

However, a more recent analysis has shown that this threshold could be moved.
I can remove it in a future commit, as that piece of code doesn’t affect the end rows in the model.

https://dune.com/queries/3997824

image

https://dune.com/queries/3996463

image

https://dune.com/queries/3996458

image


notion link to the same

@tomfutago
Copy link
Contributor

A slightly more detailed report..

"slightly more detailed" 😳😂 that's probably most detailed response ever! will take a look, but it will take me some time (most likely earliest next week, if that..) @jeff-dude if you're happy with the above - no need to wait for me 👍

@tomfutago
Copy link
Contributor

hey @darvinrio it sounds to me like you know much more than me how compound v3 works 🙌 thanks for providing this added info in so much depth! I looked at your explanations and comp queries - and I'm convinced updated logic is correct.

@darvinrio
Copy link
Contributor Author

hey @darvinrio it sounds to me like you know much more than me how compound v3 works 🙌 thanks for providing this added info in so much depth! I looked at your explanations and comp queries - and I'm convinced updated logic is correct.

thank you for providing me with my Monday morning validation 🤗

Copy link
Member

@jeff-dude jeff-dude left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you @darvinrio for the clean code updates & clear communication on what is changing and why.

thank you @tomfutago for taking the time to give a review 🙏

i'm good with the changes based on the two of you agreeing here

@jeff-dude jeff-dude added ready-for-merging and removed WIP work in progress in review Assignee is currently reviewing the PR labels Aug 26, 2024
@jeff-dude jeff-dude merged commit 376ec39 into duneanalytics:main Aug 28, 2024
3 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Aug 28, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants