-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrebalance_scenario3b_test.cdc
More file actions
268 lines (224 loc) · 13.3 KB
/
Copy pathrebalance_scenario3b_test.cdc
File metadata and controls
268 lines (224 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import Test
import BlockchainHelpers
import "test_helpers.cdc"
import "FlowToken"
import "MOET"
import "YieldToken"
import "MockStrategies"
import "FlowALPv0"
access(all) let protocolAccount = Test.getAccount(0x0000000000000008)
access(all) let flowYieldVaultsAccount = Test.getAccount(0x0000000000000009)
access(all) let yieldTokenAccount = Test.getAccount(0x0000000000000010)
access(all) var strategyIdentifier = Type<@MockStrategies.TracerStrategy>().identifier
access(all) var flowTokenIdentifier = Type<@FlowToken.Vault>().identifier
access(all) var yieldTokenIdentifier = Type<@YieldToken.Vault>().identifier
access(all) var moetTokenIdentifier = Type<@MOET.Vault>().identifier
access(all) let collateralFactor = 0.8
access(all) let targetHealthFactor = 1.3
access(all) var snapshot: UInt64 = 0
access(all)
fun setup() {
deployContracts()
// set mocked token prices
setMockOraclePrice(signer: flowYieldVaultsAccount, forTokenIdentifier: yieldTokenIdentifier, price: 1.0)
setMockOraclePrice(signer: flowYieldVaultsAccount, forTokenIdentifier: flowTokenIdentifier, price: 1.0)
// mint tokens & set liquidity in mock swapper contract
let reserveAmount = 100_000_00.0
setupMoetVault(protocolAccount, beFailed: false)
setupYieldVault(protocolAccount, beFailed: false)
mintFlow(to: protocolAccount, amount: reserveAmount)
mintMoet(signer: protocolAccount, to: protocolAccount.address, amount: reserveAmount, beFailed: false)
mintYield(signer: yieldTokenAccount, to: protocolAccount.address, amount: reserveAmount, beFailed: false)
setMockSwapperLiquidityConnector(signer: protocolAccount, vaultStoragePath: MOET.VaultStoragePath)
setMockSwapperLiquidityConnector(signer: protocolAccount, vaultStoragePath: YieldToken.VaultStoragePath)
setMockSwapperLiquidityConnector(signer: protocolAccount, vaultStoragePath: /storage/flowTokenVault)
// setup FlowALP with a Pool & add FLOW as supported token
createAndStorePool(signer: protocolAccount, defaultTokenIdentifier: moetTokenIdentifier, beFailed: false)
addSupportedTokenFixedRateInterestCurve(
signer: protocolAccount,
tokenTypeIdentifier: flowTokenIdentifier,
collateralFactor: 0.8,
borrowFactor: 1.0,
yearlyRate: UFix128(0.1),
depositRate: 1_000_000.0,
depositCapacityCap: 1_000_000.0
)
// open wrapped position (pushToDrawDownSink)
// the equivalent of depositing reserves
let openRes = executeTransaction(
"../../lib/FlowALP/cadence/transactions/flow-alp/position/create_position.cdc",
[reserveAmount/2.0, /storage/flowTokenVault, true],
protocolAccount
)
Test.expect(openRes, Test.beSucceeded())
// enable mocked Strategy creation
addStrategyComposer(
signer: flowYieldVaultsAccount,
strategyIdentifier: strategyIdentifier,
composerIdentifier: Type<@MockStrategies.TracerStrategyComposer>().identifier,
issuerStoragePath: MockStrategies.IssuerStoragePath,
beFailed: false
)
// Fund FlowYieldVaults account for scheduling fees (atomic initial scheduling)
mintFlow(to: flowYieldVaultsAccount, amount: 100.0)
snapshot = getCurrentBlockHeight()
}
access(all)
fun test_RebalanceYieldVaultScenario3B() {
// Test.reset(to: snapshot)
let fundingAmount = 1000.0
let flowPriceIncrease = 1.5
let yieldPriceIncrease = 1.3
let expectedYieldTokenValues = [615.38461539, 923.07692308, 841.14701866]
let expectedFlowCollateralValues = [1000.0, 1500.0, 1776.92307692]
let expectedDebtValues = [615.38461539, 923.07692308, 1093.49112426]
let user = Test.createAccount()
// Likely 0.0
let flowBalanceBefore = getBalance(address: user.address, vaultPublicPath: /public/flowTokenReceiver)!
log("[TEST] flow balance before \(flowBalanceBefore)")
mintFlow(to: user, amount: fundingAmount)
grantBeta(flowYieldVaultsAccount, user)
createYieldVault(
signer: user,
strategyIdentifier: strategyIdentifier,
vaultIdentifier: flowTokenIdentifier,
amount: fundingAmount,
beFailed: false
)
var yieldVaultIDs = getYieldVaultIDs(address: user.address)
var pid = 1 as UInt64
log("[TEST] YieldVault ID: \(yieldVaultIDs![0])")
Test.assert(yieldVaultIDs != nil, message: "Expected user's YieldVault IDs to be non-nil but encountered nil")
Test.assertEqual(1, yieldVaultIDs!.length)
setMockOraclePrice(signer: flowYieldVaultsAccount, forTokenIdentifier: flowTokenIdentifier, price: flowPriceIncrease)
let yieldTokensBefore = getAutoBalancerBalance(id: yieldVaultIDs![0])!
let debtBefore = getMOETDebtFromPosition(pid: pid)
let flowCollateralBefore = getFlowCollateralFromPosition(pid: pid)
let flowCollateralValueBefore = flowCollateralBefore * 1.0 // Initial price is 1.0
log("\n=== PRECISION COMPARISON (Initial State) ===")
log("Expected Yield Tokens: \(expectedYieldTokenValues[0])")
log("Actual Yield Tokens: \(yieldTokensBefore)")
let diff0 = yieldTokensBefore > expectedYieldTokenValues[0] ? yieldTokensBefore - expectedYieldTokenValues[0] : expectedYieldTokenValues[0] - yieldTokensBefore
let sign0 = yieldTokensBefore > expectedYieldTokenValues[0] ? "+" : "-"
log("Difference: \(sign0)\(diff0)")
log("")
log("Expected Flow Collateral Value: \(expectedFlowCollateralValues[0])")
log("Actual Flow Collateral Value: \(flowCollateralValueBefore)")
let flowDiff0 = flowCollateralValueBefore > expectedFlowCollateralValues[0] ? flowCollateralValueBefore - expectedFlowCollateralValues[0] : expectedFlowCollateralValues[0] - flowCollateralValueBefore
let flowSign0 = flowCollateralValueBefore > expectedFlowCollateralValues[0] ? "+" : "-"
log("Difference: \(flowSign0)\(flowDiff0)")
log("")
log("Expected MOET Debt: \(expectedDebtValues[0])")
log("Actual MOET Debt: \(debtBefore)")
let debtDiff0 = debtBefore > expectedDebtValues[0] ? debtBefore - expectedDebtValues[0] : expectedDebtValues[0] - debtBefore
let debtSign0 = debtBefore > expectedDebtValues[0] ? "+" : "-"
log("Difference: \(debtSign0)\(debtDiff0)")
log("=========================================================\n")
Test.assert(
equalAmounts(a:yieldTokensBefore, b:expectedYieldTokenValues[0], tolerance:0.01),
message: "Expected yield tokens to be \(expectedYieldTokenValues[0]) but got \(yieldTokensBefore)"
)
Test.assert(
equalAmounts(a:flowCollateralValueBefore, b:expectedFlowCollateralValues[0], tolerance:0.01),
message: "Expected flow collateral value to be \(expectedFlowCollateralValues[0]) but got \(flowCollateralValueBefore)"
)
Test.assert(
equalAmounts(a:debtBefore, b:expectedDebtValues[0], tolerance:0.01),
message: "Expected MOET debt to be \(expectedDebtValues[0]) but got \(debtBefore)"
)
rebalancePosition(signer: protocolAccount, pid: pid, force: true, beFailed: false)
let yieldTokensAfterFlowPriceIncrease = getAutoBalancerBalance(id: yieldVaultIDs![0])!
let flowCollateralAfterFlowIncrease = getFlowCollateralFromPosition(pid: pid)
let flowCollateralValueAfterFlowIncrease = flowCollateralAfterFlowIncrease * flowPriceIncrease
let debtAfterFlowIncrease = getMOETDebtFromPosition(pid: pid)
log("\n=== PRECISION COMPARISON (After Flow Price Increase) ===")
log("Expected Yield Tokens: \(expectedYieldTokenValues[1])")
log("Actual Yield Tokens: \(yieldTokensAfterFlowPriceIncrease)")
let diff1 = yieldTokensAfterFlowPriceIncrease > expectedYieldTokenValues[1] ? yieldTokensAfterFlowPriceIncrease - expectedYieldTokenValues[1] : expectedYieldTokenValues[1] - yieldTokensAfterFlowPriceIncrease
let sign1 = yieldTokensAfterFlowPriceIncrease > expectedYieldTokenValues[1] ? "+" : "-"
log("Difference: \(sign1)\(diff1)")
log("")
log("Expected Flow Collateral Value: \(expectedFlowCollateralValues[1])")
log("Actual Flow Collateral Value: \(flowCollateralValueAfterFlowIncrease)")
log("Actual Flow Collateral Amount: \(flowCollateralAfterFlowIncrease) Flow tokens")
let flowDiff1 = flowCollateralValueAfterFlowIncrease > expectedFlowCollateralValues[1] ? flowCollateralValueAfterFlowIncrease - expectedFlowCollateralValues[1] : expectedFlowCollateralValues[1] - flowCollateralValueAfterFlowIncrease
let flowSign1 = flowCollateralValueAfterFlowIncrease > expectedFlowCollateralValues[1] ? "+" : "-"
log("Difference: \(flowSign1)\(flowDiff1)")
log("")
log("Expected MOET Debt: \(expectedDebtValues[1])")
log("Actual MOET Debt: \(debtAfterFlowIncrease)")
let debtDiff1 = debtAfterFlowIncrease > expectedDebtValues[1] ? debtAfterFlowIncrease - expectedDebtValues[1] : expectedDebtValues[1] - debtAfterFlowIncrease
let debtSign1 = debtAfterFlowIncrease > expectedDebtValues[1] ? "+" : "-"
log("Difference: \(debtSign1)\(debtDiff1)")
log("=========================================================\n")
Test.assert(
equalAmounts(a:yieldTokensAfterFlowPriceIncrease, b:expectedYieldTokenValues[1], tolerance:0.01),
message: "Expected yield tokens after flow price increase to be \(expectedYieldTokenValues[1]) but got \(yieldTokensAfterFlowPriceIncrease)"
)
Test.assert(
equalAmounts(a:flowCollateralValueAfterFlowIncrease, b:expectedFlowCollateralValues[1], tolerance:0.01),
message: "Expected flow collateral value after flow price increase to be \(expectedFlowCollateralValues[1]) but got \(flowCollateralValueAfterFlowIncrease)"
)
Test.assert(
equalAmounts(a:debtAfterFlowIncrease, b:expectedDebtValues[1], tolerance:0.01),
message: "Expected MOET debt after flow price increase to be \(expectedDebtValues[1]) but got \(debtAfterFlowIncrease)"
)
setMockOraclePrice(signer: flowYieldVaultsAccount, forTokenIdentifier: yieldTokenIdentifier, price: yieldPriceIncrease)
rebalanceYieldVault(signer: flowYieldVaultsAccount, id: yieldVaultIDs![0], force: true, beFailed: false)
//rebalancePosition(signer: protocolAccount, pid: 0, force: true, beFailed: false)
let yieldTokensAfterYieldPriceIncrease = getAutoBalancerBalance(id: yieldVaultIDs![0])!
let flowCollateralAfterYieldIncrease = getFlowCollateralFromPosition(pid: pid)
let flowCollateralValueAfterYieldIncrease = flowCollateralAfterYieldIncrease * flowPriceIncrease // Flow price remains at 1.5
let debtAfterYieldIncrease = getMOETDebtFromPosition(pid: pid)
log("\n=== PRECISION COMPARISON (After Yield Price Increase) ===")
log("Expected Yield Tokens: \(expectedYieldTokenValues[2])")
log("Actual Yield Tokens: \(yieldTokensAfterYieldPriceIncrease)")
let diff2 = yieldTokensAfterYieldPriceIncrease > expectedYieldTokenValues[2] ? yieldTokensAfterYieldPriceIncrease - expectedYieldTokenValues[2] : expectedYieldTokenValues[2] - yieldTokensAfterYieldPriceIncrease
let sign2 = yieldTokensAfterYieldPriceIncrease > expectedYieldTokenValues[2] ? "+" : "-"
log("Difference: \(sign2)\(diff2)")
log("")
log("Expected Flow Collateral Value: \(expectedFlowCollateralValues[2])")
log("Actual Flow Collateral Value: \(flowCollateralValueAfterYieldIncrease)")
log("Actual Flow Collateral Amount: \(flowCollateralAfterYieldIncrease) Flow tokens")
let flowDiff2 = flowCollateralValueAfterYieldIncrease > expectedFlowCollateralValues[2] ? flowCollateralValueAfterYieldIncrease - expectedFlowCollateralValues[2] : expectedFlowCollateralValues[2] - flowCollateralValueAfterYieldIncrease
let flowSign2 = flowCollateralValueAfterYieldIncrease > expectedFlowCollateralValues[2] ? "+" : "-"
log("Difference: \(flowSign2)\(flowDiff2)")
log("")
log("Expected MOET Debt: \(expectedDebtValues[2])")
log("Actual MOET Debt: \(debtAfterYieldIncrease)")
let debtDiff2 = debtAfterYieldIncrease > expectedDebtValues[2] ? debtAfterYieldIncrease - expectedDebtValues[2] : expectedDebtValues[2] - debtAfterYieldIncrease
let debtSign2 = debtAfterYieldIncrease > expectedDebtValues[2] ? "+" : "-"
log("Difference: \(debtSign2)\(debtDiff2)")
log("=========================================================\n")
Test.assert(
equalAmounts(a:yieldTokensAfterYieldPriceIncrease, b:expectedYieldTokenValues[2], tolerance:0.01),
message: "Expected yield tokens after yield price increase to be \(expectedYieldTokenValues[2]) but got \(yieldTokensAfterYieldPriceIncrease)"
)
Test.assert(
equalAmounts(a:flowCollateralValueAfterYieldIncrease, b:expectedFlowCollateralValues[2], tolerance:0.01),
message: "Expected flow collateral value after yield price increase to be \(expectedFlowCollateralValues[2]) but got \(flowCollateralValueAfterYieldIncrease)"
)
Test.assert(
equalAmounts(a:debtAfterYieldIncrease, b:expectedDebtValues[2], tolerance:0.01),
message: "Expected MOET debt after yield price increase to be \(expectedDebtValues[2]) but got \(debtAfterYieldIncrease)"
)
// Check getYieldVaultBalance vs actual available balance before closing
let yieldVaultBalance = getYieldVaultBalance(address: user.address, yieldVaultID: yieldVaultIDs![0])!
// Get the actual available balance from the position
let positionDetails = getPositionDetails(pid: 1, beFailed: false)
var positionFlowBalance = 0.0
for balance in positionDetails.balances {
if balance.vaultType == Type<@FlowToken.Vault>() && balance.direction == FlowALPv0.BalanceDirection.Credit {
positionFlowBalance = balance.balance
break
}
}
log("\n=== DIAGNOSTIC: YieldVault Balance vs Position Available ===")
log("getYieldVaultBalance() reports: \(yieldVaultBalance)")
log("Position Flow balance: \(positionFlowBalance)")
log("Difference: \(positionFlowBalance - yieldVaultBalance)")
log("========================================\n")
// Skip closeYieldVault for now due to getYieldVaultBalance precision issues
closeYieldVault(signer: user, id: yieldVaultIDs![0], beFailed: false)
log("\n=== TEST COMPLETE ===")
}