fix: implement repayAndClosePosition to enable collateral withdrawal#16
fix: implement repayAndClosePosition to enable collateral withdrawal#16kgrgpg wants to merge 2 commits intocadence-testing-patterns-forkfrom
Conversation
- Added repayAndClosePosition() method to Pool contract that handles both debt repayment and collateral return - Added getId() method to Position struct to expose position ID - Created repay_and_close_position_v2.cdc transaction that uses the new contract method - Updated position_lifecycle_happy_test.cdc to verify collateral is returned This fixes the critical issue where users could not retrieve their collateral after repaying loans. The contract method has internal access to withdraw funds, bypassing the FungibleToken.Withdraw authorization issue. Test now shows: - MOET debt: 615.38 → 0 (repaid) - Flow balance: 0 → 1000 (collateral returned) - Position successfully closed
CRITICAL: The repayAndClosePosition method allows anyone to close anyone's position This must be fixed before production deployment
|
During review, I realized that the I've documented this in This MUST be fixed before any production deployment. The test passes because it's only testing the happy path where the position owner closes their own position. But in reality, any address could call this method with any position ID. Proposed fixes:
For now, this PR demonstrates the solution to the collateral withdrawal problem, but it introduces a critical security vulnerability that needs immediate attention. |
- Merged PR #17 which provides a clean solution for collateral withdrawal - Uses MockTidalProtocolConsumer.borrowPositionForWithdraw() to provide FungibleToken.Withdraw authorization - withdrawAndPull with pullFromTopUpSource: true automatically repays debt and returns collateral - Updated misleading comments and logs to reflect that collateral IS successfully returned - This approach avoids the security vulnerability in PR #16's repayAndClosePosition method
Suggestion: Close this PR in favor of PR #14I've merged Kay-Zee's solution from PR #17 into PR #14, which provides a better approach to the collateral withdrawal issue without the security vulnerability present in this PR. The solution in PR #14:
Since PR #14 now includes a complete and secure solution for the position lifecycle (including collateral withdrawal), I suggest closing this PR to avoid the security risk. |
Summary
This PR fixes the critical issue discovered in #14 where users cannot retrieve their collateral after repaying their loans.
Problem
Position.withdraw()requiresFungibleToken.Withdrawauthorization that transactions cannot obtainSolution
Added
repayAndClosePosition()method to the Pool contract that:Changes
repayAndClosePosition()method to TidalProtocol.PoolgetId()method to Position struct to expose position IDrepay_and_close_position_v2.cdctransaction using the new methodposition_lifecycle_happy_test.cdcto verify collateral returnTest Results
✅ All tests pass with collateral successfully returned:
Testing
Run:
flow test cadence/tests/position_lifecycle_happy_test.cdcThis PR is stacked on top of #14 and completes the position lifecycle functionality.