Skip to content

Commit bf53cf6

Browse files
committed
refactor: use max int + add tests
1 parent 82cc894 commit bf53cf6

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

src/adapters/WstEthChainlinkAdapter.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ contract WstEthChainlinkAdapter is AggregatorV3Interface {
2929

3030
function latestRoundData() public view returns (uint80, int256, uint256, uint256, uint80) {
3131
uint256 answer = ST_ETH.getPooledEthByShares(10 ** decimals);
32-
require(answer < type(uint256).max, ErrorsLib.OVERFLOW);
32+
require(answer <= uint256(type(int256).max), ErrorsLib.OVERFLOW);
3333
return (0, int256(answer), 0, 0, 0);
3434
}
3535
}

test/WstEthOracle.sol

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,25 @@ contract ChainlinkOracleTest is Test {
1616
oracle = new WstEthChainlinkAdapter(address(ST_ETH));
1717
}
1818

19-
function testLastRoundDataUintMax() public {
19+
function testLatestRoundDataOverflow(uint256 ethByShares) public {
20+
ethByShares = bound(ethByShares, uint256(type(int256).max) + 1, type(uint256).max);
21+
2022
vm.mockCall(
2123
address(ST_ETH),
2224
abi.encodeWithSelector(ST_ETH.getPooledEthByShares.selector, 10 ** 18),
23-
abi.encode(type(uint256).max)
25+
abi.encode(ethByShares)
2426
);
2527
vm.expectRevert(bytes(ErrorsLib.OVERFLOW));
2628
oracle.latestRoundData();
2729
}
2830

29-
function testGetRoundDataUintMax() public {
31+
function testGetRoundDataOverflow(uint256 ethByShares) public {
32+
ethByShares = bound(ethByShares, uint256(type(int256).max) + 1, type(uint256).max);
33+
3034
vm.mockCall(
3135
address(ST_ETH),
3236
abi.encodeWithSelector(ST_ETH.getPooledEthByShares.selector, 10 ** 18),
33-
abi.encode(type(uint256).max)
37+
abi.encode(ethByShares)
3438
);
3539
vm.expectRevert(bytes(ErrorsLib.OVERFLOW));
3640
oracle.getRoundData(1);
@@ -50,7 +54,7 @@ contract ChainlinkOracleTest is Test {
5054
assertEq(oracle.version(), 1);
5155
}
5256

53-
function testLastRoundData() public {
57+
function testLatestRoundData() public {
5458
(uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) =
5559
oracle.latestRoundData();
5660
assertEq(roundId, 0);
@@ -60,7 +64,7 @@ contract ChainlinkOracleTest is Test {
6064
assertEq(answeredInRound, 0);
6165
}
6266

63-
function testGetLastRoundData() public {
67+
function testGetRoundData() public {
6468
(uint80 roundId, int256 answer, uint256 startedAt, uint256 updatedAt, uint80 answeredInRound) =
6569
oracle.getRoundData(1);
6670
assertEq(roundId, 0);
@@ -70,7 +74,33 @@ contract ChainlinkOracleTest is Test {
7074
assertEq(answeredInRound, 0);
7175
}
7276

73-
function testLastRoundDataBounds() public {
77+
function testLatestRoundDataNoOverflow(uint256 ethByShares) public {
78+
ethByShares = bound(ethByShares, 0, uint256(type(int256).max));
79+
80+
vm.mockCall(
81+
address(ST_ETH),
82+
abi.encodeWithSelector(ST_ETH.getPooledEthByShares.selector, 10 ** 18),
83+
abi.encode(ethByShares)
84+
);
85+
86+
(, int256 answer,,,) = oracle.latestRoundData();
87+
assertEq(uint256(answer), ethByShares);
88+
}
89+
90+
function testGetRoundDataNoOverflow(uint256 ethByShares) public {
91+
ethByShares = bound(ethByShares, 0, uint256(type(int256).max));
92+
93+
vm.mockCall(
94+
address(ST_ETH),
95+
abi.encodeWithSelector(ST_ETH.getPooledEthByShares.selector, 10 ** 18),
96+
abi.encode(ethByShares)
97+
);
98+
99+
(, int256 answer,,,) = oracle.getRoundData(1);
100+
assertEq(uint256(answer), ethByShares);
101+
}
102+
103+
function testLatestRoundDataBounds() public {
74104
(, int256 answer,,,) = oracle.latestRoundData();
75105
assertGe(uint256(answer), 1154690031824824994); // Exchange rate queried at block 19070943
76106
assertLe(uint256(answer), 1.5e18); // Max bounds of the exchange rate. Should work for a long enough time.

0 commit comments

Comments
 (0)