forked from ethersphere/storage-incentives
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriceOracle.test.ts
More file actions
311 lines (236 loc) · 12.7 KB
/
Copy pathPriceOracle.test.ts
File metadata and controls
311 lines (236 loc) · 12.7 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
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
import { expect } from './util/chai';
import { ethers, deployments, getNamedAccounts, getUnnamedAccounts } from 'hardhat';
import { Contract } from 'ethers';
import { mineNBlocks, getBlockNumber } from './util/tools';
// Named accounts used by tests.
let updater: string;
let deployer: string;
let others: string[];
// Before the tests, set named accounts and read deployments.
before(async function () {
const namedAccounts = await getNamedAccounts();
deployer = namedAccounts.deployer;
updater = namedAccounts.stamper;
others = await getUnnamedAccounts();
});
const increaseRate = [0, 1036, 1027, 1025, 1024, 1023, 1021, 1017, 1012];
const errors = {
manual: {
notAdmin: 'caller is not the admin',
},
auto: {
notZero: 'unexpected zero',
},
};
describe('PriceOracle', function () {
describe('when deploying contract', function () {
beforeEach(async function () {
await deployments.fixture();
});
it('should deploy PriceOracle', async function () {
const priceOracle = await ethers.getContract('PriceOracle');
expect(priceOracle.address).to.be.properAddress;
});
it('should set the default admin role', async function () {
const priceOracle = await ethers.getContract('PriceOracle');
const updaterRole = await priceOracle.DEFAULT_ADMIN_ROLE();
expect(await priceOracle.hasRole(updaterRole, deployer)).to.be.true;
});
it('should set the postage stamp contract', async function () {
const priceOracle = await ethers.getContract('PriceOracle');
const postageStamp = await priceOracle.postageStamp();
expect(postageStamp).to.be.eq((await ethers.getContract('PostageStamp')).address);
});
it('current price should be equal to minimum price', async function () {
const priceOracle = await ethers.getContract('PriceOracle');
const minimumPrice = await priceOracle.minimumPrice();
const currentPrice = await priceOracle.currentPrice();
expect(currentPrice).to.be.gte(minimumPrice);
});
});
describe('with deployed contract', async function () {
beforeEach(async function () {
await deployments.fixture();
});
describe('manual update', function () {
let minPriceString: string;
let priceOracle: Contract, postageStamp: Contract;
let initialPriceSetBlock: number;
let price0SetBlock: number;
beforeEach(async function () {
priceOracle = await ethers.getContract('PriceOracle', deployer);
postageStamp = await ethers.getContract('PostageStamp');
const updaterRole = await priceOracle.PRICE_UPDATER_ROLE();
await priceOracle.grantRole(updaterRole, updater);
//initialise, set minimum price, todo: move to deployment
const minimumPrice = await priceOracle.minimumPrice();
minPriceString = minimumPrice.toString();
await priceOracle.setPrice(minPriceString);
price0SetBlock = await getBlockNumber();
//since postage contract was deployed in block 0
initialPriceSetBlock = await getBlockNumber();
});
it('is initialised', async function () {
expect(await priceOracle.currentPrice()).to.be.eq(minPriceString);
expect(await postageStamp.lastPrice()).to.be.eq(minPriceString);
});
it('cannot be updated manually by non admin', async function () {
const currentPrice = await priceOracle.currentPrice();
const newPrice = parseInt(currentPrice) + 1024;
const priceOracleN = await ethers.getContract('PriceOracle', others[0]);
await expect(priceOracleN.setPrice(newPrice)).to.be.revertedWith(errors.manual.notAdmin);
const priceOracleU = await ethers.getContract('PriceOracle', updater);
await expect(priceOracleU.setPrice(newPrice)).to.be.revertedWith(errors.manual.notAdmin);
});
it('can be updated manually by admin', async function () {
const currentPrice = await priceOracle.currentPrice();
const newPrice = parseInt(currentPrice) + 1024;
await expect(priceOracle.setPrice(newPrice)).to.emit(priceOracle, 'PriceUpdate').withArgs(newPrice);
expect(await priceOracle.currentPrice()).to.be.eq(newPrice);
expect(await postageStamp.lastPrice()).to.be.eq(newPrice);
});
it('does not set price less than minimum price', async function () {
const newPrice = 2048;
await expect(priceOracle.setPrice(newPrice)).to.emit(priceOracle, 'PriceUpdate').withArgs(newPrice);
const tooLowPrice = 100;
await expect(priceOracle.setPrice(tooLowPrice)).to.emit(priceOracle, 'PriceUpdate').withArgs(minPriceString);
expect(await priceOracle.currentPrice()).to.be.eq(minPriceString);
expect(await postageStamp.lastPrice()).to.be.eq(minPriceString);
});
it('should update the outpayments', async function () {
const price0 = parseInt(minPriceString);
//price 0 was set during bootstrapping deploynent of the contract to be the minimum price
const blocksElapsed0Price0 = (await getBlockNumber()) - initialPriceSetBlock;
const outPayment0 = price0 * blocksElapsed0Price0;
// elapsed total based on current block 18
// i | price | set | elapsed | outPayment
// --------------------------------------
// 0 | 1024 | 14 | 14 | 0
// --------------------------------------
// total => | 0
expect(await postageStamp.currentTotalOutPayment()).to.be.eq(outPayment0);
const price1 = 1025;
await priceOracle.setPrice(price1);
const price1SetBlock = await getBlockNumber();
await mineNBlocks(3);
const blocksElapsed1Price0 = price1SetBlock - price0SetBlock;
const blocksElapsed1Price1 = (await getBlockNumber()) - price1SetBlock;
const outPayment1 = outPayment0 + price0 * blocksElapsed1Price0 + price1 * blocksElapsed1Price1;
// elapsed total based on current block 18
// |------------------------------------------------------|
// | price | price set | block set | elapsed | outPayment |
// |------------------------------------------------------|
// | 0 | 1024 | 14 | 14 | 0 |
// | 1024 | 1025 | 15 | 1 | 1024 |
// | 1025 | | | 3 | 3075 |
// |------------------------------------------------------|
// | total => | 4099 |
// |------------------------------------------------------|
expect(await postageStamp.currentTotalOutPayment()).to.be.eq(outPayment1);
await mineNBlocks(25);
const price2 = 1026;
await priceOracle.setPrice(price2);
const price2SetBlock = await getBlockNumber();
await mineNBlocks(52);
const blocksElapsed2Price0 = price1SetBlock - price0SetBlock;
const blocksElapsed2Price1 = price2SetBlock - price1SetBlock;
const blocksElapsed2Price2 = (await getBlockNumber()) - price2SetBlock;
const outPayment2 =
price0 * blocksElapsed2Price0 + price1 * blocksElapsed2Price1 + price2 * blocksElapsed2Price2;
// elapsed total based on current block 96
// |------------------------------------------------------|
// | price | price set | block set | elapsed | outPayment |
// |------------------------------------------------------|
// | 0 | 1024 | 14 | 14 | 0 |
// | 1024 | 1025 | 15 | 1 | 1024 |
// | 1025 | 1026 | 44 | 29 | 29725 |
// | 1026 | | | 52 | 53352 |
// |------------------------------------------------------|
// | total => | 84101 |
// |------------------------------------------------------|
expect(await postageStamp.currentTotalOutPayment()).to.be.eq(outPayment2);
});
});
describe('automatic update', function () {
let minPriceString: string;
let priceOracle: Contract, postageStamp: Contract;
beforeEach(async function () {
priceOracle = await ethers.getContract('PriceOracle', deployer);
postageStamp = await ethers.getContract('PostageStamp');
const updaterRole = await priceOracle.PRICE_UPDATER_ROLE();
await priceOracle.grantRole(updaterRole, updater);
//initialise, set minimum price
const minimumPrice = await priceOracle.minimumPrice();
minPriceString = minimumPrice.toString();
await priceOracle.unPause(); // TODO: remove when price oracle is not paused by default.
await priceOracle.setPrice(minPriceString);
});
it('if redundany factor is 0', async function () {
const priceOracleU = await ethers.getContract('PriceOracle', updater);
await expect(priceOracleU.adjustPrice(0)).to.be.revertedWith(errors.auto.notZero);
});
it('if redundany factor is 1 twice', async function () {
const priceOracleU = await ethers.getContract('PriceOracle', updater);
const currentPrice = await priceOracle.currentPrice();
expect(currentPrice).to.be.eq(minPriceString);
expect(await postageStamp.lastPrice()).to.be.eq(minPriceString);
await priceOracleU.adjustPrice(1);
const newPrice1 = (increaseRate[1] * parseInt(currentPrice)) / parseInt(minPriceString);
expect(await priceOracle.currentPrice()).to.be.eq(newPrice1);
expect(await postageStamp.lastPrice()).to.be.eq(newPrice1);
await priceOracleU.adjustPrice(1);
const newPrice2 = Math.floor((increaseRate[1] * newPrice1) / parseInt(minPriceString));
expect(await priceOracle.currentPrice()).to.be.eq(newPrice2);
expect(await postageStamp.lastPrice()).to.be.eq(newPrice2);
});
it('does not adjust price if paused', async function () {
const priceOracleU = await ethers.getContract('PriceOracle', updater);
const currentPrice = await priceOracle.currentPrice();
expect(currentPrice).to.be.eq(minPriceString);
expect(await postageStamp.lastPrice()).to.be.eq(minPriceString);
await priceOracleU.adjustPrice(1);
await priceOracle.pause();
const newPrice1 = (increaseRate[1] * parseInt(currentPrice)) / parseInt(minPriceString);
expect(await priceOracle.currentPrice()).to.be.eq(newPrice1);
expect(await postageStamp.lastPrice()).to.be.eq(newPrice1);
await priceOracleU.adjustPrice(1);
expect(await priceOracle.currentPrice()).to.be.eq(newPrice1);
expect(await postageStamp.lastPrice()).to.be.eq(newPrice1);
});
it('if redundany factor modulates', async function () {
const priceOracleU = await ethers.getContract('PriceOracle', updater);
const currentPrice = await priceOracle.currentPrice();
expect(currentPrice).to.be.eq(minPriceString);
expect(await postageStamp.lastPrice()).to.be.eq(minPriceString);
const redundancySignal1 = 1;
const newPrice1 = (increaseRate[redundancySignal1] * parseInt(currentPrice)) / parseInt(minPriceString);
await expect(priceOracleU.adjustPrice(redundancySignal1))
.to.emit(priceOracle, 'PriceUpdate')
.withArgs(newPrice1);
expect(await priceOracle.currentPrice()).to.be.eq(newPrice1);
expect(await postageStamp.lastPrice()).to.be.eq(newPrice1);
const redundancySignal2 = 2;
const newPrice2 = Math.floor((increaseRate[redundancySignal2] * newPrice1) / parseInt(minPriceString));
await expect(priceOracleU.adjustPrice(redundancySignal2))
.to.emit(priceOracle, 'PriceUpdate')
.withArgs(newPrice2);
expect(await priceOracle.currentPrice()).to.be.eq(newPrice2);
expect(await postageStamp.lastPrice()).to.be.eq(newPrice2);
const redundancySignal3 = 3;
const newPrice3 = Math.floor((increaseRate[redundancySignal3] * newPrice2) / parseInt(minPriceString));
await expect(priceOracleU.adjustPrice(redundancySignal3))
.to.emit(priceOracle, 'PriceUpdate')
.withArgs(newPrice3);
expect(await priceOracle.currentPrice()).to.be.eq(newPrice3);
expect(await postageStamp.lastPrice()).to.be.eq(newPrice3);
const redundancySignal4 = 3;
const newPrice4 = Math.floor((increaseRate[redundancySignal4] * newPrice3) / parseInt(minPriceString));
await expect(priceOracleU.adjustPrice(redundancySignal4))
.to.emit(priceOracle, 'PriceUpdate')
.withArgs(newPrice4);
expect(await priceOracle.currentPrice()).to.be.eq(newPrice4);
expect(await postageStamp.lastPrice()).to.be.eq(newPrice4);
});
});
});
});