feat(looping): persist looping position state and close positions (#414) - #447
Merged
Merged
Conversation
open_position now records the full position (owner, collateral and debt assets/amounts, leverage, status) under a monotonically increasing position id instead of discarding its inputs, and close_position is implemented as an owner-authenticated, idempotent transition that returns the closed record. Validation failures and lifecycle conflicts surface as defined LoopingError contract errors rather than host traps, and a get_position query exposes the on-chain state for reconciliation with the backend's position lifecycle. Adds unit and property-based tests for open/close round-trips, double-close and non-owner rejection, missing positions, and leverage range validation, and updates the open_position fuzz target to the new signature.
5 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
Implements #414: the looping contract now keeps a persistent on-chain record of every leveraged position and has a working, owner-gated close path.
Changes
Position storage
Positioncontract type (owner,collateral_asset/collateral_amount,debt_asset/debt_amount,leverage,status) stored under a monotonically increasing position id.pos_cntinstance counter is preserved, so ids never repeat and existing deployments keep their numbering.PositionStatus(Open/Closed) mirrors the backendStatuslifecycle (opened/closed);pendingremains a backend-only pre-relay state.Open position
open_positionnow takes the collateral/debt assets and amounts and persists the full record instead of discarding its inputs.LoopingErrorcontract errors instead of raw panics.Close position
close_positionauthenticates the caller, verifies the position exists and is open, marks it closed, and returns the closed record.LoopingErrorerrors (idempotent, no host traps).Queries
get_position(position_id)returns the on-chain state (owner/status/amounts) so the backend's position lifecycle and outbox relay can reconcile.Tests
commoncrate style) assertingopen_positionsucceeds exactly for valid inputs.open_positionsignature.Verified locally:
cargo test,cargo clippy -- -D warnings,cargo fmt --check, andcargo build --target wasm32-unknown-unknown --releaseall pass; the fuzz target compiles under the nightly toolchain with the CI pin steps.Out of scope (per the issue): borrow->swap->redeposit mechanics and multi-asset support.
Closes #414