Skip to content

Commit

Permalink
test: refactor tests to make it work
Browse files Browse the repository at this point in the history
  • Loading branch information
xenide committed Jul 27, 2024
1 parent 33d0980 commit cf88644
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/libraries/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ library Constants {
uint256 public constant WAD = 1e18;
uint256 public constant MAX_SUPPORTED_PRICE = type(uint128).max;
uint256 public constant MAX_AMOUNT_IN = type(uint128).max;
uint256 public constant BP_SCALE = 1e4;
uint16 public constant BP_SCALE = 1e4;
}
89 changes: 58 additions & 31 deletions test/unit/ReservoirPriceOracle.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ contract ReservoirPriceOracleTest is BaseTest {
require(lAccesses.length == 1, "incorrect number of accesses");

int256 lDecimalDiff = int256(uint256(IERC20(aToken1).decimals())) - int256(uint256(IERC20(aToken0).decimals()));
bytes32 lData = lDecimalDiff.packSimplePrice(aPrice);
uint16 lBpDiffMaxReward = Constants.BP_SCALE;
bytes32 lData = lDecimalDiff.packSimplePrice(aPrice, lBpDiffMaxReward);
require(lData.getDecimalDifference() == lDecimalDiff, "decimal diff incorrect");
require(lData.isSimplePrice(), "flag incorrect");
vm.store(address(_oracle), lAccesses[0], lData);
Expand Down Expand Up @@ -81,11 +82,12 @@ contract ReservoirPriceOracleTest is BaseTest {
function setUp() external {
// define route
address[] memory lRoute = new address[](2);
uint16[] memory lBpDiffForMaxReward = new uint16[](1);
lRoute[0] = address(_tokenA);
lRoute[1] = address(_tokenB);

_oracle.designatePair(address(_tokenB), address(_tokenA), _pair);
_oracle.setRoute(address(_tokenA), address(_tokenB), lRoute);
_oracle.setRoute(address(_tokenA), address(_tokenB), lRoute, lBpDiffForMaxReward);
}

function testWritePriceCache(uint256 aPrice) external {
Expand Down Expand Up @@ -144,11 +146,14 @@ contract ReservoirPriceOracleTest is BaseTest {
_writePriceCache(address(_tokenC), address(_tokenD), lPriceCD);

address[] memory lRoute = new address[](4);
uint16[] memory lBpDiffForMaxReward = new uint16[](3);
lBpDiffForMaxReward[0] = lBpDiffForMaxReward[1] = lBpDiffForMaxReward[2] = Constants.BP_SCALE;

lRoute[0] = address(_tokenA);
lRoute[1] = address(_tokenB);
lRoute[2] = address(_tokenC);
lRoute[3] = address(_tokenD);
_oracle.setRoute(address(_tokenA), address(_tokenD), lRoute);
_oracle.setRoute(address(_tokenA), address(_tokenD), lRoute, lBpDiffForMaxReward);

uint256 lAmountIn = 789e6;

Expand All @@ -171,11 +176,13 @@ contract ReservoirPriceOracleTest is BaseTest {
_writePriceCache(address(_tokenC), address(_tokenD), lPriceCD);

address[] memory lRoute = new address[](4);
uint16[] memory lBpDiffForMaxReward = new uint16[](3);
lBpDiffForMaxReward[0] = lBpDiffForMaxReward[1] = lBpDiffForMaxReward[2] = Constants.BP_SCALE;
lRoute[0] = address(_tokenA);
lRoute[1] = address(_tokenB);
lRoute[2] = address(_tokenC);
lRoute[3] = address(_tokenD);
_oracle.setRoute(address(_tokenA), address(_tokenD), lRoute);
_oracle.setRoute(address(_tokenA), address(_tokenD), lRoute, lBpDiffForMaxReward);

uint256 lAmountIn = 789e6;

Expand Down Expand Up @@ -211,9 +218,13 @@ contract ReservoirPriceOracleTest is BaseTest {
lRoute[1] = address(lTokenB);
lRoute[2] = address(lTokenC);

_oracle.setRoute(address(lTokenA), address(lTokenC), lRoute);
{
uint16[] memory lBpDiffForMaxReward = new uint16[](2);
lBpDiffForMaxReward[0] = lBpDiffForMaxReward[1] = Constants.BP_SCALE;
_oracle.setRoute(address(lTokenA), address(lTokenC), lRoute, lBpDiffForMaxReward);
_writePriceCache(address(lTokenA), address(lTokenB), 1e18);
_writePriceCache(address(lTokenC), address(lTokenB), 1e18);
}

// act
uint256 lAmtCOut = _oracle.getQuote(10 ** lTokenADecimals, address(lTokenA), address(lTokenC));
Expand Down Expand Up @@ -252,9 +263,10 @@ contract ReservoirPriceOracleTest is BaseTest {
_oracle.designatePair(address(lTokenA), address(lTokenB), lPair);

address[] memory lRoute = new address[](2);
uint16[] memory lBpDiffForMaxReward = new uint16[](1);
(lRoute[0], lRoute[1]) =
lTokenA < lTokenB ? (address(lTokenA), address(lTokenB)) : (address(lTokenB), address(lTokenA));
_oracle.setRoute(lRoute[0], lRoute[1], lRoute);
_oracle.setRoute(lRoute[0], lRoute[1], lRoute, lBpDiffForMaxReward);
_writePriceCache(lRoute[0], lRoute[1], lPrice); // price written could be tokenB/tokenA or tokenA/tokenB depending on the fuzz addresses

// act
Expand Down Expand Up @@ -312,7 +324,8 @@ contract ReservoirPriceOracleTest is BaseTest {
lTokenA < lTokenC ? (address(lTokenA), address(lTokenC)) : (address(lTokenC), address(lTokenA));
lRoute[1] = address(lTokenB);

_oracle.setRoute(lRoute[0], lRoute[2], lRoute);
uint16[] memory lBpDiffMaxReward = new uint16[](2);
_oracle.setRoute(lRoute[0], lRoute[2], lRoute, lBpDiffMaxReward);
_writePriceCache(
address(lTokenA) < address(lTokenB) ? address(lTokenA) : address(lTokenB),
address(lTokenA) < address(lTokenB) ? address(lTokenB) : address(lTokenA),
Expand Down Expand Up @@ -402,17 +415,6 @@ contract ReservoirPriceOracleTest is BaseTest {
assertEq(lAmtOut / 1e12, lAmtIn * lRate / 1e18);
}

function testUpdatePriceDeviationThreshold(uint256 aNewThreshold) external {
// assume
uint64 lNewThreshold = uint64(bound(aNewThreshold, 0, 0.1e18));

// act
_oracle.updatePriceDeviationThreshold(lNewThreshold);

// assert
assertEq(_oracle.priceDeviationThreshold(), lNewThreshold);
}

function testUpdateTwapPeriod(uint256 aNewPeriod) external {
// assume
uint64 lNewPeriod = uint64(bound(aNewPeriod, 1, 1 hours));
Expand Down Expand Up @@ -554,11 +556,13 @@ contract ReservoirPriceOracleTest is BaseTest {
address lIntermediate2 = address(_tokenD);
address lEnd = address(_tokenB);
address[] memory lRoute = new address[](4);
uint16[] memory lBpDiffForMaxReward = new uint16[](3);
lBpDiffForMaxReward[0] = lBpDiffForMaxReward[1] = lBpDiffForMaxReward[2] = Constants.BP_SCALE;
lRoute[0] = lStart;
lRoute[1] = lIntermediate1;
lRoute[2] = lIntermediate2;
lRoute[3] = lEnd;
_oracle.setRoute(lStart, lEnd, lRoute);
_oracle.setRoute(lStart, lEnd, lRoute, lBpDiffForMaxReward);

ReservoirPair lAC = ReservoirPair(_createPair(address(_tokenA), address(_tokenC), 0));
ReservoirPair lCD = ReservoirPair(_createPair(address(_tokenC), address(_tokenD), 0));
Expand Down Expand Up @@ -606,13 +610,15 @@ contract ReservoirPriceOracleTest is BaseTest {
address lToken0 = address(_tokenB);
address lToken1 = address(_tokenC);
address[] memory lRoute = new address[](2);
uint16[] memory lBpDiffForMaxReward = new uint16[](1);
lBpDiffForMaxReward[0] = Constants.BP_SCALE;
lRoute[0] = lToken0;
lRoute[1] = lToken1;

// act
vm.expectEmit(false, false, false, false);
emit Route(lToken0, lToken1, lRoute);
_oracle.setRoute(lToken0, lToken1, lRoute);
_oracle.setRoute(lToken0, lToken1, lRoute, lBpDiffForMaxReward);

// assert
address[] memory lQueriedRoute = _oracle.route(lToken0, lToken1);
Expand All @@ -628,13 +634,15 @@ contract ReservoirPriceOracleTest is BaseTest {
address lToken0 = address(_tokenB);
address lToken1 = address(_tokenC);
address[] memory lRoute = new address[](4);
uint16[] memory lBpDiffForMaxReward = new uint16[](3);
lBpDiffForMaxReward[0] = lBpDiffForMaxReward[1] = lBpDiffForMaxReward[2] = Constants.BP_SCALE;
lRoute[0] = lToken0;
lRoute[1] = address(_tokenA);
lRoute[2] = address(_tokenD);
lRoute[3] = lToken1;

// act
_oracle.setRoute(lToken0, lToken1, lRoute);
_oracle.setRoute(lToken0, lToken1, lRoute, lBpDiffForMaxReward);

// assert
address[] memory lQueriedRoute = _oracle.route(lToken0, lToken1);
Expand Down Expand Up @@ -667,6 +675,9 @@ contract ReservoirPriceOracleTest is BaseTest {
lIntermediateRoute3[0] = lIntermediate2;
lIntermediateRoute3[1] = lEnd;

uint16[] memory lBpDiffForMaxReward = new uint16[](3);
lBpDiffForMaxReward[0] = lBpDiffForMaxReward[1] = lBpDiffForMaxReward[2] = Constants.BP_SCALE;

// act
vm.expectEmit(false, false, false, true);
emit Route(lIntermediate2, lEnd, lIntermediateRoute3);
Expand All @@ -677,7 +688,8 @@ contract ReservoirPriceOracleTest is BaseTest {
emit Route(lIntermediate2, lIntermediate1, lIntermediateRoute2);
vm.expectEmit(false, false, false, true);
emit Route(lStart, lEnd, lRoute);
_oracle.setRoute(lStart, lEnd, lRoute);

_oracle.setRoute(lStart, lEnd, lRoute, lBpDiffForMaxReward);

// assert
assertEq(_oracle.route(lStart, lEnd), lRoute);
Expand All @@ -691,9 +703,11 @@ contract ReservoirPriceOracleTest is BaseTest {
address lToken0 = address(_tokenB);
address lToken1 = address(_tokenC);
address[] memory lRoute = new address[](2);
uint16[] memory lBpDiffForMaxReward = new uint16[](1);
lBpDiffForMaxReward[0] = Constants.BP_SCALE;
lRoute[0] = lToken0;
lRoute[1] = lToken1;
_oracle.setRoute(lToken0, lToken1, lRoute);
_oracle.setRoute(lToken0, lToken1, lRoute, lBpDiffForMaxReward);
address[] memory lQueriedRoute = _oracle.route(lToken0, lToken1);
assertEq(lQueriedRoute, lRoute);
_writePriceCache(lToken0, lToken1, 1e18);
Expand All @@ -713,11 +727,13 @@ contract ReservoirPriceOracleTest is BaseTest {
function testClearRoute_AllWordsCleared() external {
// arrange
address[] memory lRoute = new address[](4);
uint16[] memory lBpDiffForMaxReward = new uint16[](3);
lBpDiffForMaxReward[0] = lBpDiffForMaxReward[1] = lBpDiffForMaxReward[2] = Constants.BP_SCALE;
lRoute[0] = address(_tokenA);
lRoute[1] = address(_tokenC);
lRoute[2] = address(_tokenB);
lRoute[3] = address(_tokenD);
_oracle.setRoute(address(_tokenA), address(_tokenD), lRoute);
_oracle.setRoute(address(_tokenA), address(_tokenD), lRoute, lBpDiffForMaxReward);
address[] memory lQueriedRoute = _oracle.route(address(_tokenA), address(_tokenD));
assertEq(lQueriedRoute, lRoute);
bytes32 lSlot1 = address(_tokenA).calculateSlot(address(_tokenD));
Expand Down Expand Up @@ -838,11 +854,13 @@ contract ReservoirPriceOracleTest is BaseTest {
lPair.sync();

address[] memory lRoute = new address[](2);
uint16[] memory lBpDiffForMaxReward = new uint16[](1);
lBpDiffForMaxReward[0] = Constants.BP_SCALE;
lRoute[0] = address(_tokenB);
lRoute[1] = address(_tokenC);

_oracle.designatePair(address(_tokenB), address(_tokenC), lPair);
_oracle.setRoute(address(_tokenB), address(_tokenC), lRoute);
_oracle.setRoute(address(_tokenB), address(_tokenC), lRoute, lBpDiffForMaxReward);

// act & assert
vm.expectRevert(
Expand All @@ -859,32 +877,38 @@ contract ReservoirPriceOracleTest is BaseTest {
address lToken0 = address(0x1);
address lToken1 = address(0x1);
address[] memory lRoute = new address[](2);
uint16[] memory lBpDiffForMaxReward = new uint16[](1);
lBpDiffForMaxReward[0] = Constants.BP_SCALE;
lRoute[0] = lToken0;
lRoute[1] = lToken1;

// act & assert
vm.expectRevert(OracleErrors.InvalidTokensProvided.selector);
_oracle.setRoute(lToken0, lToken1, lRoute);
_oracle.setRoute(lToken0, lToken1, lRoute, lBpDiffForMaxReward);
}

function testSetRoute_NotSorted() external {
// arrange
address lToken0 = address(0x21);
address lToken1 = address(0x2);
address[] memory lRoute = new address[](2);
uint16[] memory lBpDiffForMaxReward = new uint16[](1);
lBpDiffForMaxReward[0] = Constants.BP_SCALE;
lRoute[0] = lToken0;
lRoute[1] = lToken1;

// act & assert
vm.expectRevert(OracleErrors.InvalidTokensProvided.selector);
_oracle.setRoute(lToken0, lToken1, lRoute);
_oracle.setRoute(lToken0, lToken1, lRoute, lBpDiffForMaxReward);
}

function testSetRoute_InvalidRouteLength() external {
// arrange
address lToken0 = address(0x1);
address lToken1 = address(0x2);
address[] memory lTooLong = new address[](5);
uint16[] memory lBpDiffForMaxReward = new uint16[](1);
lBpDiffForMaxReward[0] = Constants.BP_SCALE;
lTooLong[0] = lToken0;
lTooLong[1] = address(0);
lTooLong[2] = address(0);
Expand All @@ -895,11 +919,11 @@ contract ReservoirPriceOracleTest is BaseTest {

// act & assert
vm.expectRevert(OracleErrors.InvalidRouteLength.selector);
_oracle.setRoute(lToken0, lToken1, lTooLong);
_oracle.setRoute(lToken0, lToken1, lTooLong, lBpDiffForMaxReward);

// act & assert
vm.expectRevert(OracleErrors.InvalidRouteLength.selector);
_oracle.setRoute(lToken0, lToken1, lTooShort);
_oracle.setRoute(lToken0, lToken1, lTooShort, lBpDiffForMaxReward);
}

function testSetRoute_InvalidRoute() external {
Expand All @@ -916,11 +940,14 @@ contract ReservoirPriceOracleTest is BaseTest {
lInvalidRoute2[1] = address(54);
lInvalidRoute2[2] = lToken1;

uint16[] memory lBpDiffForMaxReward = new uint16[](2);
lBpDiffForMaxReward[0] = lBpDiffForMaxReward[1] = Constants.BP_SCALE;

// act & assert
vm.expectRevert(OracleErrors.InvalidRoute.selector);
_oracle.setRoute(lToken0, lToken1, lInvalidRoute1);
_oracle.setRoute(lToken0, lToken1, lInvalidRoute1, lBpDiffForMaxReward);
vm.expectRevert(OracleErrors.InvalidRoute.selector);
_oracle.setRoute(lToken0, lToken1, lInvalidRoute2);
_oracle.setRoute(lToken0, lToken1, lInvalidRoute2, lBpDiffForMaxReward);
}

function testUpdateRewardGasAmount_NotOwner() external {
Expand Down
4 changes: 3 additions & 1 deletion test/unit/libraries/RoutesLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.0;
import { Test, console2, stdError } from "forge-std/Test.sol";

import { RoutesLib } from "src/libraries/RoutesLib.sol";
import { Constants } from "src/libraries/Constants.sol";

contract RoutesLibTest is Test {
using RoutesLib for bytes32;
Expand Down Expand Up @@ -40,11 +41,12 @@ contract RoutesLibTest is Test {
uint256 lPrice = bound(aPrice, 1, 1e36);

// act
bytes32 lResult = int256(aDiff).packSimplePrice(lPrice);
bytes32 lResult = int256(aDiff).packSimplePrice(lPrice, Constants.BP_SCALE);

// assert
assertEq(lResult[0], RoutesLib.FLAG_SIMPLE_PRICE);
assertEq(lResult[1], bytes1(uint8(aDiff)));
assertEq(lResult.getBpDiffForMaxReward(), Constants.BP_SCALE);
assertEq(lResult.getPrice(), lPrice);
}
}

0 comments on commit cf88644

Please sign in to comment.