-
Notifications
You must be signed in to change notification settings - Fork 239
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
feat (#major); aave-forks; upgrade aave-forks to schema 3.0.1 and use sdk #1993
Conversation
We can either use fee type other and note in the readme or update the schema. Same w the interest rate type. I am going to opt for the readme option, I don't think the extra work to update is worth it for 1 protocol. |
We currently don't specify stable vs variable borrows.. do we need to add it? I think it is valuable info, but we can skip for now and make a note in the readme. I think the logic we have is working regardless of borrow type. Also, you would know which side based on the token, variable and stable borrow tokens have different contract addresses. You can add a field to the token for which side it is and compare |
As I think of it, interest rate type can be found in matched position, e.g. using this query. I can add an interestRateType field to Borrow entity if you think it still makes sense to add it now. It is possible to get it from
I believe the asset arg in Borrow and Repay event is the underlying (input) token, not the vToken or sToken, so we cannot know whether it is stable or variable interest type. The info is available in the params struct, but not in the event. https://github.com/aave/aave-v3-core/blob/29ff9b9f89af7cd8255231bc5faf26c3ce0fb7ce/contracts/protocol/libraries/logic/BorrowLogic.sol#L157 and https://github.com/aave/aave-v3-core/blob/29ff9b9f89af7cd8255231bc5faf26c3ce0fb7ce/contracts/protocol/libraries/logic/BorrowLogic.sol#L262 Similarly, the LiquidationCall event only provides the asset token: |
Okay this is what I found (lmk if this doesn't work, but it makes sense in my head):
|
I believe it is possible for a user to borrow in both variable and stable interest rate mode at the same time. For example, in this dune query, you can see user 0x7c04e80e5d8a19ef014e51e27b3d92cc4d87b515 borrowed 25000000000 with variable rate mode (mode=2) at block 27289290, and then another 25000000000 with the stable rate mode (mode = 1) at block 27289554. Neither position was repaid until block 27633344 and 27633420, according to this query. And there was no SwapBorrowRateMode event. Since this is the case, if we remove the interestRateType suffix from positionID, we will be adding the later stable borrow in the above example into the previous variable borrow position. But may be this is OK?
If we are not having the interest rate type in position, it'll be a good idea to have a helper field for interest rate type in Borrow entity.
If we are not tracking interestRateType for positions, Borrow, Repay, or Liquidation), then we don't need to watching this, right? |
@tnkrxyz you are right!
Sounds like you can have 2 pos in a market with different interest rate types, so we should definitely separate by interest rate type and add any helper fields that are needed to enable this. lmk if you have any other questions! |
I have to think how to achieve this without using callHandler. |
@tnkrxyz I think one of the biggest issues with the call handlers is not being supported on every chain and we need to support all chains. One way I can think of would be:
|
@dmelotik Yes, this can work! thx for helping brainstorming ideas! I ended up adding an _AccountDebtBalance helper entity to keep track of vToken and sToken balance for borrower account in the market. Inside handleRepay and handleLiquidation, I check which balance(s) goes down. For repay, only one type of debt can be repaid in a repay call and it should be the one with lower balance. Similarly for liquidation, but here, both token balances can go down. I am running test deployment to see if it works as expected. |
Doesn't the repayer specify in the call function parameters which side is being paid? So it could be either? |
@dmelotik yes, correct. I see that my sentence above may be confusing, what I meant is that, for each repay event, it is either Variable or Stable debt that's been repaid, not both types in one event. And the type should correspond to the (v or s) token with the lower balance. For liquidation, both v & s token (if both > 0) can be repaid/liquidated within the same event. sorry for the confusion. |
I am seeing crazy Quick fix: we just need to normalize subgraphs/subgraphs/aave-forks/src/mapping.ts Line 672 in 6b7078b
I am also noticing the new subgraph has less Something I can tell is wrong: In both examples of checking for transfer events to get the interest rate type in Repay there are approvals that occur. But that is not always the case. In this example the Transfer event we want to see is only 4 before and not 5. To account for this we may want to search for the However, it should still pickup the ones that are 5 away, yet there are no repays |
ok, both should be fixed now. |
@dmelotik , the negative revenue is caused by the deduction of flash loan premium from totalRevenue in handleReserveDataUpdate, but I am not sure why LiquidityIndex in reserveDataUpdate event does not include the flashloan premium to LP as we believed. For example, according to these subgraph log
At flashloan tx 0xeb87ebc0a18aca7d2a9ffcabf61aa69c9e8d3c6efade9e2303f8857717fb9eb7, the premium amount is 30 WETH, or $52620.9987, of which $52599.95030052 goes to the LP and supposedly should be deducted from total revenue. However, the ReserveDataUpdated event followed (0x1a520aea1fa610109a332a7408aa8d595fd83a40328b4709438937ee83c5b989), the calculated total revenue from LiquidityIndex is only $62.46. Revenue became negative after deducting flash loan premium of $52599.95030052. This makes me think that we don't need to deduct flashloan premium. But from what I can see, the premium to LP is accrued in LiquidityIndex in the smart contract. I am not sure what is wrong and leads to the revenue not included in our totalRevenue. Any idea? |
Let me ask the aave team. This is something we need to ensure we have right. And it may be the case that we don't need to subtract the revenue |
@tnkrxyz A few things I found:
|
@dmelotik , I think I finally figured out what is causing this and am testing a fix. more update soon. |
@dmelotik , ok, here is what I found so far: the reason we are getting negative revenue is due to mis-match of FlashLoan event and ReserveDataUpdated event in the current implementation. The ReserveDataUpdated event associated (with the updated liquidityIndex) with a FlashLoan event is emitted before the FlashLoan event. My current code assumes the ReserveDataUpdated event is after the FlashLoan event, which leads to the accrued LP revenue lower than flashloan premium. I implemented a fix and it fixes the large negative revenue we have seen, but it still has some tiny negative revenues (the most negative number I see is totalRevenueDeltaUSD=$-0.00106344452993117741167457972125, most is about |
Nice job catching this @tnkrxyz ! I reviewed the new code and it looks good, makes sense to me. I would think those small, small discrepancies are also due to rounding. I see 2 ways to go about it.
I would say 3 things before we merge:
|
merged.
fixed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good work @tnkrxyz !
See #1519 a list of tasks in this PR.
Almost there. I have a few questions, @dmelotik, maybe you can help:
What should be the FeeType for Flashloan fees? FeeType.OTHER?Added FeeType.FLASHLOAN_FEE_LP and FeeType.FLASHLOAN_FEE_PROTOCOLCurrently risk type is set at protocol level, but aave-v3 allow both isolated and global risk type at user/account level. It seems it may be necessary to add a RiskType.MIXED?Noted in READMEinterestRateType info is available in Borrow events, but not in Repay events. Unless we use callHandler for Repay (but then it still won't work for chains not supporting callHandler), we don't know the interestRateType for Repays. This creates a problem that repays cannot find matched borrowing positions to subtract: https://github.com/aave/aave-v3-core/blob/29ff9b9f89af7cd8255231bc5faf26c3ce0fb7ce/contracts/protocol/libraries/logic/BorrowLogic.sol#L32-L47 . But if we ignore interestRateType for borrows, we may combine stable and variable interest positions (this may be fine?) There is a similar issue for Liquidations, we don't know the interestRateType for liquidated Borrow position and both Variable and Stable positions may be liquidated at the same time.came up with a solution to detect interestRateType for repays and liquidationsA couple possible improvements to lending sdk I hope to check whether makes sense:
use enum type to define function argument type, e.g.
getOrCreateRewardType(rewardTokenType: string)
==>getOrCreateRewardType(rewardTokenType: RewardTokenType)
. We just need to addexport type RewardTokenType = string
after the enum definition. Similarily for InterestRateType, PositionSide, TransactionTypespecify a tokenPricier object in the manger class, that basically calls
getAssetPriceInUSDC()
(this is how bridge sdk handle prices) so the sdk can determine best place to refresh prices.Deployment & Dashboard