From dcdfda6aa364595c69e4aa1a79819435798eac29 Mon Sep 17 00:00:00 2001 From: "A.L." Date: Sun, 24 Nov 2024 11:28:14 +0800 Subject: [PATCH] fix: rm `OracleCaller` * wip: axing oracle caller * wip: compiles * fix; update internal test function --- script/DeployScript.s.sol | 2 - script/optimized-deployer-meta | 7 +-- script/test_deploy_curves.s.sol | 7 --- src/ReservoirDeployer.sol | 41 ++----------- src/ReservoirPair.sol | 24 ++------ src/oracle/OracleCaller.sol | 22 ------- test/__fixtures/BaseTest.sol | 18 ++---- test/unit/ConstantProductPair.t.sol | 22 +++---- test/unit/OracleCaller.t.sol | 55 ----------------- test/unit/OracleWriter.t.sol | 95 +++++++++-------------------- test/unit/StablePair.t.sol | 14 ++--- 11 files changed, 64 insertions(+), 243 deletions(-) delete mode 100644 src/oracle/OracleCaller.sol delete mode 100644 test/unit/OracleCaller.t.sol diff --git a/script/DeployScript.s.sol b/script/DeployScript.s.sol index 994d5fd3..3cd89bd1 100644 --- a/script/DeployScript.s.sol +++ b/script/DeployScript.s.sol @@ -6,7 +6,6 @@ import "script/BaseScript.sol"; import { GenericFactory } from "src/GenericFactory.sol"; import { ConstantProductPair } from "src/curve/constant-product/ConstantProductPair.sol"; import { StablePair } from "src/curve/stable/StablePair.sol"; -import { OracleCaller } from "src/oracle/OracleCaller.sol"; contract DeployScript is BaseScript { address internal _riley = 0x01569E14C2134d0b2e960654Cf47212e9cEc4bA9; @@ -23,7 +22,6 @@ contract DeployScript is BaseScript { _deployer.deployFactory(type(GenericFactory).creationCode); _deployer.deployConstantProduct(type(ConstantProductPair).creationCode); _deployer.deployStable(type(StablePair).creationCode); - _deployer.deployOracleCaller(type(OracleCaller).creationCode); vm.stopBroadcast(); require(_deployer.guardian1() == _riley); diff --git a/script/optimized-deployer-meta b/script/optimized-deployer-meta index 9fdf50e5..fb970c1e 100644 --- a/script/optimized-deployer-meta +++ b/script/optimized-deployer-meta @@ -1,6 +1,5 @@ { - "constant_product_hash": "0x9648a01d3a6113d80e9a5d5f18de0e7012f75a01484a95c828f3dcd35cfb22aa", - "factory_hash": "0xa523b0a7a0ce341269049600e4d36d77a062aed3f61ac5411d1a617b243852fa", - "oracle_caller_hash": "0x803c82dbd08939f65e45c0cea69820c1e909da1afcceb6775504084062bb0647", - "stable_hash": "0x20f50448f6a2819aa2013d691d7b091dcdef9caefd369ad6be2f47aa7d37be99" + "constant_product_hash": "0xe3022cd6d0397e990bc6eb954dcf917dc7779bc75cf3ccb827e3361af8e4a4da", + "factory_hash": "0x419250835880ba2bbc5535e1a66eae6c96005200131467f2033d5ca0b2e333a7", + "stable_hash": "0xfd70a1442e2709a6d366103eaf2f111bb1ffeff0b29e1ee5829165b55e4be32e" } \ No newline at end of file diff --git a/script/test_deploy_curves.s.sol b/script/test_deploy_curves.s.sol index 065e27f1..adc17ecc 100644 --- a/script/test_deploy_curves.s.sol +++ b/script/test_deploy_curves.s.sol @@ -10,7 +10,6 @@ import { FactoryStoreLib } from "src/libraries/FactoryStore.sol"; import { GenericFactory, IERC20 } from "src/GenericFactory.sol"; import { ConstantProductPair } from "src/curve/constant-product/ConstantProductPair.sol"; import { StablePair } from "src/curve/stable/StablePair.sol"; -import { OracleCaller } from "src/oracle/OracleCaller.sol"; contract VaultScript is BaseScript { using FactoryStoreLib for GenericFactory; @@ -22,7 +21,6 @@ contract VaultScript is BaseScript { address internal _platformFeeTo = _makeAddress("platformFeeTo"); GenericFactory internal _factory; - OracleCaller private _oracleCaller; uint256 private _privateKey = vm.envUint("TEST_PRIVATE_KEY"); address private _wallet = vm.rememberKey(_privateKey); @@ -53,16 +51,11 @@ contract VaultScript is BaseScript { _factory = _deployer.deployFactory(type(GenericFactory).creationCode); _deployer.deployConstantProduct(type(ConstantProductPair).creationCode); _deployer.deployStable(type(StablePair).creationCode); - _oracleCaller = _deployer.deployOracleCaller(type(OracleCaller).creationCode); // Claim ownership of all contracts for our test contract. _deployer.proposeOwner(msg.sender); _deployer.claimOwnership(); _deployer.claimFactory(); - _deployer.claimOracleCaller(); - - // Whitelist our test contract to call the oracle. - _oracleCaller.whitelistAddress(address(this), true); _factory.createPair(IERC20(address(_usdt)), IERC20(address(_usdc)), 0); _factory.createPair(IERC20(address(_usdt)), IERC20(address(_usdc)), 1); diff --git a/src/ReservoirDeployer.sol b/src/ReservoirDeployer.sol index 94480936..1cf288c3 100644 --- a/src/ReservoirDeployer.sol +++ b/src/ReservoirDeployer.sol @@ -6,27 +6,23 @@ import { Address } from "@openzeppelin/utils/Address.sol"; import { FactoryStoreLib } from "src/libraries/FactoryStore.sol"; import { Constants } from "src/Constants.sol"; -import { OracleCaller } from "src/oracle/OracleCaller.sol"; import { GenericFactory } from "src/GenericFactory.sol"; contract ReservoirDeployer { using FactoryStoreLib for GenericFactory; // Steps. - uint256 public constant TERMINAL_STEP = 4; + uint256 public constant TERMINAL_STEP = 3; uint256 public step = 0; // Bytecode hashes. - bytes32 public constant FACTORY_HASH = bytes32(0xa523b0a7a0ce341269049600e4d36d77a062aed3f61ac5411d1a617b243852fa); + bytes32 public constant FACTORY_HASH = bytes32(0x419250835880ba2bbc5535e1a66eae6c96005200131467f2033d5ca0b2e333a7); bytes32 public constant CONSTANT_PRODUCT_HASH = - bytes32(0x9648a01d3a6113d80e9a5d5f18de0e7012f75a01484a95c828f3dcd35cfb22aa); - bytes32 public constant STABLE_HASH = bytes32(0x20f50448f6a2819aa2013d691d7b091dcdef9caefd369ad6be2f47aa7d37be99); - bytes32 public constant ORACLE_CALLER_HASH = - bytes32(0x803c82dbd08939f65e45c0cea69820c1e909da1afcceb6775504084062bb0647); + bytes32(0xe3022cd6d0397e990bc6eb954dcf917dc7779bc75cf3ccb827e3361af8e4a4da); + bytes32 public constant STABLE_HASH = bytes32(0xfd70a1442e2709a6d366103eaf2f111bb1ffeff0b29e1ee5829165b55e4be32e); // Deployment addresses. GenericFactory public factory; - OracleCaller public oracleCaller; constructor(address aGuardian1, address aGuardian2, address aGuardian3) { require( @@ -103,31 +99,6 @@ contract ReservoirDeployer { step += 1; } - function deployOracleCaller(bytes memory aOracleCallerBytecode) external returns (OracleCaller) { - require(step == 3, "OC_STEP: OUT_OF_ORDER"); - require(keccak256(aOracleCallerBytecode) == ORACLE_CALLER_HASH, "DEPLOYER: OC_HASH"); - - // Manual deployment from validated bytecode. - address lOracleCallerAddress; - assembly ("memory-safe") { - lOracleCallerAddress := - create( - 0, // value - add(aOracleCallerBytecode, 0x20), // offset - mload(aOracleCallerBytecode) // size - ) - } - require(lOracleCallerAddress != address(0), "OC_STEP: DEPLOYMENT_FAILED"); - - factory.write("Shared::oracleCaller", lOracleCallerAddress); - - // Step complete. - oracleCaller = OracleCaller(lOracleCallerAddress); - step += 1; - - return oracleCaller; - } - /*////////////////////////////////////////////////////////////////////////// OWNERSHIP CLAIM //////////////////////////////////////////////////////////////////////////*/ @@ -174,10 +145,6 @@ contract ReservoirDeployer { factory.transferOwnership(msg.sender); } - function claimOracleCaller() external onlyOwner { - oracleCaller.transferOwnership(msg.sender); - } - function rawCall(address aTarget, bytes calldata aCalldata, uint256 aValue) external onlyOwner diff --git a/src/ReservoirPair.sol b/src/ReservoirPair.sol index c86d6cdb..5d1209f8 100644 --- a/src/ReservoirPair.sol +++ b/src/ReservoirPair.sol @@ -50,7 +50,6 @@ abstract contract ReservoirPair is IAssetManagedPair, ReservoirERC20 { if (aNotStableMintBurn) { updateSwapFee(); updatePlatformFee(); - updateOracleCaller(); setClampParams( factory.read(MAX_CHANGE_RATE_NAME).toUint128(), factory.read(MAX_CHANGE_PER_TRADE_NAME).toUint128() ); @@ -517,7 +516,6 @@ abstract contract ReservoirPair is IAssetManagedPair, ReservoirERC20 { //////////////////////////////////////////////////////////////////////////*/ - event OracleCallerUpdated(address oldCaller, address newCaller); event ClampParamsUpdated(uint128 newMaxChangeRatePerSecond, uint128 newMaxChangePerTrade); // 1% per second which is 60% per minute @@ -526,9 +524,12 @@ abstract contract ReservoirPair is IAssetManagedPair, ReservoirERC20 { uint256 internal constant MAX_CHANGE_PER_TRADE = 0.1e18; string internal constant MAX_CHANGE_RATE_NAME = "Shared::maxChangeRate"; string internal constant MAX_CHANGE_PER_TRADE_NAME = "Shared::maxChangePerTrade"; - string internal constant ORACLE_CALLER_NAME = "Shared::oracleCaller"; - mapping(uint256 => Observation) internal _observations; + mapping(uint256 => Observation) public _observations; + + function observation(uint256 aIndex) external view returns (Observation memory) { + return _observations[aIndex]; + } // maximum allowed rate of change of price per second to mitigate oracle manipulation attacks in the face of // post-merge ETH. 1e18 == 100% @@ -536,21 +537,6 @@ abstract contract ReservoirPair is IAssetManagedPair, ReservoirERC20 { // how much the clamped price can move within one trade. 1e18 == 100% uint128 public maxChangePerTrade; - address public oracleCaller; - - function observation(uint256 aIndex) external view returns (Observation memory rObservation) { - require(msg.sender == oracleCaller, "RP: NOT_ORACLE_CALLER"); - rObservation = _observations[aIndex]; - } - - function updateOracleCaller() public { - address lNewCaller = factory.read(ORACLE_CALLER_NAME).toAddress(); - if (lNewCaller != oracleCaller) { - emit OracleCallerUpdated(oracleCaller, lNewCaller); - oracleCaller = lNewCaller; - } - } - function setClampParams(uint128 aMaxChangeRate, uint128 aMaxChangePerTrade) public onlyFactory { require(0 < aMaxChangeRate && aMaxChangeRate <= MAX_CHANGE_PER_SEC, "RP: INVALID_CHANGE_PER_SECOND"); require(0 < aMaxChangePerTrade && aMaxChangePerTrade <= MAX_CHANGE_PER_TRADE, "RP: INVALID_CHANGE_PER_TRADE"); diff --git a/src/oracle/OracleCaller.sol b/src/oracle/OracleCaller.sol deleted file mode 100644 index 254b972e..00000000 --- a/src/oracle/OracleCaller.sol +++ /dev/null @@ -1,22 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity ^0.8.0; - -import { Owned } from "solmate/auth/Owned.sol"; - -import { Observation, ReservoirPair } from "src/ReservoirPair.sol"; - -contract OracleCaller is Owned(msg.sender) { - event WhitelistChanged(address caller, bool whitelist); - - mapping(address => bool) public whitelist; - - function observation(ReservoirPair aPair, uint256 aIndex) external view returns (Observation memory rObservation) { - require(whitelist[msg.sender], "OC: NOT_WHITELISTED"); - rObservation = aPair.observation(aIndex); - } - - function whitelistAddress(address aCaller, bool aWhitelist) external onlyOwner { - whitelist[aCaller] = aWhitelist; - emit WhitelistChanged(aCaller, aWhitelist); - } -} diff --git a/test/__fixtures/BaseTest.sol b/test/__fixtures/BaseTest.sol index b45533ab..9fa2fdd3 100644 --- a/test/__fixtures/BaseTest.sol +++ b/test/__fixtures/BaseTest.sol @@ -8,7 +8,6 @@ import { MintableERC20 } from "test/__fixtures/MintableERC20.sol"; import { FactoryStoreLib } from "src/libraries/FactoryStore.sol"; import { Create2Lib } from "src/libraries/Create2Lib.sol"; import { Constants } from "src/Constants.sol"; -import { OracleCaller } from "src/oracle/OracleCaller.sol"; import { ReservoirDeployer } from "src/ReservoirDeployer.sol"; import { GenericFactory, IERC20 } from "src/GenericFactory.sol"; @@ -39,8 +38,6 @@ abstract contract BaseTest is Test { ConstantProductPair internal _constantProductPair; StablePair internal _stablePair; - OracleCaller internal _oracleCaller; - modifier randomizeStartTime(uint32 aNewStartTime) { vm.assume(aNewStartTime > 1); @@ -61,16 +58,11 @@ abstract contract BaseTest is Test { _factory = _deployer.deployFactory(type(GenericFactory).creationCode); _deployer.deployConstantProduct(type(ConstantProductPair).creationCode); _deployer.deployStable(type(StablePair).creationCode); - _oracleCaller = _deployer.deployOracleCaller(type(OracleCaller).creationCode); // Claim ownership of all contracts for our test contract. _deployer.proposeOwner(address(this)); _deployer.claimOwnership(); _deployer.claimFactory(); - _deployer.claimOracleCaller(); - - // Whitelist our test contract to call the oracle. - _oracleCaller.whitelistAddress(address(this), true); // Setup default ConstantProductPair. _constantProductPair = ConstantProductPair(_createPair(address(_tokenA), address(_tokenB), 0)); @@ -90,9 +82,7 @@ abstract contract BaseTest is Test { vm.serializeBytes32(lObjectKey, "factory_hash", keccak256(type(GenericFactory).creationCode)); vm.serializeBytes32(lObjectKey, "constant_product_hash", keccak256(type(ConstantProductPair).creationCode)); - vm.serializeBytes32(lObjectKey, "stable_hash", keccak256(type(StablePair).creationCode)); - rDeployerMetadata = - vm.serializeBytes32(lObjectKey, "oracle_caller_hash", keccak256(type(OracleCaller).creationCode)); + rDeployerMetadata = vm.serializeBytes32(lObjectKey, "stable_hash", keccak256(type(StablePair).creationCode)); } function _ensureDeployerExists() internal returns (ReservoirDeployer rDeployer) { @@ -146,10 +136,10 @@ abstract contract BaseTest is Test { ); vm.record(); - _oracleCaller.observation(aPair, aIndex); + aPair.observation(aIndex); (bytes32[] memory lAccesses,) = vm.accesses(address(aPair)); - require(lAccesses.length == 2, "invalid number of accesses"); + require(lAccesses.length == 1, "invalid number of accesses"); - vm.store(address(aPair), lAccesses[1], lEncoded); + vm.store(address(aPair), lAccesses[0], lEncoded); } } diff --git a/test/unit/ConstantProductPair.t.sol b/test/unit/ConstantProductPair.t.sol index 6855f708..631c985a 100644 --- a/test/unit/ConstantProductPair.t.sol +++ b/test/unit/ConstantProductPair.t.sol @@ -318,9 +318,9 @@ contract ConstantProductPairTest is BaseTest, IReservoirCallee { lPair.sync(); // obs2 written here // assert - Observation memory lObs0 = _oracleCaller.observation(lPair, 0); - Observation memory lObs1 = _oracleCaller.observation(lPair, 1); - Observation memory lObs2 = _oracleCaller.observation(lPair, 2); + Observation memory lObs0 = lPair.observation(0); + Observation memory lObs1 = lPair.observation(1); + Observation memory lObs2 = lPair.observation(2); assertApproxEqRel( LogCompression.fromLowResLog( @@ -352,8 +352,8 @@ contract ConstantProductPairTest is BaseTest, IReservoirCallee { lPair.sync(); // assert - Observation memory lObs0 = _oracleCaller.observation(lPair, 0); - Observation memory lObs1 = _oracleCaller.observation(lPair, 1); + Observation memory lObs0 = lPair.observation(0); + Observation memory lObs1 = lPair.observation(1); assertApproxEqRel( LogCompression.fromLowResLog((lObs1.logAccRawPrice - lObs0.logAccRawPrice) / 5), 0.5e18, 0.0001e18 ); @@ -389,9 +389,9 @@ contract ConstantProductPairTest is BaseTest, IReservoirCallee { lPair.sync(); // obs2 is written here // assert - Observation memory lObs0 = _oracleCaller.observation(lPair, 0); - Observation memory lObs1 = _oracleCaller.observation(lPair, 1); - Observation memory lObs2 = _oracleCaller.observation(lPair, 2); + Observation memory lObs0 = lPair.observation(0); + Observation memory lObs1 = lPair.observation(1); + Observation memory lObs2 = lPair.observation(2); assertEq(lObs0.logAccRawPrice, LogCompression.toLowResLog(1e18) * 10, "1"); assertEq( @@ -446,7 +446,7 @@ contract ConstantProductPairTest is BaseTest, IReservoirCallee { _constantProductPair.sync(); // assert - Observation memory lObs1 = _oracleCaller.observation(_constantProductPair, 1); + Observation memory lObs1 = _constantProductPair.observation(1); // no diff between raw and clamped prices assertEq(lObs1.logAccClampedPrice, lObs1.logAccRawPrice); assertEq(lObs1.logInstantClampedPrice, lObs1.logInstantRawPrice); @@ -466,7 +466,7 @@ contract ConstantProductPairTest is BaseTest, IReservoirCallee { _constantProductPair.sync(); // assert - Observation memory lObs1 = _oracleCaller.observation(_constantProductPair, 1); + Observation memory lObs1 = _constantProductPair.observation(1); // no diff between raw and clamped prices assertEq(lObs1.logAccClampedPrice, lObs1.logAccRawPrice); assertEq(lObs1.logInstantClampedPrice, lObs1.logInstantRawPrice); @@ -485,7 +485,7 @@ contract ConstantProductPairTest is BaseTest, IReservoirCallee { _constantProductPair.sync(); // assert - Observation memory lObs1 = _oracleCaller.observation(_constantProductPair, 1); + Observation memory lObs1 = _constantProductPair.observation(1); assertGt(lObs1.logAccRawPrice, lObs1.logAccClampedPrice); assertApproxEqRel(LogCompression.fromLowResLog(lObs1.logInstantClampedPrice), 1.0025e18, 0.0002e18); // 0.02% error } diff --git a/test/unit/OracleCaller.t.sol b/test/unit/OracleCaller.t.sol deleted file mode 100644 index 39dae75f..00000000 --- a/test/unit/OracleCaller.t.sol +++ /dev/null @@ -1,55 +0,0 @@ -pragma solidity ^0.8.0; - -import "test/__fixtures/BaseTest.sol"; - -import { ReservoirPair } from "src/ReservoirPair.sol"; - -contract OracleCallerTest is BaseTest { - event WhitelistChanged(address caller, bool whitelist); - - ReservoirPair[] internal _pairs; - ReservoirPair internal _pair; - - function setUp() public { - _pairs.push(_constantProductPair); - _pairs.push(_stablePair); - } - - modifier allPairs() { - for (uint256 i = 0; i < _pairs.length; ++i) { - uint256 lBefore = vm.snapshot(); - _pair = _pairs[i]; - _; - vm.revertTo(lBefore); - } - } - - function testObservation_NotWhitelisted(uint256 aIndex) external allPairs { - // assume - uint256 lIndex = bound(aIndex, 0, type(uint16).max); - vm.startPrank(_alice); - - // act & assert - vm.expectRevert("OC: NOT_WHITELISTED"); - _oracleCaller.observation(_pair, lIndex); - vm.stopPrank(); - } - - function testWhitelistAddress() external allPairs { - // act & assert - vm.expectEmit(false, false, false, true); - emit WhitelistChanged(_alice, true); - _oracleCaller.whitelistAddress(_alice, true); - - // alice can call observation without reverting now - vm.prank(_alice); - _oracleCaller.observation(_pair, 0); - } - - function testWhitelistAddress_NotOwner() external { - // act & assert - vm.prank(_bob); - vm.expectRevert("UNAUTHORIZED"); - _oracleCaller.whitelistAddress(_cal, true); - } -} diff --git a/test/unit/OracleWriter.t.sol b/test/unit/OracleWriter.t.sol index 45c0430d..ab65c598 100644 --- a/test/unit/OracleWriter.t.sol +++ b/test/unit/OracleWriter.t.sol @@ -17,7 +17,6 @@ contract OracleWriterTest is BaseTest { using FactoryStoreLib for GenericFactory; using FixedPointMathLib for uint256; - event OracleCallerUpdated(address oldCaller, address newCaller); event ClampParamsUpdated(uint128 newMaxChangeRatePerSecond, uint128 newMaxChangePerTrade); ReservoirPair[] internal _pairs; @@ -69,14 +68,14 @@ contract OracleWriterTest is BaseTest { (,,, uint16 lIndex) = _pair.getReserves(); assertEq(lIndex, 1); - Observation memory lObs = _oracleCaller.observation(_pair, 0); + Observation memory lObs = _pair.observation(0); assertEq(lObs.logAccRawPrice, 0); assertEq(lObs.logAccClampedPrice, 0); assertNotEq(lObs.logInstantRawPrice, 0); assertNotEq(lObs.logInstantClampedPrice, 0); assertNotEq(lObs.timestamp, 0); - lObs = _oracleCaller.observation(_pair, 1); + lObs = _pair.observation(1); assertNotEq(lObs.logAccRawPrice, 0); assertNotEq(lObs.logAccClampedPrice, 0); assertNotEq(lObs.logInstantRawPrice, 0); @@ -87,53 +86,19 @@ contract OracleWriterTest is BaseTest { _writeObservation(_pair, 0, int24(123), int24(-456), int88(789), int56(-1011), uint32(666)); // assert - lObs = _oracleCaller.observation(_pair, 0); + lObs = _pair.observation(0); assertEq(lObs.logInstantRawPrice, int24(123)); assertEq(lObs.logInstantClampedPrice, int24(-456)); assertEq(lObs.logAccRawPrice, int88(789)); assertEq(lObs.logAccClampedPrice, int88(-1011)); assertEq(lObs.timestamp, uint32(666)); - lObs = _oracleCaller.observation(_pair, 1); + lObs = _pair.observation(1); assertNotEq(lObs.logAccRawPrice, 0); assertNotEq(lObs.logAccClampedPrice, 0); assertNotEq(lObs.timestamp, 0); } - function testObservation_NotOracleCaller(uint256 aIndex) external allPairs { - // assume - uint256 lIndex = bound(aIndex, 0, type(uint16).max); - - // act & assert - vm.expectRevert("RP: NOT_ORACLE_CALLER"); - _pair.observation(lIndex); - } - - function testUpdateOracleCaller() external allPairs { - // arrange - address lNewOracleCaller = address(0x555); - _factory.write("Shared::oracleCaller", lNewOracleCaller); - - // act - vm.expectEmit(false, false, false, true); - emit OracleCallerUpdated(address(_oracleCaller), lNewOracleCaller); - _pair.updateOracleCaller(); - - // assert - assertEq(_pair.oracleCaller(), lNewOracleCaller); - } - - function testUpdateOracleCaller_NoChange() external allPairs { - // arrange - address lBefore = _pair.oracleCaller(); - - // act - _pair.updateOracleCaller(); - - // assert - assertEq(_pair.oracleCaller(), lBefore); - } - function testMaxChangeRate_Default() external allPairs { // assert assertEq(_pair.maxChangeRate(), Constants.DEFAULT_MAX_CHANGE_RATE); @@ -196,7 +161,7 @@ contract OracleWriterTest is BaseTest { // sanity (,,, uint16 lIndex) = _pair.getReserves(); - Observation memory lObs = _oracleCaller.observation(_pair, lIndex); + Observation memory lObs = _pair.observation(lIndex); assertEq(lIndex, Buffer.SIZE - 1); assertEq(LogCompression.fromLowResLog(lObs.logInstantRawPrice), lOriginalPrice, "instant 1"); @@ -206,7 +171,7 @@ contract OracleWriterTest is BaseTest { // assert (,,, lIndex) = _pair.getReserves(); - lObs = _oracleCaller.observation(_pair, lIndex); + lObs = _pair.observation(lIndex); assertEq(lIndex, Buffer.SIZE - 1); assertNotEq(LogCompression.fromLowResLog(lObs.logInstantRawPrice), lOriginalPrice); @@ -231,19 +196,19 @@ contract OracleWriterTest is BaseTest { // sanity - observation after first swap (,,, uint16 lIndex) = _pair.getReserves(); assertEq(lIndex, 0); - Observation memory lObs0 = _oracleCaller.observation(_pair, lIndex); + Observation memory lObs0 = _pair.observation(lIndex); // second swap _tokenA.mint(address(_pair), lSwapAmt); _pair.swap(int256(lSwapAmt), true, address(this), ""); - Observation memory lObs1 = _oracleCaller.observation(_pair, lIndex); + Observation memory lObs1 = _pair.observation(lIndex); // third swap _tokenA.mint(address(_pair), lSwapAmt); _pair.swap(int256(lSwapAmt), true, address(this), ""); - Observation memory lObs2 = _oracleCaller.observation(_pair, lIndex); + Observation memory lObs2 = _pair.observation(lIndex); // assert assertEq(lObs0.timestamp, lObs1.timestamp); @@ -271,7 +236,7 @@ contract OracleWriterTest is BaseTest { // assert - make sure that the accumulator accumulated with the previous prices, not the new prices (uint256 lNewReserve0,,, uint16 lIndex) = _pair.getReserves(); - Observation memory lObs = _oracleCaller.observation(_pair, lIndex); + Observation memory lObs = _pair.observation(lIndex); assertEq(lNewReserve0, 110e18); assertApproxEqRel( LogCompression.fromLowResLog(lObs.logAccRawPrice / int88(int256(lJumpAhead))), lOriginalPrice, 0.0001e18 @@ -292,7 +257,7 @@ contract OracleWriterTest is BaseTest { // assert (,,, uint16 lIndex) = _pair.getReserves(); - Observation memory lObs = _oracleCaller.observation(_pair, lIndex); + Observation memory lObs = _pair.observation(lIndex); assertEq(lObs.timestamp, lStartingTimestamp + lJumpAhead); } @@ -309,7 +274,7 @@ contract OracleWriterTest is BaseTest { // assert - since the max change rate is useless price given the long time elapsed // the clamped price should only be limited by the max change per trade (,,, uint16 lIndex) = _pair.getReserves(); - Observation memory lObs = _oracleCaller.observation(_pair, lIndex); + Observation memory lObs = _pair.observation(lIndex); uint256 lMaxChangePerTrade = _pair.maxChangePerTrade(); assertApproxEqRel( LogCompression.fromLowResLog(lObs.logInstantClampedPrice), @@ -333,7 +298,7 @@ contract OracleWriterTest is BaseTest { // assert (,,, uint16 lIndex) = _pair.getReserves(); - Observation memory lObs = _oracleCaller.observation(_pair, lIndex); + Observation memory lObs = _pair.observation(lIndex); uint256 lMaxChangeRate = _pair.maxChangeRate(); assertApproxEqRel( LogCompression.fromLowResLog(lObs.logInstantClampedPrice), @@ -356,7 +321,7 @@ contract OracleWriterTest is BaseTest { // assert (,,, uint16 lIndex) = _pair.getReserves(); - Observation memory lObs = _oracleCaller.observation(_pair, lIndex); + Observation memory lObs = _pair.observation(lIndex); uint256 lMaxChangePerTrade = _pair.maxChangePerTrade(); assertApproxEqRel( LogCompression.fromLowResLog(lObs.logInstantClampedPrice), @@ -378,7 +343,7 @@ contract OracleWriterTest is BaseTest { // assert (,,, uint16 lIndex) = _pair.getReserves(); - Observation memory lObs = _oracleCaller.observation(_pair, lIndex); + Observation memory lObs = _pair.observation(lIndex); uint256 lMaxChangeRate = _pair.maxChangeRate(); uint256 lMaxChangePerTrade = _pair.maxChangePerTrade(); @@ -412,8 +377,8 @@ contract OracleWriterTest is BaseTest { lSP.sync(); // assert - Observation memory lObsCP1 = _oracleCaller.observation(lCP, 1); - Observation memory lObsSP1 = _oracleCaller.observation(lSP, 1); + Observation memory lObsCP1 = lCP.observation(1); + Observation memory lObsSP1 = lSP.observation(1); if (lCP.token0() == IERC20(address(_tokenB))) { assertGt(lObsSP1.logAccRawPrice, lObsCP1.logAccRawPrice); } else { @@ -451,10 +416,10 @@ contract OracleWriterTest is BaseTest { assertEq(lIndex, 1); // assert - Observation memory lObs0CP = _oracleCaller.observation(lCP, 0); - Observation memory lObs1CP = _oracleCaller.observation(lCP, 1); - Observation memory lObs0SP = _oracleCaller.observation(lSP, 0); - Observation memory lObs1SP = _oracleCaller.observation(lSP, 1); + Observation memory lObs0CP = lCP.observation(0); + Observation memory lObs1CP = lCP.observation(1); + Observation memory lObs0SP = lSP.observation(0); + Observation memory lObs1SP = lSP.observation(1); uint256 lUncompressedPriceCP = LogCompression.fromLowResLog((lObs1CP.logAccRawPrice - lObs0CP.logAccRawPrice) / 12); uint256 lUncompressedPriceSP = @@ -483,10 +448,10 @@ contract OracleWriterTest is BaseTest { _stepTime(12); lCP.sync(); lSP.sync(); - Observation memory lObsCP0 = _oracleCaller.observation(lCP, 0); - Observation memory lObsCP1 = _oracleCaller.observation(lCP, 1); - Observation memory lObsSP0 = _oracleCaller.observation(lSP, 0); - Observation memory lObsSP1 = _oracleCaller.observation(lSP, 1); + Observation memory lObsCP0 = lCP.observation(0); + Observation memory lObsCP1 = lCP.observation(1); + Observation memory lObsSP0 = lSP.observation(0); + Observation memory lObsSP1 = lSP.observation(1); uint256 lUncompressedPriceCP = LogCompression.fromLowResLog((lObsCP1.logAccRawPrice - lObsCP0.logAccRawPrice) / 12); uint256 lUncompressedPriceSP = @@ -515,7 +480,7 @@ contract OracleWriterTest is BaseTest { // arrange - make the last observation close to overflowing (,,, uint16 lIndex) = _pair.getReserves(); _writeObservation(_pair, lIndex, 1e3, 1e3, type(int88).max, type(int88).max, uint32(block.timestamp % 2 ** 31)); - Observation memory lPrevObs = _oracleCaller.observation(_pair, lIndex); + Observation memory lPrevObs = _pair.observation(lIndex); // act uint256 lAmountToSwap = 5e18; @@ -527,7 +492,7 @@ contract OracleWriterTest is BaseTest { // assert - when it overflows it goes from a very positive number to a very negative number (,,, lIndex) = _pair.getReserves(); - Observation memory lCurrObs = _oracleCaller.observation(_pair, lIndex); + Observation memory lCurrObs = _pair.observation(lIndex); assertLt(lCurrObs.logAccRawPrice, lPrevObs.logAccRawPrice); } @@ -546,7 +511,7 @@ contract OracleWriterTest is BaseTest { // sanity - instant price is 3M (,,, uint16 lIndex) = lCP.getReserves(); - Observation memory lObs = _oracleCaller.observation(lCP, lIndex); + Observation memory lObs = lCP.observation(lIndex); assertApproxEqRel(LogCompression.fromLowResLog(lObs.logInstantClampedPrice), 3_000_000e18, 0.0001e18); // act - arbitrage happens that make the price go to around 3500 USD / ETH in one trade @@ -561,7 +526,7 @@ contract OracleWriterTest is BaseTest { // the instant raw price now is at 3494 USD (,,, lIndex) = lCP.getReserves(); - lObs = _oracleCaller.observation(lCP, lIndex); + lObs = lCP.observation(lIndex); assertApproxEqRel(LogCompression.fromLowResLog(lObs.logInstantRawPrice), 3494e18, 0.01e18); // but clamped price is at 2.98M assertApproxEqRel(LogCompression.fromLowResLog(lObs.logInstantClampedPrice), 2_984_969e18, 0.01e18); @@ -570,7 +535,7 @@ contract OracleWriterTest is BaseTest { _stepTime(30); lCP.sync(); (,,, lIndex) = lCP.getReserves(); - lObs = _oracleCaller.observation(lCP, lIndex); + lObs = lCP.observation(lIndex); } } } diff --git a/test/unit/StablePair.t.sol b/test/unit/StablePair.t.sol index a81ee32a..5f163dc8 100644 --- a/test/unit/StablePair.t.sol +++ b/test/unit/StablePair.t.sol @@ -1447,9 +1447,9 @@ contract StablePairTest is BaseTest { lPair.sync(); // assert - Observation memory lObs0 = _oracleCaller.observation(lPair, 0); - Observation memory lObs1 = _oracleCaller.observation(lPair, 1); - Observation memory lObs2 = _oracleCaller.observation(lPair, 2); + Observation memory lObs0 = lPair.observation(0); + Observation memory lObs1 = lPair.observation(1); + Observation memory lObs2 = lPair.observation(2); assertApproxEqRel( LogCompression.fromLowResLog( @@ -1504,9 +1504,9 @@ contract StablePairTest is BaseTest { lPair.sync(); // obs2 is written here // assert - Observation memory lObs0 = _oracleCaller.observation(lPair, 0); - Observation memory lObs1 = _oracleCaller.observation(lPair, 1); - Observation memory lObs2 = _oracleCaller.observation(lPair, 2); + Observation memory lObs0 = lPair.observation(0); + Observation memory lObs1 = lPair.observation(1); + Observation memory lObs2 = lPair.observation(2); assertEq(lObs0.logAccRawPrice, LogCompression.toLowResLog(1e18) * 10, "1"); assertEq( @@ -1567,7 +1567,7 @@ contract StablePairTest is BaseTest { _stablePair.sync(); // assert - Observation memory lObs1 = _oracleCaller.observation(_stablePair, 1); + Observation memory lObs1 = _stablePair.observation(1); // no diff between raw and clamped prices assertEq(lObs1.logAccClampedPrice, lObs1.logAccRawPrice); assertEq(lObs1.logInstantClampedPrice, lObs1.logInstantRawPrice);