Skip to content

Commit 068457d

Browse files
committed
tweak mock
1 parent f74da70 commit 068457d

2 files changed

Lines changed: 37 additions & 38 deletions

File tree

cadence/contracts/mocks/MockStrategies.cdc

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -137,45 +137,40 @@ access(all) contract MockStrategies {
137137
// Pre-swapping with quoteIn guarantees the exact debt amount is delivered to the pool.
138138
var repaymentSources: [{DeFiActions.Source}] = []
139139
var debtCaps: [Capability<auth(FungibleToken.Withdraw) &{FungibleToken.Vault}>] = []
140-
var debtIdx: UInt64 = 0
140+
var debtPaths: [StoragePath] = []
141141

142142
for debtType in debtsByType.keys {
143-
if debtType == ytType {
144-
// Same token: ytSource can repay directly, no swap needed
145-
repaymentSources.append(ytSource)
143+
let debtAmount = debtsByType[debtType]!
144+
let swapper = MockSwapper.Swapper(inVault: ytType, outVault: debtType, uniqueID: self.copyID()!)
145+
let ytAvailable = ytSource.minimumAvailable()
146+
let ytNeededQuote = swapper.quoteIn(forDesired: debtAmount, reverse: false)
147+
148+
let debtVault <- DeFiActionsUtils.getEmptyVault(debtType)
149+
if ytAvailable >= ytNeededQuote.inAmount {
150+
// Sufficient YT: quoteIn guarantees exactly debtAmount out
151+
let ytPortion <- ytSource.withdrawAvailable(maxAmount: ytNeededQuote.inAmount)
152+
debtVault.deposit(from: <-swapper.swap(quote: ytNeededQuote, inVault: <-ytPortion))
146153
} else {
147-
let debtAmount = debtsByType[debtType]!
148-
let swapper = MockSwapper.Swapper(inVault: ytType, outVault: debtType, uniqueID: self.copyID()!)
149-
let ytAvailable = ytSource.minimumAvailable()
150-
let ytNeededQuote = swapper.quoteIn(forDesired: debtAmount, reverse: false)
151-
152-
let debtVault <- DeFiActionsUtils.getEmptyVault(debtType)
153-
if ytAvailable >= ytNeededQuote.inAmount {
154-
// Sufficient YT: quoteIn guarantees exactly debtAmount out
155-
let ytPortion <- ytSource.withdrawAvailable(maxAmount: ytNeededQuote.inAmount)
156-
debtVault.deposit(from: <-swapper.swap(quote: ytNeededQuote, inVault: <-ytPortion))
157-
} else {
158-
// Insufficient YT: swap all available YT, then cover shortfall from collateral
159-
let ytAllQuote = swapper.quoteOut(forProvided: ytAvailable, reverse: false)
160-
let ytPortion <- ytSource.withdrawAvailable(maxAmount: ytAvailable)
161-
debtVault.deposit(from: <-swapper.swap(quote: ytAllQuote, inVault: <-ytPortion))
162-
let shortfall = debtAmount - debtVault.balance
163-
if shortfall > 0.0 {
164-
let collateralToDebtSwapper = MockSwapper.Swapper(
165-
inVault: collateralType, outVault: debtType, uniqueID: self.copyID()!)
166-
let collateralQuote = collateralToDebtSwapper.quoteIn(forDesired: shortfall, reverse: false)
167-
let collateralForDebt <- self.source.withdrawAvailable(maxAmount: collateralQuote.inAmount)
168-
debtVault.deposit(from: <-collateralToDebtSwapper.swap(quote: collateralQuote, inVault: <-collateralForDebt))
169-
}
154+
// Insufficient YT: swap all available YT, then cover shortfall from collateral
155+
let ytAllQuote = swapper.quoteOut(forProvided: ytAvailable, reverse: false)
156+
let ytPortion <- ytSource.withdrawAvailable(maxAmount: ytAvailable)
157+
debtVault.deposit(from: <-swapper.swap(quote: ytAllQuote, inVault: <-ytPortion))
158+
let shortfall = debtAmount - debtVault.balance
159+
if shortfall > 0.0 {
160+
let collateralToDebtSwapper = MockSwapper.Swapper(
161+
inVault: collateralType, outVault: debtType, uniqueID: self.copyID()!)
162+
let collateralQuote = collateralToDebtSwapper.quoteIn(forDesired: shortfall, reverse: false)
163+
let collateralForDebt <- self.source.withdrawAvailable(maxAmount: collateralQuote.inAmount)
164+
debtVault.deposit(from: <-collateralToDebtSwapper.swap(quote: collateralQuote, inVault: <-collateralForDebt))
170165
}
171-
172-
let debtTempPath = StoragePath(identifier: "mockCloseDebt_\(self.uuid)_\(debtIdx)")!
173-
MockStrategies.account.storage.save(<-debtVault, to: debtTempPath)
174-
let debtCap = MockStrategies.account.capabilities.storage.issue<auth(FungibleToken.Withdraw) &{FungibleToken.Vault}>(debtTempPath)
175-
repaymentSources.append(FungibleTokenConnectors.VaultSource(min: nil, withdrawVault: debtCap, uniqueID: nil))
176-
debtCaps.append(debtCap)
177-
debtIdx = debtIdx + 1
178166
}
167+
168+
let debtTempPath = StoragePath(identifier: "mockCloseDebt_\(self.uuid)_\(debtType.identifier)")!
169+
MockStrategies.account.storage.save(<-debtVault, to: debtTempPath)
170+
let debtCap = MockStrategies.account.capabilities.storage.issue<auth(FungibleToken.Withdraw) &{FungibleToken.Vault}>(debtTempPath)
171+
repaymentSources.append(FungibleTokenConnectors.VaultSource(min: nil, withdrawVault: debtCap, uniqueID: nil))
172+
debtCaps.append(debtCap)
173+
debtPaths.append(debtTempPath)
179174
}
180175

181176
// Step 6: Close position - pool pulls each debt type's exact amount from its source
@@ -201,9 +196,11 @@ access(all) contract MockStrategies {
201196
collateralVault.deposit(from: <-ytToCollateralSwapper.swap(quote: nil, inVault: <-remainingYt))
202197
}
203198

204-
// Step 9: Recover any un-consumed balance from pre-swapped debt temp vaults via their caps
205-
for debtCap in debtCaps {
206-
let debtRef = debtCap.borrow()!
199+
// Step 9: Recover any un-consumed balance from pre-swapped debt temp vaults, then
200+
// remove the now-empty vaults from storage to avoid accumulating stale state.
201+
var capIdx = 0
202+
while capIdx < debtCaps.length {
203+
let debtRef = debtCaps[capIdx].borrow()!
207204
let remaining = debtRef.balance
208205
if remaining > 0.0 {
209206
let remainingDebt <- debtRef.withdraw(amount: remaining)
@@ -214,6 +211,8 @@ access(all) contract MockStrategies {
214211
)
215212
collateralVault.deposit(from: <-swapper.swap(quote: nil, inVault: <-remainingDebt))
216213
}
214+
destroy MockStrategies.account.storage.load<@{FungibleToken.Vault}>(from: debtPaths[capIdx])!
215+
capIdx = capIdx + 1
217216
}
218217

219218
// Step 10: Handle any additional vaults returned by closePosition (overpayments)

cadence/tests/rebalance_scenario4_test.cdc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ fun test_RebalanceHighCollateralLowYieldPrices() {
284284
Test.assert(ytAfterFlowDrop < ytBefore,
285285
message: "Expected AutoBalancer YT to decrease after using topUpSource to repay debt, got \(ytAfterFlowDrop) (was \(ytBefore))")
286286
// Debt repayment only affects the MOET debit — FLOW collateral is untouched.
287-
Test.assert(equalAmounts(a: collateralAfterFlowDrop, b: collateralBefore, tolerance: 0.00000001),
287+
Test.assert(equalAmounts(a: collateralAfterFlowDrop, b: collateralBefore, tolerance: 0.000001),
288288
message: "Expected FLOW collateral to be unchanged after debt repayment, got \(collateralAfterFlowDrop) (was \(collateralBefore))")
289289
// The AutoBalancer has sufficient YT to cover the full repayment needed to reach targetHealth (1.3).
290290
Test.assert(equalAmounts(a: healthAfterRebalance, b: 1.3, tolerance: 0.00000001),

0 commit comments

Comments
 (0)