Skip to content

Get positions script should not panic#297

Open
illia-malachyn wants to merge 3 commits intov0from
illia-malachyn/get-positions-should-not-panic
Open

Get positions script should not panic#297
illia-malachyn wants to merge 3 commits intov0from
illia-malachyn/get-positions-should-not-panic

Conversation

@illia-malachyn
Copy link
Collaborator

@illia-malachyn illia-malachyn commented Mar 26, 2026

What is done

getPositionDetails panics when called with a closed/non-existent position ID.
This is problematic for batch queries (get_positions_by_ids.cdc) - one stale ID
causes the entire batch to fail.

Refactor _borrowPosition to delegate to a new _tryBorrowPosition that returns
an optional reference instead of panicking. Add tryGetPositionDetails that
guards on existence before delegating to getPositionDetails.

Update get_positions_by_ids.cdc to use tryGetPositionDetails so closed positions
are silently skipped instead of failing the batch.

Why

The fcm-observer queries positions in batches using get_positions_by_ids.cdc. Here's the problem:

  1. We call get_position_ids.cdc -> get back [0, 3, 7, 12, ...]
  2. We call get_positions_by_ids.cdc with those IDs
  3. Between step 1 and step 2, position 7 gets closed on-chain
  4. getPositionDetails(pid: 7) panics -> the entire batch fails, all 50 positions get no update

We're always behind - by the time we use the IDs, some may already be stale. tryGetPositionDetails skips missing positions instead of panicking, so the rest of the batch succeeds.

getPositionDetails panics when called with a closed/non-existent position ID.
This is problematic for batch queries (get_positions_by_ids.cdc) - one stale ID
causes the entire batch to fail.

Refactor _borrowPosition to delegate to a new _tryBorrowPosition that returns
an optional reference instead of panicking. Add tryGetPositionDetails that
guards on existence before delegating to getPositionDetails.

Update get_positions_by_ids.cdc to use tryGetPositionDetails so closed positions
are silently skipped instead of failing the batch.
@illia-malachyn illia-malachyn changed the base branch from main to v0 March 26, 2026 14:24
@illia-malachyn illia-malachyn marked this pull request as ready for review March 26, 2026 14:26
@illia-malachyn illia-malachyn requested a review from a team as a code owner March 26, 2026 14:27
jordanschalm
jordanschalm previously approved these changes Mar 26, 2026
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.

2 participants