Skip to content

Instantly share code, notes, and snippets.

@WelToHackerLand
Created October 23, 2022 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WelToHackerLand/012e44bb85420fb53eb0bbb7f0f13769 to your computer and use it in GitHub Desktop.
Save WelToHackerLand/012e44bb85420fb53eb0bbb7f0f13769 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;
import "./TestHelper.sol";
import "forge-std/console.sol";
contract High1Test is TestHelper {
// copy setUp from LBFactory.t.sol
function setUp() public {
token6D = new ERC20MockDecimals(6);
token12D = new ERC20MockDecimals(12);
token18D = new ERC20MockDecimals(18);
wavax = new WAVAX();
factory = new LBFactory(DEV, 8e14);
ILBPair _LBPairImplementation = new LBPair(factory);
factory.setLBPairImplementation(address(_LBPairImplementation));
addAllAssetsToQuoteWhitelist(factory);
setDefaultFactoryPresets(DEFAULT_BIN_STEP);
}
function testBugSetFeesParametersOnPair() public {
// Create pair with default fees
pair = createLBPairDefaultFees(token6D, token18D);
// feeParameters: binStep = DEFAULT_BIN_STEP, volatilityAccummulated = 0
assertEq(pair.feeParameters().binStep, DEFAULT_BIN_STEP);
assertEq(pair.feeParameters().volatilityAccumulated, 0);
console.log("DEFAULT_BIN_STEP", DEFAULT_BIN_STEP); // DEFAULT_BIN_STEP = 25 (contract TestHelper)
// Add liquidity to pair from ID_ONE, number of bins = 9, gap = 5
(uint256[] memory _ids, , , ) = addLiquidity(5e18, ID_ONE, 9, 5);
(,,uint256 activeId) = pair.getReservesAndId();
(uint256 binReserveX, uint256 binReserveY) = pair.getBin(uint24(activeId));
assertEq(binReserveY, 1e18); // reserveY in current bin = 1e18
// Mint token for ALICE
vm.startPrank(ALICE);
token6D.mint(ALICE, 10e18);
token18D.mint(ALICE, 10e18);
// Alice swap 1e18 + 10 token Y to token X
token18D.transfer(address(pair), 1e18 + 10);
pair.swap(false, ALICE);
// It passed 1 bin to swap then increased volatilityAccumulated
uint256 volatilityAccumulated = pair.feeParameters().volatilityAccumulated;
assertGt(volatilityAccumulated, 0);
console.log("volatilityAccumulated", volatilityAccumulated); // volatilityAccumulated = 60000
// Call setFeesParametersOnPair with binStep = DEFAULT_BIN_STEP (not change)
vm.stopPrank();
vm.startPrank(factory.owner());
factory.setFeesParametersOnPair(
token6D,
token18D,
DEFAULT_BIN_STEP,
DEFAULT_BASE_FACTOR - 1,
DEFAULT_FILTER_PERIOD - 1,
DEFAULT_DECAY_PERIOD - 1,
DEFAULT_REDUCTION_FACTOR - 1,
DEFAULT_VARIABLE_FEE_CONTROL - 1,
DEFAULT_PROTOCOL_SHARE - 1,
DEFAULT_MAX_VOLATILITY_ACCUMULATED - 1
);
// After setFeesParameters, new volatilityAccumulated of feeParameters is 0
// It will make the incorrect fee calculation for new exchange in pair (swap, flashloan)
uint256 newVolatilityAccumulated = pair.feeParameters().volatilityAccumulated;
assertEq(newVolatilityAccumulated, 0);
// Now binStep = or(uint16(volatilityAccumulated), DEFAULT_BIN_STEP) //new binStep
// binStep is different from which setter expected (DEFAULT_BIN_STEP)
// Notice volatilityAccumulated is old `feeParameters.volatilityAccumulated` of pair (before setFeesParametersOnPair)
uint16 binStep = pair.feeParameters().binStep;
assertEq(binStep, uint16(volatilityAccumulated) | DEFAULT_BIN_STEP);
console.log("binStep", binStep); //binStep = or(60000,25) = 60025
// When binStep > Constant.BASIS_POINT_MAX (In contract Constant.sol, BASIS_POINT_MAX = 10000)
// Then we can not swap anything in pair because it will revert `BinHelper__BinStepOverflows` in function BinHelper._getBPValue()
vm.stopPrank();
vm.startPrank(ALICE);
// ALICE try to swap 100 tokens Y and fail
token18D.transfer(address(pair), 100);
vm.expectRevert(abi.encodeWithSelector(BinHelper__BinStepOverflows.selector, binStep));
pair.swap(false, ALICE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment