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

feat(PRT): Add Prt and PrtFeeSplitExtension #176

Merged
merged 19 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion contracts/adapters/FeeSplitExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ contract FeeSplitExtension is BaseExtension, TimeLockUpgrade, MutualUpgrade {
* will automatically be sent to this address so reading the balance of the SetToken in the contract after accrual is
* sufficient for accounting for all collected fees.
*/
function accrueFeesAndDistribute() public {
function accrueFeesAndDistribute() public virtual {
// Emits a FeeActualized event
streamingFeeModule.accrueFee(setToken);

Expand Down Expand Up @@ -260,6 +260,7 @@ contract FeeSplitExtension is BaseExtension, TimeLockUpgrade, MutualUpgrade {
*/
function updateFeeSplit(uint256 _newFeeSplit)
external
virtual
mutualUpgrade(manager.operator(), manager.methodologist())
{
require(_newFeeSplit <= PreciseUnitMath.preciseUnit(), "Fee must be less than 100%");
Expand Down
224 changes: 224 additions & 0 deletions contracts/adapters/PrtFeeSplitExtension.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
/*
Copyright 2024 Index Cooperative

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

SPDX-License-Identifier: Apache License, Version 2.0
*/

pragma solidity 0.6.10;
pragma experimental ABIEncoderV2;

import { Address } from "@openzeppelin/contracts/utils/Address.sol";
import { SafeMath } from "@openzeppelin/contracts/math/SafeMath.sol";

import { FeeSplitExtension } from "./FeeSplitExtension.sol";
import { IBaseManager } from "../interfaces/IBaseManager.sol";
import { IIssuanceModule } from "../interfaces/IIssuanceModule.sol";
import { IPrt } from "../interfaces/IPrt.sol";
import { IPrtStakingPool } from "../interfaces/IPrtStakingPool.sol";
import { IStreamingFeeModule } from "../interfaces/IStreamingFeeModule.sol";
import { PreciseUnitMath } from "../lib/PreciseUnitMath.sol";

/**
* @title PrtFeeSplitExtension
* @dev Extension that allows for splitting and setting streaming and mint/redeem fees with a
* PRT Staking Pool. The operator can accrue fees from the streaming fee module and distribute
* them to the operator and the PRT Staking Pool, snapshotting the PRT Staking Pool. The operator
* can update the PRT staking pool address and the fee split between the operator and the
* PRT staking pool. Includes an optional allow list and timelock on accrue function.
*/
contract PrtFeeSplitExtension is FeeSplitExtension {
using Address for address;
using PreciseUnitMath for uint256;
using SafeMath for uint256;

/* ============ Events ============ */

event AnyoneAccrueUpdated(bool isAnyoneAllowedToBid);
event AccruerStatusUpdated(address indexed accruer, bool isBidderAllowed);
ckoopmann marked this conversation as resolved.
Show resolved Hide resolved
event OperatorFeeSplitUpdated(uint256 newFeeSplit);
event PrtFeesDistributed(
address indexed operatorFeeRecipient,
address indexed prtStakingPool,
uint256 operatorTake,
uint256 prtTake
);
event PrtStakingPoolUpdated(address newPrtStakingPool);

/* ============ Immutables ============ */

IPrt public immutable prt;

/* ============ State Variables ============ */

bool public isAnyoneAllowedToAccrue;
address[] accruersHistory;
mapping(address => bool) accrueAllowList;
ckoopmann marked this conversation as resolved.
Show resolved Hide resolved
IPrtStakingPool public prtStakingPool;

/* ============ Modifiers ============ */

modifier onlyAllowedAccruer() {
require(_isAllowedAccruer(msg.sender), "Not allowed to accrue");
_;
}

/* ============ Constructor ============ */

constructor(
IBaseManager _manager,
IStreamingFeeModule _streamingFeeModule,
IIssuanceModule _issuanceModule,
uint256 _operatorFeeSplit,
address _operatorFeeRecipient,
IPrt _prt
)
public
FeeSplitExtension(
_manager,
_streamingFeeModule,
_issuanceModule,
_operatorFeeSplit,
_operatorFeeRecipient
)
{
require(_prt.setToken() == address(manager.setToken()), "SetToken mismatch with Prt");
prt = _prt;
}

/* ============ External Functions ============ */

/**
* @notice MUTUAL UPGRADE: Updates PRT staking pool. PRT staking pool must have this extension set as the feeSplitExtension.
* @param _prtStakingPool Address of the new PRT staking pool
*/
function updatePrtStakingPool(IPrtStakingPool _prtStakingPool)
external
mutualUpgrade(manager.operator(), manager.methodologist())
{
require(address(_prtStakingPool) != address(0), "Zero address not valid");
require(_prtStakingPool.feeSplitExtension() == address(this), "PrtFeeSplitExtension must be set");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that in the newer version of SnapshotStakingPool.sol the public variable is called distributor and not feeSplitExtension

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also check to make sure the Staking Pool's stakeToken matches the address stored in prt?

In the constructor we check if prt.setToken() is correct, but I don't think that's effective unless we also check that the StakingPool uses that same prt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added additional checks to match the SnapshotStakingPool here 515e783

prtStakingPool = _prtStakingPool;
ckoopmann marked this conversation as resolved.
Show resolved Hide resolved
emit PrtStakingPoolUpdated(address(_prtStakingPool));
}

/**
* @notice ONLY ALLOWED ACCRUER: Accrues fees from streaming fee module. Gets resulting balance after fee accrual, calculates fees for
* operator and PRT staking pool, and sends to operator fee recipient and PRT Staking Pool respectively. NOTE: mint/redeem fees
* will automatically be sent to this address so reading the balance of the SetToken in the contract after accrual is
* sufficient for accounting for all collected fees. If the PRT take is greater than 0, the PRT Staking Pool will accrue the fees
* and update the snapshot.
*/
function accrueFeesAndDistribute() public override onlyAllowedAccruer {
ckoopmann marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add a line for require(address(prtStakingPool) != address(0), "PRT Staking Pool not set"), as a reminder that this function can be called before the prtStakingPool is defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added here e7a8a8c

// Emits a FeeActualized event
streamingFeeModule.accrueFee(setToken);

uint256 totalFees = setToken.balanceOf(address(this));

uint256 operatorTake = totalFees.preciseMul(operatorFeeSplit);
uint256 prtTake = totalFees.sub(operatorTake);

if (operatorTake > 0) {
setToken.transfer(operatorFeeRecipient, operatorTake);
}

// Accrue PRT Staking Pool rewards and update snapshot
if (prtTake > 0) {
setToken.approve(address(prtStakingPool), prtTake);
prtStakingPool.accrue(prtTake);
ckoopmann marked this conversation as resolved.
Show resolved Hide resolved
}

emit PrtFeesDistributed(operatorFeeRecipient, address(prtStakingPool), operatorTake, prtTake);
}

/**
* @notice MUTUAL UPGRADE: Updates fee split between operator and PRT Staking Pool. Split defined in precise units (1% = 10^16).
* Does not accrue fees and snapshot PRT Staking Pool.
* @param _newFeeSplit Percent of fees in precise units (10^16 = 1%) sent to operator, (rest go to the PRT Staking Pool).
*/
function updateFeeSplit(uint256 _newFeeSplit)
external
override
mutualUpgrade(manager.operator(), manager.methodologist())
{
require(_newFeeSplit <= PreciseUnitMath.preciseUnit(), "Fee must be less than 100%");
operatorFeeSplit = _newFeeSplit;
ckoopmann marked this conversation as resolved.
Show resolved Hide resolved
emit OperatorFeeSplitUpdated(_newFeeSplit);
}

/**
* @notice MUTUAL UPGRADE: Toggles the permission status of specified addresses to call the `accrueFeesAndDistribute()` function.
* @param _accruers An array of addresses whose accrue permission status is to be toggled.
* @param _statuses An array of booleans indicating the new accrue permission status for each corresponding address in `_accruers`.
*/
function setAccruersStatus(
address[] memory _accruers,
bool[] memory _statuses
)
external
mutualUpgrade(manager.operator(), manager.methodologist())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this function have to be mutual upgrade? To me it feels like this is something the operator should be responsible over. Also, the Leverage Strategy extensions updateCallerStatus is onlyOperator rather than mutual upgrade.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to onlyOperator here 8015c24

{
_accruers.validatePairsWithArray(_statuses);
for (uint256 i = 0; i < _accruers.length; i++) {
_updateAccruersHistory(_accruers[i], _statuses[i]);
accrueAllowList[_accruers[i]] = _statuses[i];
emit AccruerStatusUpdated(_accruers[i], _statuses[i]);
}
}

/**
* @notice MUTUAL UPGRADE: Toggles whether or not anyone is allowed to call the `accrueFeesAndDistribute()` function.
* If set to true, it bypasses the accrueAllowList, allowing any address to call the `accrueFeesAndDistribute()` function.
* @param _status A boolean indicating if anyone can accrue.
*/
function updateAnyoneAccrue(bool _status)
external
mutualUpgrade(manager.operator(), manager.methodologist())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above for setAccruersStatus

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to onlyOperator here 8015c24

{
isAnyoneAllowedToAccrue = _status;
emit AnyoneAccrueUpdated(_status);
}

/**
* @notice Determines whether the given address is permitted to `accrueFeesAndDistribute()`.
* @param _accruer Address of the accruer.
* @return bool True if the given `_accruer` is permitted to accrue, false otherwise.
*/
function isAllowedAccruer(address _accruer) external view returns (bool) {
return _isAllowedAccruer(_accruer);
}

/**
* @dev Retrieves the list of addresses that are permitted to `accrueFeesAndDistribute()`.
* @return address[] Array of addresses representing the allowed accruers.
*/
function getAllowedAccruers() external view returns (address[] memory) {
return accruersHistory;
}


/* ============ Internal Functions ============ */

function _isAllowedAccruer(address _accruer) internal view returns (bool) {
return isAnyoneAllowedToAccrue || accrueAllowList[_accruer];
}

function _updateAccruersHistory(address _accruer, bool _status) internal {
if (_status && !accruersHistory.contains(_accruer)) {
accruersHistory.push(_accruer);
} else if(!_status && accruersHistory.contains(_accruer)) {
accruersHistory.removeStorage(_accruer);
ckoopmann marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
8 changes: 8 additions & 0 deletions contracts/interfaces/IPrt.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: Apache License, Version 2.0
pragma solidity 0.6.10;

import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

interface IPrt is IERC20 {
function setToken() external view returns (address);
}
15 changes: 15 additions & 0 deletions contracts/interfaces/IPrtStakingPool.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: Apache License, Version 2.0
pragma solidity ^0.6.10;

interface IPrtStakingPool {
function stake(uint256 _amount) external;
function unstake(uint256 _amount) external;
function accrue(uint256 _amount) external;
function claim() external;
function setFeeSplitExtension(address _feeSplitExtension) external;
function getCurrentId() external view returns (uint256);
function getPendingRewards(address _account) external view returns (uint256);
function getSnapshotRewards(uint256 _snapshotId, address _account) external view returns (uint256);
function feeSplitExtension() external view returns (address);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting the newer version of SnapshotStakingPool uses the variable name distributor instead of feeSplitExtension

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed here 515e783

function prt() external view returns (address);
}
Loading
Loading