Skip to content
Verified contractPartial

JrnyHook

Address0x8cec12bc7ea6b92cd330b77e163fe5dbde88c0cc
Balance

0.073978 ETH

 

Transactions

18

 

Compiler

solc 0.8.26

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.26+commit.8a97fa7a
Language
solidity
Optimization
Yes, 200 runs
EVM version
cancun
License
Not stated
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
25,253,533
Creation transaction
0x857cd07677…3858f18ece

63 files

The source code is large: one file is shown at a time. Choose a file in the list.

src/JrnyHook.sol

pragma solidity ^0.8.26;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";

import {BaseHook} from "v4-periphery/src/utils/BaseHook.sol";
import {FeeManager} from "./utils/FeeManager.sol";
import {JrnyToken} from "./JrnyToken.sol";

import {BalanceDelta} from "v4-core/src/types/BalanceDelta.sol";

import {BeforeSwapDelta, toBeforeSwapDelta} from "v4-core/src/types/BeforeSwapDelta.sol";
import {Hooks} from "v4-core/src/libraries/Hooks.sol";
import {IPoolManager, SwapParams, ModifyLiquidityParams} from "v4-core/src/interfaces/IPoolManager.sol";
import {PoolKey} from "v4-core/src/types/PoolKey.sol";

contract JrnyHook is BaseHook, FeeManager {
    // ── Errors ────────────────────────────────────────────────
    error ProtocolFeeTooLarge();
    error ProtocolTokenNotSupported();

    // ── Variables ─────────────────────────────────────────────
    bytes32 private constant HOOK_STORAGE_LOCATION = keccak256(abi.encode(uint256(keccak256("JrnyHook.HookStorage")) - 1)) & ~bytes32(uint256(0xff));

    struct HookStorage {
        JrnyToken token;
    }

    // ── Initialization ────────────────────────────────────────
    constructor(
        IPoolManager _poolManager,
        address _tokenAddress,
        address _owner
    ) BaseHook(_poolManager) Ownable(_owner) {
        _getStorage().token = JrnyToken(_tokenAddress);
    }


    // ── External pure ─────────────────────────────────────────
    function getHookPermissions() public pure 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: true,
            afterSwapReturnDelta: true,
            afterAddLiquidityReturnDelta: false,
            afterRemoveLiquidityReturnDelta: false
        });
    }

    // ── Internal state-modifying ──────────────────────────────
    function _beforeSwap(
        address,
        PoolKey calldata key,
        SwapParams calldata params,
        bytes calldata
    ) internal override returns (bytes4, BeforeSwapDelta, uint24)
    {
        if(!key.currency0.isAddressZero()) {
            revert ProtocolTokenNotSupported();
        }

        (uint256 fee, bool isBuy, bool isSell, bool isExactIn, bool isExactOut) = _getContext(params.amountSpecified, params);

        BeforeSwapDelta returnDelta = toBeforeSwapDelta(0, 0); // No default delta balance change

        // If we are buying Token by paying ETH and we are providing the ETH value
        if (isBuy && isExactIn) {
            _autoDistributeFees(); // Autodistribute now, before reporting new gathered fee

            // Because we know the initial ETH value, we can just collect our fee here and the swap will proceed with X-fee ETH
            // in: ETH / out: Token

            poolManager.take(key.currency0, address(this), uint256(fee));
            _reportFeeGathered(fee);

            returnDelta = toBeforeSwapDelta(
                int128(uint128(fee)),
                0
            );
        }

        // If we are selling Token by receiving ETH and we are providing the ETH value
        if (isSell && isExactOut) {
            _autoDistributeFees(); // Autodistribute now, before reporting new gathered fee

            // Because we know the final ETH value, we can just specify the amount of ETH we are owed. This will inflate the price in Token required to do the swap.
            // in: Token / out: ETH

            poolManager.take(key.currency0, address(this), fee);
            _reportFeeGathered(fee);

            returnDelta = toBeforeSwapDelta(
                int128(uint128(fee)),
                0
            );
        }

        // We do not change LP (third parameter);
        return (BaseHook.beforeSwap.selector, returnDelta, 0);
    }

    function _afterSwap(
        address,
        PoolKey calldata key,
        SwapParams calldata params,
        BalanceDelta delta,
        bytes calldata
    ) internal override returns (bytes4, int128) {
        if(!key.currency0.isAddressZero()) {
            revert ProtocolTokenNotSupported();
        }

        // We will know if it is the amount of eth/Token based on whether is buy/sell and is exact in/out.
        (uint256 fee, bool isBuy, bool isSell, bool isExactIn, bool isExactOut) = _getContext(delta.amount0(), params);

        // If we are buying Token by paying ETH and we are providing the Token value
        if (isBuy && isExactOut) {
            _autoDistributeFees(); // Autodistribute now, before reporting new gathered fee

            // We just found out how much ETH we had to pay;
            poolManager.take(key.currency0, address(this), fee);
            _reportFeeGathered(fee);
            return (BaseHook.afterSwap.selector, int128(uint128(fee)));
        }

        // If we are selling Token by receiving ETH and we are providing the Token value
        if (isSell && isExactIn) {
            _autoDistributeFees(); // Autodistribute now, before reporting new gathered fee

            // We just found out how much ETH we will get for our exactly provided token:
            poolManager.take(key.currency0, address(this), fee);
            _reportFeeGathered(fee);
            return (BaseHook.afterSwap.selector, int128(uint128(fee)));
        }

        return (BaseHook.afterSwap.selector, int128(0));
    }

    // ── Internal view ─────────────────────────────────────────
    // @dev Access the namespace scoped storage
    function _getStorage() private pure returns (HookStorage storage $) {
        bytes32 slot = HOOK_STORAGE_LOCATION;
        assembly {
            $.slot := slot
        }
    }

    // @dev Computes the fee that is owed and translates the params to a readable form
    function _getContext(int256 _originalAmount, SwapParams calldata params) internal view returns (uint256 _fee, bool _isBuy, bool _isSell, bool _isExactIn, bool _isExactOut)  {
        uint256 _amount = _originalAmount < 0 ? uint256(- _originalAmount) : uint256(_originalAmount);

        // If this swap is buyToken (ETH->Token)
        _isBuy = params.zeroForOne;
        _isSell = !params.zeroForOne;

            // exactIn if negative and exactOut if positive.
        _isExactIn = (params.amountSpecified < 0);
        _isExactOut = (params.amountSpecified > 0);

        _fee = _amount * _compoundedFee(_isBuy) / 10000;

        if (_fee > ((uint256(1) << 127) - 1)) {
            revert ProtocolFeeTooLarge();
        }

        return (_fee, _isBuy, _isSell, _isExactIn, _isExactOut);
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "none",
    "useLiteralContent": false
  },
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "forge-std/=lib/forge-std/src/",
    "@uniswap/v4-core/=lib/uniswap-hooks/lib/v4-core/",
    "@uniswap/v4-periphery/=lib/uniswap-hooks/lib/v4-periphery/",
    "@uniswap/universal-router/=lib/universal-router/",
    "v4-core/=lib/uniswap-hooks/lib/v4-core/",
    "v4-periphery/=lib/uniswap-hooks/lib/v4-periphery/",
    "@openzeppelin/uniswap-hooks/=lib/uniswap-hooks/",
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "hookmate/=lib/hookmate/src/",
    "permit2/=lib/uniswap-hooks/lib/v4-periphery/lib/permit2/",
    "solmate/=lib/uniswap-hooks/lib/v4-core/lib/solmate/",
    "@ensdomains/=lib/uniswap-hooks/lib/v4-core/node_modules/@ensdomains/",
    "@uniswap/v2-core/=lib/universal-router/node_modules/@uniswap/v2-core/",
    "@uniswap/v3-core/=lib/universal-router/node_modules/@uniswap/v3-core/",
    "@uniswap/v3-periphery/=lib/universal-router/lib/v3-periphery/",
    "ds-test/=lib/universal-router/lib/solmate/lib/ds-test/src/",
    "erc4626-tests/=lib/openzeppelin-contracts-upgradeable/lib/erc4626-tests/",
    "forge-gas-snapshot/=lib/universal-router/lib/permit2/lib/forge-gas-snapshot/src/",
    "halmos-cheatcodes/=lib/openzeppelin-contracts-upgradeable/lib/halmos-cheatcodes/src/",
    "hardhat/=lib/uniswap-hooks/lib/v4-core/node_modules/hardhat/",
    "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "uniswap-hooks/=lib/uniswap-hooks/src/",
    "universal-router/=lib/universal-router/",
    "v3-periphery/=lib/universal-router/lib/v3-periphery/contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.