From 755ae9536920d46f157f20b0ef875e9cd53f302b Mon Sep 17 00:00:00 2001 From: Patrick Fuchs Date: Tue, 10 Feb 2026 17:05:32 +0100 Subject: [PATCH] add rebalancing through position --- cadence/contracts/FlowCreditMarket.cdc | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cadence/contracts/FlowCreditMarket.cdc b/cadence/contracts/FlowCreditMarket.cdc index 122a8cf..a005ee8 100644 --- a/cadence/contracts/FlowCreditMarket.cdc +++ b/cadence/contracts/FlowCreditMarket.cdc @@ -180,6 +180,11 @@ access(all) contract FlowCreditMarket { /// not just individual position owners' positions. access(all) entitlement EPosition + /// ERebalance + /// + /// Entitlement for rebalancing positions. + access(all) entitlement ERebalance + /// EGovernance /// /// Entitlement for governance operations that control pool-wide parameters and configuration. @@ -3412,7 +3417,7 @@ access(all) contract FlowCreditMarket { /// Rebalancing is done on a best effort basis (even when force=true). If the position has no sink/source, /// of either cannot accept/provide sufficient funds for rebalancing, the rebalance will still occur but will /// not cause the position to reach its target health. - access(EPosition) fun rebalancePosition(pid: UInt64, force: Bool) { + access(EPosition | ERebalance) fun rebalancePosition(pid: UInt64, force: Bool) { post { self.positionLock[pid] == nil: "Position is not unlocked" } @@ -4075,6 +4080,19 @@ access(all) contract FlowCreditMarket { let pool = self.pool.borrow()! pool.provideTopUpSource(pid: self.id, source: source) } + + /// Rebalances the position to the target health value, if the position is under- or over-collateralized, + /// as defined by the position-specific min/max health thresholds. + /// If force=true, the position will be rebalanced regardless of its current health. + /// + /// When rebalancing, funds are withdrawn from the position's topUpSource or deposited to its drawDownSink. + /// Rebalancing is done on a best effort basis (even when force=true). If the position has no sink/source, + /// of either cannot accept/provide sufficient funds for rebalancing, the rebalance will still occur but will + /// not cause the position to reach its target health. + access(EPosition | ERebalance) fun rebalance(force: Bool) { + let pool = self.pool.borrow()! + pool.rebalancePosition(pid: self.id, force: force) + } } /// PositionManager