PerpRewardDistributor

Inherits: RewardDistributor, IPerpRewardDistributor

Author: webthethird

Handles reward accrual and distribution for liquidity providers in Perpetual markets

State Variables

clearingHouse

Clearing House contract

IClearingHouse public immutable clearingHouse;

_earlyWithdrawalThreshold

Amount of time after which LPs can remove liquidity without penalties

uint256 internal _earlyWithdrawalThreshold;

_withdrawTimerStartByUserByMarket

Last timestamp when user changed their position in a market

First address is user, second is the market

mapping(address => mapping(address => uint256)) internal _withdrawTimerStartByUserByMarket;

Functions

onlyClearingHouse

Modifier for functions that can only be called by the ClearingHouse, i.e., updatePosition

modifier onlyClearingHouse();

constructor

PerpRewardDistributor constructor

constructor(
    uint88 _initialInflationRate,
    uint88 _initialReductionFactor,
    address _rewardToken,
    address _clearingHouse,
    address _ecosystemReserve,
    uint256 _earlyWithdrawThreshold,
    uint256[] memory _initialRewardWeights
) payable RewardDistributor(_ecosystemReserve);

Parameters

NameTypeDescription

_initialInflationRate

uint88

The initial inflation rate for the first reward token, scaled by 1e18

_initialReductionFactor

uint88

The initial reduction factor for the first reward token, scaled by 1e18

_rewardToken

address

The address of the first reward token

_clearingHouse

address

The address of the ClearingHouse contract, which calls updatePosition

_ecosystemReserve

address

The address of the EcosystemReserve contract, which stores reward tokens

_earlyWithdrawThreshold

uint256

The amount of time after which LPs can remove liquidity without penalties

_initialRewardWeights

uint256[]

The initial reward weights for the first reward token, as basis points

updatePosition

Accrues rewards and updates the stored LP position of a user and the total LP of a market

Only callable by the clearing house, executes whenever a user's liquidity is updated for any reason

function updatePosition(address market, address user) external virtual override onlyClearingHouse;

Parameters

NameTypeDescription

market

address

Address of the perpetual market

user

address

Address of the liquidity provier

earlyWithdrawalThreshold

Gets the number of seconds that a user must leave their liquidity in the market to avoid the early withdrawal penalty

function earlyWithdrawalThreshold() external view returns (uint256);

Returns

NameTypeDescription

<none>

uint256

Length of the early withdrawal period in seconds

withdrawTimerStartByUserByMarket

Start time of the user's early withdrawal timer for a specific market, i.e., when they last changed their position in the market

The user can withdraw their liquidity without penalty after withdrawTimerStartByUserByMarket(user, market) + earlyWithdrawalThreshold

function withdrawTimerStartByUserByMarket(address _user, address _market) external view returns (uint256);

Parameters

NameTypeDescription

_user

address

Address of the user

_market

address

Address of the market

Returns

NameTypeDescription

<none>

uint256

Timestamp when user last changed their position in the market

paused

Indicates whether claiming rewards is currently paused

Contract is paused if either this contract or the ClearingHouse has been paused

function paused() public view override returns (bool);

Returns

NameTypeDescription

<none>

bool

True if paused, false otherwise

setEarlyWithdrawalThreshold

Sets the number of seconds that a user must leave their liquidity in the market to avoid the early withdrawal penalty

Only callable by governance

function setEarlyWithdrawalThreshold(uint256 _newEarlyWithdrawalThreshold) external onlyRole(GOVERNANCE);

Parameters

NameTypeDescription

_newEarlyWithdrawalThreshold

uint256

New early withdrawal threshold in seconds

_getNumMarkets

Gets the number of markets to be used for reward distribution

Markets are the perpetual markets (for the PerpRewardDistributor) or staked tokens (for the SafetyModule)

function _getNumMarkets() internal view override returns (uint256);

Returns

NameTypeDescription

<none>

uint256

Number of markets

_getMarketAddress

Gets the address of a market at a given index

Markets are the perpetual markets (for the PerpRewardDistributor) or staked tokens (for the SafetyModule)

function _getMarketAddress(uint256 idx) internal view override returns (address);

Parameters

NameTypeDescription

idx

uint256

Index of the market

Returns

NameTypeDescription

<none>

address

Address of the market

_getMarketIdx

Gets the index of an allowlisted market

Markets are the perpetual markets (for the PerpRewardDistributor) or staked tokens (for the SafetyModule)

function _getMarketIdx(uint256 i) internal view override returns (uint256);

Parameters

NameTypeDescription

i

uint256

Index of the market in the allowlist ClearingHouse.ids (for the PerpRewardDistributor) or stakingTokens (for the SafetyModule)

Returns

NameTypeDescription

<none>

uint256

Index of the market in the market list

_getCurrentPosition

Returns the current position of the user in the market (i.e., perpetual market or staked token)

function _getCurrentPosition(address user, address market) internal view override returns (uint256);

Parameters

NameTypeDescription

user

address

Address of the user

market

address

Address of the market

Returns

NameTypeDescription

<none>

uint256

Current position of the user in the market

_accrueRewards

Accrues rewards to a user for a given market

Assumes LP position hasn't changed since last accrual, since updating rewards due to changes in LP position is handled by updatePosition

function _accrueRewards(address market, address user) internal virtual override;

Parameters

NameTypeDescription

market

address

Address of the market in ClearingHouse.perpetuals

user

address

Address of the user

Last updated