-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDataFeedReaderExample.sol.js
84 lines (80 loc) · 3.78 KB
/
DataFeedReaderExample.sol.js
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
const helpers = require('@nomicfoundation/hardhat-network-helpers');
const { expect } = require('chai');
const { ethers } = require('hardhat');
describe('DataFeedReaderExample', function () {
async function deploy() {
const accounts = await ethers.getSigners();
const roles = {
owner: accounts[0],
randomPerson: accounts[9],
};
const mockApi3ReaderProxyFactory = await ethers.getContractFactory('MockApi3ReaderProxy', roles.owner);
const mockApi3ReaderProxy = await mockApi3ReaderProxyFactory.deploy();
const dataFeedReaderExampleFactory = await ethers.getContractFactory('DataFeedReaderExample', roles.owner);
const dataFeedReaderExample = await dataFeedReaderExampleFactory.deploy(mockApi3ReaderProxy.address);
return {
roles,
mockApi3ReaderProxy,
dataFeedReaderExample,
};
}
describe('constructor', function () {
it('constructs', async function () {
const { roles, mockApi3ReaderProxy, dataFeedReaderExample } = await helpers.loadFixture(deploy);
expect(await dataFeedReaderExample.owner()).to.equal(roles.owner.address);
expect(await dataFeedReaderExample.proxy()).to.equal(mockApi3ReaderProxy.address);
});
});
describe('setProxy', function () {
context('Sender is the owner', function () {
it('sets proxy', async function () {
const { roles, dataFeedReaderExample } = await helpers.loadFixture(deploy);
const newProxyAddress = ethers.Wallet.createRandom().address;
await dataFeedReaderExample.connect(roles.owner).setProxy(newProxyAddress);
expect(await dataFeedReaderExample.proxy()).to.equal(newProxyAddress);
});
});
context('Sender is not the owner', function () {
it('reverts', async function () {
const { roles, dataFeedReaderExample } = await helpers.loadFixture(deploy);
const newProxyAddress = ethers.Wallet.createRandom().address;
await expect(dataFeedReaderExample.connect(roles.randomPerson).setProxy(newProxyAddress)).to.be.revertedWith(
'Ownable: caller is not the owner'
);
});
});
});
describe('readDataFeed', function () {
context('Value is positive', function () {
context('Timestamp is not older than a day', function () {
it('reads data feed', async function () {
const { mockApi3ReaderProxy, dataFeedReaderExample } = await helpers.loadFixture(deploy);
const dataFeedValue = 123_456;
const dataFeedTimestamp = await helpers.time.latest();
await mockApi3ReaderProxy.mock(dataFeedValue, dataFeedTimestamp);
const dataFeed = await dataFeedReaderExample.readDataFeed();
expect(dataFeed.value).to.equal(dataFeedValue);
expect(dataFeed.timestamp).to.equal(dataFeedTimestamp);
});
});
context('Timestamp is older than a day', function () {
it('reverts', async function () {
const { mockApi3ReaderProxy, dataFeedReaderExample } = await helpers.loadFixture(deploy);
const dataFeedValue = 123_456;
const dataFeedTimestamp = (await helpers.time.latest()) - 24 * 60 * 60;
await mockApi3ReaderProxy.mock(dataFeedValue, dataFeedTimestamp);
await expect(dataFeedReaderExample.readDataFeed()).to.be.revertedWith('Timestamp older than one day');
});
});
});
context('Value is not positive', function () {
it('reverts', async function () {
const { mockApi3ReaderProxy, dataFeedReaderExample } = await helpers.loadFixture(deploy);
const dataFeedValue = -123_456;
const dataFeedTimestamp = await helpers.time.latest();
await mockApi3ReaderProxy.mock(dataFeedValue, dataFeedTimestamp);
await expect(dataFeedReaderExample.readDataFeed()).to.be.revertedWith('Value not positive');
});
});
});
});