@@ -1237,8 +1237,8 @@ access(all) contract FlowALPv0 {
12371237 /// Withdraws the requested funds from the specified position
12381238 /// with the configurable `pullFromTopUpSource` option.
12391239 ///
1240- /// If `pullFromTopUpSource` is true, deficient value putting the position below its min health
1241- /// is pulled from the position's configured `topUpSource`.
1240+ /// If `pullFromTopUpSource` is true, any deficiency below the position's target health
1241+ /// is pulled from the position's configured `topUpSource` (consistent with depositAndPush) .
12421242 /// TODO(jord): ~150-line function - consider refactoring.
12431243 access (FlowALPModels.EPosition ) fun withdrawAndPull (
12441244 pid : UInt64 ,
@@ -1275,40 +1275,46 @@ access(all) contract FlowALPv0 {
12751275 let topUpSource = position .borrowTopUpSource ()
12761276 let topUpType = topUpSource ?. getSourceType () ?? self .state .getDefaultToken ()
12771277
1278- let requiredDeposit = self .fundsRequiredForTargetHealthAfterWithdrawing (
1278+ // Compute the deposit required to maintain minHealth — the hard requirement.
1279+ let minHealthDeposit = self .fundsRequiredForTargetHealthAfterWithdrawing (
12791280 pid : pid ,
12801281 depositType : topUpType ,
12811282 targetHealth : position .getMinHealth (),
12821283 withdrawType : type ,
12831284 withdrawAmount : amount
12841285 )
12851286
1287+ // When pullFromTopUpSource is true, also check whether a deposit is needed
1288+ // to maintain targetHealth (consistent with depositAndPush behaviour).
1289+ let targetHealthDeposit = pullFromTopUpSource
1290+ ? self .fundsRequiredForTargetHealthAfterWithdrawing (
1291+ pid : pid ,
1292+ depositType : topUpType ,
1293+ targetHealth : position .getTargetHealth (),
1294+ withdrawType : type ,
1295+ withdrawAmount : amount
1296+ )
1297+ : 0.0
1298+
12861299 var canWithdraw = false
12871300
1288- if requiredDeposit == 0.0 {
1289- // We can service this withdrawal without any top up
1301+ if minHealthDeposit == 0.0 && targetHealthDeposit == 0.0 {
1302+ // No top-up needed: position stays above targetHealth (or minHealth when not pulling)
12901303 canWithdraw = true
12911304 } else if pullFromTopUpSource {
12921305 // We need more funds to service this withdrawal, see if they are available from the top up source
12931306 if let topUpSource = topUpSource {
1294- // If we have to rebalance, let's try to rebalance to the target health, not just the minimum
1295- let idealDeposit = self .fundsRequiredForTargetHealthAfterWithdrawing (
1296- pid : pid ,
1297- depositType : topUpType ,
1298- targetHealth : position .getTargetHealth (),
1299- withdrawType : type ,
1300- withdrawAmount : amount
1301- )
1307+ // Try to rebalance to target health
1308+ let idealDeposit = targetHealthDeposit > 0.0 ? targetHealthDeposit : minHealthDeposit
13021309
13031310 let pulledVault <- topUpSource .withdrawAvailable (maxAmount : idealDeposit )
13041311 assert (pulledVault .getType () == topUpType , message : " topUpSource returned unexpected token type" )
13051312 let pulledAmount = pulledVault .balance
13061313
1307-
1308- // NOTE: We requested the "ideal" deposit, but we compare against the required deposit here.
1309- // The top up source may not have enough funds get us to the target health, but could have
1310- // enough to keep us over the minimum.
1311- if pulledAmount > = requiredDeposit {
1314+ // NOTE: We requested the "ideal" deposit (targetHealth), but the hard requirement
1315+ // is minHealth. The top up source may not have enough to reach targetHealth,
1316+ // but the withdrawal can proceed as long as we stay above minHealth.
1317+ if pulledAmount > = minHealthDeposit {
13121318 // We can service this withdrawal if we deposit funds from our top up source
13131319 self ._depositEffectsOnly (
13141320 pid : pid ,
@@ -1334,7 +1340,7 @@ access(all) contract FlowALPv0 {
13341340 log (" [CONTRACT] Token type: \( type .identifier ) " )
13351341 log (" [CONTRACT] Requested amount: \( amount ) " )
13361342 log (" [CONTRACT] Available balance (without topUp): \( availableBalance ) " )
1337- log (" [CONTRACT] Required deposit for minHealth: \( requiredDeposit ) " )
1343+ log (" [CONTRACT] Required deposit for minHealth: \( minHealthDeposit ) " )
13381344 log (" [CONTRACT] Pull from topUpSource: \( pullFromTopUpSource ) " )
13391345 }
13401346 // We can't service this withdrawal, so we just abort
0 commit comments