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

make ComponentToken inherit from ReentrancyGuardUpgradeable #105

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
90 changes: 81 additions & 9 deletions nest/src/AggregateToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
uint256 askPrice;
/// @dev Price at which users can sell the AggregateToken to receive `asset`, times the base
uint256 bidPrice;
/// @dev True if the AggregateToken contract is paused for deposits, false otherwise
bool paused;
}

// keccak256(abi.encode(uint256(keccak256("plume.storage.AggregateToken")) - 1)) & ~bytes32(uint256(0xff))
Expand All @@ -47,6 +49,14 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
/// @notice Role for the price updater of the AggregateToken
bytes32 public constant PRICE_UPDATER_ROLE = keccak256("PRICE_UPDATER_ROLE");

// Events

/// @notice Emitted when the AggregateToken contract is paused for deposits
event Paused();

/// @notice Emitted when the AggregateToken contract is unpaused for deposits
event Unpaused();

// Errors

/**
Expand Down Expand Up @@ -80,6 +90,15 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
*/
error InvalidAsset(IERC20 invalidAsset, IERC20 asset);

/// @notice Indicates a failure because the contract is paused for deposits
error DepositPaused();

/// @notice Indicates a failure because the contract is already paused for deposits
error AlreadyPaused();

/// @notice Indicates a failure because the contract is not paused for deposits
error NotPaused();

// Initializer

/**
Expand Down Expand Up @@ -114,6 +133,7 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
$.componentTokenMap[asset_] = true;
$.askPrice = askPrice;
$.bidPrice = bidPrice;
$.paused = false;
}

// Override Functions
Expand Down Expand Up @@ -143,6 +163,18 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
return super.asset();
}

/// @inheritdoc IComponentToken
function deposit(
uint256 assets,
address receiver,
address controller
) public override(ComponentToken, IComponentToken) returns (uint256 shares) {
if (_getAggregateTokenStorage().paused) {
revert DepositPaused();
}
return super.deposit(assets, receiver, controller);
}

/// @inheritdoc IComponentToken
function redeem(
uint256 shares,
Expand All @@ -165,7 +197,10 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
* @param componentToken ComponentToken to approve
* @param amount Amount of `asset` to approve
*/
function approveComponentToken(IComponentToken componentToken, uint256 amount) external onlyRole(ADMIN_ROLE) {
function approveComponentToken(
IComponentToken componentToken,
uint256 amount
) external nonReentrant onlyRole(ADMIN_ROLE) {
IERC20(componentToken.asset()).approve(address(componentToken), amount);
}

Expand All @@ -176,7 +211,7 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
*/
function addComponentToken(
IComponentToken componentToken
) external onlyRole(ADMIN_ROLE) {
) external nonReentrant onlyRole(ADMIN_ROLE) {
AggregateTokenStorage storage $ = _getAggregateTokenStorage();
if ($.componentTokenMap[componentToken]) {
revert ComponentTokenAlreadyListed(componentToken);
Expand All @@ -193,7 +228,10 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
* @param componentToken ComponentToken to buy
* @param assets Amount of `asset` to pay to receive the ComponentToken
*/
function buyComponentToken(IComponentToken componentToken, uint256 assets) public onlyRole(ADMIN_ROLE) {
function buyComponentToken(
IComponentToken componentToken,
uint256 assets
) public nonReentrant onlyRole(ADMIN_ROLE) {
AggregateTokenStorage storage $ = _getAggregateTokenStorage();

if (!$.componentTokenMap[componentToken]) {
Expand All @@ -216,7 +254,7 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
function sellComponentToken(
IComponentToken componentToken,
uint256 componentTokenAmount
) public onlyRole(ADMIN_ROLE) {
) public nonReentrant onlyRole(ADMIN_ROLE) {
uint256 assets = componentToken.redeem(componentTokenAmount, address(this), address(this));
emit ComponentTokenSold(msg.sender, componentToken, componentTokenAmount, assets);
}
Expand All @@ -228,7 +266,10 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
* @param componentToken ComponentToken to buy
* @param assets Amount of `asset` to pay to receive the ComponentToken
*/
function requestBuyComponentToken(IComponentToken componentToken, uint256 assets) public onlyRole(ADMIN_ROLE) {
function requestBuyComponentToken(
IComponentToken componentToken,
uint256 assets
) public nonReentrant onlyRole(ADMIN_ROLE) {
uint256 requestId = componentToken.requestDeposit(assets, address(this), address(this));
emit ComponentTokenBuyRequested(msg.sender, componentToken, assets, requestId);
}
Expand All @@ -243,12 +284,12 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
function requestSellComponentToken(
IComponentToken componentToken,
uint256 componentTokenAmount
) public onlyRole(ADMIN_ROLE) {
) public nonReentrant onlyRole(ADMIN_ROLE) {
uint256 requestId = componentToken.requestRedeem(componentTokenAmount, address(this), address(this));
emit ComponentTokenSellRequested(msg.sender, componentToken, componentTokenAmount, requestId);
}

// Admin Setter Functions
// Admin Functions

/**
* @notice Set the price at which users can buy the AggregateToken using `asset`
Expand All @@ -257,7 +298,7 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
*/
function setAskPrice(
uint256 askPrice
) external onlyRole(PRICE_UPDATER_ROLE) {
) external nonReentrant onlyRole(PRICE_UPDATER_ROLE) {
_getAggregateTokenStorage().askPrice = askPrice;
}

Expand All @@ -268,10 +309,36 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
*/
function setBidPrice(
uint256 bidPrice
) external onlyRole(PRICE_UPDATER_ROLE) {
) external nonReentrant onlyRole(PRICE_UPDATER_ROLE) {
_getAggregateTokenStorage().bidPrice = bidPrice;
}

/**
* @notice Pause the AggregateToken contract for deposits
* @dev Only the owner can pause the AggregateToken contract for deposits
*/
function pause() external onlyRole(ADMIN_ROLE) {
AggregateTokenStorage storage $ = _getAggregateTokenStorage();
if ($.paused) {
revert AlreadyPaused();
}
$.paused = true;
emit Paused();
}

/**
* @notice Unpause the AggregateToken contract for deposits
* @dev Only the owner can unpause the AggregateToken contract for deposits
*/
function unpause() external nonReentrant onlyRole(ADMIN_ROLE) {
AggregateTokenStorage storage $ = _getAggregateTokenStorage();
if (!$.paused) {
revert NotPaused();
}
$.paused = false;
emit Unpaused();
}

// Getter View Functions

/// @notice Price at which users can buy the AggregateToken using `asset`, times the base
Expand All @@ -289,6 +356,11 @@ contract AggregateToken is ComponentToken, IAggregateToken, ERC1155Holder {
return _getAggregateTokenStorage().componentTokenList;
}

/// @notice Returns true if the AggregateToken contract is paused for deposits
function isPaused() external view returns (bool) {
return _getAggregateTokenStorage().paused;
}

/**
* @notice Check if the given ComponentToken is in the component token list
* @param componentToken ComponentToken to check
Expand Down
32 changes: 23 additions & 9 deletions nest/src/ComponentToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { AccessControlUpgradeable } from "@openzeppelin/contracts-upgradeable/ac
import { Initializable } from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import { UUPSUpgradeable } from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import { ERC4626Upgradeable } from "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC4626Upgradeable.sol";

import { ReentrancyGuardUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/ReentrancyGuardUpgradeable.sol";
import { IERC4626 } from "@openzeppelin/contracts/interfaces/IERC4626.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import { IERC20Metadata } from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
Expand All @@ -26,6 +28,7 @@ abstract contract ComponentToken is
ERC4626Upgradeable,
AccessControlUpgradeable,
UUPSUpgradeable,
ReentrancyGuardUpgradeable,
ERC165,
IERC7540
{
Expand Down Expand Up @@ -155,6 +158,7 @@ abstract contract ComponentToken is
__ERC4626_init(asset_);
__AccessControl_init();
__UUPSUpgradeable_init();
__ReentrancyGuard_init();

_grantRole(DEFAULT_ADMIN_ROLE, owner);
_grantRole(ADMIN_ROLE, owner);
Expand Down Expand Up @@ -209,7 +213,9 @@ abstract contract ComponentToken is
/// @dev Reverts with Unimplemented() until convertToAssets is implemented by the concrete contract
/// @param owner Address to query the balance of
/// @return assets Total value held by the owner
function assetsOf(address owner) public view virtual returns (uint256 assets) {
function assetsOf(
address owner
) public view virtual returns (uint256 assets) {
return convertToAssets(balanceOf(owner));
}

Expand All @@ -234,7 +240,7 @@ abstract contract ComponentToken is
uint256 assets,
address controller,
address owner
) public virtual returns (uint256 requestId) {
) public virtual nonReentrant returns (uint256 requestId) {
if (assets == 0) {
revert ZeroAmount();
}
Expand Down Expand Up @@ -262,7 +268,7 @@ abstract contract ComponentToken is
* @param shares Amount of shares to receive in exchange
* @param controller Controller of the request
*/
function _notifyDeposit(uint256 assets, uint256 shares, address controller) internal virtual {
function _notifyDeposit(uint256 assets, uint256 shares, address controller) internal virtual nonReentrant {
if (assets == 0) {
revert ZeroAmount();
}
Expand All @@ -283,7 +289,11 @@ abstract contract ComponentToken is
}

/// @inheritdoc IComponentToken
function deposit(uint256 assets, address receiver, address controller) public virtual returns (uint256 shares) {
function deposit(
uint256 assets,
address receiver,
address controller
) public virtual nonReentrant returns (uint256 shares) {
if (assets == 0) {
revert ZeroAmount();
}
Expand Down Expand Up @@ -312,7 +322,11 @@ abstract contract ComponentToken is
}

/// @inheritdoc IERC7540
function mint(uint256 shares, address receiver, address controller) public virtual returns (uint256 assets) {
function mint(
uint256 shares,
address receiver,
address controller
) public virtual nonReentrant returns (uint256 assets) {
if (shares == 0) {
revert ZeroAmount();
}
Expand Down Expand Up @@ -345,7 +359,7 @@ abstract contract ComponentToken is
uint256 shares,
address controller,
address owner
) public virtual returns (uint256 requestId) {
) public virtual nonReentrant returns (uint256 requestId) {
if (shares == 0) {
revert ZeroAmount();
}
Expand All @@ -371,7 +385,7 @@ abstract contract ComponentToken is
* @param shares Amount of shares that was redeemed by `requestRedeem`
* @param controller Controller of the request
*/
function _notifyRedeem(uint256 assets, uint256 shares, address controller) internal virtual {
function _notifyRedeem(uint256 assets, uint256 shares, address controller) internal virtual nonReentrant {
if (shares == 0) {
revert ZeroAmount();
}
Expand All @@ -396,7 +410,7 @@ abstract contract ComponentToken is
uint256 shares,
address receiver,
address controller
) public virtual override(ERC4626Upgradeable, IERC7540) returns (uint256 assets) {
) public virtual override(ERC4626Upgradeable, IERC7540) nonReentrant returns (uint256 assets) {
if (shares == 0) {
revert ZeroAmount();
}
Expand Down Expand Up @@ -429,7 +443,7 @@ abstract contract ComponentToken is
uint256 assets,
address receiver,
address controller
) public virtual override(ERC4626Upgradeable, IERC7540) returns (uint256 shares) {
) public virtual override(ERC4626Upgradeable, IERC7540) nonReentrant returns (uint256 shares) {
if (assets == 0) {
revert ZeroAmount();
}
Expand Down
4 changes: 3 additions & 1 deletion nest/src/interfaces/IComponentToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ interface IComponentToken {
* @param owner Address to query the balance of
* @return assets Total value held by the owner
*/
function assetsOf(address owner) external view returns (uint256 assets);
function assetsOf(
address owner
) external view returns (uint256 assets);

/**
* @notice Equivalent amount of shares for the given amount of assets
Expand Down