Skip to content

Commit

Permalink
fix: rm OracleCaller
Browse files Browse the repository at this point in the history
* wip: axing oracle caller
* wip: compiles
* fix; update internal test function
  • Loading branch information
xenide authored Nov 24, 2024
1 parent 251fc5a commit dcdfda6
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 243 deletions.
2 changes: 0 additions & 2 deletions script/DeployScript.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions script/optimized-deployer-meta
Original file line number Diff line number Diff line change
@@ -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"
}
7 changes: 0 additions & 7 deletions script/test_deploy_curves.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
41 changes: 4 additions & 37 deletions src/ReservoirDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
//////////////////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -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
Expand Down
24 changes: 5 additions & 19 deletions src/ReservoirPair.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down Expand Up @@ -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
Expand All @@ -526,31 +524,19 @@ 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%
uint128 public maxChangeRate;
// 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");
Expand Down
22 changes: 0 additions & 22 deletions src/oracle/OracleCaller.sol

This file was deleted.

18 changes: 4 additions & 14 deletions test/__fixtures/BaseTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);

Expand All @@ -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));
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
}
}
22 changes: 11 additions & 11 deletions test/unit/ConstantProductPair.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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
}
Expand Down
55 changes: 0 additions & 55 deletions test/unit/OracleCaller.t.sol

This file was deleted.

Loading

0 comments on commit dcdfda6

Please sign in to comment.