@@ -130,88 +130,39 @@ access(all) contract MockStrategies {
130130 let ytSource = FlowYieldVaultsAutoBalancers .createExternalSource (id : self .id ()! )
131131 ?? panic (" Could not create external source from AutoBalancer" )
132132
133- // Step 5: Withdraw ALL available YT from AutoBalancer to avoid losing funds when Strategy is destroyed
134- let availableYt = ytSource .minimumAvailable ()
135- let totalYtVault <- ytSource .withdrawAvailable (maxAmount : availableYt )
136- let totalYtAmount = totalYtVault .balance
137-
138- // Step 6: Create YT→MOET swapper
139- let ytToMoetSwapper = MockSwapper .Swapper (
140- inVault : Type <@YieldToken.Vault >(),
141- outVault : Type <@MOET.Vault >(),
142- uniqueID : self .copyID ()!
143- )
144-
145- // Step 7: Calculate how much MOET we can get from the available YT
146- // Use quoteOut to see how much MOET we'll get from all available YT
147- let ytQuote = ytToMoetSwapper .quoteOut (forProvided : totalYtAmount , reverse : false )
148- let estimatedMoetFromYt = ytQuote .outAmount
149-
150- // Step 8: Swap ALL YT to MOET to see how much we can cover
151- var moetVault <- ytToMoetSwapper .swap (quote : ytQuote , inVault : <- totalYtVault )
152- let moetFromYt = moetVault .balance
153-
154- // Step 8: If YT didn't cover full debt, withdraw collateral to make up shortfall
155- if moetFromYt < totalDebtAmount {
156- let shortfall = totalDebtAmount - moetFromYt
157-
158- // Create collateral→MOET swapper to convert collateral for debt repayment
159- let collateralToMoetSwapper = MockSwapper .Swapper (
160- inVault : collateralType ,
161- outVault : Type <@MOET.Vault >(),
133+ // Step 5: Build one SwapSource per debt type, each drawing from the AutoBalancer's YT.
134+ // ytSource is a struct (capability-backed), so each copy references the same underlying vault.
135+ // The pool drains each source sequentially to repay each debt type.
136+ var repaymentSources : [{DeFiActions .Source }] = []
137+ for debtType in debtsByType .keys {
138+ let ytToDebtSwapper = MockSwapper .Swapper (
139+ inVault : Type <@YieldToken.Vault >(),
140+ outVault : debtType ,
162141 uniqueID : self .copyID ()!
163142 )
164-
165- // Calculate how much collateral we need to cover the shortfall
166- let collateralQuote = collateralToMoetSwapper .quoteIn (
167- forDesired : shortfall ,
168- reverse : false
169- )
170-
171- // Withdraw collateral from position to cover shortfall
172- let collateralForDebt <- self .source .withdrawAvailable (maxAmount : collateralQuote .inAmount )
173-
174- // Swap collateral to MOET and add to repayment vault
175- let additionalMoet <- collateralToMoetSwapper .swap (
176- quote : collateralQuote ,
177- inVault : <- collateralForDebt
178- )
179- moetVault .deposit (from : <- additionalMoet )
143+ repaymentSources .append (SwapConnectors .SwapSource (swapper : ytToDebtSwapper , source : ytSource , uniqueID : nil ))
180144 }
181145
182- // Step 9: Store MOET vault temporarily and create a VaultSource for closePosition.
183- // closePosition now takes [{DeFiActions.Source}] instead of @[{FungibleToken.Vault}].
184- let tempPath = StoragePath (identifier : " mockClosePositionMoet_\( self .uuid ) " )!
185- MockStrategies .account .storage .save (<- (moetVault as ! @MOET.Vault ), to : tempPath )
186- let moetCap = MockStrategies .account .capabilities .storage .issue <auth (FungibleToken.Withdraw ) &{FungibleToken .Vault }>(tempPath )
187- let moetSource = FungibleTokenConnectors .VaultSource (min : nil , withdrawVault : moetCap , uniqueID : nil )
188-
189- // Step 10: Close position - pool pulls exactly the debt amount from moetSource
190- let resultVaults <- self .position .closePosition (repaymentSources : [moetSource ])
146+ // Step 6: Close position - pool pulls each debt type's amount from its corresponding SwapSource
147+ let resultVaults <- self .position .closePosition (repaymentSources : repaymentSources )
191148
192- // Step 11: Recover any MOET not consumed by repayment from temp storage
193- let remainingMoet <- MockStrategies .account .storage .load <@MOET.Vault >(from : tempPath )!
194-
195- // Extract all returned vaults
149+ // Step 7: Extract collateral vault (first returned vault)
196150 assert (resultVaults .length > 0 , message : " No vaults returned from closePosition" )
197-
198- // First vault should be collateral
199151 var collateralVault <- resultVaults .removeFirst ()
200152
201- // Swap any remaining MOET (not consumed by repayment) back to collateral
202- if remainingMoet .balance > 0.0 {
203- let moetToCollateralSwapper = MockSwapper .Swapper (
204- inVault : Type <@MOET.Vault >(),
153+ // Step 8: Recover any remaining YT from the AutoBalancer and swap back to collateral
154+ let remainingYtAmount = ytSource .minimumAvailable ()
155+ if remainingYtAmount > 0.0 {
156+ let remainingYt <- ytSource .withdrawAvailable (maxAmount : remainingYtAmount )
157+ let ytToCollateralSwapper = MockSwapper .Swapper (
158+ inVault : Type <@YieldToken.Vault >(),
205159 outVault : collateralType ,
206160 uniqueID : self .copyID ()!
207161 )
208- let swappedCollateral <- moetToCollateralSwapper .swap (quote : nil , inVault : <- remainingMoet )
209- collateralVault .deposit (from : <- swappedCollateral )
210- } else {
211- destroy remainingMoet
162+ collateralVault .deposit (from : <- ytToCollateralSwapper .swap (quote : nil , inVault : <- remainingYt ))
212163 }
213164
214- // Handle any additional vaults in resultVaults (e.g., overpayment credits ) by swapping back to collateral
165+ // Step 9: Handle any additional vaults returned by closePosition (overpayments ) by swapping to collateral
215166 while resultVaults .length > 0 {
216167 let dustVault <- resultVaults .removeFirst ()
217168 if dustVault .balance > 0.0 && dustVault .getType () ! = collateralType {
@@ -220,11 +171,7 @@ access(all) contract MockStrategies {
220171 outVault : collateralType ,
221172 uniqueID : self .copyID ()!
222173 )
223- let swappedCollateral <- dustToCollateralSwapper .swap (
224- quote : nil ,
225- inVault : <- dustVault
226- )
227- collateralVault .deposit (from : <- swappedCollateral )
174+ collateralVault .deposit (from : <- dustToCollateralSwapper .swap (quote : nil , inVault : <- dustVault ))
228175 } else {
229176 destroy dustVault
230177 }
0 commit comments