forked from alveraboquet/redstone-oracles-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnumbers.test.ts
144 lines (128 loc) · 4.21 KB
/
numbers.test.ts
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
import { expect } from "chai";
import { ethers } from "hardhat";
import { utils } from "@redstone-finance/protocol";
import {
DEFAULT_TIMESTAMP_FOR_TESTS,
getMockNumericPackage,
} from "../../src/helpers/test-utils";
import { WrapperBuilder } from "../../src/index";
import { MockDataPackageConfig } from "../../src/wrappers/MockWrapper";
import { SampleRedstoneConsumerNumericMock } from "../../typechain-types";
import {
expectedNumericValues,
mockNumericPackageConfigs,
mockNumericPackages,
NUMBER_OF_MOCK_NUMERIC_SIGNERS,
UNAUTHORISED_SIGNER_INDEX,
} from "../tests-common";
describe("SampleRedstoneConsumerNumericMock", function () {
let contract: SampleRedstoneConsumerNumericMock;
const testShouldPass = async (
mockNumericPackages: MockDataPackageConfig[],
dataFeedId: "ETH" | "BTC"
) => {
const wrappedContract =
WrapperBuilder.wrap(contract).usingMockDataPackages(mockNumericPackages);
const tx = await wrappedContract.saveOracleValueInContractStorage(
utils.convertStringToBytes32(dataFeedId)
);
await tx.wait();
const valueFromContract = await contract.latestSavedValue();
expect(valueFromContract.toNumber()).to.be.equal(
expectedNumericValues[dataFeedId]
);
};
const testShouldRevertWith = async (
mockNumericPackages: MockDataPackageConfig[],
dataFeedId: string,
revertMsg: string,
...args: unknown[]
) => {
const wrappedContract =
WrapperBuilder.wrap(contract).usingMockDataPackages(mockNumericPackages);
await expect(
wrappedContract.saveOracleValueInContractStorage(
utils.convertStringToBytes32(dataFeedId)
)
)
.to.be.revertedWith(revertMsg)
.withArgs(...args);
};
this.beforeEach(async () => {
const ContractFactory = await ethers.getContractFactory(
"SampleRedstoneConsumerNumericMock"
);
contract = await ContractFactory.deploy();
await contract.deployed();
});
it("Should properly execute transaction on RedstoneConsumerBase contract (ETH)", async () => {
await testShouldPass(mockNumericPackages, "ETH");
});
it("Should properly execute transaction on RedstoneConsumerBase contract (BTC)", async () => {
await testShouldPass(mockNumericPackages, "BTC");
});
it("Should work properly with the greater number of unique signers than required", async () => {
const newMockPackages = [
...mockNumericPackages,
getMockNumericPackage({
...mockNumericPackageConfigs[0],
mockSignerIndex: NUMBER_OF_MOCK_NUMERIC_SIGNERS,
}),
];
await testShouldPass(newMockPackages, "BTC");
});
it("Should revert if data feed id not found", async () => {
await testShouldRevertWith(
mockNumericPackages,
"NOT_BTC_AND_NOT_ETH",
"InsufficientNumberOfUniqueSigners",
0,
10
);
});
it("Should revert for too old timestamp", async () => {
const newMockPackages = [...mockNumericPackages];
newMockPackages[1] = getMockNumericPackage({
...mockNumericPackageConfigs[1],
timestampMilliseconds: DEFAULT_TIMESTAMP_FOR_TESTS - 1,
});
await testShouldRevertWith(newMockPackages, "BTC", "TimestampIsNotValid");
});
it("Should revert for an unauthorised signer", async () => {
const newMockPackages = [...mockNumericPackages];
newMockPackages[1] = getMockNumericPackage({
...mockNumericPackageConfigs[1],
mockSignerIndex: UNAUTHORISED_SIGNER_INDEX,
});
await testShouldRevertWith(
newMockPackages,
"BTC",
"SignerNotAuthorised",
"0x8626f6940E2eb28930eFb4CeF49B2d1F2C9C1199"
);
});
it("Should revert for insufficient number of signers", async () => {
const newMockPackages = mockNumericPackages.slice(
0,
NUMBER_OF_MOCK_NUMERIC_SIGNERS - 1
);
await testShouldRevertWith(
newMockPackages,
"BTC",
"InsufficientNumberOfUniqueSigners",
9,
10
);
});
it("Should revert for duplicated packages (not enough unique signers)", async () => {
const newMockPackages = [...mockNumericPackages];
newMockPackages[1] = mockNumericPackages[0];
await testShouldRevertWith(
newMockPackages,
"BTC",
"InsufficientNumberOfUniqueSigners",
9,
10
);
});
});