-
Notifications
You must be signed in to change notification settings - Fork 2
Add automatic rebalancing contracts #131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e741071
Bump FlowActions commit
Kay-Zee 8675a81
Add automatic rebalancing contracts
holyfuchs e202507
add more tests for auto rebalancing, small fixes and cleanup
holyfuchs 1a0a59f
allow public calls to fixReschedule
holyfuchs 11769e4
add supervisor for rebalancing
holyfuchs 220271c
Apply suggestions from code review
holyfuchs c8802a7
address review comments
holyfuchs d5da5a5
add admin functions to enable,disable rebalancing for specific reabal…
holyfuchs 6970361
Merge branch 'main' into holyfuchs/scheduled-rebalance
holyfuchs 31b6726
Revert "add admin functions to enable,disable rebalancing for specifi…
holyfuchs 85683ba
Merge branch 'main' into holyfuchs/scheduled-rebalance
holyfuchs 6566beb
address PR comments
holyfuchs 700ba93
replace FlowTransactionScheduler.estimate with cheaper alternative
holyfuchs 9ac561b
address PR comments
holyfuchs 26e80d4
Merge branch 'main' into holyfuchs/scheduled-rebalance
nialexsan 0eb479c
improve documentation
holyfuchs ae8812c
Merge branch 'main' into holyfuchs/scheduled-rebalance
holyfuchs 9a296db
rename txn to tx & simplify scheduleNextRebalance() errors
holyfuchs 50ed5ec
Merge branch 'main' into holyfuchs/scheduled-rebalance
holyfuchs e64034a
panic on invalid fees balance
holyfuchs 7d01ea8
small cleanup
holyfuchs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule FlowActions
updated
19 files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| ## Updated Rebalance Architecture | ||
|
|
||
| The core philosophy is **decoupling**: each component operates independently with the least privilege necessary. | ||
|
|
||
| ### Key Principles | ||
|
|
||
| * **Isolation:** FCM, Rebalancer, and Supervisor are fully independent. | ||
| * **Least Privilege:** The Rebalancer can *only* trigger the `rebalance` function. | ||
| * **Resilience:** The `fixReschedule()` call is idempotent and permissionless, ensuring the system can recover without complex auth. | ||
|
|
||
| ### Rebalancer variants | ||
|
|
||
| There are two rebalancer types; they behave the same for triggering rebalances. | ||
|
|
||
| | | **Standard Rebalancer** | **Paid Rebalancer** | | ||
| |---|---|---| | ||
| | **Who pays** | User pays | Admin pays | | ||
| | **Configuration** | User can set it | Admin sets it | | ||
| | **Use case** | User wants full autonomy | Admin retains autonomy | | ||
| | **Who can withdraw** | Only user | Only user | | ||
|
|
||
| The paid rebalancer is otherwise the same: it holds a rebalance capability and runs on the same schedule/trigger model; only who pays and who controls config differ. | ||
|
|
||
| ### creating a position | ||
| ```mermaid | ||
| sequenceDiagram | ||
| actor anyone | ||
| participant FCMHelper as FCM<br/>Helper | ||
| participant FCM | ||
| participant AB as Rebalancer | ||
| participant Supervisor | ||
| anyone->>FCMHelper: createPosition() | ||
| FCMHelper->>FCM: createPosition() | ||
| FCMHelper->>AB: createRebalancer(rebalanceCapability) | ||
| FCMHelper->>Supervisor: addRebalancer(uuid) | ||
| ``` | ||
|
|
||
| ### while running | ||
|
|
||
| ```mermaid | ||
| sequenceDiagram | ||
| participant AB1 as AutoRebalancer1 | ||
| participant FCM | ||
| participant AB2 as AutoRebalancer2 | ||
| participant SUP as Supervisor | ||
| loop every x min | ||
| AB1->>FCM: rebalance() | ||
| end | ||
| loop every y min | ||
| AB2->>FCM: rebalance() | ||
| end | ||
| loop every z min | ||
| SUP->>AB2: fixReschedule() | ||
| SUP->>AB1: fixReschedule() | ||
| end | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import "FlowCreditMarket" | ||
| import "FlowCreditMarketRebalancerV1" | ||
| import "FlowTransactionScheduler" | ||
|
|
||
| // FlowCreditMarketRebalancerPaidV1 — Managed rebalancer service for Flow Credit Market positions. | ||
| // | ||
| // This contract hosts scheduled rebalancers on behalf of users. Instead of users storing and | ||
| // configuring Rebalancer resources themselves, they call createPaidRebalancer with a position | ||
| // rebalance capability and receive a lightweight RebalancerPaid resource. The contract stores | ||
| // the underlying Rebalancer, wires it to the FlowTransactionScheduler, and applies a shared | ||
| // defaultRecurringConfig (interval, priority, txnFunder, etc.). Users can fixReschedule by UUID | ||
| // or delete their RebalancerPaid to stop and remove the rebalancer. Admins control the default | ||
| // config and can update or remove individual paid rebalancers. | ||
| access(all) contract FlowCreditMarketRebalancerPaidV1 { | ||
|
|
||
| access(all) var defaultRecurringConfig: FlowCreditMarketRebalancerV1.RecurringConfig? | ||
| access(all) var storageAdminPath: StoragePath | ||
| access(all) let adminCapabilityStoragePath: StoragePath | ||
|
holyfuchs marked this conversation as resolved.
Outdated
|
||
|
|
||
| access(all) fun createPaidRebalancer( | ||
| positionRebalanceCapability: Capability<auth(FlowCreditMarket.ERebalance) &{FlowCreditMarketRebalancerV1.Rebalancable}>, | ||
| ): @RebalancerPaid { | ||
| assert(positionRebalanceCapability.check(), message: "Invalid position rebalance capability") | ||
| let rebalancer <- FlowCreditMarketRebalancerV1.createRebalancer( | ||
| recurringConfig: self.defaultRecurringConfig!, | ||
| positionRebalanceCapability: positionRebalanceCapability | ||
| ) | ||
| let uuid = rebalancer.uuid | ||
| self.storeRebalancer(rebalancer: <-rebalancer) | ||
| self.setSelfCapability(uuid: uuid) | ||
| self.borrowRebalancer(uuid: uuid)!.fixReschedule() | ||
| return <- create RebalancerPaid(rebalancerUUID: uuid) | ||
| } | ||
|
|
||
| access(all) resource Admin { | ||
| access(all) fun updateDefaultRecurringConfig(recurringConfig: FlowCreditMarketRebalancerV1.RecurringConfig) { | ||
| FlowCreditMarketRebalancerPaidV1.defaultRecurringConfig = recurringConfig | ||
|
holyfuchs marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| access(all) fun borrowRebalancer( | ||
| uuid: UInt64, | ||
| ): auth(FlowCreditMarket.ERebalance, FlowCreditMarketRebalancerV1.Rebalancer.Configure) &FlowCreditMarketRebalancerV1.Rebalancer? { | ||
| return FlowCreditMarketRebalancerPaidV1.borrowRebalancer(uuid: uuid) | ||
| } | ||
|
|
||
| access(all) fun updateRecurringConfig( | ||
| uuid: UInt64, | ||
| recurringConfig: FlowCreditMarketRebalancerV1.RecurringConfig) | ||
| { | ||
| let rebalancer = FlowCreditMarketRebalancerPaidV1.borrowRebalancer(uuid: uuid)! | ||
| rebalancer.setRecurringConfig(recurringConfig) | ||
| } | ||
|
|
||
| access(account) fun removePaidRebalancer(uuid: UInt64) { | ||
| FlowCreditMarketRebalancerPaidV1.removePaidRebalancer(uuid: uuid) | ||
|
holyfuchs marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| access(all) entitlement Delete | ||
|
|
||
| access(all) resource RebalancerPaid { | ||
| access(all) var rebalancerUUID : UInt64 | ||
|
|
||
| init(rebalancerUUID: UInt64) { | ||
| self.rebalancerUUID = rebalancerUUID | ||
| } | ||
|
|
||
| access(Delete) fun delete() { | ||
| FlowCreditMarketRebalancerPaidV1.removePaidRebalancer(uuid: self.rebalancerUUID) | ||
| } | ||
|
|
||
| access(all) fun fixReschedule() { | ||
| let rebalancer = FlowCreditMarketRebalancerPaidV1.borrowRebalancer(uuid: self.rebalancerUUID)! | ||
| rebalancer.fixReschedule() | ||
| } | ||
| } | ||
|
|
||
| access(all) fun fixReschedule( | ||
| uuid: UInt64, | ||
| ) { | ||
| let rebalancer = FlowCreditMarketRebalancerPaidV1.borrowRebalancer(uuid: uuid)! | ||
| rebalancer.fixReschedule() | ||
| } | ||
|
|
||
| access(self) fun borrowRebalancer( | ||
| uuid: UInt64, | ||
| ): auth(FlowCreditMarket.ERebalance, FlowCreditMarketRebalancerV1.Rebalancer.Configure) &FlowCreditMarketRebalancerV1.Rebalancer? { | ||
| return self.account.storage.borrow<auth(FlowCreditMarket.ERebalance, FlowCreditMarketRebalancerV1.Rebalancer.Configure) &FlowCreditMarketRebalancerV1.Rebalancer>(from: self.getPath(uuid: uuid)) | ||
| } | ||
|
|
||
| access(self) fun removePaidRebalancer(uuid: UInt64) { | ||
| let rebalancer <- self.account.storage.load<@FlowCreditMarketRebalancerV1.Rebalancer>(from: self.getPath(uuid: uuid)) | ||
| rebalancer?.cancelAllScheduledTransactions() | ||
| destroy <- rebalancer | ||
| } | ||
|
|
||
| access(self) fun storeRebalancer( | ||
| rebalancer: @FlowCreditMarketRebalancerV1.Rebalancer, | ||
| ) { | ||
| let path = self.getPath(uuid: rebalancer.uuid) | ||
| self.account.storage.save(<-rebalancer, to: path) | ||
| } | ||
|
|
||
| // issue and set the capability that the scheduler will use to call back into this rebalancer | ||
| access(self) fun setSelfCapability( | ||
| uuid: UInt64, | ||
| ) { | ||
|
holyfuchs marked this conversation as resolved.
Outdated
|
||
| let selfCap = self.account.capabilities.storage.issue<auth(FlowTransactionScheduler.Execute) &{FlowTransactionScheduler.TransactionHandler}>(self.getPath(uuid: uuid)) | ||
| // The Rebalancer is stored in the contract storage (storeRebalancer), | ||
| // it needs a capability pointing to itself to pass to the scheduler. | ||
| // We issue this capability here and set it on the Rebalancer, so that when | ||
| // fixReschedule is called, the Rebalancer can pass it to the transaction scheduler | ||
| // as a callback for executing scheduled rebalances. | ||
| self.borrowRebalancer(uuid: uuid)!.setSelfCapability(selfCap) | ||
|
holyfuchs marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| access(self) view fun getPath(uuid: UInt64): StoragePath { | ||
| return StoragePath(identifier: "FlowCreditMarket.RebalancerV1\(uuid)")! | ||
| } | ||
|
|
||
| init() { | ||
| self.adminCapabilityStoragePath = StoragePath(identifier: "FlowCreditMarket.RebalancerPaidV1.Admin")! | ||
| self.storageAdminPath = self.adminCapabilityStoragePath | ||
| self.defaultRecurringConfig = nil | ||
| let admin <- create Admin() | ||
| self.account.storage.save(<-admin, to: self.storageAdminPath) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.