This repository has been archived by the owner on Mar 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RootOracleIntegrationTest.t.sol
622 lines (555 loc) · 29 KB
/
RootOracleIntegrationTest.t.sol
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
// SPDX-License-Identifier: UNLICENSED
// Copyright (c) 2023 Tokemak Foundation. All rights reserved.
pragma solidity 0.8.17;
// solhint-disable func-name-mixedcase
// solhint-disable var-name-mixedcase
// solhint-disable max-states-count
import { Test } from "forge-std/Test.sol";
import {
BAL_VAULT,
CURVE_META_REGISTRY_MAINNET,
// SFRXETH_MAINNET,
TELLOR_ORACLE,
WSTETH_MAINNET,
STETH_MAINNET,
RETH_MAINNET,
DAI_MAINNET,
USDC_MAINNET,
USDT_MAINNET,
CBETH_MAINNET,
STETH_CL_FEED_MAINNET,
RETH_CL_FEED_MAINNET,
DAI_CL_FEED_MAINNET,
USDC_CL_FEED_MAINNET,
USDT_CL_FEED_MAINNET,
CBETH_CL_FEED_MAINNET,
// WSETH_RETH_SFRXETH_BAL_POOL,
USDC_DAI_USDT_BAL_POOL,
CBETH_WSTETH_BAL_POOL,
RETH_WETH_BAL_POOL,
WSETH_WETH_BAL_POOL,
ST_ETH_CURVE_LP_TOKEN_MAINNET,
STETH_ETH_CURVE_POOL,
THREE_CURVE_POOL_MAINNET_LP,
STETH_ETH_UNIV2,
ETH_USDT_UNIV2,
WETH9_ADDRESS,
CURVE_ETH,
THREE_CURVE_MAINNET,
USDC_IN_USD_CL_FEED_MAINNET,
ETH_CL_FEED_MAINNET,
// ETH_FRXETH_CURVE_POOL_LP,
// ETH_FRXETH_CURVE_POOL,
STETH_STABLESWAP_NG_POOL,
// STETH_FRXETH_POOL_AND_TOKEN_CURVE,
RETH_WSTETH_CURVE_POOL_LP,
RETH_WSTETH_CURVE_POOL,
RETH_WETH_CURVE_POOL,
RETH_ETH_CURVE_LP,
// FRXETH_MAINNET,
TOKE_MAINNET,
WSTETH_WETH_MAV,
ETH_SWETH_MAV,
SWETH_MAINNET,
USDT_IN_USD_CL_FEED_MAINNET,
CRVUSD_MAINNET,
USDP_CL_FEED_MAINNET,
TUSD_CL_FEED_MAINNET,
FRAX_MAINNET,
SUSD_MAINNET,
USDP_MAINNET,
TUSD_MAINNET,
USDP_CL_FEED_MAINNET,
TUSD_CL_FEED_MAINNET,
FRAX_CL_FEED_MAINNET,
SUSD_CL_FEED_MAINNET,
USDC_STABLESWAP_NG_POOL,
USDT_STABLESWAP_NG_POOL,
TUSD_STABLESWAP_NG_POOL,
USDP_STABLESWAP_NG_POOL,
FRAX_STABLESWAP_NG_POOL,
SUSD_STABLESWAP_NG_POOL,
CRV_ETH_CURVE_V2_LP,
LDO_ETH_CURVE_V2_LP,
CRV_ETH_CURVE_V2_POOL,
LDO_ETH_CURVE_V2_POOL,
CRV_CL_FEED_MAINNET,
LDO_CL_FEED_MAINNET,
CRV_MAINNET,
LDO_MAINNET
} from "../utils/Addresses.sol";
import { SystemRegistry } from "src/SystemRegistry.sol";
import { RootPriceOracle, IPriceOracle } from "src/oracles/RootPriceOracle.sol";
import { AccessController } from "src/security/AccessController.sol";
import { BalancerLPComposableStableEthOracle } from "src/oracles/providers/BalancerLPComposableStableEthOracle.sol";
import { BalancerLPMetaStableEthOracle } from "src/oracles/providers/BalancerLPMetaStableEthOracle.sol";
import { ChainlinkOracle } from "src/oracles/providers/ChainlinkOracle.sol";
import { CurveV1StableEthOracle } from "src/oracles/providers/CurveV1StableEthOracle.sol";
import { EthPeggedOracle } from "src/oracles/providers/EthPeggedOracle.sol";
// import { SfrxEthEthOracle } from "src/oracles/providers/SfrxEthEthOracle.sol";
import { UniswapV2EthOracle } from "src/oracles/providers/UniswapV2EthOracle.sol";
import { WstETHEthOracle } from "src/oracles/providers/WstETHEthOracle.sol";
import { MavEthOracle } from "src/oracles/providers/MavEthOracle.sol";
import { SwEthEthOracle } from "src/oracles/providers/SwEthEthOracle.sol";
import { CurveV2CryptoEthOracle } from "src/oracles/providers/CurveV2CryptoEthOracle.sol";
import { BaseOracleDenominations } from "src/oracles/providers/base/BaseOracleDenominations.sol";
import { CrvUsdOracle } from "test/mocks/CrvUsdOracle.sol";
import { IVault as IBalancerVault } from "src/interfaces/external/balancer/IVault.sol";
import { CurveResolverMainnet, ICurveResolver, ICurveMetaRegistry } from "src/utils/CurveResolverMainnet.sol";
import { IAggregatorV3Interface } from "src/interfaces/external/chainlink/IAggregatorV3Interface.sol";
import { IswETH } from "src/interfaces/external/swell/IswETH.sol";
/**
* This series of tests compares expected values with contract calculated values for lp token pricing. Below is a guide
* that can be used to add tests to this contract.
*
* 1) Using `vm.createSelectFork`, create a new fork at a recent block number. This ensures that the safe price
* calculated is using recent data.
* 2) Register new pool with `priceOracle`, check to see if individual tokens need to be registered with Chainlink
* or Tellor, and if lp token needs to be registered with a specific lp token oracle.
* 3) Using an external source (coingecko, protocol UI, Etherscan), retrieve total value of the pool in USD.
* Divide this value by the current price of Eth in USD to get the total value of the pool in Eth.
* 4) Normalize value of pool in Eth to e18, divide by total number of lp tokens (will already be in e18 in most
* cases). Normalize value returned to e18 decimals, this will be the value expected to be returned by
* the safe price contract.
*/
contract RootOracleIntegrationTest is Test {
SystemRegistry public systemRegistry;
RootPriceOracle public priceOracle;
AccessController public accessControl;
CurveResolverMainnet public curveResolver;
BalancerLPComposableStableEthOracle public balancerComposableOracle;
BalancerLPMetaStableEthOracle public balancerMetaOracle;
ChainlinkOracle public chainlinkOracle;
CurveV1StableEthOracle public curveStableOracle;
EthPeggedOracle public ethPegOracle;
// SfrxEthEthOracle public sfrxEthOracle;
UniswapV2EthOracle public uniV2EthOracle;
WstETHEthOracle public wstEthOracle;
MavEthOracle public mavEthOracle;
SwEthEthOracle public swEthOracle;
CurveV2CryptoEthOracle public curveCryptoOracle;
CrvUsdOracle public crvUsdOracle;
function setUp() external {
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_474_729);
// Set up system level contracts.
systemRegistry = new SystemRegistry(TOKE_MAINNET, WETH9_ADDRESS);
accessControl = new AccessController(address(systemRegistry));
systemRegistry.setAccessController(address(accessControl));
priceOracle = new RootPriceOracle(systemRegistry);
systemRegistry.setRootPriceOracle(address(priceOracle));
curveResolver = new CurveResolverMainnet(ICurveMetaRegistry(CURVE_META_REGISTRY_MAINNET));
// Set up various oracle contracts
balancerComposableOracle = new BalancerLPComposableStableEthOracle(systemRegistry, IBalancerVault(BAL_VAULT));
balancerMetaOracle = new BalancerLPMetaStableEthOracle(systemRegistry, IBalancerVault(BAL_VAULT));
chainlinkOracle = new ChainlinkOracle(systemRegistry);
curveStableOracle = new CurveV1StableEthOracle(systemRegistry, ICurveResolver(curveResolver));
ethPegOracle = new EthPeggedOracle(systemRegistry);
// sfrxEthOracle = new SfrxEthEthOracle(systemRegistry, SFRXETH_MAINNET);
uniV2EthOracle = new UniswapV2EthOracle(systemRegistry);
wstEthOracle = new WstETHEthOracle(systemRegistry, WSTETH_MAINNET);
mavEthOracle = new MavEthOracle(systemRegistry);
swEthOracle = new SwEthEthOracle(systemRegistry, IswETH(SWETH_MAINNET));
curveCryptoOracle = new CurveV2CryptoEthOracle(systemRegistry, ICurveResolver(curveResolver));
crvUsdOracle = new CrvUsdOracle(
systemRegistry,
IAggregatorV3Interface(USDC_IN_USD_CL_FEED_MAINNET),
IAggregatorV3Interface(USDT_IN_USD_CL_FEED_MAINNET),
IAggregatorV3Interface(ETH_CL_FEED_MAINNET)
);
//
// Make persistent for multiple forks
//
vm.makePersistent(address(systemRegistry));
vm.makePersistent(address(accessControl));
vm.makePersistent(address(priceOracle));
vm.makePersistent(address(curveResolver));
vm.makePersistent(address(balancerComposableOracle));
vm.makePersistent(address(balancerMetaOracle));
vm.makePersistent(address(chainlinkOracle));
vm.makePersistent(address(curveStableOracle));
vm.makePersistent(address(ethPegOracle));
// vm.makePersistent(address(sfrxEthOracle));
vm.makePersistent(address(uniV2EthOracle));
vm.makePersistent(address(wstEthOracle));
vm.makePersistent(address(mavEthOracle));
vm.makePersistent(address(swEthOracle));
vm.makePersistent(address(curveCryptoOracle));
vm.makePersistent(address(crvUsdOracle));
//
// Root price oracle setup
//
priceOracle.registerMapping(STETH_MAINNET, IPriceOracle(address(chainlinkOracle)));
priceOracle.registerMapping(RETH_MAINNET, IPriceOracle(address(chainlinkOracle)));
priceOracle.registerMapping(DAI_MAINNET, IPriceOracle(address(chainlinkOracle)));
priceOracle.registerMapping(USDC_MAINNET, IPriceOracle(address(chainlinkOracle)));
priceOracle.registerMapping(USDT_MAINNET, IPriceOracle(address(chainlinkOracle)));
priceOracle.registerMapping(CBETH_MAINNET, IPriceOracle(address(chainlinkOracle)));
priceOracle.registerMapping(FRAX_MAINNET, IPriceOracle(address(chainlinkOracle)));
priceOracle.registerMapping(SUSD_MAINNET, IPriceOracle(address(chainlinkOracle)));
priceOracle.registerMapping(USDP_MAINNET, IPriceOracle(address(chainlinkOracle)));
priceOracle.registerMapping(TUSD_MAINNET, IPriceOracle(address(chainlinkOracle)));
priceOracle.registerMapping(CRVUSD_MAINNET, IPriceOracle(address(crvUsdOracle)));
priceOracle.registerMapping(CRV_MAINNET, IPriceOracle(address(chainlinkOracle)));
priceOracle.registerMapping(LDO_MAINNET, IPriceOracle(address(chainlinkOracle)));
// Balancer composable stable pool
// priceOracle.registerMapping(WSETH_RETH_SFRXETH_BAL_POOL, IPriceOracle(address(balancerComposableOracle)));
priceOracle.registerMapping(USDC_DAI_USDT_BAL_POOL, IPriceOracle(address(balancerComposableOracle)));
// Balancer meta stable pool
priceOracle.registerMapping(CBETH_WSTETH_BAL_POOL, IPriceOracle(address(balancerMetaOracle)));
priceOracle.registerMapping(RETH_WETH_BAL_POOL, IPriceOracle(address(balancerMetaOracle)));
priceOracle.registerMapping(WSETH_WETH_BAL_POOL, IPriceOracle(address(balancerMetaOracle)));
// Curve V1
priceOracle.registerMapping(ST_ETH_CURVE_LP_TOKEN_MAINNET, IPriceOracle(address(curveStableOracle)));
priceOracle.registerMapping(THREE_CURVE_POOL_MAINNET_LP, IPriceOracle(address(curveStableOracle)));
// priceOracle.registerMapping(ETH_FRXETH_CURVE_POOL_LP, IPriceOracle(address(curveStableOracle)));
// priceOracle.registerMapping(STETH_FRXETH_POOL_AND_TOKEN_CURVE, IPriceOracle(address(curveStableOracle)));
priceOracle.registerMapping(RETH_WSTETH_CURVE_POOL_LP, IPriceOracle(address(curveStableOracle)));
priceOracle.registerMapping(STETH_STABLESWAP_NG_POOL, IPriceOracle(address(curveStableOracle)));
priceOracle.registerMapping(USDC_STABLESWAP_NG_POOL, IPriceOracle(address(curveStableOracle)));
priceOracle.registerMapping(USDT_STABLESWAP_NG_POOL, IPriceOracle(address(curveStableOracle)));
priceOracle.registerMapping(TUSD_STABLESWAP_NG_POOL, IPriceOracle(address(curveStableOracle)));
priceOracle.registerMapping(USDP_STABLESWAP_NG_POOL, IPriceOracle(address(curveStableOracle)));
priceOracle.registerMapping(FRAX_STABLESWAP_NG_POOL, IPriceOracle(address(curveStableOracle)));
priceOracle.registerMapping(SUSD_STABLESWAP_NG_POOL, IPriceOracle(address(curveStableOracle)));
// CurveV2
priceOracle.registerMapping(RETH_ETH_CURVE_LP, IPriceOracle(address(curveCryptoOracle)));
priceOracle.registerMapping(CRV_ETH_CURVE_V2_LP, IPriceOracle(address(curveCryptoOracle)));
priceOracle.registerMapping(LDO_ETH_CURVE_V2_LP, IPriceOracle(address(curveCryptoOracle)));
// UniV2
priceOracle.registerMapping(STETH_ETH_UNIV2, IPriceOracle(address(uniV2EthOracle)));
priceOracle.registerMapping(ETH_USDT_UNIV2, IPriceOracle(address(uniV2EthOracle)));
// Mav
priceOracle.registerMapping(WSTETH_WETH_MAV, IPriceOracle(address(mavEthOracle)));
priceOracle.registerMapping(ETH_SWETH_MAV, IPriceOracle(address(mavEthOracle)));
// Eth 1:1 setup
priceOracle.registerMapping(WETH9_ADDRESS, IPriceOracle(address(ethPegOracle)));
priceOracle.registerMapping(CURVE_ETH, IPriceOracle(address(ethPegOracle)));
// priceOracle.registerMapping(FRXETH_MAINNET, IPriceOracle(address(ethPegOracle)));
// Lst special pricing case setup
// priceOracle.registerMapping(SFRXETH_MAINNET, IPriceOracle(address(sfrxEthOracle)));
priceOracle.registerMapping(WSTETH_MAINNET, IPriceOracle(address(wstEthOracle)));
priceOracle.registerMapping(SWETH_MAINNET, IPriceOracle(address(swEthOracle)));
// Chainlink setup
chainlinkOracle.registerChainlinkOracle(
STETH_MAINNET,
IAggregatorV3Interface(STETH_CL_FEED_MAINNET),
BaseOracleDenominations.Denomination.ETH,
24 hours
);
chainlinkOracle.registerChainlinkOracle(
RETH_MAINNET,
IAggregatorV3Interface(RETH_CL_FEED_MAINNET),
BaseOracleDenominations.Denomination.ETH,
24 hours
);
chainlinkOracle.registerChainlinkOracle(
DAI_MAINNET, IAggregatorV3Interface(DAI_CL_FEED_MAINNET), BaseOracleDenominations.Denomination.ETH, 24 hours
);
chainlinkOracle.registerChainlinkOracle(
USDC_MAINNET,
IAggregatorV3Interface(USDC_CL_FEED_MAINNET),
BaseOracleDenominations.Denomination.ETH,
24 hours
);
chainlinkOracle.registerChainlinkOracle(
USDT_MAINNET,
IAggregatorV3Interface(USDT_CL_FEED_MAINNET),
BaseOracleDenominations.Denomination.ETH,
24 hours
);
chainlinkOracle.registerChainlinkOracle(
CBETH_MAINNET,
IAggregatorV3Interface(CBETH_CL_FEED_MAINNET),
BaseOracleDenominations.Denomination.ETH,
24 hours
);
chainlinkOracle.registerChainlinkOracle(
FRAX_MAINNET,
IAggregatorV3Interface(FRAX_CL_FEED_MAINNET),
BaseOracleDenominations.Denomination.ETH,
24 hours
);
chainlinkOracle.registerChainlinkOracle(
USDP_MAINNET,
IAggregatorV3Interface(USDP_CL_FEED_MAINNET),
BaseOracleDenominations.Denomination.ETH,
24 hours
);
chainlinkOracle.registerChainlinkOracle(
TUSD_MAINNET,
IAggregatorV3Interface(TUSD_CL_FEED_MAINNET),
BaseOracleDenominations.Denomination.ETH,
24 hours
);
chainlinkOracle.registerChainlinkOracle(
SUSD_MAINNET,
IAggregatorV3Interface(SUSD_CL_FEED_MAINNET),
BaseOracleDenominations.Denomination.ETH,
24 hours
);
chainlinkOracle.registerChainlinkOracle(
CRV_MAINNET, IAggregatorV3Interface(CRV_CL_FEED_MAINNET), BaseOracleDenominations.Denomination.ETH, 24 hours
);
chainlinkOracle.registerChainlinkOracle(
LDO_MAINNET, IAggregatorV3Interface(LDO_CL_FEED_MAINNET), BaseOracleDenominations.Denomination.ETH, 24 hours
);
// Curve V1 pool setup
curveStableOracle.registerPool(STETH_ETH_CURVE_POOL, ST_ETH_CURVE_LP_TOKEN_MAINNET, true);
curveStableOracle.registerPool(THREE_CURVE_MAINNET, THREE_CURVE_POOL_MAINNET_LP, false);
// curveStableOracle.registerPool(ETH_FRXETH_CURVE_POOL, ETH_FRXETH_CURVE_POOL_LP, false);
curveStableOracle.registerPool(STETH_STABLESWAP_NG_POOL, STETH_STABLESWAP_NG_POOL, false);
// curveStableOracle.registerPool(STETH_FRXETH_POOL_AND_TOKEN_CURVE, STETH_FRXETH_POOL_AND_TOKEN_CURVE, false);
curveStableOracle.registerPool(RETH_WSTETH_CURVE_POOL, RETH_WSTETH_CURVE_POOL_LP, false);
curveStableOracle.registerPool(USDC_STABLESWAP_NG_POOL, USDC_STABLESWAP_NG_POOL, false);
curveStableOracle.registerPool(USDT_STABLESWAP_NG_POOL, USDT_STABLESWAP_NG_POOL, false);
curveStableOracle.registerPool(TUSD_STABLESWAP_NG_POOL, TUSD_STABLESWAP_NG_POOL, false);
curveStableOracle.registerPool(USDP_STABLESWAP_NG_POOL, USDP_STABLESWAP_NG_POOL, false);
curveStableOracle.registerPool(FRAX_STABLESWAP_NG_POOL, FRAX_STABLESWAP_NG_POOL, false);
// Curve V2 pool setup
curveCryptoOracle.registerPool(RETH_WETH_CURVE_POOL, RETH_ETH_CURVE_LP, false);
curveCryptoOracle.registerPool(CRV_ETH_CURVE_V2_POOL, CRV_ETH_CURVE_V2_LP, false);
curveCryptoOracle.registerPool(LDO_ETH_CURVE_V2_POOL, LDO_ETH_CURVE_V2_LP, false);
// Uni pool setup
uniV2EthOracle.register(STETH_ETH_UNIV2);
uniV2EthOracle.register(ETH_USDT_UNIV2);
}
//
// Test Lp token pricing
//
function test_BalComposableStablePoolOracle() external {
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_475_350);
// Calculated - 573334720000000
// Safe price - 575991341828605
uint256 calculatedPrice = uint256(573_334_720_000_000);
uint256 safePrice = priceOracle.getPriceInEth(USDC_DAI_USDT_BAL_POOL);
(uint256 upperBound, uint256 lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_478_924);
// Calculated - 1010782811000000000
// Safe price - 1055299120697408989
// calculatedPrice = uint256(1_010_782_811_000_000_000);
// safePrice = priceOracle.getPriceInEth(WSETH_RETH_SFRXETH_BAL_POOL);
// (upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
// assertGt(upperBound, safePrice);
// assertLt(lowerBound, safePrice);
}
function test_CurveStableV1PoolOracle() external {
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_475_426);
// Calculated - 1073735977000000000
// Safe price - 1073637176979605953
uint256 calculatedPrice = uint256(1_073_735_977_000_000_000);
uint256 safePrice = priceOracle.getPriceInEth(ST_ETH_CURVE_LP_TOKEN_MAINNET);
(uint256 upperBound, uint256 lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Calculated - 587546836000000
// Safe price - 590481873156925
calculatedPrice = uint256(587_546_836_000_000);
safePrice = priceOracle.getPriceInEth(THREE_CURVE_POOL_MAINNET_LP);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Newer tests, new fork.
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_480_014);
// Calculated - 1003869775000000000
// Safe price - 1001074825252786600
// calculatedPrice = uint256(1_003_869_775_000_000_000);
// safePrice = priceOracle.getPriceInEth(ETH_FRXETH_CURVE_POOL_LP);
// (upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
// assertGt(upperBound, safePrice);
// assertLt(lowerBound, safePrice);
// Calculated - 1012223904000000000
// Safe price - 1008312837172276871
// calculatedPrice = uint256(1_012_223_904_000_000_000);
// safePrice = priceOracle.getPriceInEth(STETH_FRXETH_POOL_AND_TOKEN_CURVE);
// (upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
// assertGt(upperBound, safePrice);
// assertLt(lowerBound, safePrice);
// Calculated - 1098321582000000000
// Safe price - 1077905860822595469
calculatedPrice = uint256(1_098_321_582_000_000_000);
safePrice = priceOracle.getPriceInEth(RETH_WSTETH_CURVE_POOL_LP);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
}
function test_UniV2PoolOracle() external {
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_475_530);
// Calculated - 2692923915000000000
// Safe price - 2719124222286442720
uint256 calculatedPrice = uint256(2_692_923_915_000_000_000);
uint256 safePrice = priceOracle.getPriceInEth(STETH_ETH_UNIV2);
(uint256 upperBound, uint256 lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Calculated - 111063607400000000000000
// Safe price - 111696966269313545001725
calculatedPrice = uint256(111_063_607_400_000_000_000_000);
safePrice = priceOracle.getPriceInEth(ETH_USDT_UNIV2);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
}
function test_BalMetaStablePoolOracle() external {
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_475_744);
// Calculated - 1010052287000000000
// Safe price - 1049623347233950707
uint256 calculatedPrice = uint256(1_010_052_287_000_000_000);
uint256 safePrice = priceOracle.getPriceInEth(CBETH_WSTETH_BAL_POOL);
(uint256 upperBound, uint256 lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Calculated - 1023468806000000000
// Safe price - 1023189295745953671
calculatedPrice = uint256(1_023_691_743_000_000_000);
safePrice = priceOracle.getPriceInEth(RETH_WETH_BAL_POOL);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Calculated - 1035273715000000000
// Safe price - 1035531137827401614
calculatedPrice = uint256(1_034_447_288_000_000_000);
safePrice = priceOracle.getPriceInEth(WSETH_WETH_BAL_POOL);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
}
// Reserves from boosted position
// price from somewhere
// total supply
function test_MavEthOracle() external {
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_528_586);
// Calculated - 1279055722000000000
// Safe price - 1281595721753262897
uint256 calculatedPrice = uint256(1_279_055_722_000_000_000);
uint256 safePrice = priceOracle.getPriceInEth(WSTETH_WETH_MAV);
(uint256 upperBound, uint256 lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Calculated - 1477192563000000000
// Safe price - 1477192560261437163
calculatedPrice = uint256(1_477_192_563_000_000_000);
safePrice = priceOracle.getPriceInEth(ETH_SWETH_MAV);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
}
/**
* @notice crvUsd / MIM and TricryptoLLAMA pool excluded as of 6/29/23. MIM does not have a Chainlink price
* feed, and TricryptoLLAMA is a v2 ng pool.
*/
function test_CurveStableSwapNGPools() external {
// Pulled stEth ng pool test from elsewhere, use older fork
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_480_014);
// Calculated - 1006028244000000000
// Safe price - 1001718276876133469
uint256 calculatedPrice = uint256(1_006_028_244_000_000_000);
uint256 safePrice = priceOracle.getPriceInEth(STETH_STABLESWAP_NG_POOL);
(uint256 upperBound, uint256 lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_586_413);
// Set up here because pool did not exist at original setup fork.
curveStableOracle.registerPool(SUSD_STABLESWAP_NG_POOL, SUSD_STABLESWAP_NG_POOL, false);
// Calculated - 540613701000000
// Safe price - 539414760524139;
calculatedPrice = uint256(540_613_701_000_000);
safePrice = priceOracle.getPriceInEth(USDC_STABLESWAP_NG_POOL);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Calculated - 540416370000000
// Safe price - 540237542722259
calculatedPrice = uint256(540_416_370_000_000);
safePrice = priceOracle.getPriceInEth(USDT_STABLESWAP_NG_POOL);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Calculated - 539978431000000
// Safe price - 538905372335699
calculatedPrice = uint256(539_978_431_000_000);
safePrice = priceOracle.getPriceInEth(TUSD_STABLESWAP_NG_POOL);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Calculated - 540443002000000
// Safe price - 534720896910672
calculatedPrice = uint256(540_443_002_000_000);
safePrice = priceOracle.getPriceInEth(USDP_STABLESWAP_NG_POOL);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Calculated - 539914597000000
// Safe price - 539944276470054
calculatedPrice = uint256(539_914_597_000_000);
safePrice = priceOracle.getPriceInEth(FRAX_STABLESWAP_NG_POOL);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Calculated - 539909058000000
// Safe price - 538554606113206
calculatedPrice = uint256(539_909_058_000_000);
safePrice = priceOracle.getPriceInEth(SUSD_STABLESWAP_NG_POOL);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
}
/**
* @notice Tested against multiple v2 pools that we are not using to test validity of approach.
*/
function test_CurveV2Pools() external {
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_672_343);
// Calculated - 2079485290000000000
// Safe price - 2077740002016828677
uint256 calculatedPrice = uint256(2_079_485_290_000_000_000);
uint256 safePrice = priceOracle.getPriceInEth(RETH_ETH_CURVE_LP);
(uint256 upperBound, uint256 lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// 28958009260000000000000
// Calculated - 42945287200000000
// Safe Price - 43072642081141667
calculatedPrice = uint256(42_945_287_200_000_000);
safePrice = priceOracle.getPriceInEth(CRV_ETH_CURVE_V2_LP);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
// Calculated - 64666948400000000
// Safe price - 64695922392289196
calculatedPrice = uint256(64_695_922_392_289_196);
safePrice = priceOracle.getPriceInEth(LDO_ETH_CURVE_V2_LP);
(upperBound, lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
}
// Specifically test path when asset is priced in USD
function test_EthInUsdPath() external {
// Use bal usdc - usdt - dai pool, usdc denominated in USD
address ETH_IN_USD = address(bytes20("ETH_IN_USD"));
vm.createSelectFork(vm.envString("MAINNET_RPC_URL"), 17_475_310);
chainlinkOracle.removeChainlinkRegistration(USDC_MAINNET);
chainlinkOracle.registerChainlinkOracle(
USDC_MAINNET,
IAggregatorV3Interface(USDC_IN_USD_CL_FEED_MAINNET),
BaseOracleDenominations.Denomination.USD,
24 hours
);
priceOracle.registerMapping(ETH_IN_USD, IPriceOracle(address(chainlinkOracle)));
chainlinkOracle.registerChainlinkOracle(
ETH_IN_USD, IAggregatorV3Interface(ETH_CL_FEED_MAINNET), BaseOracleDenominations.Denomination.USD, 0
);
// calculated - 588167942000000
// safe price - 587583813652788
uint256 calculatedPrice = uint256(588_167_942_000_000);
uint256 safePrice = priceOracle.getPriceInEth(THREE_CURVE_POOL_MAINNET_LP);
(uint256 upperBound, uint256 lowerBound) = _getTwoPercentTolerance(calculatedPrice);
assertGt(upperBound, safePrice);
assertLt(lowerBound, safePrice);
}
function _getTwoPercentTolerance(uint256 price) internal pure returns (uint256 upperBound, uint256 lowerBound) {
uint256 twoPercentToleranceValue = (price * 2) / 100;
upperBound = price + twoPercentToleranceValue;
lowerBound = price - twoPercentToleranceValue;
}
}