Validate debt type before withdrawal credit-to-debit flip#200
Open
liobrasil wants to merge 2 commits intonialexsan/multi-debtfrom
Open
Validate debt type before withdrawal credit-to-debit flip#200liobrasil wants to merge 2 commits intonialexsan/multi-debtfrom
liobrasil wants to merge 2 commits intonialexsan/multi-debtfrom
Conversation
6 tasks
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.
Summary
Prevent confusing "Internal error" reverts by validating debt type before a withdrawal flips a balance into debt.
Previous behavior (bug example)
Example position state:
FLOWdebtMOETcreditAction:
MOETin a flow that would flipMOETfrom Credit to Debit.Before this fix:
withdrawAndPullfirst calledrecordWithdrawal.FLOW+MOET).validateDebtTyperan and hit this invariant assert:"Internal error: Position has multiple debt types"User-facing error improvement
"Internal error: Position has multiple debt types"validateDebtType"Position already has debt type X. Cannot borrow Y. Only one debt type allowed per position."Result: transaction still reverts safely (no fund loss), but now returns a clearer and actionable error.
Why this fix is needed
PR #184 adds temporary one-debt-type guardrails. Validation should happen before mutation on flip paths (check-before-effect) so users get deterministic, domain-specific errors and we avoid relying on post-mutation invariant panics.
What changed
withdrawAndPull:withdraw amountvstrue credit balancebefore mutation.position.validateDebtType(type)beforerecordWithdrawalfor that flip path.Tests
flow test cadence/tests/single_token_constraint_test.cdcflow test cadence/tests/debt_type_constraint_three_token_test.cdc