Phase 0: availableBalance refactor to pure helpers + tests#36
Closed
kgrgpg wants to merge 2 commits intorefactor/better-codefrom
Closed
Phase 0: availableBalance refactor to pure helpers + tests#36kgrgpg wants to merge 2 commits intorefactor/better-codefrom
kgrgpg wants to merge 2 commits intorefactor/better-codefrom
Conversation
…add phase0 pure math tests; remove DeFiBlocks dir and REFACTOR_EVALUATION.md
…gn with Phase 0 implementation
Comment on lines
+622
to
+644
| let view = self.buildPositionView(pid: pid) | ||
|
|
||
| // Build a TokenSnapshot for the requested withdraw type (may not exist in view.snaps) | ||
| let tokenState = self._borrowUpdatedTokenState(type: type) | ||
| let snap = TidalProtocol.TokenSnapshot( | ||
| price: TidalProtocolUtils.ufix64ToUInt256(self.priceOracle.price(ofToken: type)!, decimals: 18), | ||
| credit: tokenState.creditInterestIndex, | ||
| debit: tokenState.debitInterestIndex, | ||
| risk: TidalProtocol.RiskParams( | ||
| cf: TidalProtocolUtils.ufix64ToUInt256(self.collateralFactor[type]!, decimals: 18), | ||
| bf: TidalProtocolUtils.ufix64ToUInt256(self.borrowFactor[type]!, decimals: 18), | ||
| lb: TidalProtocolUtils.e18 + 50_000_000_000_000_000 | ||
| ) | ||
| ) | ||
|
|
||
| let withdrawBal = view.balances[type] | ||
| let uintMax = TidalProtocol.maxWithdraw( | ||
| view: view, | ||
| withdrawSnap: snap, | ||
| withdrawBal: withdrawBal, | ||
| targetHealth: view.minHealth | ||
| ) | ||
| return TidalProtocolUtils.uint256ToUFix64(uintMax, decimals: 18) |
Contributor
There was a problem hiding this comment.
Not sure what's going on, but these changes appear to be included in #35
Closed
Contributor
Author
Contributor
Author
|
Closing in favor of #35; changes are already included and updated to UInt128/e24 on refactor/better-code. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Phase 0: availableBalance refactor to pure helpers + tests
This PR is stacked on top of PR #35 (base:
refactor/better-code). It completes the Phase 0 vertical slice for health/withdraw by wiring the public query path to pure helpers while preserving existing top-up behavior.Summary
pullFromTopUpSource=trueby delegating tofundsAvailableAboveTargetHealthAfterDepositing(...).buildPositionView(pid)+maxWithdraw(...)with 18-decimalUInt256math.cadence/tests/phase0_pure_math_test.cdccoveringhealthFactorandmaxWithdraw(both debt-increase and collateral-drawdown paths). Expectations computed inUInt256to match on-chain truncation.REFACTORING_PLAN.mdcode sections to use current Cadence syntax and mirror the implemented Phase 0 shape (incl. top-up path note).DeFiBlocks/andREFACTOR_EVALUATION.md(superseded by DeFiActions and current plan).Rationale
healthFactor,maxWithdraw).topUpSource.Implementation Notes
buildPositionView(pid)creates immutable snapshots per token (price, indices, risk) and copies of position balances; no storage mutation.maxWithdraw(view, withdrawSnap, withdrawBal, targetHealth)solves the linear constraint to maintain target health; handles both credit and debit cases.availableBalancenow chooses:uint256ToUFix64(...).UInt256arithmetic to match chain truncation.Tests
cadence/tests/phase0_pure_math_test.cdctest_healthFactor_zeroBalances_returnsZerotest_healthFactor_simpleCollateralAndDebttest_maxWithdraw_increasesDebtWhenNoCredittest_maxWithdraw_fromCollateralLimitedByHealthrun_tests.sh.Backward Compatibility
pullFromTopUpSource=true.Documentation
REFACTORING_PLAN.mdsnippets updated to current Cadence (access(all), correct entitlements/types) and reflect the preserved top-up path inavailableBalance.Next Phases (follow-ups)
applyWithdraw/applyDeposit/applyAccrual/applyLiquidationwith pre/post invariants, using the same pure helpers for validation before mutation.How to verify locally
All tests should pass, including the new Phase 0 tests.