Skip to content

add liquidation flag#37

Closed
nialexsan wants to merge 10 commits intomainfrom
nialexsan/liquidation-flag
Closed

add liquidation flag#37
nialexsan wants to merge 10 commits intomainfrom
nialexsan/liquidation-flag

Conversation

@nialexsan
Copy link
Collaborator

@nialexsan nialexsan commented Aug 12, 2025

connected PR
onflow/FlowActions#25

@nialexsan nialexsan changed the base branch from nialexsan/tidal-calculations to main August 13, 2025 00:23
@codecov
Copy link

codecov bot commented Aug 13, 2025

Codecov Report

❌ Patch coverage is 91.30435% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cadence/contracts/TidalProtocol.cdc 91.30% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@nialexsan nialexsan marked this pull request as ready for review August 14, 2025 16:11
@sisyphusSmiling sisyphusSmiling self-requested a review August 14, 2025 16:39

/// Simulates the withdrawable amount of `type` if the position were fully rebalanced (liquidation scenario).
/// Returns the amount in units of `type`.
access(all) fun simulateLiquidationAmount(pid: UInt64, type: Type): UFix64 {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm realizing we may be overloading the term "liquidation" with this. When we say "liquidation" here, does this have anything to do with the liquidation path in the sense one would think of with respect to a lending protocol? In other words, are we using liquidation here to mean fully exiting the position or reclaiming collateral in the case of bad debt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

in this case it means fully closing/exiting a position at that moment

Copy link
Contributor

@sisyphusSmiling sisyphusSmiling left a comment

Choose a reason for hiding this comment

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

I left several comments, but I'll highlight my main concerns here:

  • I don't think the simulation method deals with balances of multiple token types
  • This method is a bit difficult to test. I think it would be best to implement this in the patterns consistent with the refactor (e.g. #36) where core calculations are made in pure functions. Maybe this method could take BalanceSheet, topUpAvailable, topUpType, and withdrawType (likely not exhaustive) which would allow the method to be more easily called from a unit test with minimal setup
  • It's not clear to me if liquidation implies the repayment of bad debt or closing out a position. Maybe we consider renaming if this isn't involved liquidation in the traditional sense. Between this and the previous point, the interface could look something like:
access(all)
fun calculateCloseoutBalance(
  balanceSheet: BalanceSheet,
  topUpAvailable: UFix64,
  topUpType: Type,
  topUpTokenState: &TokenState,
  withdrawType: Type,
  withdrawBalance: &InternalBalance?
): UFix64 {

let uintSourceAmount = DeFiActionsMathUtils.toUInt128(sourceAmountUFix)

// ----- Deposit token leg (this position's balance in `type`) -----
let maybeDepositBalance = position.balances[type]
Copy link
Contributor

Choose a reason for hiding this comment

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

Couldn't a balance also exist for withdrawals on a type? I think the variable name is throwing me off since I'd think the balance returned is the amount available for withdrawal from the position, which in my mind conflicts with deposit here.

Also, I think we need to check the full position balance across all withdrawals, not just the source type and provided type. Once we're dealing with balances related to more than two type, I think the balances of the third type (and beyond) will be omitted from this calculation.

For instance, let's say the topUpSource provides MOET and the requested Type is FLOW, how would this method consider a credit or debit in a third token type?

let sourceType = topUpSource.getSourceType()

// Prices (in default token units)
let maybeDepositTokenPrice = self.priceOracle.price(ofToken: type)
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this the price of type being withdrawn?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes, it is, I think the confusion is between deposit as a noun and deposit as a verb, I'll rename it

}
let uintLiquidationAmountInType = DeFiActionsMathUtils.div(netQuote, uintDepositTokenPrice)

return DeFiActionsMathUtils.toUFix64Round(uintLiquidationAmountInType)
Copy link
Contributor

Choose a reason for hiding this comment

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

I would think we want to round down here since this amount denotes a withdrawal from a position. Is that right?

Co-authored-by: Giovanni Sanchez <108043524+sisyphusSmiling@users.noreply.github.com>
@nialexsan nialexsan mentioned this pull request Aug 21, 2025
@nialexsan nialexsan marked this pull request as draft August 22, 2025 20:04
@nialexsan
Copy link
Collaborator Author

closed in favour of another approach

@nialexsan nialexsan closed this Aug 25, 2025
@nialexsan nialexsan deleted the nialexsan/liquidation-flag branch September 16, 2025 15:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments