|
| 1 | +import "FungibleToken" |
| 2 | +import "FungibleTokenConnectors" |
| 3 | +import "DeFiActions" |
| 4 | +import "AutoBalancers" |
| 5 | +import "FlowTransactionScheduler" |
| 6 | + |
| 7 | +import "FlowYieldVaultsAutoBalancersV1" |
| 8 | + |
| 9 | +/// Sets recurring config for a specific AutoBalancer and immediately schedules its next rebalance. |
| 10 | +/// |
| 11 | +/// @param id: The YieldVault/AutoBalancer ID |
| 12 | +/// @param interval: The interval at which to rebalance (in seconds) |
| 13 | +/// @param priorityRaw: The priority of the rebalance (0=High, 1=Medium, 2=Low) |
| 14 | +/// @param executionEffort: The execution effort of the rebalance (1-9999) |
| 15 | +/// @param forceRebalance: The force rebalance flag (true=force rebalance, false=normal rebalance) |
| 16 | +transaction( |
| 17 | + id: UInt64, |
| 18 | + interval: UInt64, |
| 19 | + priorityRaw: UInt8, |
| 20 | + executionEffort: UInt64, |
| 21 | + forceRebalance: Bool |
| 22 | +) { |
| 23 | + let autoBalancer: auth(DeFiActions.Identify, AutoBalancers.Configure, AutoBalancers.Schedule) &AutoBalancers.AutoBalancer |
| 24 | + |
| 25 | + prepare(signer: auth(BorrowValue, CopyValue) &Account) { |
| 26 | + let storagePath = FlowYieldVaultsAutoBalancersV1.deriveAutoBalancerPath(id: id, storage: true) as! StoragePath |
| 27 | + self.autoBalancer = signer.storage |
| 28 | + .borrow<auth(DeFiActions.Identify, AutoBalancers.Configure, AutoBalancers.Schedule) &AutoBalancers.AutoBalancer>(from: storagePath) |
| 29 | + ?? panic("Could not borrow AutoBalancer id \(id) at path \(storagePath)") |
| 30 | + |
| 31 | + let feeCapStoragePath = /storage/strategiesFeeSource |
| 32 | + let fundingVault = signer.storage |
| 33 | + .copy<Capability<auth(FungibleToken.Withdraw) &{FungibleToken.Vault}>>(from: feeCapStoragePath) |
| 34 | + ?? panic("Could not find funding vault Capability at \(feeCapStoragePath)") |
| 35 | + |
| 36 | + var txnFunder = FungibleTokenConnectors.VaultSinkAndSource( |
| 37 | + min: nil, |
| 38 | + max: nil, |
| 39 | + vault: fundingVault, |
| 40 | + uniqueID: nil |
| 41 | + ) |
| 42 | + |
| 43 | + DeFiActions.alignID( |
| 44 | + toUpdate: &txnFunder as auth(DeFiActions.Extend) &{DeFiActions.IdentifiableStruct}, |
| 45 | + with: self.autoBalancer |
| 46 | + ) |
| 47 | + |
| 48 | + let priority = FlowTransactionScheduler.Priority(rawValue: priorityRaw) |
| 49 | + ?? panic("Invalid priority: \(priorityRaw) - must be 0=High, 1=Medium, 2=Low") |
| 50 | + |
| 51 | + let config = AutoBalancers.AutoBalancerRecurringConfig( |
| 52 | + interval: interval, |
| 53 | + priority: priority, |
| 54 | + executionEffort: executionEffort, |
| 55 | + forceRebalance: forceRebalance, |
| 56 | + txnFunder: txnFunder |
| 57 | + ) |
| 58 | + |
| 59 | + self.autoBalancer.setRecurringConfig(config) |
| 60 | + } |
| 61 | + |
| 62 | + execute { |
| 63 | + if let err = self.autoBalancer.scheduleNextRebalance(whileExecuting: nil) { |
| 64 | + panic("Failed to schedule next rebalance for AutoBalancer \(id): \(err)") |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments