feat: add get_position read-only view function - #480
Merged
Mimah97 merged 2 commits intoJun 30, 2026
Conversation
Adds a public read-only get_position() function to query user positions in
markets without modifying contract state.
## Function Signature
pub fn get_position(
env: Env,
market_id: u32,
user: Address,
) -> Result<Option<crate::types::Position>, ContractError>
## Purpose
Provides off-chain and on-chain clients with a clean, read-only view of:
- User's current share balances (YES/NO)
- Locked collateral backing current position
- Historical deposit data
- Settlement status
## Returns
- Ok(Some(Position)): User has a position in the market with:
- yes_shares: Number of YES shares held
- no_shares: Number of NO shares held
- locked_collateral: Collateral locked to back shares
- total_deposited: Total collateral ever deposited
- is_settled: Whether position has been settled
- Ok(None): User has no position in this market
- Err(ContractError::UpgradeRequired): Storage version mismatch
## Documentation
Comprehensive docstring includes:
✓ Function description and purpose
✓ Full argument documentation
✓ Return value documentation
✓ Error cases
✓ Usage example with pattern matching
## Use Cases
✓ Query position for UI display
✓ Check shares and collateral for trading calculations
✓ Verify settlement status
✓ Off-chain position tracking and analytics
## Features
✓ Read-only: No state modification (safe for querying)
✓ Optional Result: Gracefully handles non-existent positions
✓ Version-checked: Ensures storage compatibility
✓ Well-documented: Clear docstring with examples
✓ Consistent API: Matches pattern of other get_* functions
## Implementation
✓ Delegates to storage::get_position()
✓ Properly qualified return type with crate::types::Position
✓ Positioned logically after get_outcome_count()
✓ Follows existing code style and patterns
✓ No breaking changes (additive only)
## Files Modified
- contracts/market/src/lib.rs: Added get_position() function (33 lines)
## Summary
Clean, read-only accessor for querying user positions in markets.
Enables client applications to safely inspect position data without
state modification. Follows existing patterns and includes comprehensive
documentation.
Total: 33 lines added
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.
Closes #317
Adds a public read-only get_position() function to query user positions in markets without modifying contract state.
Function Signature
pub fn get_position(
env: Env,
market_id: u32,
user: Address,
) -> Result<Optioncrate::types::Position, ContractError>
Purpose
Provides off-chain and on-chain clients with a clean, read-only view of:
Returns
Ok(Some(Position)): User has a position in the market with:
Ok(None): User has no position in this market
Err(ContractError::UpgradeRequired): Storage version mismatch
Documentation
Comprehensive docstring includes:
✓ Function description and purpose
✓ Full argument documentation
✓ Return value documentation
✓ Error cases
✓ Usage example with pattern matching
Use Cases
✓ Query position for UI display
✓ Check shares and collateral for trading calculations ✓ Verify settlement status
✓ Off-chain position tracking and analytics
Features
✓ Read-only: No state modification (safe for querying)
✓ Optional Result: Gracefully handles non-existent positions
✓ Version-checked: Ensures storage compatibility
✓ Well-documented: Clear docstring with examples
✓ Consistent API: Matches pattern of other get_* functions
Implementation
✓ Delegates to storage::get_position()
✓ Properly qualified return type with crate::types::Position ✓ Positioned logically after get_outcome_count()
✓ Follows existing code style and patterns
✓ No breaking changes (additive only)
Files Modified
Summary
Clean, read-only accessor for querying user positions in markets. Enables client applications to safely inspect position data without state modification. Follows existing patterns and includes comprehensive documentation.
Total: 33 lines added