Skip to content

Nialexsan/position lock#113

Merged
nialexsan merged 23 commits intomainfrom
nialexsan/position-lock
Feb 7, 2026
Merged

Nialexsan/position lock#113
nialexsan merged 23 commits intomainfrom
nialexsan/position-lock

Conversation

@nialexsan
Copy link
Copy Markdown
Contributor

@nialexsan nialexsan commented Jan 19, 2026

Closes: https://github.com/onflow/flow-consumer-defi-internal/issues/2

Description


For contributor use:

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work.
  • Code follows the standards mentioned here.
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@nialexsan nialexsan requested a review from a team as a code owner January 19, 2026 20:46
Comment thread cadence/contracts/FlowCreditMarket.cdc Outdated
Comment thread cadence/contracts/FlowCreditMarket.cdc Outdated
Comment thread cadence/contracts/FlowCreditMarket.cdc Outdated
Comment thread cadence/contracts/FlowCreditMarket.cdc Outdated
nialexsan and others added 2 commits January 20, 2026 10:09
Co-authored-by: Bastian Müller <bastian@turbolent.com>
@holyfuchs
Copy link
Copy Markdown
Member

given the huge implications it would have forgetting to unlock a pid, I would suggest adding the following to all the functions that lock

            post {
                self.positionLock[pid] == nil: "Position is not unlocked"
            }

Comment thread cadence/contracts/FlowCreditMarket.cdc Outdated
Comment thread cadence/contracts/FlowCreditMarket.cdc Outdated
Copy link
Copy Markdown
Member

@jordanschalm jordanschalm left a comment

Choose a reason for hiding this comment

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

+1 to Patrick's suggestion about using pre/post-conditions for the position locking:

  • functions which expect their caller to have locked a pid should have pre-condition to that effect
  • functions which lock a pid should have a post-condition verifying that the pid was unlocked

/// is pushed to the position's configured `drawDownSink`.
/// TODO(jord): ~100-line function - consider refactoring.
access(EPosition) fun depositAndPush(
access(self) fun _depositEffectsOnly(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we add documentation for this function?

Comment thread cadence/contracts/FlowCreditMarket.cdc Outdated
Comment thread cadence/contracts/FlowCreditMarket.cdc Outdated
self._rebalancePositionNoLock(pid: pid, force: force)
self._unlockPosition(pid)
}
access(self) fun _rebalancePositionNoLock(pid: UInt64, force: Bool) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we add documentation for this function?

Comment thread cadence/contracts/FlowCreditMarket.cdc
Comment thread cadence/tests/adversarial_recursive_withdraw_source_test.cdc Outdated
Comment thread cadence/tests/adversarial_recursive_withdraw_source_test.cdc Outdated
Comment thread cadence/tests/async_update_position_test.cdc Outdated
Comment thread cadence/tests/async_update_position_test.cdc Outdated
[ 0 as UInt64 ],
PROTOCOL_ACCOUNT
)
Test.expect(updatePositionRes, Test.beSucceeded())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are there any other expected side effects of the position update we can test here besides that the transaction succeeded? I think the user's position collateral amount (denominated in Flow) should have increased, and the user's vault which backs their top-up source should have decreased.

If this is the only test case we have for the async update function, I'd also suggest adding an issue to expand test coverage (no top-up source, top-up source insufficient, position has queued updates, etc.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this test is specifically to test locking mechanism, but I can add more checks here

Comment thread cadence/contracts/FlowCreditMarket.cdc Outdated
// TODO(jord): Sink/source should be valid
}
post {
self.positionLock[self.nextPositionID] == nil: "Position is not unlocked"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

might be worth noting that this snapshots the self.nextPositionID, and doesn't take the self.nextPositionID = self.nextPositionID + 1 3 lines down, even though this check happens "after" that line is executed

Copy link
Copy Markdown
Member

@Kay-Zee Kay-Zee Feb 7, 2026

Choose a reason for hiding this comment

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

or, actually, i guess should the before() function be used here?

or maybe result.id?

Comment thread cadence/contracts/mocks/MockFlowCreditMarketConsumer.cdc Outdated
Comment thread cadence/contracts/FlowCreditMarket.cdc Outdated
Comment thread cadence/contracts/FlowCreditMarket.cdc
Comment thread cadence/contracts/FlowCreditMarket.cdc
Comment thread cadence/contracts/FlowCreditMarket.cdc Outdated
Comment thread cadence/contracts/FlowCreditMarket.cdc
@nialexsan nialexsan merged commit f76929d into main Feb 7, 2026
1 check passed
@nialexsan nialexsan deleted the nialexsan/position-lock branch February 7, 2026 01:39
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.

6 participants