Skip to content

Commit

Permalink
Bug: Require different currencies (#380)
Browse files Browse the repository at this point in the history
* Require different currencies

* hardhat snapshots

* Add new custom type function

* remove extra paren
  • Loading branch information
hensha256 authored and zhongeric committed Dec 14, 2023
1 parent 9875e9f commit 81e2f1f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .forge-snapshots/initialize.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
37964
38123
2 changes: 1 addition & 1 deletion contracts/PoolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ contract PoolManager is IPoolManager, Fees, NoDelegateCall, ERC6909 {
// see TickBitmap.sol for overflow conditions that can arise from tick spacing being too large
if (key.tickSpacing > MAX_TICK_SPACING) revert TickSpacingTooLarge();
if (key.tickSpacing < MIN_TICK_SPACING) revert TickSpacingTooSmall();
if (key.currency0 > key.currency1) revert CurrenciesInitializedOutOfOrder();
if (key.currency0 >= key.currency1) revert CurrenciesInitializedOutOfOrder();
if (!key.hooks.isValidHookAddress(key.fee)) revert Hooks.HookAddressNotValid(address(key.hooks));

if (key.hooks.shouldCallBeforeInitialize()) {
Expand Down
6 changes: 5 additions & 1 deletion contracts/types/Currency.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {IERC20Minimal} from "../interfaces/external/IERC20Minimal.sol";

type Currency is address;

using {greaterThan as >, lessThan as <, equals as ==} for Currency global;
using {greaterThan as >, lessThan as <, greaterThanOrEqualTo as >=, equals as ==} for Currency global;

function equals(Currency currency, Currency other) pure returns (bool) {
return Currency.unwrap(currency) == Currency.unwrap(other);
Expand All @@ -19,6 +19,10 @@ function lessThan(Currency currency, Currency other) pure returns (bool) {
return Currency.unwrap(currency) < Currency.unwrap(other);
}

function greaterThanOrEqualTo(Currency currency, Currency other) pure returns (bool) {
return Currency.unwrap(currency) >= Currency.unwrap(other);
}

/// @title CurrencyLibrary
/// @dev This library allows for transferring and holding native tokens and ERC20 tokens
library CurrencyLibrary {
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/PoolManager.gas.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
exports[`PoolManager gas tests ERC20 tokens #initialize initialize pool with no hooks and no protocol fee 1`] = `
Object {
"calldataByteLength": 292,
"gasUsed": 55267,
"gasUsed": 55264,
}
`;

Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/PoolManager.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`PoolManager bytecode size 1`] = `29268`;
exports[`PoolManager bytecode size 1`] = `29267`;
13 changes: 13 additions & 0 deletions test/foundry-tests/PoolManager.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,19 @@ contract PoolManagerTest is Test, Deployers, TokenFixture, GasSnapshot {
assertEq(slot0.sqrtPriceX96, sqrtPriceX96);
}

function testPoolManagerInitializeRevertsWithIdenticalTokens(uint160 sqrtPriceX96) public {
// Assumptions tested in Pool.t.sol
vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO);
vm.assume(sqrtPriceX96 < TickMath.MAX_SQRT_RATIO);

// Both currencies are currency0
PoolKey memory key =
PoolKey({currency0: currency0, currency1: currency0, fee: 3000, hooks: IHooks(address(0)), tickSpacing: 60});

vm.expectRevert(IPoolManager.CurrenciesInitializedOutOfOrder.selector);
manager.initialize(key, sqrtPriceX96, ZERO_BYTES);
}

function testPoolManagerInitializeRevertsWithSameTokenCombo(uint160 sqrtPriceX96) public {
// Assumptions tested in Pool.t.sol
vm.assume(sqrtPriceX96 >= TickMath.MIN_SQRT_RATIO);
Expand Down

0 comments on commit 81e2f1f

Please sign in to comment.