@@ -4,6 +4,7 @@ import "test_helpers.cdc"
44
55import " DeFiActions"
66import " FlowTransactionScheduler"
7+ import " ExecutionCallbackRecorder"
78
89import " TokenA"
910import " TokenB"
@@ -69,6 +70,12 @@ access(all) fun setup() {
6970 arguments : [tokenAIdentifier ], // unitOfAccountIdentifier
7071 )
7172 Test .expect (err , Test .beNil ())
73+ err = Test .deployContract (
74+ name : " ExecutionCallbackRecorder" ,
75+ path : " ./contracts/ExecutionCallbackRecorder.cdc" ,
76+ arguments : [],
77+ )
78+ Test .expect (err , Test .beNil ())
7279
7380 // set TokenB price in MockOracle
7481 let setRes = executeTransaction (
@@ -161,6 +168,55 @@ access(all) fun test_SetRebalanceSourceSucceeds() {
161168 Test .assertEqual (0.0 , tokenBBalance ! )
162169}
163170
171+ access (all ) fun test_ExecutionCallbackRuns () {
172+ if snapshot < getCurrentBlockHeight () {
173+ Test .reset (to : snapshot )
174+ }
175+ let user = Test .createAccount ()
176+ transferFlow (signer : serviceAccount , recipient : user .address , amount : 100.0 )
177+ let lowerThreshold = 0.9
178+ let upperThreshold = 1.1
179+
180+ // create AutoBalancer
181+ let setupRes = executeTransaction (
182+ " ../transactions/auto-balance-adapter/create_auto_balancer.cdc" ,
183+ [tokenAIdentifier , nil , lowerThreshold , upperThreshold , tokenBIdentifier , autoBalancerStoragePath , autoBalancerPublicPath ],
184+ user
185+ )
186+ Test .expect (setupRes , Test .beSucceeded ())
187+
188+ let createdEvts = Test .eventsOfType (Type <DeFiActions .CreatedAutoBalancer >())
189+ Test .assertEqual (1 , createdEvts .length )
190+ let createdEvt = createdEvts [0 ] as ! DeFiActions .CreatedAutoBalancer
191+ let balancerUUID = createdEvt .uuid
192+
193+ let interval : UInt64 = 10
194+ let executionEffort : UInt64 = 1_000
195+ let priority : UInt8 = 2
196+ let configRes = executeTransaction (
197+ " ../transactions/auto-balance-adapter/set_recurring_config.cdc" ,
198+ [autoBalancerStoragePath , interval , priority , executionEffort , true ],
199+ user
200+ )
201+ Test .expect (configRes , Test .beSucceeded ())
202+
203+ // set execution callback (invoked only from executeTransaction, not from rebalance())
204+ let setCbRes = executeTransaction (
205+ " ./transactions/auto-balance-adapter/set_execution_callback.cdc" ,
206+ [autoBalancerStoragePath ],
207+ user
208+ )
209+ Test .expect (setCbRes , Test .beSucceeded ())
210+
211+ // advance time so scheduler runs executeTransaction on the AutoBalancer
212+ Test .moveTime (by : 11.0 )
213+
214+ let invokedEvts = Test .eventsOfType (Type <ExecutionCallbackRecorder .Invoked >())
215+ Test .assertEqual (1 , invokedEvts .length )
216+ let invokedEvt = invokedEvts [0 ] as ! ExecutionCallbackRecorder .Invoked
217+ Test .assertEqual (balancerUUID , invokedEvt .balancerUUID )
218+ }
219+
164220access (all ) fun test_ForceRebalanceToSinkSucceeds () {
165221 Test .reset (to : snapshot )
166222 let user = Test .createAccount ()
@@ -628,7 +684,7 @@ access(all) fun test_RecurringRebalanceToSinkSucceeds() {
628684 now = getCurrentBlockTimestamp ()
629685 schedEvts = Test .eventsOfType (Type <FlowTransactionScheduler .Scheduled >())
630686 Test .assertEqual (2 , schedEvts .length )
631-
687+
632688 schedEvts = Test .eventsOfType (Type <FlowTransactionScheduler .Scheduled >())
633689 schedEvt = schedEvts [schedEvts .length - 1 ] as ! FlowTransactionScheduler .Scheduled
634690 Test .assertEqual (user .address , schedEvt .transactionHandlerOwner )
@@ -760,7 +816,7 @@ access(all) fun test_RecurringRebalanceFromSourceSucceeds() {
760816 now = getCurrentBlockTimestamp ()
761817 schedEvts = Test .eventsOfType (Type <FlowTransactionScheduler .Scheduled >())
762818 Test .assertEqual (2 , schedEvts .length )
763-
819+
764820 schedEvts = Test .eventsOfType (Type <FlowTransactionScheduler .Scheduled >())
765821 schedEvt = schedEvts [schedEvts .length - 1 ] as ! FlowTransactionScheduler .Scheduled
766822 Test .assertEqual (user .address , schedEvt .transactionHandlerOwner )
0 commit comments