Skip to content

Fix: Add 5% fee margin buffer for scheduling estimation variance#68

Merged
nialexsan merged 5 commits intomainfrom
fix/restart-recurring-flag
Nov 28, 2025
Merged

Fix: Add 5% fee margin buffer for scheduling estimation variance#68
nialexsan merged 5 commits intomainfrom
fix/restart-recurring-flag

Conversation

@kgrgpg
Copy link
Contributor

@kgrgpg kgrgpg commented Nov 27, 2025

Summary

Adds a 5% margin buffer to the fee estimation in AutoBalancer.scheduleNextRebalance() to handle variance in scheduling cost calculations.

Problem

When AutoBalancers self-schedule recurring rebalances, they estimate the required fees via FlowTransactionScheduler.estimate(). However, the actual fee at scheduling time can sometimes slightly exceed the estimate, causing the scheduling to fail with insufficient fees even when the account has adequate funds.

Solution

Add a 5% buffer (1.05 multiplier) when:

  1. Checking if sufficient fees are available
  2. Withdrawing fees for scheduling
// Check with 5% margin buffer to match withdrawal
if config.txnFunder.minimumAvailable() < (estimate.flowFee! * 1.05) {
    return "INSUFFICIENT_FEES_AVAILABLE"
}

// withdraw the fees from the funder with a margin buffer
let feeWithMargin = estimate.flowFee! * 1.05
let fees <- config.txnFunder.withdrawAvailable(maxAmount: feeWithMargin) as! @FlowToken.Vault

Changes

  • DeFiActions.cdc: Added 5% fee margin buffer in scheduleNextRebalance()
  • Improved nil-safety for error handling
  • Minor comment improvements

Testing

Tested in FlowVaults-sc with comprehensive scheduling tests - all 18 tests pass.

Removed the isInternallyManaged check in executeTransaction().
Now ANY execution (internal or external) will trigger scheduleNextRebalance()
if the AutoBalancer has a recurring config.

This ensures externally-scheduled transactions (e.g., Supervisor recovery)
will restart the self-scheduling cycle instead of being fire-once.

The scheduleNextRebalance function already handles deduplication by
checking for existing scheduled transactions within the interval.
This adds a new optional flag 'restartRecurring' in the data parameter that,
when true, signals the AutoBalancer to resume self-scheduling after execution
even for externally-scheduled transactions.

Use case: Supervisor recovery - when a stuck tide is seeded by the Supervisor,
it should resume its normal self-scheduling cycle rather than being 'fire once'.

Design:
- Default behavior unchanged: external schedules are 'fire once'
- Pass {"restartRecurring": true} to restart the self-scheduling cycle
- Preserves original design from PR #45 while enabling recovery scenarios

See: #45 for original design context
…overy instead

Keep only the fee buffer fix (5% margin) in scheduleNextRebalance().
The recovery mechanism now uses Schedule capability to directly call
scheduleNextRebalance() rather than passing a flag through executeTransaction().
@kgrgpg kgrgpg changed the title Fix: Add restartRecurring flag and fee margin buffer for Supervisor recovery Fix: Add 5% fee margin buffer for scheduling estimation variance Nov 27, 2025
@nialexsan nialexsan force-pushed the fix/restart-recurring-flag branch from 46cb774 to 94560b3 Compare November 28, 2025 00:29
@nialexsan nialexsan merged commit 04a27a4 into main Nov 28, 2025
7 checks passed
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