Skip to content

Commit

Permalink
issue 04: gas optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
0x0Louis committed Mar 22, 2023
1 parent 2b9f292 commit 7ab0ce8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 28 deletions.
37 changes: 18 additions & 19 deletions src/LBFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,12 @@ contract LBFactory is PendingOwnable, ILBFactory {
if (!_presets.contains(binStep)) revert LBFactory__BinStepHasNoPreset(binStep);

bytes32 preset = bytes32(_presets.get(binStep));
bool isOwner = msg.sender == owner();

if (!_isPresetOpen(preset) && msg.sender != owner()) {
revert LBFactory__FunctionIsLockedForUsers(msg.sender, binStep);
if (!_isPresetOpen(preset) && !isOwner) {
revert LBFactory__PresetIsLockedForUsers(msg.sender, binStep);
}

address implementation = _lbPairImplementation;

if (implementation == address(0)) revert LBFactory__ImplementationNotSet();

if (!_quoteAssetWhitelist.contains(address(tokenY))) revert LBFactory__QuoteAssetNotWhitelisted(tokenY);

if (tokenX == tokenY) revert LBFactory__IdenticalAddresses(tokenX);
Expand All @@ -354,13 +351,19 @@ contract LBFactory is PendingOwnable, ILBFactory {
revert LBFactory__LBPairAlreadyExists(tokenX, tokenY, binStep);
}

pair = ILBPair(
ImmutableClone.cloneDeterministic(
implementation,
abi.encodePacked(tokenX, tokenY, binStep),
keccak256(abi.encode(tokenA, tokenB, binStep))
)
);
{
address implementation = _lbPairImplementation;

if (implementation == address(0)) revert LBFactory__ImplementationNotSet();

pair = ILBPair(
ImmutableClone.cloneDeterministic(
implementation,
abi.encodePacked(tokenX, tokenY, binStep),
keccak256(abi.encode(tokenA, tokenB, binStep))
)
);
}

pair.initialize(
preset.getBaseFactor(),
Expand All @@ -373,12 +376,8 @@ contract LBFactory is PendingOwnable, ILBFactory {
activeId
);

_lbPairsInfo[tokenA][tokenB][binStep] = LBPairInformation({
binStep: binStep,
LBPair: pair,
createdByOwner: msg.sender == owner(),
ignoredForRouting: false
});
_lbPairsInfo[tokenA][tokenB][binStep] =
LBPairInformation({binStep: binStep, LBPair: pair, createdByOwner: isOwner, ignoredForRouting: false});

_allLBPairs.push(pair);
_availableLBPairBinSteps[tokenA][tokenB].add(binStep);
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces/ILBFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ interface ILBFactory is IPendingOwnable {
error LBFactory__LBPairNotCreated(IERC20 tokenX, IERC20 tokenY, uint256 binStep);
error LBFactory__FlashLoanFeeAboveMax(uint256 fees, uint256 maxFees);
error LBFactory__BinStepTooLow(uint256 binStep);
error LBFactory__FunctionIsLockedForUsers(address user, uint256 binStep);
error LBFactory__PresetIsLockedForUsers(address user, uint256 binStep);
error LBFactory__LBPairIgnoredIsAlreadyInTheSameState();
error LBFactory__BinStepHasNoPreset(uint256 binStep);
error LBFactory__PresetOpenStateIsAlreadyInTheSameState();
Expand Down
16 changes: 9 additions & 7 deletions test/LBFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ contract LiquidityBinFactoryTest is TestHelper {
LBFactory anotherFactory = new LBFactory(DEV, DEFAULT_FLASHLOAN_FEE);

anotherFactory.setPreset(1, 1, 1, 1, 1, 1, 1, 1, false);
anotherFactory.addQuoteAsset(usdc);

// Reverts if there is no implementation set
vm.expectRevert(ILBFactory.LBFactory__ImplementationNotSet.selector);
Expand Down Expand Up @@ -176,7 +177,7 @@ contract LiquidityBinFactoryTest is TestHelper {
// Users should not be able to create pairs by default
vm.prank(ALICE);
vm.expectRevert(
abi.encodeWithSelector(ILBFactory.LBFactory__FunctionIsLockedForUsers.selector, ALICE, DEFAULT_BIN_STEP)
abi.encodeWithSelector(ILBFactory.LBFactory__PresetIsLockedForUsers.selector, ALICE, DEFAULT_BIN_STEP)
);
factory.createLBPair(link, usdc, ID_ONE, DEFAULT_BIN_STEP);

Expand Down Expand Up @@ -211,7 +212,7 @@ contract LiquidityBinFactoryTest is TestHelper {

vm.prank(ALICE);
vm.expectRevert(
abi.encodeWithSelector(ILBFactory.LBFactory__FunctionIsLockedForUsers.selector, ALICE, DEFAULT_BIN_STEP)
abi.encodeWithSelector(ILBFactory.LBFactory__PresetIsLockedForUsers.selector, ALICE, DEFAULT_BIN_STEP)
);
factory.createLBPair(link, usdc, ID_ONE, DEFAULT_BIN_STEP);

Expand All @@ -222,7 +223,7 @@ contract LiquidityBinFactoryTest is TestHelper {
// Alice can't create a pair if the factory is locked
vm.prank(ALICE);
vm.expectRevert(
abi.encodeWithSelector(ILBFactory.LBFactory__FunctionIsLockedForUsers.selector, ALICE, DEFAULT_BIN_STEP)
abi.encodeWithSelector(ILBFactory.LBFactory__PresetIsLockedForUsers.selector, ALICE, DEFAULT_BIN_STEP)
);
factory.createLBPair(usdt, usdc, ID_ONE, DEFAULT_BIN_STEP);

Expand All @@ -245,11 +246,7 @@ contract LiquidityBinFactoryTest is TestHelper {
DEFAULT_OPEN_STATE
);

vm.expectRevert(abi.encodeWithSelector(ILBFactory.LBFactory__ImplementationNotSet.selector));
newFactory.createLBPair(usdt, usdc, ID_ONE, DEFAULT_BIN_STEP);

// Can't create pair if the quote asset is not whitelisted
newFactory.setLBPairImplementation(address(new LBPair(newFactory)));
vm.expectRevert(abi.encodeWithSelector(ILBFactory.LBFactory__QuoteAssetNotWhitelisted.selector, usdc));
newFactory.createLBPair(usdt, usdc, ID_ONE, DEFAULT_BIN_STEP);

Expand All @@ -262,6 +259,11 @@ contract LiquidityBinFactoryTest is TestHelper {
vm.expectRevert(abi.encodeWithSelector(ILBFactory.LBFactory__AddressZero.selector));
newFactory.createLBPair(IERC20(address(0)), usdc, ID_ONE, DEFAULT_BIN_STEP);

// Can't create a pair if the implementation is not set
vm.expectRevert(abi.encodeWithSelector(ILBFactory.LBFactory__ImplementationNotSet.selector));
newFactory.createLBPair(usdt, usdc, ID_ONE, DEFAULT_BIN_STEP);

newFactory.setLBPairImplementation(address(new LBPair(newFactory)));
// Can't create the same pair twice (a revision should be created instead)
newFactory.createLBPair(usdt, usdc, ID_ONE, DEFAULT_BIN_STEP);
vm.expectRevert(
Expand Down
2 changes: 1 addition & 1 deletion test/LBRouter.Liquidity.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ contract LiquidityBinRouterTest is TestHelper {

vm.expectRevert(
abi.encodeWithSelector(
ILBFactory.LBFactory__FunctionIsLockedForUsers.selector, address(router), DEFAULT_BIN_STEP
ILBFactory.LBFactory__PresetIsLockedForUsers.selector, address(router), DEFAULT_BIN_STEP
)
);
router.createLBPair(bnb, usdc, ID_ONE, DEFAULT_BIN_STEP);
Expand Down

0 comments on commit 7ab0ce8

Please sign in to comment.