Skip to content

feat(looping): persist looping position state and close positions (#414) - #447

Merged
YaronZaki merged 1 commit into
Quantarq:mainfrom
nasalehj:feat/looping-position-state
Aug 24, 2026
Merged

feat(looping): persist looping position state and close positions (#414)#447
YaronZaki merged 1 commit into
Quantarq:mainfrom
nasalehj:feat/looping-position-state

Conversation

@nasalehj

Copy link
Copy Markdown
Contributor

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

  • New Position contract type (owner, collateral_asset/collateral_amount, debt_asset/debt_amount, leverage, status) stored under a monotonically increasing position id.
  • The existing pos_cnt instance counter is preserved, so ids never repeat and existing deployments keep their numbering.
  • PositionStatus (Open/Closed) mirrors the backend Status lifecycle (opened/closed); pending remains a backend-only pre-relay state.

Open position

  • open_position now takes the collateral/debt assets and amounts and persists the full record instead of discarding its inputs.
  • Validation (collateral > 0, debt >= 0, leverage in 100..=500) returns defined LoopingError contract errors instead of raw panics.

Close position

  • close_position authenticates the caller, verifies the position exists and is open, marks it closed, and returns the closed record.
  • Double-close, non-owner close, and close-of-missing-position are all defined LoopingError errors (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

  • Deterministic unit tests: open/close round-trip, double-close rejection, non-owner rejection, missing-position rejection, persistence of all fields, and leverage boundary validation.
  • Property-based test (proptest, matching the common crate style) asserting open_position succeeds exactly for valid inputs.
  • Fuzz target updated to the new open_position signature.

Verified locally: cargo test, cargo clippy -- -D warnings, cargo fmt --check, and cargo build --target wasm32-unknown-unknown --release all 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

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.

@YaronZaki YaronZaki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@YaronZaki
YaronZaki merged commit a6a307b into Quantarq:main Aug 24, 2026
14 checks passed
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.

Feature: Persist Looping Position State And Close Positions

2 participants