Skip to content

Commit

Permalink
Bips library (#367)
Browse files Browse the repository at this point in the history
* update core and add bips library

* format

* tests

* format again
  • Loading branch information
dianakocsis authored Oct 16, 2024
1 parent 3819809 commit 74caaa6
Show file tree
Hide file tree
Showing 37 changed files with 103 additions and 35 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125574
125584
Original file line number Diff line number Diff line change
@@ -1 +1 @@
125021
125031
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132427
132446
Original file line number Diff line number Diff line change
@@ -1 +1 @@
131874
131893
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
146282
146294
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154848
154872
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154848
154872
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_collect_withTakePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
154169
154193
Original file line number Diff line number Diff line change
@@ -1 +1 @@
111971
111980
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119729
119753
Original file line number Diff line number Diff line change
@@ -1 +1 @@
119050
119074
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_burnEmpty.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
135224
135243
Original file line number Diff line number Diff line change
@@ -1 +1 @@
128371
128380
Original file line number Diff line number Diff line change
@@ -1 +1 @@
132416
132440
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_decrease_take_take.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
120305
120329
Original file line number Diff line number Diff line change
@@ -1 +1 @@
159033
159057
Original file line number Diff line number Diff line change
@@ -1 +1 @@
157973
157997
Original file line number Diff line number Diff line change
@@ -1 +1 @@
140860
140872
Original file line number Diff line number Diff line change
@@ -1 +1 @@
177340
177364
Original file line number Diff line number Diff line change
@@ -1 +1 @@
148016
148040
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_native.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
364721
364745
Original file line number Diff line number Diff line change
@@ -1 +1 @@
373244
373268
Original file line number Diff line number Diff line change
@@ -1 +1 @@
372467
372491
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickLower.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
317569
317617
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_onSameTickUpper.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
318239
318287
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_sameRange.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
243808
243856
Original file line number Diff line number Diff line change
@@ -1 +1 @@
418988
419060
Original file line number Diff line number Diff line change
@@ -1 +1 @@
323600
323648
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withClose.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
420122
420170
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_mint_withSettlePair.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
419180
419228
Original file line number Diff line number Diff line change
@@ -1 +1 @@
463927
455975
2 changes: 1 addition & 1 deletion .forge-snapshots/PositionManager_permit_twice.snap
Original file line number Diff line number Diff line change
@@ -1 +1 @@
44876
44852
2 changes: 1 addition & 1 deletion lib/v4-core
Submodule v4-core updated 46 files
+1 −1 .forge-snapshots/add liquidity to already existing position with salt.snap
+1 −1 .forge-snapshots/addLiquidity CA fee.snap
+1 −1 .forge-snapshots/addLiquidity with empty hook.snap
+1 −1 .forge-snapshots/addLiquidity with native token.snap
+1 −1 .forge-snapshots/create new liquidity to a position with salt.snap
+1 −1 .forge-snapshots/donate gas with 1 token.snap
+1 −1 .forge-snapshots/donate gas with 2 tokens.snap
+1 −1 .forge-snapshots/erc20 collect protocol fees.snap
+1 −1 .forge-snapshots/initialize.snap
+1 −1 .forge-snapshots/poolManager bytecode size.snap
+1 −1 .forge-snapshots/removeLiquidity CA fee.snap
+1 −1 .forge-snapshots/removeLiquidity with empty hook.snap
+1 −1 .forge-snapshots/removeLiquidity with native token.snap
+1 −1 .forge-snapshots/simple addLiquidity second addition same range.snap
+1 −1 .forge-snapshots/simple addLiquidity.snap
+1 −1 .forge-snapshots/simple removeLiquidity some liquidity remains.snap
+1 −1 .forge-snapshots/simple removeLiquidity.snap
+1 −1 .forge-snapshots/simple swap with native.snap
+1 −1 .forge-snapshots/simple swap.snap
+1 −1 .forge-snapshots/swap CA custom curve + swap noop.snap
+1 −1 .forge-snapshots/swap CA fee on unspecified.snap
+1 −1 .forge-snapshots/swap against liquidity with native token.snap
+1 −1 .forge-snapshots/swap against liquidity.snap
+1 −1 .forge-snapshots/swap burn 6909 for input.snap
+1 −1 .forge-snapshots/swap burn native 6909 for input.snap
+1 −1 .forge-snapshots/swap mint native output as 6909.snap
+1 −1 .forge-snapshots/swap mint output as 6909.snap
+1 −1 .forge-snapshots/swap skips hook call if hook is caller.snap
+1 −1 .forge-snapshots/swap with hooks.snap
+1 −1 .forge-snapshots/swap with return dynamic fee.snap
+1 −0 .github/CODEOWNERS
+0 −89 .github/workflows/coverage.yml
+3 −4 src/PoolManager.sol
+0 −42 src/ProtocolFees.sol
+1 −2 src/interfaces/IProtocolFees.sol
+0 −17 src/libraries/BipsLibrary.sol
+2 −2 src/libraries/Hooks.sol
+4 −7 src/libraries/Pool.sol
+0 −39 src/test/ProtocolFeeControllerTest.sol
+0 −4 src/test/ProtocolFeesImplementation.sol
+1 −2 src/test/ProxyPoolManager.sol
+0 −121 test/PoolManagerInitialize.t.sol
+1 −46 test/ProtocolFeesImplementation.t.sol
+0 −51 test/libraries/BipsLibrary.t.sol
+13 −13 test/libraries/Pool.t.sol
+1 −15 test/utils/Deployers.sol
2 changes: 1 addition & 1 deletion src/V4Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol";
import {Currency} from "@uniswap/v4-core/src/types/Currency.sol";
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol";
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol";
import {BipsLibrary} from "@uniswap/v4-core/src/libraries/BipsLibrary.sol";

import {PathKey, PathKeyLibrary} from "./libraries/PathKey.sol";
import {CalldataDecoder} from "./libraries/CalldataDecoder.sol";
Expand All @@ -16,6 +15,7 @@ import {BaseActionsRouter} from "./base/BaseActionsRouter.sol";
import {DeltaResolver} from "./base/DeltaResolver.sol";
import {Actions} from "./libraries/Actions.sol";
import {ActionConstants} from "./libraries/ActionConstants.sol";
import {BipsLibrary} from "./libraries/BipsLibrary.sol";

/// @title UniswapV4Router
/// @notice Abstract contract that contains all internal logic needed for routing through Uniswap v4 pools
Expand Down
17 changes: 17 additions & 0 deletions src/libraries/BipsLibrary.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity ^0.8.0;

/// @title For calculating a percentage of an amount, using bips
library BipsLibrary {
uint256 internal constant BPS_DENOMINATOR = 10_000;

/// @notice emitted when an invalid percentage is provided
error InvalidBips();

/// @param amount The total amount to calculate a percentage of
/// @param bips The percentage to calculate, in bips
function calculatePortion(uint256 amount, uint256 bips) internal pure returns (uint256) {
if (bips > BPS_DENOMINATOR) revert InvalidBips();
return (amount * bips) / BPS_DENOMINATOR;
}
}
51 changes: 51 additions & 0 deletions test/libraries/BipsLibrary.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "forge-std/Test.sol";
import {BipsLibrary} from "../../src/libraries/BipsLibrary.sol";

contract BipsLibraryTest is Test {
using BipsLibrary for uint256;

// The block gas limit set in foundry config is 300_000_000 (300M) for testing purposes
uint256 BLOCK_GAS_LIMIT;

function setUp() public {
BLOCK_GAS_LIMIT = block.gaslimit;
}

function test_fuzz_calculatePortion(uint256 amount, uint256 bips) public {
amount = bound(amount, 0, uint256(type(uint128).max));
if (bips > BipsLibrary.BPS_DENOMINATOR) {
vm.expectRevert(BipsLibrary.InvalidBips.selector);
amount.calculatePortion(bips);
} else {
assertEq(amount.calculatePortion(bips), amount * bips / BipsLibrary.BPS_DENOMINATOR);
}
}

function test_fuzz_gasLimit(uint256 bips) public {
if (bips > BipsLibrary.BPS_DENOMINATOR) {
vm.expectRevert(BipsLibrary.InvalidBips.selector);
block.gaslimit.calculatePortion(bips);
} else {
assertEq(block.gaslimit.calculatePortion(bips), BLOCK_GAS_LIMIT * bips / BipsLibrary.BPS_DENOMINATOR);
}
}

function test_gasLimit_100_percent() public view {
assertEq(block.gaslimit, block.gaslimit.calculatePortion(10_000));
}

function test_gasLimit_1_percent() public view {
// 100 bps = 1%
// 1% of 30M is 300K
assertEq(BLOCK_GAS_LIMIT / 100, block.gaslimit.calculatePortion(100));
}

function test_gasLimit_1BP() public view {
// 1bp is 0.01%
// 0.01% of 30M is 300
assertEq(BLOCK_GAS_LIMIT / 10000, block.gaslimit.calculatePortion(1));
}
}
2 changes: 1 addition & 1 deletion test/router/Payments.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ pragma solidity ^0.8.19;

import {GasSnapshot} from "forge-gas-snapshot/GasSnapshot.sol";
import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol";
import {BipsLibrary} from "@uniswap/v4-core/src/libraries/BipsLibrary.sol";

import {IV4Router} from "../../src/interfaces/IV4Router.sol";
import {RoutingTestHelpers} from "../shared/RoutingTestHelpers.sol";
import {Plan, Planner} from "../shared/Planner.sol";
import {Actions} from "../../src/libraries/Actions.sol";
import {ActionConstants} from "../../src/libraries/ActionConstants.sol";
import {BipsLibrary} from "../../src/libraries/BipsLibrary.sol";

contract PaymentsTests is RoutingTestHelpers, GasSnapshot {
using CurrencyLibrary for Currency;
Expand Down

0 comments on commit 74caaa6

Please sign in to comment.