-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrsiManager.ts
430 lines (357 loc) · 15.7 KB
/
rsiManager.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import chai, {expect} from "chai";
import { deployContract, solidity} from "ethereum-waffle";
import { ethers, run, ethereum, network } from "@nomiclabs/buidler";
import { Signer, constants, BigNumber, utils, Contract, BytesLike, BigNumberish } from "ethers";
import BasketFacetArtifact from "../artifacts/BasketFacet.json";
import Erc20FacetArtifact from "../artifacts/ERC20Facet.json";
import CallFacetArtifact from "../artifacts/CallFacet.json";
import DiamondCutFacetArtifact from "../artifacts/DiamondCutFacet.json";
import DiamondLoupeFacetArtifact from "../artifacts/DiamondLoupeFacet.json";
import OwnershipFacetArtifact from "../artifacts/OwnershipFacet.json";
import PieFactoryContractArtifact from "../artifacts/PieFactoryContract.json";
import MockSynthetixArtifact from "../artifacts/MockSynthetix.json";
import RSISynthetixManagerArtifact from "../artifacts/RSISynthetixManager.json";
import ManualPriceReferenceFeedArtifact from "../artifacts/ManualPriceReferenceFeed.json";
import DiamondArtifact from "../artifacts/Diamond.json";
import {
Erc20Facet,
BasketFacet,
CallFacet,
MockToken,
MockTokenFactory,
MockSynthetix,
DiamondCutFacet,
DiamondLoupeFacet,
OwnershipFacet,
PieFactoryContract,
RsiSynthetixManager,
RsiSynthetixManagerFactory,
ManualPriceReferenceFeed,
Diamond,
} from "../typechain";
import { IExperiPie } from "../typechain/IExperiPie";
import { IExperiPieFactory } from "../typechain/IExperiPieFactory";
import TimeTraveler from "../utils/TimeTraveler";
import { parseEther, formatBytes32String } from "ethers/lib/utils";
chai.use(solidity);
const FacetCutAction = {
Add: 0,
Replace: 1,
Remove: 2,
};
const PLACE_HOLDER_ADDRESS = "0x0000000000000000000000000000000000000001";
function getSelectors(contract: Contract) {
const signatures: BytesLike[] = [];
for(const key of Object.keys(contract.functions)) {
signatures.push(utils.keccak256(utils.toUtf8Bytes(key)).substr(0, 10));
}
return signatures;
}
describe("RSIManager", function() {
this.timeout(300000000);
let pieFactory: PieFactoryContract;
let pie: IExperiPie;
let account: string;
let signers: Signer[];
let timeTraveler: TimeTraveler;
let diamondCut: any[];
let shortToken: MockToken;
const shortTokenKey = formatBytes32String("sUSD");
let longToken: MockToken;
const longTokenKey = formatBytes32String("sBTC");
let synthetix: MockSynthetix;
let rsiManager: RsiSynthetixManager;
let priceFeed: ManualPriceReferenceFeed;
before(async() => {
signers = await ethers.getSigners();
account = await signers[0].getAddress();
timeTraveler = new TimeTraveler(ethereum);
const basketFacet = (await deployContract(signers[0], BasketFacetArtifact)) as BasketFacet;
const erc20Facet = (await deployContract(signers[0], Erc20FacetArtifact)) as Erc20Facet;
const callFacet = (await deployContract(signers[0], CallFacetArtifact)) as CallFacet;
const diamondCutFacet = (await deployContract(signers[0], DiamondCutFacetArtifact)) as DiamondCutFacet;
const diamondLoupeFacet = (await deployContract(signers[0], DiamondLoupeFacetArtifact)) as DiamondLoupeFacet;
const ownershipFacet = (await deployContract(signers[0], OwnershipFacetArtifact)) as OwnershipFacet;
diamondCut = [
{
action: FacetCutAction.Add,
facetAddress: basketFacet.address,
functionSelectors: getSelectors(basketFacet)
},
{
action: FacetCutAction.Add,
facetAddress: erc20Facet.address,
functionSelectors: getSelectors(erc20Facet)
},
{
action: FacetCutAction.Add,
facetAddress: callFacet.address,
functionSelectors: getSelectors(callFacet)
},
{
action: FacetCutAction.Add,
facetAddress: diamondCutFacet.address,
functionSelectors: getSelectors(diamondCutFacet)
},
{
action: FacetCutAction.Add,
facetAddress: diamondLoupeFacet.address,
functionSelectors: getSelectors(diamondLoupeFacet)
},
{
action: FacetCutAction.Add,
facetAddress: ownershipFacet.address,
functionSelectors: getSelectors(ownershipFacet)
},
];
pieFactory = (await deployContract(signers[0], PieFactoryContractArtifact)) as PieFactoryContract;
const diamondImplementation = await(deployContract(signers[0], DiamondArtifact)) as Diamond;
diamondImplementation.initialize([], constants.AddressZero);
pieFactory.setDiamondImplementation(diamondImplementation.address);
// Add default facets
for(const facet of diamondCut) {
await pieFactory.addFacet(facet);
}
await pieFactory.setDefaultController(account);
// Deploy synthetix
synthetix = await deployContract(signers[0], MockSynthetixArtifact) as MockSynthetix;
// Deploy mock short token
await synthetix.getOrSetToken(shortTokenKey);
shortToken = MockTokenFactory.connect(await synthetix.getToken(shortTokenKey), signers[0]);
// Deploy longToken
await synthetix.getOrSetToken(longTokenKey);
longToken = MockTokenFactory.connect(await synthetix.getToken(longTokenKey), signers[0]);
// Set prices
await synthetix.setPrice(shortTokenKey, parseEther("1"));
await synthetix.setPrice(longTokenKey, parseEther("10000"));
// approve sUSD
await shortToken.approve(pieFactory.address, constants.MaxUint256);
await shortToken.mint(parseEther("1000"), account);
// deploy pie
await pieFactory.bakePie([shortToken.address], [parseEther("1")], parseEther("1"), "RSI", "RSI");
pie = await IExperiPieFactory.connect(await pieFactory.pies(0), signers[0]) as IExperiPie;
// deploy manual price feed
priceFeed = await deployContract(signers[0], ManualPriceReferenceFeedArtifact) as ManualPriceReferenceFeed;
// deploy RSI manager
rsiManager = await deployContract(signers[0], RSISynthetixManagerArtifact, [
shortToken.address,
longToken.address,
shortTokenKey,
longTokenKey,
parseEther("30"),
parseEther("70"),
priceFeed.address,
pie.address,
synthetix.address
]) as RsiSynthetixManager;
await pie.addCaller(rsiManager.address);
await timeTraveler.snapshot();
});
beforeEach(async() => {
await timeTraveler.revertSnapshot();
});
describe("constructor", async() => {
it("Deploying with an invalid assetShort should fail", async() => {
const promise = deployContract(signers[0], RSISynthetixManagerArtifact, [
constants.AddressZero,
longToken.address,
shortTokenKey,
longTokenKey,
parseEther("30"),
parseEther("70"),
priceFeed.address,
pie.address,
synthetix.address
]);
await expect(promise).to.be.revertedWith("INVALID_ASSET_SHORT");
});
it("Deploying with an invalid assetLong should fail", async() => {
const promise = deployContract(signers[0], RSISynthetixManagerArtifact, [
shortToken.address,
constants.AddressZero,
shortTokenKey,
longTokenKey,
parseEther("30"),
parseEther("70"),
priceFeed.address,
pie.address,
synthetix.address
]);
await expect(promise).to.be.revertedWith("INVALID_ASSET_LONG");
});
it("Deploying with an invalid assetShortKey should fail", async() => {
const promise = deployContract(signers[0], RSISynthetixManagerArtifact, [
shortToken.address,
longToken.address,
"0x0000000000000000000000000000000000000000000000000000000000000000",
longTokenKey,
parseEther("30"),
parseEther("70"),
priceFeed.address,
pie.address,
synthetix.address
]);
await expect(promise).to.be.revertedWith("INVALID_ASSET_SHORT_KEY");
});
it("Deploying with an invalid assetLongKey should fail", async() => {
const promise = deployContract(signers[0], RSISynthetixManagerArtifact, [
shortToken.address,
longToken.address,
shortTokenKey,
"0x0000000000000000000000000000000000000000000000000000000000000000",
parseEther("30"),
parseEther("70"),
priceFeed.address,
pie.address,
synthetix.address
]);
await expect(promise).to.be.revertedWith("INVALID_ASSET_LONG_KEY");
});
it("Deploying with a RSI bottom bigger than RSI top should fail", async() => {
const promise = deployContract(signers[0], RSISynthetixManagerArtifact, [
shortToken.address,
longToken.address,
shortTokenKey,
longTokenKey,
parseEther("30"),
parseEther("29"),
priceFeed.address,
pie.address,
synthetix.address
]);
await expect(promise).to.be.revertedWith("RSI bottom should be bigger than RSI top");
});
it("Deploying with a RSI bottom below zero should fail", async() => {
const promise = deployContract(signers[0], RSISynthetixManagerArtifact, [
shortToken.address,
longToken.address,
shortTokenKey,
longTokenKey,
-1,
parseEther("29"),
priceFeed.address,
pie.address,
synthetix.address
]);
await expect(promise).to.be.revertedWith("RSI bottom should be bigger than 0");
});
it("Deploying with a RSI top above 100 should should fail", async() => {
const promise = deployContract(signers[0], RSISynthetixManagerArtifact, [
shortToken.address,
longToken.address,
shortTokenKey,
longTokenKey,
parseEther("10"),
parseEther("101"),
priceFeed.address,
pie.address,
synthetix.address
]);
await expect(promise).to.be.revertedWith("RSI top should be less than 100");
});
it("Deploying with an invalid pricefeed should fail", async() => {
const promise = deployContract(signers[0], RSISynthetixManagerArtifact, [
shortToken.address,
longToken.address,
shortTokenKey,
longTokenKey,
parseEther("30"),
parseEther("70"),
constants.AddressZero,
pie.address,
synthetix.address
]);
await expect(promise).to.be.revertedWith("INVALID_PRICE_FEED");
});
it("Deploying with an invalid basket should fail", async() => {
const promise = deployContract(signers[0], RSISynthetixManagerArtifact, [
shortToken.address,
longToken.address,
shortTokenKey,
longTokenKey,
parseEther("30"),
parseEther("70"),
priceFeed.address,
constants.AddressZero,
synthetix.address
]);
await expect(promise).to.be.revertedWith("INVALID_BASKET");
});
it("Deploying with an invalid synthetix address should fail", async() => {
const promise = deployContract(signers[0], RSISynthetixManagerArtifact, [
shortToken.address,
longToken.address,
shortTokenKey,
longTokenKey,
parseEther("30"),
parseEther("70"),
priceFeed.address,
pie.address,
constants.AddressZero
]);
await expect(promise).to.be.revertedWith("INVALID_SYNTHETIX");
});
});
describe("long", async() => {
it("Switching from short to long", async() => {
await priceFeed.update(parseEther("10"));
const tokensBefore = await pie.getTokens();
expect(tokensBefore.length).to.eq(1);
expect(tokensBefore[0]).to.eq(shortToken.address);
await rsiManager.rebalance();
const tokensAfter = await pie.getTokens();
expect(tokensAfter.length).to.eq(1);
expect(tokensAfter[0]).to.eq(longToken.address);
const shortTokenBalanceAfter = await shortToken.balanceOf(pie.address);
const longTokenBalanceAfter = await longToken.balanceOf(pie.address);
expect(shortTokenBalanceAfter).to.eq(0);
expect(longTokenBalanceAfter).to.eq(parseEther("0.0001"));
const locked = await pie.getLock();
expect(locked).to.eq(true);
});
it("Non zero short balance afterwards should fail", async() => {
await priceFeed.update(parseEther("10"));
await synthetix.setSubtractSourceAmount(parseEther("0.001"));
await expect(rsiManager.rebalance()).to.be.revertedWith("Current token balance should be zero");
});
});
describe("short", async() => {
beforeEach(async() => {
// first long to later short
await priceFeed.update(parseEther("10"));
await rsiManager.rebalance();
});
it("Switching from short to long", async() => {
await priceFeed.update(parseEther("90"));
await rsiManager.rebalance();
const tokensAfter = await pie.getTokens();
expect(tokensAfter.length).to.eq(1);
expect(tokensAfter[0]).to.eq(shortToken.address);
const shortTokenBalanceAfter = await shortToken.balanceOf(pie.address);
const longTokenBalanceAfter = await longToken.balanceOf(pie.address);
expect(shortTokenBalanceAfter).to.eq(parseEther("1"));
expect(longTokenBalanceAfter).to.eq(0);
const locked = await pie.getLock();
expect(locked).to.eq(true);
});
it("Non zero long balance afterwards should fail", async() => {
await priceFeed.update(parseEther("90"));
await synthetix.setSubtractSourceAmount(parseEther("0.000000001"));
await expect(rsiManager.rebalance()).to.be.revertedWith("Current token balance should be zero");
});
});
it("Should not rebalance with a RSI between 30 and 70 (non inclusive)", async() => {
await priceFeed.update(parseEther("31"));
await rsiManager.rebalance();
const tokensAfter = await pie.getTokens();
expect(tokensAfter.length).to.eq(1);
expect(tokensAfter[0]).to.eq(shortToken.address);
const shortTokenBalanceAfter = await shortToken.balanceOf(pie.address);
const longTokenBalanceAfter = await longToken.balanceOf(pie.address);
expect(shortTokenBalanceAfter).to.eq(parseEther("1"));
expect(longTokenBalanceAfter).to.eq(0);
});
it("Rebalancing when price feed result is not final should fail", async() => {
await expect(rsiManager.rebalance()).to.be.revertedWith("Round not complete");
});
});