forked from OasisDEX/oasis-borrow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manageMultiplyVaultOtherActionsValidations.test.ts
372 lines (311 loc) · 12.5 KB
/
manageMultiplyVaultOtherActionsValidations.test.ts
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import BigNumber from 'bignumber.js'
import { maxUint256 } from 'blockchain/calls/erc20.constants'
import { mockManageMultiplyVault$ } from 'helpers/mocks/manageMultiplyVault.mock'
import { mockedStopLossTrigger } from 'helpers/mocks/stopLoss.mock'
import { DEFAULT_PROXY_ADDRESS } from 'helpers/mocks/vaults.mock'
import { getStateUnpacker } from 'helpers/testHelpers'
import { zero } from 'helpers/zero'
import { of } from 'rxjs'
import { legacyToggle } from './legacyToggle'
// TODO: [Mocha -> Jest] Rewrite in Jest compatible format.
describe.skip('manageVaultOtherActionsValidations', () => {
beforeAll(() => {
// TODO: remove after mainnet deployment
window.location.search = ['?network=goerli'] as any
})
afterAll(() => {
window.location.search = [] as any
})
it('validates if deposit amount exceeds collateral balance or depositing all ETH', () => {
const depositAmountExceeds = new BigNumber('2')
const depositAmountAll = new BigNumber('1')
const state = getStateUnpacker(
mockManageMultiplyVault$({
balanceInfo: {
collateralBalance: new BigNumber('1'),
},
vault: {
ilk: 'ETH-A',
},
}),
)
legacyToggle(state())
state().updateDepositAmount!(depositAmountExceeds)
expect(state().errorMessages).toEqual(['depositAmountExceedsCollateralBalance'])
state().updateDepositAmount!(depositAmountAll)
expect(state().errorMessages).toEqual(['depositingAllEthBalance'])
})
it(`validates if generate doesn't exceeds debt ceiling, debt floor`, () => {
const generateAmountAboveCeiling = new BigNumber('30')
const generateAmountBelowFloor = new BigNumber('9')
const state = getStateUnpacker(
mockManageMultiplyVault$({
ilkData: {
debtCeiling: new BigNumber('8000025'),
debtFloor: new BigNumber('2000'),
},
vault: {
collateral: new BigNumber('3'),
debt: new BigNumber('1990'),
ilk: 'ETH-A',
},
priceInfo: {
ethChangePercentage: new BigNumber(-0.1),
},
}),
)
legacyToggle(state())
state().setOtherAction!('withdrawDai')
state().updateGenerateAmount!(generateAmountAboveCeiling)
expect(state().errorMessages).toEqual(['generateAmountExceedsDebtCeiling'])
state().updateGenerateAmount!(generateAmountBelowFloor)
expect(state().errorMessages).toEqual(['generateAmountLessThanDebtFloor'])
})
it(`validates if generate amount is putting vault at risk, danger or exceeding day yield`, () => {
const generateAmountExceedsYield = new BigNumber(1200)
const generateAmountWarning = new BigNumber(100)
const generateAmountDanger = new BigNumber(700)
const generateAmountExceedsYieldNextPrice = new BigNumber(1000)
const generateAmountWarningNextPrice = new BigNumber(40)
const generateAmountDangerNextPrice = new BigNumber(500)
const state = getStateUnpacker(
mockManageMultiplyVault$({
ilkData: {
debtFloor: new BigNumber('1500'),
},
vault: {
collateral: new BigNumber('3.4'),
debt: new BigNumber('1990'),
ilk: 'ETH-A',
},
priceInfo: {
ethChangePercentage: new BigNumber('-0.05'),
},
}),
)
legacyToggle(state())
state().setOtherAction!('withdrawDai')
state().updateGenerateAmount!(generateAmountExceedsYield)
expect(state().errorMessages).toEqual(['generateAmountExceedsDaiYieldFromTotalCollateral'])
state().updateGenerateAmount!(generateAmountWarning)
expect(state().warningMessages).toEqual(['vaultWillBeAtRiskLevelWarning'])
state().updateGenerateAmount!(generateAmountDanger)
expect(state().warningMessages).toEqual(['vaultWillBeAtRiskLevelDanger'])
state().updateGenerateAmount!(generateAmountExceedsYieldNextPrice)
expect(state().errorMessages).toEqual([
'generateAmountExceedsDaiYieldFromTotalCollateralAtNextPrice',
])
state().updateGenerateAmount!(generateAmountWarningNextPrice)
expect(state().warningMessages).toEqual(['vaultWillBeAtRiskLevelWarningAtNextPrice'])
state().updateGenerateAmount!(generateAmountDangerNextPrice)
expect(state().warningMessages).toEqual([
'vaultWillBeAtRiskLevelDangerAtNextPrice',
'vaultWillBeAtRiskLevelWarning',
])
})
it(`validates if withdraw amount is putting vault at risk, danger or exceeding free collateral`, () => {
const withdrawAmountExceedsFreeCollateral = new BigNumber(1.2)
const withdrawAmountWarning = new BigNumber(0.4)
const withdrawAmountDanger = new BigNumber(0.85)
const withdrawAmountExceedsFreeCollateralAtNextPrice = new BigNumber(1.1)
const withdrawAmountWarningNextPrice = new BigNumber(0.05)
const withdrawAmountDangerNextPrice = new BigNumber(0.65)
const state = getStateUnpacker(
mockManageMultiplyVault$({
ilkData: {
debtFloor: new BigNumber('1500'),
},
vault: {
collateral: new BigNumber('3.4'),
debt: new BigNumber('1990'),
ilk: 'ETH-A',
},
priceInfo: {
ethChangePercentage: new BigNumber('-0.05'),
},
}),
)
legacyToggle(state())
state().setOtherAction!('withdrawCollateral')
state().updateWithdrawAmount!(withdrawAmountExceedsFreeCollateral)
expect(state().errorMessages).toEqual(['withdrawAmountExceedsFreeCollateral'])
state().updateWithdrawAmount!(withdrawAmountWarning)
expect(state().warningMessages).toEqual(['vaultWillBeAtRiskLevelWarning'])
state().updateWithdrawAmount!(withdrawAmountDanger)
expect(state().warningMessages).toEqual(['vaultWillBeAtRiskLevelDanger'])
state().updateWithdrawAmount!(withdrawAmountExceedsFreeCollateralAtNextPrice)
expect(state().errorMessages).toEqual(['withdrawAmountExceedsFreeCollateralAtNextPrice'])
state().updateWithdrawAmount!(withdrawAmountWarningNextPrice)
expect(state().warningMessages).toEqual(['vaultWillBeAtRiskLevelWarningAtNextPrice'])
state().updateWithdrawAmount!(withdrawAmountDangerNextPrice)
expect(state().warningMessages).toEqual([
'vaultWillBeAtRiskLevelDangerAtNextPrice',
'vaultWillBeAtRiskLevelWarning',
])
})
it('validates custom allowance setting for collateral', () => {
const depositAmount = new BigNumber('100')
const customAllowanceAmount = new BigNumber('99')
const state = getStateUnpacker(
mockManageMultiplyVault$({
proxyAddress: DEFAULT_PROXY_ADDRESS,
collateralAllowance: zero,
}),
)
legacyToggle(state())
state().updateDepositAmount!(depositAmount)
state().progress!()
expect(state().stage).toEqual('collateralAllowanceWaitingForConfirmation')
state().resetCollateralAllowanceAmount!()
state().updateCollateralAllowanceAmount!(customAllowanceAmount)
expect(state().collateralAllowanceAmount!).toEqual(customAllowanceAmount)
expect(state().errorMessages).toEqual(['customCollateralAllowanceAmountLessThanDepositAmount'])
state().updateCollateralAllowanceAmount!(maxUint256.plus(new BigNumber('1')))
expect(state().errorMessages).toEqual(['customCollateralAllowanceAmountExceedsMaxUint256'])
})
it('validates custom allowance setting for dai', () => {
const paybackAmount = new BigNumber('100')
const customAllowanceAmount = new BigNumber('99')
const state = getStateUnpacker(
mockManageMultiplyVault$({
proxyAddress: DEFAULT_PROXY_ADDRESS,
daiAllowance: zero,
}),
)
legacyToggle(state())
state().setOtherAction!('paybackDai')
state().updatePaybackAmount!(paybackAmount)
state().progress!()
expect(state().stage).toEqual('daiAllowanceWaitingForConfirmation')
state().resetDaiAllowanceAmount!()
state().updateDaiAllowanceAmount!(customAllowanceAmount)
expect(state().daiAllowanceAmount!).toEqual(customAllowanceAmount)
expect(state().errorMessages).toEqual(['customDaiAllowanceAmountLessThanPaybackAmount'])
state().updateDaiAllowanceAmount!(maxUint256.plus(new BigNumber('1')))
expect(state().errorMessages).toEqual(['customDaiAllowanceAmountExceedsMaxUint256'])
})
it('validates payback amount', () => {
const paybackAmountExceedsVaultDebt = new BigNumber('100')
const paybackAmountNotEnough = new BigNumber('20')
const state = getStateUnpacker(
mockManageMultiplyVault$({
ilkData: {
debtFloor: new BigNumber('30'),
},
vault: {
debt: new BigNumber('40'),
},
}),
)
legacyToggle(state())
state().setOtherAction!('paybackDai')
state().updatePaybackAmount!(paybackAmountExceedsVaultDebt)
expect(state().errorMessages).toEqual(['paybackAmountExceedsVaultDebt'])
state().updatePaybackAmount!(paybackAmountNotEnough)
expect(state().errorMessages).toEqual(['debtWillBeLessThanDebtFloor'])
})
it('validates if dai allowance is enough to payback whole amount and account debt offset', () => {
const paybackAmount = new BigNumber('500')
const state = getStateUnpacker(
mockManageMultiplyVault$({
proxyAddress: DEFAULT_PROXY_ADDRESS,
vault: {
collateral: new BigNumber('31'),
debt: new BigNumber('2000'),
},
daiAllowance: paybackAmount,
priceInfo: {
collateralPrice: new BigNumber('100'),
},
}),
)
legacyToggle(state())
state().setOtherAction!('paybackDai')
state().updatePaybackAmount!(paybackAmount.plus(state().vault.debtOffset))
expect(state().insufficientDaiAllowance).toBe(true)
state().updatePaybackAmount!(paybackAmount.minus(state().vault.debtOffset))
expect(state().insufficientDaiAllowance).toBe(false)
state().updatePaybackAmount!(paybackAmount)
expect(state().insufficientDaiAllowance).toBe(true)
state().progress!()
expect(state().stage).toEqual('daiAllowanceWaitingForConfirmation')
})
it('should show meaningful message when trying to withdraw collateral on dusty vault', () => {
const withdrawAmount = new BigNumber('5')
const state = getStateUnpacker(
mockManageMultiplyVault$({
ilkData: {
debtFloor: new BigNumber(1000),
},
vault: {
debt: new BigNumber(100),
collateral: new BigNumber(10),
},
balanceInfo: {
daiBalance: new BigNumber(1000),
},
proxyAddress: DEFAULT_PROXY_ADDRESS,
daiAllowance: zero,
}),
)
legacyToggle(state())
state().setOtherAction!('withdrawCollateral')
state().updateWithdrawAmount!(withdrawAmount)
expect(state().errorMessages).toEqual(['withdrawCollateralOnVaultUnderDebtFloor'])
})
it('validates if vault has no collateral and show correct error message', () => {
const state = getStateUnpacker(
mockManageMultiplyVault$({
vault: {
collateral: zero,
},
}),
)
expect(state().errorMessages).toEqual([])
state().setOtherAction!('withdrawCollateral')
expect(state().errorMessages).toEqual(['hasToDepositCollateralOnEmptyVault'])
expect(state().canProgress).toEqual(false)
})
it('validates if next coll ratio on collateral withdraw is below stop loss level', () => {
localStorage.setItem('features', '{"Automation":true}')
const withdrawAmountStopLossError = new BigNumber(0.05)
const state = getStateUnpacker(
mockManageMultiplyVault$({
ilkData: {
debtFloor: new BigNumber('1500'),
},
vault: {
collateral: new BigNumber('3.4'),
debt: new BigNumber('1990'),
ilk: 'ETH-A',
},
_automationTriggersData$: of(mockedStopLossTrigger),
}),
)
legacyToggle(state())
state().setOtherAction!('withdrawCollateral')
state().updateWithdrawAmount!(withdrawAmountStopLossError)
expect(state().errorMessages).toEqual(['afterCollRatioBelowStopLossRatio'])
})
it('validates if next coll ratio on dai withdraw is below stop loss level', () => {
localStorage.setItem('features', '{"Automation":true}')
const withdrawAmountStopLossError = new BigNumber(500)
const state = getStateUnpacker(
mockManageMultiplyVault$({
ilkData: {
debtFloor: new BigNumber('1500'),
},
vault: {
collateral: new BigNumber('3.4'),
debt: new BigNumber('1990'),
ilk: 'ETH-A',
},
_automationTriggersData$: of(mockedStopLossTrigger),
}),
)
legacyToggle(state())
state().setOtherAction!('withdrawDai')
state().updateGenerateAmount!(withdrawAmountStopLossError)
expect(state().errorMessages).toEqual(['afterCollRatioBelowStopLossRatio'])
})
})