Skip to content

TidalProtocol.cdc: implement dete review feedback (configurable insurance/limits; remove capacity growth; safety/asserts; event types; logging guard; health APIs)#46

Merged
kgrgpg merged 16 commits intomainfrom
review/dete-comments-audit
Oct 13, 2025

Conversation

@kgrgpg
Copy link
Contributor

@kgrgpg kgrgpg commented Sep 22, 2025

This PR addresses feedback from dete’s review (see PR #44: #44). Full triage and mapping are in PR_REVIEW_TRIAGE_dete.md in the repo.

Implemented

  • Events and Types: use vaultType: Type in Deposited/Withdrawn; emit sites updated.
  • Safety/Correctness: replace silent clamp-to-zero with explicit underflow assertions in credit/debit balance updates; harmonize recordDeposit comparison to >=; refine post-withdrawal health assertion to skip only when a top-up was used in the same call and immediate health is 0 (transitional).
  • Configurability: add per-token insuranceRate (default 0.001) with governance setter; add per-token depositLimitFraction (default 0.05) with governance setter; updateInterestRates() uses insuranceRate; depositLimit() = capacity * depositLimitFraction.
  • Capacity: remove unused deposit-capacity growth (capacity is fixed at cap; growth never executed); retain per-deposit limiting with clear rationale.
  • Logging: add debugLogging flag and wrap verbose logs (default true).
  • API and Docs: implement get/set for {min,target,max} health with validation via capability-safe pool methods; move BalanceDirection near core types; add design notes on where/why 128-bit fixed-point is used vs UFix64 at boundaries; clarify comments for negative credit rate handling and deposit-limit rationale.
  • Tests: ./run_tests.sh passes across the suite.

Deferred / needs clarification (see PR_REVIEW_TRIAGE_dete.md)

  • Numeric types/ranges: switch recordDeposit/Withdrawal params to UFix64 and reduce UInt128 usage; evaluate future UFix128 migration when available. Added design notes now; type changes deliberately deferred.
  • Duplicate health/effective computations: further unify all paths via pure helpers; unify top-up vs no-top-up logic into a single path parameterized by optional source.
  • Optional follow-ups: governance-configurable debugLogging; script adapter to stringify event vaultType for off-chain viewing; make liquidation bonus configurable (discussion pending).

Related Work: Liquidation mechanism branches

Scope notes

  • Off-chain consumers are out-of-scope for this PR; focus is the TidalProtocol contract.

Links

… comments

refactor(protocol): harmonize >= in recordDeposit; add invariants to balance updates; replace zero-health guard with explicit health assertion
…e BalanceDirection; remove dead deposit-capacity growth; keep debug logging enabled
@codecov
Copy link

codecov bot commented Sep 22, 2025

Codecov Report

❌ Patch coverage is 57.03125% with 55 lines in your changes missing coverage. Please review.

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

📢 Thoughts on this report? Let us know!

Copy link
Member

Choose a reason for hiding this comment

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

This comment only makes sense in the context of this PR. We should not have these types of comments in the code. If we're trying to add context for the reviewer, let's do that on GH PR, and not in SC comments

Copy link
Member

Choose a reason for hiding this comment

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

if default true here, i don't think it changes much. I think if this flag is how we're moving forward with debug logging, it should probably not be true by default.

Copy link
Member

Choose a reason for hiding this comment

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

so many unnecessary checks no? I feel like re-checking self.debug on every line is not providing anything.

…solidate failure logs; remove PR-specific comments
@kgrgpg
Copy link
Contributor Author

kgrgpg commented Sep 26, 2025

Addressed review feedback on TidalProtocol.cdc:

  • Removed PR-specific inline comments; rephrased to neutral.
  • Set Pool.debugLogging default to false.
  • Consolidated repeated debugLogging checks in withdraw failure logging under a single guarded block.
  • Kept functional logs; they emit only when debugLogging is true.

Changes pushed to review/dete-comments-audit. Please re-review.

Copy link
Member

Choose a reason for hiding this comment

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

let's fix this issue similarly to how you fixed the other debug logs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Consolidated at other places

@kgrgpg kgrgpg force-pushed the review/dete-comments-audit branch 3 times, most recently from 35df16a to b81b037 Compare October 7, 2025 23:48
// Skip the assertion only when a top-up was used in this call and the immediate
// post-withdrawal health is 0 (transitional state before top-up effects fully reflect).
let postHealth = self.positionHealth(pid: pid)
if !(usedTopUp && postHealth == 0) {
Copy link
Member

Choose a reason for hiding this comment

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

Currently, the cadence/tests/position_lifecycle_happy_test.cdc test fails on this assert, due to usedTopUp being false, even though postHealth is 0. Can we rethink why this might be the case?

does it matter that we didn't use the top up source here? or should we have attempted to use it anyways (and i guess likely have gotten 0?)

…d update phase0_pure_math_test.cdc to match new logic with added test for zero collateral positive debt case
@Kay-Zee
Copy link
Member

Kay-Zee commented Oct 8, 2025

The commit looks good, but can we double check why the coverage is so low? i would've expected most of the changes to have been covered by the existing tests

@kgrgpg
Copy link
Contributor Author

kgrgpg commented Oct 13, 2025

Added more governance tests and debugging logs enabled to improve coverage from 32 to 57%

@kgrgpg kgrgpg merged commit 19b333e into main Oct 13, 2025
1 of 2 checks passed
@kgrgpg kgrgpg deleted the review/dete-comments-audit branch October 13, 2025 16:25
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