Skip to content

Commit 055ad88

Browse files
committed
add autobalancer callback
1 parent 752aaa5 commit 055ad88

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

cadence/contracts/interfaces/DeFiActions.cdc

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,14 @@ access(all) contract DeFiActions {
687687
}
688688
}
689689

690+
/// Callback invoked every time an AutoBalancer executes (runs rebalance).
691+
///
692+
access(all) resource interface AutoBalancerExecutionCallback {
693+
/// Called at the end of each rebalance run.
694+
/// @param balancerUUID: The AutoBalancer's UUID
695+
access(all) fun onExecuted(balancerUUID: UInt64)
696+
}
697+
690698
/// AutoBalancer
691699
///
692700
/// A resource designed to enable permissionless rebalancing of value around a wrapped Vault. An
@@ -728,6 +736,8 @@ access(all) contract DeFiActions {
728736
access(self) var _recurringConfig: AutoBalancerRecurringConfig?
729737
/// ScheduledTransaction objects used to manage automated rebalances
730738
access(self) var _scheduledTransactions: @{UInt64: FlowTransactionScheduler.ScheduledTransaction}
739+
/// Optional callback invoked every time rebalance() runs
740+
access(self) var _executionCallback: Capability<&{AutoBalancerExecutionCallback}>?
731741
/// An optional UniqueIdentifier tying this AutoBalancer to a given stack
732742
access(contract) var uniqueID: UniqueIdentifier?
733743

@@ -772,6 +782,7 @@ access(all) contract DeFiActions {
772782
self._recurringConfig = recurringConfig
773783
self._recurringConfig?.setAssignedAutoBalancer(self.uuid)
774784
self._scheduledTransactions <- {}
785+
self._executionCallback = nil
775786
self.uniqueID = uniqueID
776787

777788
emit CreatedAutoBalancer(
@@ -939,6 +950,11 @@ access(all) contract DeFiActions {
939950
}
940951
self._rebalanceRange = range
941952
}
953+
/// Sets the optional callback invoked every time this AutoBalancer runs rebalance.
954+
/// Pass nil to clear the callback.
955+
access(Set) fun setExecutionCallback(_ cap: Capability<&{AutoBalancerExecutionCallback}>?) {
956+
self._executionCallback = cap
957+
}
942958
/// Returns a copy of the struct's UniqueIdentifier, used in extending a stack to identify another connector in
943959
/// a DeFiActions stack. See DeFiActions.align() for more information.
944960
access(contract) view fun copyID(): UniqueIdentifier? {
@@ -1039,7 +1055,7 @@ access(all) contract DeFiActions {
10391055
// execute as declared, otherwise execute as currently configured, otherwise default to false
10401056
let dataDict = data as? {String: AnyStruct} ?? {}
10411057
let force = dataDict["force"] as? Bool ?? self._recurringConfig?.forceRebalance as? Bool ?? false
1042-
1058+
10431059
self.rebalance(force: force)
10441060

10451061
// If configured as recurring, schedule the next execution only if this is an internally-managed
@@ -1060,10 +1076,18 @@ access(all) contract DeFiActions {
10601076
}
10611077
}
10621078
}
1079+
if let cap = self._executionCallback {
1080+
if cap.check() {
1081+
if let callback = cap.borrow() {
1082+
callback.onExecuted(balancerUUID: self.uuid)
1083+
}
1084+
}
1085+
}
10631086
// clean up internally-managed historical scheduled transactions
10641087
self._cleanupScheduledTransactions()
10651088
}
1066-
/// Schedules the next execution of the rebalance if the AutoBalancer is configured as such and there is not
1089+
1090+
/// Schedules the next execution of the rebalance if the AutoBalancer is configured as such and there is not
10671091
/// already a scheduled transaction within the desired interval. This method is written to fail as gracefully as
10681092
/// possible, reporting any failures to schedule the next execution to the as an event. This allows
10691093
/// `executeTransaction` to continue execution even if the next execution cannot be scheduled while still
@@ -1171,14 +1195,14 @@ access(all) contract DeFiActions {
11711195
///
11721196
/// @param id: The ID of the scheduled transaction
11731197
///
1174-
/// @return &FlowTransactionScheduler.ScheduledTransaction?: The reference to the scheduled transaction, or nil
1198+
/// @return &FlowTransactionScheduler.ScheduledTransaction?: The reference to the scheduled transaction, or nil
11751199
/// if the scheduled transaction is not found
11761200
///
11771201
access(all) view fun borrowScheduledTransaction(id: UInt64): &FlowTransactionScheduler.ScheduledTransaction? {
11781202
return &self._scheduledTransactions[id]
11791203
}
11801204
/// Calculates the next execution timestamp for a recurring rebalance if the AutoBalancer is configured as such.
1181-
/// Returns nil if either unconfigured for recurring rebalancing or the interval is greater than the maximum
1205+
/// Returns nil if either unconfigured for recurring rebalancing or the interval is greater than the maximum
11821206
/// possible timestamp.
11831207
///
11841208
/// @return UFix64?: The next execution timestamp, or nil if a recurring rebalance is not configured

0 commit comments

Comments
 (0)