forked from redstone-finance/redstone-oracles-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy-connector.test.ts
170 lines (154 loc) · 5.53 KB
/
proxy-connector.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
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
import { utils } from "@redstone-finance/protocol";
import { expect } from "chai";
import { ethers } from "hardhat";
import { WrapperBuilder } from "../../src";
import {
DEFAULT_TIMESTAMP_FOR_TESTS,
MockSignerIndex,
getMockNumericPackage,
getRange,
} from "../../src/helpers/test-utils";
import { MockDataPackageConfig } from "../../src/wrappers/MockWrapper";
import { SampleProxyConnector } from "../../typechain-types";
import {
NUMBER_OF_MOCK_NUMERIC_SIGNERS,
UNAUTHORISED_SIGNER_INDEX,
expectedNumericValues,
mockNumericPackageConfigs,
mockNumericPackages,
} from "../tests-common";
describe("SampleProxyConnector", function () {
let contract: SampleProxyConnector;
const ethDataFeedId = utils.convertStringToBytes32("ETH");
const testShouldRevertWith = async (
mockPackages: MockDataPackageConfig[],
revertMsg: string,
...args: unknown[]
) => {
const wrappedContract =
WrapperBuilder.wrap(contract).usingMockDataPackages(mockPackages);
await expect(wrappedContract.getOracleValueUsingProxy(ethDataFeedId))
.to.be.revertedWith(revertMsg)
.withArgs(...args);
};
this.beforeEach(async () => {
const ContractFactory = await ethers.getContractFactory(
"SampleProxyConnector"
);
contract = await ContractFactory.deploy();
await contract.deployed();
});
it("Should return correct oracle value for one asset", async () => {
const wrappedContract =
WrapperBuilder.wrap(contract).usingMockDataPackages(mockNumericPackages);
const fetchedValue =
await wrappedContract.getOracleValueUsingProxy(ethDataFeedId);
expect(fetchedValue).to.eq(expectedNumericValues.ETH);
});
it("Should return correct oracle values for 10 assets", async () => {
const dataPoints = [
{ dataFeedId: "ETH", value: 4000 },
{ dataFeedId: "AVAX", value: 5 },
{ dataFeedId: "BTC", value: 100000 },
{ dataFeedId: "LINK", value: 2 },
{ dataFeedId: "UNI", value: 200 },
{ dataFeedId: "FRAX", value: 1 },
{ dataFeedId: "OMG", value: 0.00003 },
{ dataFeedId: "DOGE", value: 2 },
{ dataFeedId: "SOL", value: 11 },
{ dataFeedId: "BNB", value: 31 },
];
const mockNumericPackages = getRange({
start: 0,
length: NUMBER_OF_MOCK_NUMERIC_SIGNERS,
}).map((i) =>
getMockNumericPackage({
dataPoints,
mockSignerIndex: i as MockSignerIndex,
})
);
const wrappedContract =
WrapperBuilder.wrap(contract).usingMockDataPackages(mockNumericPackages);
for (const dataPoint of dataPoints) {
await expect(
wrappedContract.checkOracleValue(
utils.convertStringToBytes32(dataPoint.dataFeedId),
Math.round(dataPoint.value * 10 ** 8)
)
).not.to.be.reverted;
}
});
it("Should forward msg.value", async () => {
const wrappedContract =
WrapperBuilder.wrap(contract).usingMockDataPackages(mockNumericPackages);
await expect(
wrappedContract.requireValueForward({
value: ethers.utils.parseUnits("2137"),
})
).not.to.be.reverted;
});
it("Should work properly with long encoded functions", async () => {
const wrappedContract =
WrapperBuilder.wrap(contract).usingMockDataPackages(mockNumericPackages);
await expect(
wrappedContract.checkOracleValueLongEncodedFunction(
ethDataFeedId,
expectedNumericValues.ETH
)
).not.to.be.reverted;
await expect(
wrappedContract.checkOracleValueLongEncodedFunction(ethDataFeedId, 9999)
)
.to.be.revertedWith("WrongValue")
.withArgs();
});
it("Should fail with correct message (timestamp invalid)", async () => {
const newMockPackages = [...mockNumericPackages];
newMockPackages[1] = getMockNumericPackage({
...mockNumericPackageConfigs[1],
timestampMilliseconds: DEFAULT_TIMESTAMP_FOR_TESTS - 1,
});
await testShouldRevertWith(
newMockPackages,
"ProxyCalldataFailedWithCustomError",
"0x355b8743"
);
});
it("Should fail with correct message (insufficient number of unique signers)", async () => {
const newMockPackages = mockNumericPackages.slice(
0,
NUMBER_OF_MOCK_NUMERIC_SIGNERS - 1
);
await testShouldRevertWith(
newMockPackages,
"ProxyCalldataFailedWithCustomError",
"0x2b13aef50000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a"
);
});
it("Should fail with correct message (signer is not authorised)", async () => {
const newMockPackages = [...mockNumericPackages];
newMockPackages[1] = getMockNumericPackage({
...mockNumericPackageConfigs[1],
mockSignerIndex: UNAUTHORISED_SIGNER_INDEX,
});
await testShouldRevertWith(
newMockPackages,
"ProxyCalldataFailedWithCustomError",
"0xec459bc00000000000000000000000008626f6940e2eb28930efb4cef49b2d1f2c9c1199"
);
});
it("Should fail with correct message (no error message)", async () => {
const wrappedContract =
WrapperBuilder.wrap(contract).usingMockDataPackages(mockNumericPackages);
await expect(wrappedContract.proxyEmptyError()).to.be.revertedWith(
"ProxyCalldataFailedWithoutErrMsg"
);
});
it("Should fail with correct message (string test message)", async () => {
const wrappedContract =
WrapperBuilder.wrap(contract).usingMockDataPackages(mockNumericPackages);
await expect(wrappedContract.proxyTestStringError())
.to.be.revertedWith("ProxyCalldataFailedWithStringMessage")
.withArgs("Test message");
});
});