Skip to content

Commit

Permalink
fix: update impl to clamp the rate of change
Browse files Browse the repository at this point in the history
  • Loading branch information
xenide committed Mar 18, 2024
1 parent 068cc49 commit 75d7d27
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
8 changes: 4 additions & 4 deletions script/optimized-deployer-meta
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"constant_product_hash": "0x6bb54a3a2e0eef36a4ee1866eee21dc514fac57496141ce405f9af4e37f34941",
"factory_hash": "0x5d635090575f999ee44232af44309f6053dc93b7844bfdd90ee7a7919a4d363c",
"oracle_caller_hash": "0x529da7083c2ffd40e327dd40a4ae6f796de1561a0191b323d0cbfab827fab3b7",
"stable_hash": "0x51e93a48df3616a3e56c131684d126d7d3ddcd8911d5b807a773aca3a576439e"
"constant_product_hash": "0xbd78fb88c65e9a8804773c3a0e0350627ae244d6daf312a549fc1e08ef9bf56e",
"factory_hash": "0x84e232ec0f6ea2ec4c9e4cfa7e5469dab805cb65ffaab4a7ee9c7c602f91345a",
"oracle_caller_hash": "0xed0f7d96dcba353321d583022005b03c09088f7de3800703dc8324b1da6c77a6",
"stable_hash": "0x24174b50e2a4c46e25d5367496b6a2ca38bf33dceb07ef3aba32e2c1266a6bf1"
}
8 changes: 4 additions & 4 deletions src/ReservoirDeployer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ contract ReservoirDeployer {
uint256 public step = 0;

// Bytecode hashes.
bytes32 public constant FACTORY_HASH = bytes32(0x5d635090575f999ee44232af44309f6053dc93b7844bfdd90ee7a7919a4d363c);
bytes32 public constant FACTORY_HASH = bytes32(0x84e232ec0f6ea2ec4c9e4cfa7e5469dab805cb65ffaab4a7ee9c7c602f91345a);
bytes32 public constant CONSTANT_PRODUCT_HASH =
bytes32(0x6bb54a3a2e0eef36a4ee1866eee21dc514fac57496141ce405f9af4e37f34941);
bytes32 public constant STABLE_HASH = bytes32(0x51e93a48df3616a3e56c131684d126d7d3ddcd8911d5b807a773aca3a576439e);
bytes32(0xbd78fb88c65e9a8804773c3a0e0350627ae244d6daf312a549fc1e08ef9bf56e);
bytes32 public constant STABLE_HASH = bytes32(0x24174b50e2a4c46e25d5367496b6a2ca38bf33dceb07ef3aba32e2c1266a6bf1);
bytes32 public constant ORACLE_CALLER_HASH =
bytes32(0x529da7083c2ffd40e327dd40a4ae6f796de1561a0191b323d0cbfab827fab3b7);
bytes32(0xed0f7d96dcba353321d583022005b03c09088f7de3800703dc8324b1da6c77a6);

// Deployment addresses.
GenericFactory public factory;
Expand Down
20 changes: 6 additions & 14 deletions src/ReservoirPair.sol
Original file line number Diff line number Diff line change
Expand Up @@ -576,25 +576,17 @@ abstract contract ReservoirPair is IAssetManagedPair, ReservoirERC20 {
(rClampedPrice, rClampedLogPrice) = (aCurrRawPrice, aCurrLogRawPrice);
} else {
// clamp the price
// multiplication of maxChangeRate and aTimeElapsed would not overflow as
// multiplication of maxChangeRate and aTimeElapsed will not overflow as
// maxChangeRate <= 0.01e18 (50 bits)
// aTimeElapsed <= 32 bits
uint256 lLowerRateOfChange = (maxChangeRate * aTimeElapsed).min(maxChangePerTrade);
if (aCurrRawPrice > aPrevClampedPrice) {
uint256 lRateLimitedPrice = aPrevClampedPrice.fullMulDiv(1e18 + maxChangeRate * aTimeElapsed, 1e18);
uint256 lPerTradeLimitedPrice = aPrevClampedPrice.fullMulDiv(1e18 + maxChangePerTrade, 1e18);
rClampedPrice = lRateLimitedPrice.min(lPerTradeLimitedPrice);
rClampedPrice = aPrevClampedPrice.fullMulDiv(1e18 + lLowerRateOfChange, 1e18);
assert(rClampedPrice < aCurrRawPrice);
} else {
// make sure that the time elapsed is not too long, else the subtraction from 1e18 will underflow
// if the time elapsed is too long, then we only depend on the per trade limited price
uint256 lChangeElapsed = maxChangeRate * aTimeElapsed;
if (lChangeElapsed > 1e18) {
lChangeElapsed = 1e18;
}
uint256 lRateLimitedPrice = aPrevClampedPrice.fullMulDiv(1e18 - lChangeElapsed, 1e18);
// subtraction will not underflow as maxChangePerTrade is limited by MAX_CHANGE_PER_TRADE
uint256 lPerTradeLimitedPrice = aPrevClampedPrice.fullMulDiv(1e18 - maxChangePerTrade, 1e18);
rClampedPrice = lRateLimitedPrice.max(lPerTradeLimitedPrice);
// subtraction will not underflow as it is limited by the max possible value of maxChangePerTrade
// which is MAX_CHANGE_PER_TRADE
rClampedPrice = aPrevClampedPrice.fullMulDiv(1e18 - lLowerRateOfChange, 1e18);
assert(rClampedPrice > aCurrRawPrice);
}
rClampedLogPrice = LogCompression.toLowResLog(rClampedPrice);
Expand Down

0 comments on commit 75d7d27

Please sign in to comment.