-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathOracleRef.test.ts
More file actions
196 lines (168 loc) · 6.95 KB
/
Copy pathOracleRef.test.ts
File metadata and controls
196 lines (168 loc) · 6.95 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
import { expect } from 'chai';
import { Signer } from 'ethers';
import hre, { ethers } from 'hardhat';
import { expectRevert, getAddresses, getCore } from '../../helpers';
const toBN = ethers.BigNumber.from;
describe('OracleRef', () => {
let userAddress: string;
let governorAddress: string;
const impersonatedSigners: { [key: string]: Signer } = {};
before(async () => {
const addresses = await getAddresses();
// add any addresses you want to impersonate here
const impersonatedAddresses = [addresses.userAddress, addresses.pcvControllerAddress, addresses.governorAddress];
for (const address of impersonatedAddresses) {
await hre.network.provider.request({
method: 'hardhat_impersonateAccount',
params: [address]
});
impersonatedSigners[address] = await ethers.getSigner(address);
}
});
beforeEach(async function () {
({ userAddress, governorAddress } = await getAddresses());
this.core = await getCore();
this.token = await (await ethers.getContractFactory('MockERC20')).deploy();
this.oracle = await (await ethers.getContractFactory('MockOracle')).deploy(500); // 500 USD per ETH exchange rate
this.backupOracle = await (await ethers.getContractFactory('MockOracle')).deploy(505); // 505 USD per ETH exchange rate
this.oracleRef = await (
await ethers.getContractFactory('ReserveStabilizer')
).deploy(this.core.address, this.oracle.address, this.backupOracle.address, this.token.address, 10000);
});
describe('Init', () => {
it('oracle', async function () {
expect(await this.oracleRef.oracle()).to.be.equal(this.oracle.address);
});
it('backup oracle', async function () {
expect(await this.oracleRef.backupOracle()).to.be.equal(this.backupOracle.address);
});
it('do invert', async function () {
expect(await this.oracleRef.doInvert()).to.be.equal(true);
});
it('decimals normalizer', async function () {
expect(await this.oracleRef.decimalsNormalizer()).to.be.equal(toBN('0'));
});
});
describe('Access', () => {
describe('Set Backup Oracle', () => {
it('Governor set succeeds', async function () {
expect(await this.oracleRef.backupOracle()).to.be.equal(this.backupOracle.address);
/*await expect(*/
await this.oracleRef.connect(impersonatedSigners[governorAddress]).setBackupOracle(userAddress);
/*'BackupOracleUpdate',
{
oldBackupOracle: this.backupOracle.address,
newBackupOracle: userAddress,
},
);*/
expect(await this.oracleRef.backupOracle()).to.be.equal(userAddress);
});
it('Non-governor set reverts', async function () {
await expectRevert(
this.oracleRef.connect(impersonatedSigners[userAddress]).setBackupOracle(userAddress),
'CoreRef: Caller is not a governor'
);
});
});
describe('Set Do Invert', () => {
it('Governor set succeeds', async function () {
expect(await this.oracleRef.doInvert()).to.be.equal(true);
/*await expect(*/
await this.oracleRef.connect(impersonatedSigners[governorAddress]).setDoInvert(false);
/*'InvertUpdate',
{
oldDoInvert: true,
newDoInvert: false,
},
);*/
expect(await this.oracleRef.doInvert()).to.be.equal(false);
});
it('Non-governor set reverts', async function () {
await expectRevert(
this.oracleRef.connect(impersonatedSigners[userAddress]).setDoInvert(false),
'CoreRef: Caller is not a governor'
);
});
});
describe('Set Decimals Normalizer', () => {
it('Governor set succeeds', async function () {
expect(await this.oracleRef.decimalsNormalizer()).to.be.equal(toBN('0'));
/*await expect(*/
await this.oracleRef.connect(impersonatedSigners[governorAddress]).setDecimalsNormalizer(4);
/*'DecimalsNormalizerUpdate',
{
oldDecimalsNormalizer: '0',
newDecimalsNormalizer: '4',
},
);*/
expect(await this.oracleRef.decimalsNormalizer()).to.be.equal(toBN('4'));
});
it('Non-governor set reverts', async function () {
await expectRevert(
this.oracleRef.connect(impersonatedSigners[userAddress]).setDecimalsNormalizer(4),
'CoreRef: Caller is not a governor'
);
});
});
});
describe('Update', () => {
it('succeeds', async function () {
await this.oracleRef.updateOracle();
expect(await this.oracle.updated()).to.be.equal(true);
});
});
describe('Invert', () => {
it('succeeds', async function () {
expect((await this.oracleRef.invert(['500000000000000000000']))[0]).to.be.equal('2000000000000000');
});
});
describe('Read', () => {
describe('Invalid Oracle', () => {
it('falls back to backup', async function () {
await this.oracle.setValid(false);
expect((await this.oracleRef.connect(impersonatedSigners[userAddress]).readOracle())[0]).to.be.equal(
'1980198019801980'
);
});
});
describe('Invalid Oracle and Backup', () => {
it('reverts', async function () {
await this.oracle.setValid(false);
await this.backupOracle.setValid(false);
await expectRevert(
this.oracleRef.connect(impersonatedSigners[userAddress]).readOracle(),
'OracleRef: oracle invalid'
);
});
});
describe('Valid Oracle', () => {
it('succeeds', async function () {
expect((await this.oracleRef.connect(impersonatedSigners[userAddress]).readOracle())[0]).to.be.equal(
'2000000000000000'
);
});
it('positive decimal normalizer scales up', async function () {
// Raw peg price: 500000000000000000000, 500e18
// Inversion is set to True by default in ReserveStabilizer
// Price after inversion = (1e18 * 1e18) / 500e18 = 0.2e16 = 2e15
// Scaling factor is set to 1e4. After applying scaling factor
// 2e15 * 1e4 = 2e19 = 20000000000000000000
await this.oracleRef.connect(impersonatedSigners[governorAddress]).setDecimalsNormalizer(4);
expect((await this.oracleRef.connect(impersonatedSigners[userAddress]).readOracle())[0]).to.be.equal(
'20000000000000000000'
);
});
it('negative decimal normalizer scales down', async function () {
// Raw peg price: 500000000000000000000, 500e18
// Inversion is set to True by default in ReserveStabilizer
// Price after inversion = (1e18 * 1e18) / 500e18 = 0.2e16 = 2e15
// Scaling factor is set to -1e4. After applying scaling factor
// 2e15 / 1e4 = 2e11 = 200000000000
await this.oracleRef.connect(impersonatedSigners[governorAddress]).setDecimalsNormalizer(-4),
expect((await this.oracleRef.connect(impersonatedSigners[userAddress]).readOracle())[0]).to.be.equal(
'200000000000'
);
});
});
});
});