1+ import " FlowCallbackScheduler"
2+ import " FlowToken"
3+
4+ access (all) contract FlowCallbackUtils {
5+
6+ /// Storage path for CallbackManager resources
7+ access (all) let managerStoragePath : StoragePath
8+
9+ /// Public path for CallbackManager resources
10+ access (all) let managerPublicPath : PublicPath
11+
12+ /// Entitlements
13+ access (all) entitlement Owner
14+
15+ /// CallbackManager resource that stores ScheduledCallback resources in a dictionary
16+ /// and provides convenience methods for scheduling and canceling callbacks
17+ access (all) resource CallbackManager {
18+ /// Dictionary storing scheduled callbacks by their ID
19+ access (self ) var scheduledCallbacks : @{UInt64 : FlowCallbackScheduler .ScheduledCallback }
20+
21+ init () {
22+ self .scheduledCallbacks <- {}
23+ }
24+
25+ /// Schedule a callback and store it in the manager's dictionary
26+ /// @param callback: A capability to a resource that implements the CallbackHandler interface
27+ /// @param data: Optional data to pass to the callback when executed
28+ /// @param timestamp: The timestamp when the callback should be executed
29+ /// @param priority: The priority of the callback (High, Medium, or Low)
30+ /// @param executionEffort: The execution effort for the callback
31+ /// @param fees: A FlowToken vault containing sufficient fees
32+ /// @return: The scheduled callback resource
33+ access (Owner) fun schedule (
34+ callback : Capability <auth (FlowCallbackScheduler .Execute ) &{FlowCallbackScheduler.CallbackHandler}> ,
35+ data: AnyStruct? ,
36+ timestamp: UFix64,
37+ priority: FlowCallbackScheduler.Priority,
38+ executionEffort: UInt64,
39+ fees: @FlowToken.Vault
40+ ) {
41+ // Clean up any invalid callbacks before scheduling a new one
42+ self .cleanup ()
43+
44+ // Route to the main FlowCallbackScheduler
45+ let scheduledCallback <- FlowCallbackScheduler.schedule (
46+ callback : callback,
47+ data : data,
48+ timestamp : timestamp,
49+ priority : priority,
50+ executionEffort : executionEffort,
51+ fees : <- fees
52+ )
53+
54+ // Store the callback in our dictionary
55+ self .scheduledCallbacks[scheduledCallback.id] <- ! scheduledCallback
56+ }
57+
58+ /// Cancel a scheduled callback by its ID
59+ /// @param id: The ID of the callback to cancel
60+ /// @return: A FlowToken vault containing the refunded fees
61+ access (Owner) fun cancel (id : UInt64 ): @FlowToken .Vault {
62+ // Remove the callback from our dictionary
63+ let callback <- self .scheduledCallbacks.remove (key : id)
64+ ?? panic (" Invalid ID: Callback with ID \(id) not found in manager" )
65+
66+ // Cancel the callback through the main scheduler
67+ let refundedFees <- FlowCallbackScheduler.cancel (callback : <- callback! )
68+
69+ return <- refundedFees
70+ }
71+
72+ /// Clean up callbacks that are no longer valid (return nil or Unknown status)
73+ /// This removes and destroys callbacks that have been executed, canceled, or are otherwise invalid
74+ /// @return: The number of callbacks that were cleaned up
75+ access (Owner) fun cleanup (): Int {
76+ var cleanedUpCount = 0
77+ var callbacksToRemove : [UInt64 ] = []
78+
79+ // First, identify callbacks that need to be removed
80+ for id in self .scheduledCallbacks.keys {
81+ let status = FlowCallbackScheduler.getStatus (id : id)
82+ if status == nil || status == FlowCallbackScheduler.Status.Unknown {
83+ callbacksToRemove.append (id)
84+ }
85+ }
86+
87+ // Then remove and destroy the identified callbacks
88+ for id in callbacksToRemove {
89+ if let callback <- self .scheduledCallbacks.remove (key : id) {
90+ destroy callback
91+ cleanedUpCount = cleanedUpCount + 1
92+ }
93+ }
94+
95+ return cleanedUpCount
96+ }
97+
98+ /// Get callback data by its ID
99+ /// @param id: The ID of the callback to retrieve
100+ /// @return: The callback data from FlowCallbackScheduler, or nil if not found
101+ access (all) fun getCallbackData (id : UInt64 ): FlowCallbackScheduler .CallbackData ? {
102+ return FlowCallbackScheduler.getCallbackData (id : id)
103+ }
104+
105+ /// Get all callback IDs stored in the manager
106+ /// @return: An array of all callback IDs
107+ access (all) fun getCallbackIDs (): [UInt64 ] {
108+ return self .scheduledCallbacks.keys
109+ }
110+
111+ /// Get the status of a callback by its ID
112+ /// @param id: The ID of the callback
113+ /// @return: The status of the callback, or Status.Unknown if not found in manager
114+ access (all) fun getCallbackStatus (id : UInt64 ): FlowCallbackScheduler .Status ? {
115+ if self .scheduledCallbacks.containsKey (id) {
116+ return FlowCallbackScheduler.getStatus (id : id)
117+ }
118+ return FlowCallbackScheduler.Status.Unknown
119+ }
120+ }
121+
122+ /// Create a new CallbackManager instance
123+ /// @return: A new CallbackManager resource
124+ access (all) fun createCallbackManager (): @CallbackManager {
125+ return <- create CallbackManager ()
126+ }
127+
128+ access (all) init () {
129+ self .managerStoragePath = / storage/ flowCallbackManager
130+ self .managerPublicPath = / public/ flowCallbackManager
131+ }
132+ }
0 commit comments