Skip to content

Commit 3d415bd

Browse files
authored
Create Oracle.test.js
1 parent 2b75df6 commit 3d415bd

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

tests/Oracle.test.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
const { expect } = require("chai");
2+
const { ethers } = require("hardhat");
3+
4+
describe("Oracle Contract", function () {
5+
let Oracle;
6+
let oracleInstance;
7+
let owner;
8+
let oracle1;
9+
let oracle2;
10+
11+
beforeEach(async function () {
12+
// Get the ContractFactory and Signers here.
13+
Oracle = await ethers.getContractFactory("Oracle");
14+
[owner, oracle1, oracle2] = await ethers.getSigners();
15+
16+
// Deploy a new Oracle contract for each test
17+
oracleInstance = await Oracle.deploy();
18+
await oracleInstance.deployed();
19+
});
20+
21+
describe("Deployment", function () {
22+
it("Should deploy the contract and set the owner as admin and oracle", async function () {
23+
expect(await oracleInstance.hasRole(await oracleInstance.ADMIN_ROLE(), owner.address)).to.be.true;
24+
expect(await oracleInstance.hasRole(await oracleInstance.ORACLE_ROLE(), owner.address)).to.be.true;
25+
});
26+
});
27+
28+
describe("Role Management", function () {
29+
it("Should allow the owner to add oracles", async function () {
30+
await oracleInstance.grantRole(await oracleInstance.ORACLE_ROLE(), oracle1.address);
31+
expect(await oracleInstance.hasRole(await oracleInstance.ORACLE_ROLE(), oracle1.address)).to.be.true;
32+
});
33+
34+
it("Should not allow non-admin to add oracles", async function () {
35+
await expect(oracleInstance.connect(oracle1).grantRole(await oracleInstance.ORACLE_ROLE(), oracle2.address))
36+
.to.be.revertedWith("AccessControl: sender must be an admin to grant");
37+
});
38+
});
39+
40+
describe("Data Submission", function () {
41+
beforeEach(async function () {
42+
// Grant oracle1 the oracle role
43+
await oracleInstance.grantRole(await oracleInstance.ORACLE_ROLE(), oracle1.address);
44+
});
45+
46+
it("Should allow oracles to submit data", async function () {
47+
await oracleInstance.connect(oracle1).submitData("ETH/USD", "3000");
48+
const data = await oracleInstance.getData("ETH/USD");
49+
expect(data[0]).to.equal("3000");
50+
expect(data[1]).to.be.greaterThan(0); // Check timestamp
51+
expect(data[2]).to.include(oracle1.address); // Check oracle address
52+
});
53+
54+
it("Should require data type and data to be non-empty", async function () {
55+
await expect(oracleInstance.connect(oracle1).submitData("", "3000"))
56+
.to.be.revertedWith("Data type cannot be empty");
57+
await expect(oracleInstance.connect(oracle1).submitData("ETH/USD", ""))
58+
.to.be.revertedWith("Data cannot be empty");
59+
});
60+
61+
it("Should require multiple confirmations for data to be valid", async function () {
62+
await oracleInstance.connect(oracle1).submitData("ETH/USD", "3000");
63+
await oracleInstance.grantRole(await oracleInstance.ORACLE_ROLE(), oracle2.address);
64+
await oracleInstance.connect(oracle2).submitData("ETH/USD", "3000");
65+
66+
const data = await oracleInstance.getData("ETH/USD");
67+
expect(data[0]).to.equal("3000");
68+
expect(data[2]).to.include(oracle1.address);
69+
expect(data[2]).to.include(oracle2.address);
70+
});
71+
72+
it("Should not allow the same oracle to submit data multiple times for the same type", async function () {
73+
await oracleInstance.connect(oracle1).submitData("ETH/USD", "3000");
74+
await expect(oracleInstance.connect(oracle1).submitData("ETH/USD", "3100"))
75+
.to.be.revertedWith("Oracle has already submitted data for this type");
76+
});
77+
});
78+
79+
describe("Data Retrieval", function () {
80+
beforeEach(async function () {
81+
await oracleInstance.grantRole(await oracleInstance.ORACLE_ROLE(), oracle1.address);
82+
await oracleInstance.connect(oracle1).submitData("ETH/USD", "3000");
83+
});
84+
85+
it("Should retrieve the correct data", async function () {
86+
const data = await oracleInstance.getData("ETH/USD");
87+
expect(data[0]).to.equal("3000");
88+
expect(data[1]).to.be.greaterThan(0); // Check timestamp
89+
expect(data[2]).to.include(oracle1.address); // Check oracle address
90+
});
91+
92+
it("Should revert if no data is found for the given type", async function () {
93+
await expect(oracleInstance.getData("BTC/USD"))
94+
.to.be.revertedWith("No data found for this type");
95+
});
96+
});
97+
98+
describe("Data Deletion", function () {
99+
before ```javascript
100+
beforeEach(async function () {
101+
await oracleInstance.grantRole(await oracleInstance.ORACLE_ROLE(), oracle1.address);
102+
await oracleInstance.connect(oracle1).submitData("ETH/USD", "3000");
103+
});
104+
105+
it("Should allow the owner to delete data", async function () {
106+
await oracleInstance.deleteData("ETH/USD");
107+
await expect(oracleInstance.getData("ETH/USD"))
108+
.to.be.revertedWith("No data found for this type");
109+
});
110+
111+
it("Should not allow non-admin to delete data", async function () {
112+
await expect(oracleInstance.connect(oracle1).deleteData("ETH/USD"))
113+
.to.be.revertedWith("AccessControl: sender must be an admin to revoke");
114+
});
115+
});
116+
});

0 commit comments

Comments
 (0)