-
Notifications
You must be signed in to change notification settings - Fork 504
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
236 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; | ||
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol"; | ||
import {Owned} from "solmate/auth/Owned.sol"; | ||
import {FeeTaker} from "./FeeTaker.sol"; | ||
|
||
contract FeeTaking is FeeTaker, Owned { | ||
using SafeCast for uint256; | ||
|
||
uint128 private constant TOTAL_BIPS = 10000; | ||
uint128 public immutable swapFeeBips; | ||
address public treasury; | ||
|
||
constructor(IPoolManager _poolManager, uint128 _swapFeeBips, address _owner, address _treasury) | ||
FeeTaker(_poolManager) | ||
Owned(_owner) | ||
{ | ||
swapFeeBips = _swapFeeBips; | ||
treasury = _treasury; | ||
} | ||
|
||
function setTreasury(address _treasury) external onlyOwner { | ||
treasury = _treasury; | ||
} | ||
|
||
function _feeAmount(int128 amountUnspecified) internal view override returns (uint256) { | ||
return uint128(amountUnspecified) * swapFeeBips / TOTAL_BIPS; | ||
} | ||
|
||
function _recipient() internal view override returns (address) { | ||
return treasury; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.24; | ||
|
||
import {BaseHook} from "./../../contracts/BaseHook.sol"; | ||
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol"; | ||
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; | ||
import {PoolKey} from "@uniswap/v4-core/src/types/PoolKey.sol"; | ||
import {BalanceDelta} from "@uniswap/v4-core/src/types/BalanceDelta.sol"; | ||
import {Currency, CurrencyLibrary} from "@uniswap/v4-core/src/types/Currency.sol"; | ||
import {SafeCast} from "@uniswap/v4-core/src/libraries/SafeCast.sol"; | ||
import {BeforeSwapDelta, BeforeSwapDeltaLibrary} from "@uniswap/v4-core/src/types/BeforeSwapDelta.sol"; | ||
import {console} from "forge-std/console.sol"; | ||
import {TickMath} from "@uniswap/v4-core/src/libraries/TickMath.sol"; | ||
|
||
contract HooksFrontrun is BaseHook { | ||
using SafeCast for uint256; | ||
|
||
bytes internal constant ZERO_BYTES = bytes(""); | ||
uint160 public constant MIN_PRICE_LIMIT = TickMath.MIN_SQRT_PRICE + 1; | ||
uint160 public constant MAX_PRICE_LIMIT = TickMath.MAX_SQRT_PRICE - 1; | ||
|
||
BalanceDelta swapDelta; | ||
IPoolManager.SwapParams swapParams; | ||
|
||
constructor(IPoolManager _poolManager) BaseHook(_poolManager) {} | ||
|
||
function getHookPermissions() public pure virtual override returns (Hooks.Permissions memory) { | ||
return Hooks.Permissions({ | ||
beforeInitialize: false, | ||
afterInitialize: false, | ||
beforeAddLiquidity: false, | ||
afterAddLiquidity: false, | ||
beforeRemoveLiquidity: false, | ||
afterRemoveLiquidity: false, | ||
beforeSwap: true, | ||
afterSwap: true, | ||
beforeDonate: false, | ||
afterDonate: false, | ||
beforeSwapReturnDelta: false, | ||
afterSwapReturnDelta: false, | ||
afterAddLiquidityReturnDelta: false, | ||
afterRemoveLiquidityReturnDelta: false | ||
}); | ||
} | ||
|
||
function beforeSwap(address, PoolKey calldata key, IPoolManager.SwapParams calldata params, bytes calldata) | ||
external | ||
override | ||
onlyByManager | ||
returns (bytes4, BeforeSwapDelta, uint24) | ||
{ | ||
swapParams = params; | ||
console.log(params.zeroForOne); | ||
console.logInt(params.amountSpecified); | ||
swapDelta = manager.swap(key, params, ZERO_BYTES); | ||
console.log("beforeDelta"); | ||
console.logInt(swapDelta.amount0()); | ||
console.logInt(swapDelta.amount1()); | ||
return (BaseHook.beforeSwap.selector, BeforeSwapDeltaLibrary.ZERO_DELTA, 0); | ||
} | ||
|
||
function afterSwap( | ||
address sender, | ||
PoolKey calldata key, | ||
IPoolManager.SwapParams calldata params, | ||
BalanceDelta delta, | ||
bytes calldata hookData | ||
) external override onlyByManager returns (bytes4, int128) { | ||
BalanceDelta afterDelta = manager.swap( | ||
key, | ||
IPoolManager.SwapParams( | ||
!swapParams.zeroForOne, | ||
-swapParams.amountSpecified, | ||
swapParams.zeroForOne ? MAX_PRICE_LIMIT : MIN_PRICE_LIMIT | ||
), | ||
ZERO_BYTES | ||
); | ||
if (swapParams.zeroForOne) { | ||
int256 profit = afterDelta.amount0() + swapDelta.amount0(); | ||
if (profit > 0) { | ||
// else hook reverts | ||
manager.mint(address(this), key.currency0.toId(), uint256(profit)); | ||
} | ||
} else { | ||
int256 profit = afterDelta.amount1() + swapDelta.amount1(); | ||
if (profit > 0) { | ||
// else hook reverts | ||
manager.mint(address(this), key.currency1.toId(), uint256(profit)); | ||
} | ||
} | ||
console.log("afterDelta"); | ||
console.logInt(afterDelta.amount0()); | ||
console.logInt(afterDelta.amount1()); | ||
return (BaseHook.afterSwap.selector, 0); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/shared/implementation/HooksFrontrunImplementation.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
pragma solidity ^0.8.19; | ||
|
||
import {BaseHook} from "../../../contracts/BaseHook.sol"; | ||
import {HooksFrontrun} from "../../middleware/HooksFrontrun.sol"; | ||
import {IPoolManager} from "@uniswap/v4-core/src/interfaces/IPoolManager.sol"; | ||
import {Hooks} from "@uniswap/v4-core/src/libraries/Hooks.sol"; | ||
|
||
contract HooksFrontrunImplementation is HooksFrontrun { | ||
constructor(IPoolManager _poolManager, HooksFrontrun addressToEtch) HooksFrontrun(_poolManager) { | ||
Hooks.validateHookPermissions(addressToEtch, getHookPermissions()); | ||
} | ||
|
||
// make this a no-op in testing | ||
function validateHookAddress(BaseHook _this) internal pure override {} | ||
} |