-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinsurance_swapper_test.cdc
More file actions
217 lines (188 loc) · 8.53 KB
/
Copy pathinsurance_swapper_test.cdc
File metadata and controls
217 lines (188 loc) · 8.53 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
import Test
import "test_helpers.cdc"
import "FlowALPv0"
access(all) let alice = Test.createAccount()
access(all)
fun setup() {
deployContracts()
setMockOraclePrice(signer: PROTOCOL_ACCOUNT, forTokenIdentifier: MOET_TOKEN_IDENTIFIER, price: 1.0)
createAndStorePool(signer: PROTOCOL_ACCOUNT, defaultTokenIdentifier: MOET_TOKEN_IDENTIFIER, beFailed: false)
}
// -----------------------------------------------------------------------------
// Test: setInsuranceSwapper with valid configuration should succeed
// Verifies that a valid insurance swapper can be set for a token type
// -----------------------------------------------------------------------------
access(all)
fun test_setInsuranceSwapper_success() {
let res = setInsuranceSwapper(
signer: PROTOCOL_ACCOUNT,
swapperInTypeIdentifier: MOET_TOKEN_IDENTIFIER,
swapperOutTypeIdentifier: MOET_TOKEN_IDENTIFIER,
priceRatio: 1.0,
)
Test.expect(res, Test.beSucceeded())
// verify swapper is configured
Test.assertEqual(true, insuranceSwapperExists(tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER))
}
// -----------------------------------------------------------------------------
// Test: setInsuranceSwapper can update existing swapper
// Verifies that an existing swapper can be replaced with a new one
// -----------------------------------------------------------------------------
access(all)
fun test_setInsuranceSwapper_updateExistingSwapper_success() {
// set initial swapper
let initialPriceRatio = 1.0
let res = setInsuranceSwapper(
signer: PROTOCOL_ACCOUNT,
swapperInTypeIdentifier: MOET_TOKEN_IDENTIFIER,
swapperOutTypeIdentifier: MOET_TOKEN_IDENTIFIER,
priceRatio: initialPriceRatio,
)
Test.expect(res, Test.beSucceeded())
// update to new swapper with different price ratio
let updatedPriceRatio = 2.0
let updatedRes = setInsuranceSwapper(
signer: PROTOCOL_ACCOUNT,
swapperInTypeIdentifier: MOET_TOKEN_IDENTIFIER,
swapperOutTypeIdentifier: MOET_TOKEN_IDENTIFIER,
priceRatio: updatedPriceRatio,
)
Test.expect(updatedRes, Test.beSucceeded())
// verify swapper is still configured
Test.assertEqual(true, insuranceSwapperExists(tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER))
}
// -----------------------------------------------------------------------------
// Test: removeInsuranceSwapper should remove configured swapper
// Verifies that an insurance swapper can be removed after being set
// -----------------------------------------------------------------------------
access(all)
fun test_removeInsuranceSwapper_success() {
// set a swapper
let res = setInsuranceSwapper(
signer: PROTOCOL_ACCOUNT,
swapperInTypeIdentifier: MOET_TOKEN_IDENTIFIER,
swapperOutTypeIdentifier: MOET_TOKEN_IDENTIFIER,
priceRatio: 1.0,
)
Test.expect(res, Test.beSucceeded())
// verify swapper is configured
Test.assertEqual(true, insuranceSwapperExists(tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER))
// remove swapper
let removeResult = removeInsuranceSwapper(
signer: PROTOCOL_ACCOUNT,
tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER,
)
Test.expect(removeResult, Test.beSucceeded())
// verify swapper is no longer configured
Test.assertEqual(false, insuranceSwapperExists(tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER))
}
// -----------------------------------------------------------------------------
// Test: test_remove_insuranceSwapper_failed should not remove configured swapper
// Verifies that an insurance swapper cannot be removed when insurance rate is being set
// -----------------------------------------------------------------------------
access(all)
fun test_remove_insuranceSwapper_failed() {
// set a swapper
var res = setInsuranceSwapper(
signer: PROTOCOL_ACCOUNT,
swapperInTypeIdentifier: MOET_TOKEN_IDENTIFIER,
swapperOutTypeIdentifier: MOET_TOKEN_IDENTIFIER,
priceRatio: 1.0,
)
Test.expect(res, Test.beSucceeded())
// verify swapper is configured
Test.assertEqual(true, insuranceSwapperExists(tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER))
// set insurance rate
res = setInsuranceRate(
signer: PROTOCOL_ACCOUNT,
tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER,
insuranceRate: 0.001,
)
Test.expect(res, Test.beSucceeded())
// remove swapper
let removeResult = removeInsuranceSwapper(
signer: PROTOCOL_ACCOUNT,
tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER,
)
Test.expect(removeResult, Test.beFailed())
Test.assertError(removeResult, errorMessage: "Cannot remove insurance swapper while insurance rate is non-zero for \(MOET_TOKEN_IDENTIFIER)")
// verify swapper is still exist
Test.assertEqual(true, insuranceSwapperExists(tokenTypeIdentifier: MOET_TOKEN_IDENTIFIER))
}
// -----------------------------------------------------------------------------
// Test: setInsuranceSwapper without EGovernance entitlement should fail
// Verifies that accounts without EGovernance entitlement cannot set swapper
// -----------------------------------------------------------------------------
access(all)
fun test_setInsuranceSwapper_withoutEGovernanceEntitlement_fails() {
let res = setInsuranceSwapper(
signer: alice,
swapperInTypeIdentifier: MOET_TOKEN_IDENTIFIER,
swapperOutTypeIdentifier: MOET_TOKEN_IDENTIFIER,
priceRatio: 1.0,
)
// should fail due to missing EGovernance entitlement or missing Pool
Test.expect(res, Test.beFailed())
}
// -----------------------------------------------------------------------------
// Test: setInsuranceSwapper with invalid token identifier should fail
// Verifies that non-existent token types are rejected
// -----------------------------------------------------------------------------
access(all)
fun test_setInsuranceSwapper_invalidTokenTypeIdentifier_fails() {
let invalidTokenIdentifier = "InvalidTokenType"
let res = setInsuranceSwapper(
signer: PROTOCOL_ACCOUNT,
swapperInTypeIdentifier: invalidTokenIdentifier,
swapperOutTypeIdentifier: MOET_TOKEN_IDENTIFIER,
priceRatio: 1.0,
)
Test.expect(res, Test.beFailed())
Test.assertError(res, errorMessage: "Invalid tokenTypeIdentifier")
}
// -----------------------------------------------------------------------------
// Test: setInsuranceSwapper with empty token identifier should fail
// Verifies that empty string token identifiers are rejected
// -----------------------------------------------------------------------------
access(all)
fun test_setInsuranceSwapper_emptyTokenTypeIdentifier_fails() {
let emptyTokenIdentifier = ""
let res = setInsuranceSwapper(
signer: PROTOCOL_ACCOUNT,
swapperInTypeIdentifier: emptyTokenIdentifier,
swapperOutTypeIdentifier: MOET_TOKEN_IDENTIFIER,
priceRatio: 1.0,
)
Test.expect(res, Test.beFailed())
Test.assertError(res, errorMessage: "Invalid tokenTypeIdentifier")
}
// -----------------------------------------------------------------------------
// Test: setInsuranceSwapper with wrong output type should fail
// Swapper must output MOET (insurance fund denomination)
// -----------------------------------------------------------------------------
access(all)
fun test_setInsuranceSwapper_wrongOutputType_fails() {
// try to set a swapper that doesn't output MOET (outputs FLOW_TOKEN_IDENTIFIER instead)
let res = _executeTransaction(
"./transactions/flow-alp/egovernance/set_insurance_swapper_mock.cdc",
[MOET_TOKEN_IDENTIFIER, 1.0, MOET_TOKEN_IDENTIFIER, FLOW_TOKEN_IDENTIFIER],
PROTOCOL_ACCOUNT
)
Test.expect(res, Test.beFailed())
Test.assertError(res, errorMessage: "Swapper output type must be MOET")
}
// -----------------------------------------------------------------------------
// Test: setInsuranceSwapper with wrong input type should fail
// Swapper input type must match the token type being configured
// -----------------------------------------------------------------------------
access(all)
fun test_setInsuranceSwapper_wrongInputType_fails() {
// try to set a swapper with wrong input type (FLOW_TOKEN_IDENTIFIER instead of MOET_TOKEN_IDENTIFIER)
let res = _executeTransaction(
"./transactions/flow-alp/egovernance/set_insurance_swapper_mock.cdc",
[MOET_TOKEN_IDENTIFIER, 1.0, FLOW_TOKEN_IDENTIFIER, MOET_TOKEN_IDENTIFIER],
PROTOCOL_ACCOUNT
)
Test.expect(res, Test.beFailed())
Test.assertError(res, errorMessage: "Swapper input type must match token type")
}