Skip to content
Verified contractPartialUNLICENSED

FarmKeeperMigrationLib

Address0xfb4b5d6d0ff3970faf127e299e852d29df1ef8e1
Balance

0 ETH

 

Transactions

0

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 600 runs
EVM version
cancun
License
UNLICENSED
Match
Partialcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,226,951
Creation transaction
0x318093af5e…22479ccac0
Libraries
src/libs/FarmKeeperMigrationLib.sol:V3OracleLib0xdd7d…506057src/libs/FarmKeeperMigrationLib.sol:V4OracleLib0x1838…4d990bsrc/libs/FarmKeeperMigrationLib.sol:FarmKeeperUniLib0xf4eb…ba89c7

122 files

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

src/libs/FarmKeeperMigrationLib.sol

// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.30;

// OpenZeppelin
import {TransientSlot} from "@openzeppelin/contracts/utils/TransientSlot.sol";

// Libraries
import {Farms, Farm, AccumulatedFees} from "@libs/Farms.sol";
import {FarmKeeperUniLib} from "@libs/FarmKeeperUniLib.sol";
import {FarmKeeperTickRangeLib} from "@libs/FarmKeeperTickRangeLib.sol";

// Core
import {FarmKeeper} from "@core/FarmKeeper.sol";

/// @title FarmKeeperMigrationLib
/// @notice Moves farm positions to a new tick range: collect fees, close the position at its current
///         range, store the new range, re-mint. Runs both for the first move off the usable extremes
///         and for any later change of a farm's range.
library FarmKeeperMigrationLib {
    using TransientSlot for *;
    using Farms for Farms.Map;

    bytes32 private constant LIQUIDITY_RECEIVABLE_SLOT =
        0x4fa854fc60ad780e589b7dfc92226667ab912d9f4f643cc82485c71526d29b00;

    error FarmKeeperMigrationLib_InvalidFarmId();
    error FarmKeeperMigrationLib_LengthMismatch();

    /// @notice The position-manager/pool-manager addresses the migration needs, grouped into one
    ///         struct purely to keep call-site stack depth down.
    struct MigrationAddresses {
        address v3PositionManager;
        address v4PositionManager;
        address v4PoolManager;
    }

    /// @notice Migrates several farms atomically in one call. See `FarmKeeper.migrateFarmTickRanges`.
    function migrateFarms(
        Farms.Map storage farms,
        mapping(bytes32 id => AccumulatedFees) storage accumulatedFees,
        bytes32[] calldata ids,
        FarmKeeperTickRangeLib.TickRange[] calldata ranges,
        uint48 deadline,
        address v3PositionManager,
        address v4PositionManager,
        address v4PoolManager
    ) external {
        MigrationAddresses memory addrs = MigrationAddresses({
            v3PositionManager: v3PositionManager, v4PositionManager: v4PositionManager, v4PoolManager: v4PoolManager
        });

        require(ranges.length == ids.length, FarmKeeperMigrationLib_LengthMismatch());
        for (uint256 i = 0; i < ids.length; i++) {
            require(farms.contains(ids[i]), FarmKeeperMigrationLib_InvalidFarmId());
            _migrateFarm(farms.get(ids[i]), accumulatedFees[ids[i]], ranges[i], deadline, addrs);
        }
    }

    function _migrateFarm(
        Farm storage farm,
        AccumulatedFees storage acc,
        FarmKeeperTickRangeLib.TickRange calldata requested,
        uint48 deadline,
        MigrationAddresses memory addrs
    ) private {
        FarmKeeperTickRangeLib.TickRange memory oldRange = FarmKeeperTickRangeLib.rangeOf(farm);
        FarmKeeperTickRangeLib.TickRange memory newRange = FarmKeeperTickRangeLib.resolveRange(
            farm.id, farm.poolKey, farm.farmType, farm.twapPeriod, requested, addrs.v4PoolManager
        );
        uint128 liquidityBefore = farm.lp.liquidity;

        // Before the close: otherwise uncollected fees come back with the principal, booked as
        // principal (V4 balance delta) or not at all (V3 `collect`), and skip the harvest split.
        FarmKeeperUniLib.collectFees(farm, acc, addrs.v3PositionManager, addrs.v4PositionManager);

        if (farm.lp.tokenId != 0) {
            if (farm.lp.liquidity > 0) {
                // Closes at `oldRange`: the decrease reads the range the position still has.
                _setLiquidityReceivable(true);
                FarmKeeperUniLib.decreaseLiquidity(
                    farm.farmType, farm, farm.lp.liquidity, deadline, addrs.v3PositionManager, addrs.v4PositionManager
                );
                _setLiquidityReceivable(false);
            }
            // Never burn or reuse the old NFT: anyone can add liquidity to a V3 position, so a burn
            // can be made to revert, and a reused V3 NFT keeps its old range. V4's full decrease
            // already burned it. The next add mints a fresh position at the new range.
            farm.lp.tokenId = 0;
        }

        // Stored before the re-mint: the mint, and every later liquidity operation, read it.
        FarmKeeperTickRangeLib.setRange(farm.id, newRange);

        // No swap happens here, so the re-mint is priced at the reference-checked spot.
        uint160 sqrtPriceX96 = FarmKeeperUniLib.checkedPreSwapSqrtPriceX96(farm, addrs.v4PoolManager);
        _setLiquidityReceivable(true);
        FarmKeeperUniLib.addLiquidity(farm, deadline, addrs.v3PositionManager, addrs.v4PositionManager, sqrtPriceX96);
        _setLiquidityReceivable(false);

        emit FarmKeeper.FarmKeeper_TickRangeMigrated(
            farm.id,
            oldRange.tickLower,
            oldRange.tickUpper,
            newRange.tickLower,
            newRange.tickUpper,
            liquidityBefore,
            farm.lp.liquidity
        );
    }

    function _setLiquidityReceivable(bool value) private {
        LIQUIDITY_RECEIVABLE_SLOT.asBoolean().tstore(value);
    }
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {
    "src/libs/FarmKeeperMigrationLib.sol": {
      "V3OracleLib": "0xdd7d80215b8607a9298a4202165cd296d0506057",
      "V4OracleLib": "0x1838a6169d85894e0b7e782c7af8ccf5a84d990b",
      "FarmKeeperUniLib": "0xf4ebbd3206c59bbf8ad53031875c96134fba89c7"
    }
  },
  "optimizer": {
    "runs": 600,
    "enabled": true
  },
  "evmVersion": "cancun",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
    "@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
    "forge-std/=lib/forge-std/src/",
    "@uniswap/v4-core/=lib/universal-router/lib/v4-periphery/lib/v4-core/",
    "@uniswap/v4-periphery/=lib/universal-router/lib/v4-periphery/",
    "@uniswap/v3-core/=lib/v3-core/",
    "@uniswap/v3-periphery/=lib/universal-router/lib/v3-periphery/",
    "@uniswap/universal-router/=lib/universal-router/",
    "permit2/=lib/universal-router/lib/permit2/",
    "@interfaces/=src/interfaces/",
    "@libs/=src/libs/",
    "@core/=src/",
    "@lens/=src/lens/",
    "@utils/=src/utils/",
    "@oracle/=src/oracle/",
    "@test/=test/",
    "@mocks/=test/mocks/",
    "@script/=script/",
    "@openzeppelin/uniswap-hooks/=lib/uniswap-hooks/",
    "@ensdomains/=lib/universal-router/lib/v4-periphery/lib/v4-core/node_modules/@ensdomains/",
    "@uniswap/v2-core/=lib/universal-router/node_modules/@uniswap/v2-core/",
    "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/universal-router/lib/v4-periphery/lib/v4-core/node_modules/hardhat/",
    "openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/",
    "solmate/=lib/universal-router/lib/solmate/",
    "uniswap-hooks/=lib/uniswap-hooks/src/",
    "universal-router/=lib/universal-router/",
    "v3-core/=lib/v3-core/",
    "v3-periphery/=lib/v3-periphery/contracts/",
    "v4-core/=lib/universal-router/lib/v4-periphery/lib/v4-core/src/",
    "v4-periphery/=lib/universal-router/lib/v4-periphery/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.