Skip to content

Commit

Permalink
fix getOracleParameters function (traderjoe-xyz#108)
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0Louis authored Dec 19, 2023
1 parent c6870ed commit 31e31f6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
14 changes: 7 additions & 7 deletions src/LBPair.sol
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ contract LBPair is LBToken, ReentrancyGuard, Clone, ILBPair {
_oracle.getSampleAt(oracleId, lookupTimestamp);

if (timeOfLastUpdate < lookupTimestamp) {
parameters.updateVolatilityParameters(parameters.getActiveId());
parameters = parameters.updateVolatilityParameters(parameters.getActiveId(), lookupTimestamp);

uint40 deltaTime = lookupTimestamp - timeOfLastUpdate;

Expand Down Expand Up @@ -373,7 +373,7 @@ contract LBPair is LBToken, ReentrancyGuard, Clone, ILBPair {

uint24 id = parameters.getActiveId();

parameters = parameters.updateReferences();
parameters = parameters.updateReferences(block.timestamp);

while (true) {
uint128 binReserves = _bins[id].decode(!swapForY);
Expand Down Expand Up @@ -434,7 +434,7 @@ contract LBPair is LBToken, ReentrancyGuard, Clone, ILBPair {

uint24 id = parameters.getActiveId();

parameters = parameters.updateReferences();
parameters = parameters.updateReferences(block.timestamp);

while (true) {
bytes32 binReserves = _bins[id];
Expand Down Expand Up @@ -494,7 +494,7 @@ contract LBPair is LBToken, ReentrancyGuard, Clone, ILBPair {

uint24 activeId = parameters.getActiveId();

parameters = parameters.updateReferences();
parameters = parameters.updateReferences(block.timestamp);

while (true) {
bytes32 binReserves = _bins[activeId];
Expand Down Expand Up @@ -526,7 +526,7 @@ contract LBPair is LBToken, ReentrancyGuard, Clone, ILBPair {
parameters.getVolatilityAccumulator(),
totalFees,
pFees
);
);
}
}

Expand Down Expand Up @@ -919,7 +919,7 @@ contract LBPair is LBToken, ReentrancyGuard, Clone, ILBPair {
variableFeeControl,
protocolShare,
maxVolatilityAccumulator
);
);
}

/**
Expand Down Expand Up @@ -986,7 +986,7 @@ contract LBPair is LBToken, ReentrancyGuard, Clone, ILBPair {
amountsInToBin = amountsIn;

if (id == activeId) {
parameters = parameters.updateVolatilityParameters(id);
parameters = parameters.updateVolatilityParameters(id, block.timestamp);

bytes32 fees = binReserves.getCompositionFees(parameters, binStep, amountsIn, supply, shares);

Expand Down
21 changes: 14 additions & 7 deletions src/libraries/PairParameterHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -356,10 +356,11 @@ library PairParameterHelper {
/**
* @dev Updates the time of last update in the encoded pair parameters
* @param params The encoded pair parameters
* @param timestamp The timestamp
* @return newParams The updated encoded pair parameters
*/
function updateTimeOfLastUpdate(bytes32 params) internal view returns (bytes32 newParams) {
uint40 currentTime = block.timestamp.safe40();
function updateTimeOfLastUpdate(bytes32 params, uint256 timestamp) internal pure returns (bytes32 newParams) {
uint40 currentTime = timestamp.safe40();
return params.set(currentTime, Encoded.MASK_UINT40, OFFSET_TIME_LAST_UPDATE);
}

Expand Down Expand Up @@ -407,27 +408,33 @@ library PairParameterHelper {
/**
* @dev Updates the volatility reference and the volatility accumulator in the encoded pair parameters
* @param params The encoded pair parameters
* @param timestamp The timestamp
* @return The updated encoded pair parameters
*/
function updateReferences(bytes32 params) internal view returns (bytes32) {
uint256 dt = block.timestamp - getTimeOfLastUpdate(params);
function updateReferences(bytes32 params, uint256 timestamp) internal pure returns (bytes32) {
uint256 dt = timestamp - getTimeOfLastUpdate(params);

if (dt >= getFilterPeriod(params)) {
params = updateIdReference(params);
params = dt < getDecayPeriod(params) ? updateVolatilityReference(params) : setVolatilityReference(params, 0);
}

return updateTimeOfLastUpdate(params);
return updateTimeOfLastUpdate(params, timestamp);
}

/**
* @dev Updates the volatility reference and the volatility accumulator in the encoded pair parameters
* @param params The encoded pair parameters
* @param activeId The active id
* @param timestamp The timestamp
* @return The updated encoded pair parameters
*/
function updateVolatilityParameters(bytes32 params, uint24 activeId) internal view returns (bytes32) {
params = updateReferences(params);
function updateVolatilityParameters(bytes32 params, uint24 activeId, uint256 timestamp)
internal
pure
returns (bytes32)
{
params = updateReferences(params, timestamp);
return updateVolatilityAccumulator(params, activeId);
}
}
8 changes: 5 additions & 3 deletions test/helpers/TestHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,9 @@ abstract contract TestHelper is Test {
router = new LBRouter(factory, factoryV1, legacyFactoryV2, legacyRouterV2, IWNATIVE(address(wnative)));

// Create quoter
quoter =
new LBQuoter( address(factoryV1), address(legacyFactoryV2), address(factory), address(legacyRouterV2), address(router));
quoter = new LBQuoter(
address(factoryV1), address(legacyFactoryV2), address(factory), address(legacyRouterV2), address(router)
);

// Label deployed contracts
vm.label(address(router), "router");
Expand Down Expand Up @@ -348,9 +349,10 @@ abstract contract TestHelper is Test {

for (uint256 i; i < total; ++i) {
uint24 id = getId(activeId, i, nbBinY);
uint256 b = lbPair.balanceOf(from, id);

ids[i] = id;
amounts[i] = lbPair.balanceOf(from, id) * percentToBurn / Constants.PRECISION;
amounts[i] = b.mulDivRoundDown(percentToBurn, Constants.PRECISION);
}

vm.prank(from);
Expand Down
16 changes: 8 additions & 8 deletions test/libraries/PairParameterHelper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ contract PairParameterHelperTest is Test {
}

function testFuzz_UpdateTimeOfLastUpdate(bytes32 params) external {
bytes32 newParams = params.updateTimeOfLastUpdate();
bytes32 newParams = params.updateTimeOfLastUpdate(block.timestamp);

assertEq(newParams.getTimeOfLastUpdate(), block.timestamp, "test_UpdateTimeOfLastUpdate::1");
assertEq(
Expand Down Expand Up @@ -280,7 +280,7 @@ contract PairParameterHelperTest is Test {
sfp.variableFeeControl,
sfp.protocolShare,
sfp.maxVolatilityAccumulator
).updateTimeOfLastUpdate();
).updateTimeOfLastUpdate(block.timestamp);

vm.warp(time);

Expand All @@ -294,7 +294,7 @@ contract PairParameterHelperTest is Test {
deltaTime >= sfp.decayPeriod ? 0 : params.updateVolatilityReference().getVolatilityReference();
}

bytes32 newParams = params.updateReferences();
bytes32 newParams = params.updateReferences(block.timestamp);

assertEq(newParams.getIdReference(), idReference, "test_UpdateReferences::1");
assertEq(newParams.getVolatilityReference(), volReference, "test_UpdateReferences::2");
Expand All @@ -312,12 +312,12 @@ contract PairParameterHelperTest is Test {

vm.warp(previousTime);

bytes32 params = bytes32(0).updateTimeOfLastUpdate();
bytes32 params = bytes32(0).updateTimeOfLastUpdate(block.timestamp);

vm.warp(time);

vm.expectRevert();
params.updateReferences();
params.updateReferences(block.timestamp);
}

function testFuzz_UpdateVolatilityParameters(
Expand All @@ -343,12 +343,12 @@ contract PairParameterHelperTest is Test {
sfp.variableFeeControl,
sfp.protocolShare,
sfp.maxVolatilityAccumulator
).updateTimeOfLastUpdate();
).updateTimeOfLastUpdate(block.timestamp);

vm.warp(time);

bytes32 trustedParams = params.updateReferences().updateVolatilityAccumulator(activeId);
bytes32 newParams = params.updateVolatilityParameters(activeId);
bytes32 trustedParams = params.updateReferences(block.timestamp).updateVolatilityAccumulator(activeId);
bytes32 newParams = params.updateVolatilityParameters(activeId, block.timestamp);

assertEq(newParams.getIdReference(), trustedParams.getIdReference(), "test_UpdateVolatilityParameters::1");
assertEq(
Expand Down

0 comments on commit 31e31f6

Please sign in to comment.