Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New range parameters #5

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/LBHooksLens.sol
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ contract LBHooksLens {
parameters.rewardToken = getRewardToken(hooks);
(parameters.rangeStart, parameters.rangeEnd) = getRewardedRange(hooks);

if (parameters.hooksType == ILBHooksManager.LBHooksType.MCRewarder) {
if (uint8(parameters.hooksType) % 3 == 1) {
parameters.pid = getPid(hooks);
parameters.moePerSecond = getMoePerSecond(parameters.pid);
} else {
Expand Down
40 changes: 25 additions & 15 deletions src/LBHooksManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,28 @@ contract LBHooksManager is Ownable2StepUpgradeable, ILBHooksManager {
* @dev Creates a new LB Hooks Rewarder
* This will also try to set the LB Hooks parameters on the pair
* Only callable by the owner
* @param lbHooksType The LB Hooks type
* @param tokenX The address of the token X
* @param tokenY The address of the token Y
* @param binStep The bin step
* @param initialOwner The address of the initial owner
* @return rewarder The address of the LB Hooks Rewarder
*/
function createLBHooksMCRewarder(IERC20 tokenX, IERC20 tokenY, uint16 binStep, address initialOwner)
external
override
onlyOwner
returns (address rewarder)
{
(ILBPair lbPair, bytes32 hooksParameters) =
_getLBPairAndHooksParameters(LBHooksType.MCRewarder, tokenX, tokenY, binStep);
function createLBHooksMCRewarder(
LBHooksType lbHooksType,
IERC20 tokenX,
IERC20 tokenY,
uint16 binStep,
address initialOwner
) external override onlyOwner returns (address rewarder) {
if (uint8(lbHooksType) % 3 != 1) revert LBHooksManager__InvalidLBHooksType();

(ILBPair lbPair, bytes32 hooksParameters) = _getLBPairAndHooksParameters(lbHooksType, tokenX, tokenY, binStep);

uint256 pid = _masterChef.getNumberOfFarms();
bytes memory immutableData = abi.encodePacked(lbPair, pid);

rewarder = _cloneHooks(LBHooksType.MCRewarder, Hooks.getHooks(hooksParameters), immutableData);
rewarder = _cloneHooks(lbHooksType, Hooks.getHooks(hooksParameters), immutableData);

_masterChef.add(IERC20(rewarder), IMasterChefRewarder(address(0)));

Expand All @@ -140,6 +143,7 @@ contract LBHooksManager is Ownable2StepUpgradeable, ILBHooksManager {
/**
* @dev Creates a new LB Hooks Simple Rewarder
* Only callable by the owner
* @param lbHooksType The LB Hooks type
* @param tokenX The address of the token X
* @param tokenY The address of the token Y
* @param binStep The bin step
Expand All @@ -148,20 +152,24 @@ contract LBHooksManager is Ownable2StepUpgradeable, ILBHooksManager {
* @return rewarder The address of the LB Hooks Simple Rewarder
*/
function createLBHooksSimpleRewarder(
LBHooksType lbHooksType,
IERC20 tokenX,
IERC20 tokenY,
uint16 binStep,
IERC20 rewardToken,
address initialOwner
) external override onlyOwner returns (address rewarder) {
(ILBPair lbPair, bytes32 hooksParameters) =
_getLBPairAndHooksParameters(LBHooksType.SimpleRewarder, tokenX, tokenY, binStep);
if (lbHooksType == LBHooksType.Invalid || uint8(lbHooksType) % 3 != 0) {
revert LBHooksManager__InvalidLBHooksType();
}

(ILBPair lbPair, bytes32 hooksParameters) = _getLBPairAndHooksParameters(lbHooksType, tokenX, tokenY, binStep);

address lbHooksAddress = Hooks.getHooks(lbPair.getLBHooksParameters());

bytes memory immutableData = abi.encodePacked(lbPair, rewardToken, lbHooksAddress);

rewarder = _cloneHooks(LBHooksType.SimpleRewarder, Hooks.getHooks(hooksParameters), immutableData);
rewarder = _cloneHooks(lbHooksType, Hooks.getHooks(hooksParameters), immutableData);

_lbFactory.setLBHooksParametersOnPair(
tokenX, tokenY, binStep, Hooks.setHooks(hooksParameters, rewarder), abi.encode(initialOwner)
Expand All @@ -180,22 +188,24 @@ contract LBHooksManager is Ownable2StepUpgradeable, ILBHooksManager {
* @return extraRewarder The address of the LB Hooks Extra Rewarder
*/
function createLBHooksExtraRewarder(
LBHooksType lbHooksType,
IERC20 tokenX,
IERC20 tokenY,
uint16 binStep,
IERC20 rewardToken,
address initialOwner
) external override onlyOwner returns (address extraRewarder) {
(ILBPair lbPair, bytes32 hooksParameters) =
_getLBPairAndHooksParameters(LBHooksType.ExtraRewarder, tokenX, tokenY, binStep);
if (uint8(lbHooksType) % 3 != 2) revert LBHooksManager__InvalidLBHooksType();

(ILBPair lbPair, bytes32 hooksParameters) = _getLBPairAndHooksParameters(lbHooksType, tokenX, tokenY, binStep);

address lbHooksAddress = Hooks.getHooks(lbPair.getLBHooksParameters());

if (lbHooksAddress == address(0)) revert LBHooksManager__LBHooksNotSetOnPair();

bytes memory immutableData = abi.encodePacked(lbPair, rewardToken, lbHooksAddress);

extraRewarder = _cloneHooks(LBHooksType.ExtraRewarder, Hooks.getHooks(hooksParameters), immutableData);
extraRewarder = _cloneHooks(lbHooksType, Hooks.getHooks(hooksParameters), immutableData);

ILBHooksBaseParentRewarder(lbHooksAddress).setLBHooksExtraRewarder(extraRewarder, abi.encode(initialOwner));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pragma solidity ^0.8.20;
import {Hooks} from "@lb-protocol/src/libraries/Hooks.sol";

import {LBHooksBaseRewarder, ILBHooksBaseRewarder} from "./LBHooksBaseRewarder.sol";
import {ILBHooksBaseParentRewarder} from "./interfaces/ILBHooksBaseParentRewarder.sol";
import {ILBHooksExtraRewarder} from "./interfaces/ILBHooksExtraRewarder.sol";
import {ILBHooksBaseParentRewarder} from "../interfaces/ILBHooksBaseParentRewarder.sol";
import {ILBHooksExtraRewarder} from "../interfaces/ILBHooksExtraRewarder.sol";

/**
* @title LB Hooks Base Parent Rewarder
Expand Down
69 changes: 9 additions & 60 deletions src/LBHooksBaseRewarder.sol → src/base/LBHooksBaseRewarder.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {
Ownable2StepUpgradeable,
OwnableUpgradeable
} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";
import {LBBaseHooks} from "@lb-protocol/src/LBBaseHooks.sol";
import {Uint256x256Math} from "@lb-protocol/src/libraries/math/Uint256x256Math.sol";
import {Clone} from "@lb-protocol/src/libraries/Clone.sol";
Expand All @@ -14,29 +10,27 @@ import {BinHelper} from "@lb-protocol/src/libraries/BinHelper.sol";
import {Hooks} from "@lb-protocol/src/libraries/Hooks.sol";
import {SafeCast} from "@lb-protocol/src/libraries/math/SafeCast.sol";

import {ILBHooksBaseRewarder} from "./interfaces/ILBHooksBaseRewarder.sol";
import {TokenHelper, IERC20} from "./library/TokenHelper.sol";
import {ILBHooksBaseRewarder} from "../interfaces/ILBHooksBaseRewarder.sol";
import {TokenHelper, IERC20} from "../library/TokenHelper.sol";
import {LBHooksRewarderVirtual} from "./LBHooksRewarderVirtual.sol";

/**
* @title LB Hooks Base Rewarder
* @dev Base contract for any LB Hooks Rewarder
*/
abstract contract LBHooksBaseRewarder is LBBaseHooks, Ownable2StepUpgradeable, Clone, ILBHooksBaseRewarder {
abstract contract LBHooksBaseRewarder is LBBaseHooks, LBHooksRewarderVirtual, Clone, ILBHooksBaseRewarder {
using Uint256x256Math for uint256;
using SafeCast for uint256;

address public immutable implementation;

int256 internal constant MAX_NUMBER_OF_BINS = 11;
uint256 internal constant MAX_NUMBER_OF_BINS = 11;
uint8 internal constant OFFSET_PRECISION = 128;
bytes32 internal constant FLAGS =
Hooks.BEFORE_SWAP_FLAG | Hooks.BEFORE_MINT_FLAG | Hooks.BEFORE_BURN_FLAG | Hooks.BEFORE_TRANSFER_FLAG;

address internal immutable _lbHooksManager;

int24 internal _deltaBinA;
int24 internal _deltaBinB;

uint256 internal _totalUnclaimedRewards;

mapping(uint256 => Bin) internal _bins;
Expand Down Expand Up @@ -177,25 +171,6 @@ abstract contract LBHooksBaseRewarder is LBBaseHooks, Ownable2StepUpgradeable, C
_claim(user, ids, _unclaimedRewards[user]);
}

/**
* @dev Sets the delta bins
* The delta bins are used to determine the range of bins to be rewarded,
* from [activeId + deltaBinA, activeId + deltaBinB[ (exclusive).
* @param deltaBinA The delta bin A
* @param deltaBinB The delta bin B
*/
function setDeltaBins(int24 deltaBinA, int24 deltaBinB) external virtual override onlyOwner {
if (deltaBinA > deltaBinB) revert LBHooksBaseRewarder__InvalidDeltaBins();
if (int256(deltaBinB) - deltaBinA > MAX_NUMBER_OF_BINS) revert LBHooksBaseRewarder__ExceedsMaxNumberOfBins();

_updateAccruedRewardsPerShare();

_deltaBinA = deltaBinA;
_deltaBinB = deltaBinB;

emit DeltaBinsSet(deltaBinA, deltaBinB);
}

/**
* @dev Sweeps the given token to the given address
* @param token The address of the token
Expand Down Expand Up @@ -238,15 +213,14 @@ abstract contract LBHooksBaseRewarder is LBBaseHooks, Ownable2StepUpgradeable, C
internal
view
virtual
override
returns (uint256[] memory rewardedIds, uint24 activeId, uint256 binStart, uint256 binEnd)
{
activeId = _getLBPair().getActiveId();
(int24 deltaBinA, int24 deltaBinB) = (_deltaBinA, _deltaBinB);

binStart = uint256(int256(uint256(activeId)) + deltaBinA);
binEnd = uint256(int256(uint256(activeId)) + deltaBinB);

(binStart, binEnd) = _getRewardedBounds(activeId);
if (binStart > type(uint24).max || binEnd > type(uint24).max) revert LBHooksBaseRewarder__Overflow();
if (binEnd - binStart > MAX_NUMBER_OF_BINS) revert LBHooksBaseRewarder__ExceedsMaxNumberOfBins();

uint256 length = binEnd - binStart;
rewardedIds = new uint256[](length);
Expand Down Expand Up @@ -326,7 +300,7 @@ abstract contract LBHooksBaseRewarder is LBBaseHooks, Ownable2StepUpgradeable, C
/**
* @dev Internal function to update the accrued rewards per share
*/
function _updateAccruedRewardsPerShare() internal virtual {
function _updateAccruedRewardsPerShare() internal virtual override {
uint256 pendingTotalRewards = _updateRewards();

if (pendingTotalRewards == 0) return;
Expand Down Expand Up @@ -480,29 +454,4 @@ abstract contract LBHooksBaseRewarder is LBBaseHooks, Ownable2StepUpgradeable, C
_updateUser(from, ids);
_updateUser(to, ids);
}

/**
* @dev Internal function that can be overriden to add custom logic when the rewarder is set
* @param data The data used to initialize the rewarder
*/
function _onHooksSet(bytes calldata data) internal virtual {}

/**
* @dev Internal function that can be overriden to add custom logic when the rewards are claimed
* @param user The address of the user
* @param ids The ids of the bins
*/
function _onClaim(address user, uint256[] memory ids) internal virtual {}

/**
* @dev Internal function that **MUST** be overriden to return the total pending rewards
* @return pendingTotalRewards The total pending rewards
*/
function _getPendingTotalRewards() internal view virtual returns (uint256 pendingTotalRewards);

/**
* @dev Internal function that **MUST** be overriden to update and return the total pending rewards
* @return pendingTotalRewards The total pending rewards
*/
function _updateRewards() internal virtual returns (uint256 pendingTotalRewards);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
pragma solidity ^0.8.20;

import {LBHooksBaseRewarder, Hooks} from "./LBHooksBaseRewarder.sol";
import {ILBHooksBaseSimpleRewarder} from "./interfaces/ILBHooksBaseSimpleRewarder.sol";
import {ILBHooksBaseSimpleRewarder} from "../interfaces/ILBHooksBaseSimpleRewarder.sol";

import {TokenHelper} from "./library/TokenHelper.sol";
import {TokenHelper} from "../library/TokenHelper.sol";

/**
* @title LB Hooks Base Simple Rewarder
Expand Down
62 changes: 62 additions & 0 deletions src/base/LBHooksRewarderVirtual.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Ownable2StepUpgradeable} from "@openzeppelin/contracts-upgradeable/access/Ownable2StepUpgradeable.sol";

/**
* @title LB Hooks Rewarder Virtual
* @dev Abstract contract for the LB Hooks Rewarders, it contains the important functions that the rewarders should implement
*/
abstract contract LBHooksRewarderVirtual is Ownable2StepUpgradeable {
/**
* @dev Internal function that can be overriden to add custom logic when the rewarder is set
* @param data The data used to initialize the rewarder
*/
function _onHooksSet(bytes calldata data) internal virtual;

/**
* @dev Internal function that can be overriden to add custom logic when the rewards are claimed
* @param user The address of the user
* @param ids The ids of the bins
*/
function _onClaim(address user, uint256[] memory ids) internal virtual;

/**
* @dev Internal function that **MUST** be overriden to return the total pending rewards
* @return pendingTotalRewards The total pending rewards
*/
function _getPendingTotalRewards() internal view virtual returns (uint256 pendingTotalRewards);

/**
* @dev Internal function that **MUST** be overriden to return the rewarded start and end id (exclusive)
* @param activeId The active id
* @return binStart The bin start to be rewarded
* @return binEnd The bin end to be rewarded, exclusive
*/
function _getRewardedBounds(uint24 activeId) internal view virtual returns (uint256 binStart, uint256 binEnd);

/**
* @dev Internal helper function that **MUST** be overriden to return the rewarded range
* @return rewardedIds The list of the rewarded ids from binStart to binEnd
* @return activeId The active id
* @return binStart The bin start to be rewarded
* @return binEnd The bin end to be rewarded
*/
function _getRewardedRange()
internal
view
virtual
returns (uint256[] memory rewardedIds, uint24 activeId, uint256 binStart, uint256 binEnd);

/**
* @dev Internal function that **MUST** be overriden to update and return the total pending rewards
* @return pendingTotalRewards The total pending rewards
*/
function _updateRewards() internal virtual returns (uint256 pendingTotalRewards);

/**
* @dev Internal function that **MUST** be overriden to update the accrued rewards per share
* @dev Internal function to update the accrued rewards per share
*/
function _updateAccruedRewardsPerShare() internal virtual;
}
66 changes: 66 additions & 0 deletions src/delta/LBHooksDelta.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "../base/LBHooksRewarderVirtual.sol";

/**
* @title LB Hooks Delta Rewarder
* @dev Abstract contract for the LB Hooks Delta Rewarder
* This contract allows to distribute rewards to LPs of the [activeId + deltaBinA, activeId + deltaBinB[ bins
*/
abstract contract LBHooksDelta is LBHooksRewarderVirtual {
error LBHooksDelta__InvalidDeltaBins();

event DeltaBinsSet(int24 deltaBinA, int24 deltaBinB);

int24 internal _deltaBinA;
int24 internal _deltaBinB;

/**
* @dev Returns the delta bins
* @return deltaBinA The delta bin A
* @return deltaBinB The delta bin B
*/
function getParameters() external view returns (int24 deltaBinA, int24 deltaBinB) {
return (_deltaBinA, _deltaBinB);
}

/**
* @dev Sets the delta bins
* The delta bins are used to determine the range of bins to be rewarded,
* from [activeId + deltaBinA, activeId + deltaBinB[ (exclusive).
* @param deltaBinA The delta bin A
* @param deltaBinB The delta bin B
*/
function setDeltaBins(int24 deltaBinA, int24 deltaBinB) external virtual onlyOwner {
if (deltaBinA > deltaBinB) revert LBHooksDelta__InvalidDeltaBins();

_updateAccruedRewardsPerShare();

_deltaBinA = deltaBinA;
_deltaBinB = deltaBinB;

_getRewardedRange(); // Make sure that the constraints are respected

emit DeltaBinsSet(deltaBinA, deltaBinB);
}

/**
* @dev Returns the rewarded start and end id (exclusive)
* @param activeId The active id
* @return binStart The bin start to be rewarded
* @return binEnd The bin end to be rewarded, exclusive
*/
function _getRewardedBounds(uint24 activeId)
internal
view
virtual
override
returns (uint256 binStart, uint256 binEnd)
{
(int24 deltaBinA, int24 deltaBinB) = (_deltaBinA, _deltaBinB);

binStart = uint256(int256(uint256(activeId)) + deltaBinA);
binEnd = uint256(int256(uint256(activeId)) + deltaBinB);
}
}
Loading