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: add AllocatorsAllowlistExtension.sol #655

Merged
merged 18 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
5 changes: 5 additions & 0 deletions .github/workflows/forge-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ jobs:
uses: foundry-rs/foundry-toolchain@v1.0.9
with:
version: nightly

- name: Run Smock
run: |
bun smock
id: smock

- name: Run Forge Format
run: |
Expand Down
2 changes: 1 addition & 1 deletion contracts/strategies/BaseStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ abstract contract BaseStrategy is IBaseStrategy {
/// @notice Checks if the '_sender' is a pool manager.
/// @dev Reverts if the '_sender' is not a pool manager.
/// @param _sender The address to check if they are a pool manager
function _checkOnlyPoolManager(address _sender) internal view {
function _checkOnlyPoolManager(address _sender) internal view virtual {
if (!allo.isPoolManager(poolId, _sender)) revert BaseStrategy_UNAUTHORIZED();
}

Expand Down
141 changes: 141 additions & 0 deletions contracts/strategies/extensions/allocate/AllocationExtension.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.19;

import {IAllocationExtension} from "contracts/strategies/extensions/allocate/IAllocationExtension.sol";
import {BaseStrategy} from "contracts/strategies/BaseStrategy.sol";

abstract contract AllocationExtension is BaseStrategy, IAllocationExtension {
/// ================================
/// ========== Storage =============
/// ================================

/// @notice The start and end times for allocations
uint64 public allocationStartTime;
uint64 public allocationEndTime;

/// @notice Defines if the strategy is sending Metadata struct in the data parameter
bool public isUsingAllocationMetadata;

/// @notice token -> isAllowed
mapping(address => bool) public allowedTokens;

/// ===============================
/// ========= Initialize ==========
/// ===============================

/// @notice This initializes the Alocation Extension
/// @dev This function MUST be called by the 'initialize' function in the strategy.
/// @param _allowedTokens The allowed tokens
/// @param _allocationStartTime The start time for the allocation period
/// @param _allocationEndTime The end time for the allocation period
/// @param _isUsingAllocationMetadata Defines if the strategy is sending Metadata struct in the data parameter
function __AllocationExtension_init(
address[] memory _allowedTokens,
uint64 _allocationStartTime,
uint64 _allocationEndTime,
bool _isUsingAllocationMetadata
) internal virtual {
if (_allowedTokens.length == 0) {
// all tokens
allowedTokens[address(0)] = true;
} else {
for (uint256 i; i < _allowedTokens.length; i++) {
allowedTokens[_allowedTokens[i]] = true;
}
}

isUsingAllocationMetadata = _isUsingAllocationMetadata;

_updateAllocationTimestamps(_allocationStartTime, _allocationEndTime);
}

/// ====================================
/// =========== Modifiers ==============
/// ====================================

/// @notice Modifier to check if allocation has ended
/// @dev Reverts if allocation has not ended
modifier onlyAfterAllocation() {
_checkOnlyAfterAllocation();
_;
}

/// @notice Modifier to check if allocation is active
/// @dev Reverts if allocation is not active
modifier onlyActiveAllocation() {
_checkOnlyActiveAllocation();
_;
}

/// @notice Modifier to check if allocation has started
/// @dev Reverts if allocation has started
modifier onlyBeforeAllocation() {
_checkBeforeAllocation();
_;
}

/// ====================================
/// ============ Internal ==============
/// ====================================

/// @notice Checks if the allocator is valid
/// @param _allocator The allocator address
/// @return 'true' if the allocator is valid, otherwise 'false'
function _isValidAllocator(address _allocator) internal view virtual returns (bool);

/// @notice Returns TRUE if the token is allowed
/// @param _token The token to check
function _isAllowedToken(address _token) internal view virtual returns (bool) {
// all tokens allowed
if (allowedTokens[address(0)]) return true;

if (allowedTokens[_token]) return true;

return false;
}

/// @notice Sets the start and end dates for allocation.
/// @dev The 'msg.sender' must be a pool manager.
/// @param _allocationStartTime The start time for the allocation
/// @param _allocationEndTime The end time for the allocation
function _updateAllocationTimestamps(uint64 _allocationStartTime, uint64 _allocationEndTime) internal virtual {
if (_allocationStartTime > _allocationEndTime) revert INVALID_ALLOCATION_TIMESTAMPS();

allocationStartTime = _allocationStartTime;
allocationEndTime = _allocationEndTime;

emit AllocationTimestampsUpdated(_allocationStartTime, _allocationEndTime, msg.sender);
}

/// @dev Ensure the function is called before allocation start time
function _checkBeforeAllocation() internal virtual {
if (block.timestamp >= allocationStartTime) revert ALLOCATION_HAS_STARTED();
}

/// @dev Ensure the function is called during allocation times
function _checkOnlyActiveAllocation() internal virtual {
if (block.timestamp < allocationStartTime) revert ALLOCATION_NOT_ACTIVE();
if (block.timestamp > allocationEndTime) revert ALLOCATION_NOT_ACTIVE();
}

/// @dev Ensure the function is called after allocation start time
function _checkOnlyAfterAllocation() internal virtual {
if (block.timestamp <= allocationEndTime) revert ALLOCATION_NOT_ENDED();
}

// ====================================
// ==== External/Public Functions =====
// ====================================

/// @notice Sets the start and end dates for allocation.
/// @dev The 'msg.sender' must be a pool manager.
/// @param _allocationStartTime The start time for the allocation
/// @param _allocationEndTime The end time for the allocation
function updateAllocationTimestamps(uint64 _allocationStartTime, uint64 _allocationEndTime)
external
virtual
onlyPoolManager(msg.sender)
{
_updateAllocationTimestamps(_allocationStartTime, _allocationEndTime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.19;

import {AllocationExtension} from "contracts/strategies/extensions/allocate/AllocationExtension.sol";
import {IAllocatorsAllowlistExtension} from "contracts/strategies/extensions/allocate/IAllocatorsAllowlistExtension.sol";

abstract contract AllocatorsAllowlistExtension is AllocationExtension, IAllocatorsAllowlistExtension {
/// ================================
/// ========== Storage =============
/// ================================

/// @dev allocator => isAllowed
mapping(address => bool) public allowedAllocators;

/// ====================================
/// ============ Internal ==============
/// ====================================

/// @notice Checks if the allocator is valid
/// @param _allocator The allocator address
/// @return true if the allocator is valid
function _isValidAllocator(address _allocator) internal view virtual override returns (bool) {
return allowedAllocators[_allocator];
}

/// @dev Mark an address as valid allocator
function _addAllocator(address _allocator) internal virtual {
allowedAllocators[_allocator] = true;
}

/// @dev Remove an address from the valid allocators
function _removeAllocator(address _allocator) internal virtual {
allowedAllocators[_allocator] = false;
}

// ====================================
// ==== External/Public Functions =====
// ====================================

/// @notice Add allocator
/// @dev Only the pool manager(s) can call this function and emits an `AllocatorAdded` event
/// @param _allocators The allocator addresses
function addAllocators(address[] memory _allocators) external onlyPoolManager(msg.sender) {
uint256 length = _allocators.length;
for (uint256 i = 0; i < length; i++) {
_addAllocator(_allocators[i]);
}

emit AllocatorsAdded(_allocators, msg.sender);
}

/// @notice Remove allocators
/// @dev Only the pool manager(s) can call this function and emits an `AllocatorRemoved` event
/// @param _allocators The allocator addresses
function removeAllocators(address[] memory _allocators) external onlyPoolManager(msg.sender) {
uint256 length = _allocators.length;
for (uint256 i = 0; i < length; i++) {
_removeAllocator(_allocators[i]);
}

emit AllocatorsRemoved(_allocators, msg.sender);
}
}
39 changes: 39 additions & 0 deletions contracts/strategies/extensions/allocate/IAllocationExtension.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.19;

interface IAllocationExtension {
/// @dev Error thrown when the allocation timestamps are invalid
error INVALID_ALLOCATION_TIMESTAMPS();

/// @dev Error thrown when trying to call the function when the allocation has started
error ALLOCATION_HAS_STARTED();

/// @dev Error thrown when trying to call the function when the allocation is not active
error ALLOCATION_NOT_ACTIVE();

/// @dev Error thrown when trying to call the function when the allocation has ended
error ALLOCATION_NOT_ENDED();

/// @notice Emitted when the allocation timestamps are updated
/// @param allocationStartTime The start time for the allocation period
/// @param allocationEndTime The end time for the allocation period
/// @param sender The sender of the transaction
event AllocationTimestampsUpdated(uint64 allocationStartTime, uint64 allocationEndTime, address sender);

/// @notice The start time for the allocation period
function allocationStartTime() external view returns (uint64);

/// @notice The end time for the allocation period
function allocationEndTime() external view returns (uint64);

/// @notice Defines if the strategy is sending Metadata struct in the data parameter
function isUsingAllocationMetadata() external view returns (bool);

/// @notice Returns TRUE if the token is allowed, FALSE otherwise
function allowedTokens(address _token) external view returns (bool);

/// @notice Sets the start and end dates for allocation.
/// @param _allocationStartTime The start time for the allocation
/// @param _allocationEndTime The end time for the allocation
function updateAllocationTimestamps(uint64 _allocationStartTime, uint64 _allocationEndTime) external;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.19;

interface IAllocatorsAllowlistExtension {
/// @notice Emitted when an allocator is added
/// @param allocators The allocator addresses
/// @param sender The sender of the transaction
event AllocatorsAdded(address[] allocators, address sender);

/// @notice Emitted when an allocator is removed
/// @param allocators The allocator addresses
/// @param sender The sender of the transaction
event AllocatorsRemoved(address[] allocators, address sender);

/// @notice Returns TRUE if the allocator is allowed, FALSE otherwise
/// @param _allocator The allocator address to check
/// @return TRUE if the allocator is allowed, FALSE otherwise
function allowedAllocators(address _allocator) external view returns (bool);
}
4 changes: 2 additions & 2 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ allow_paths = ['node_modules']

[fmt]
ignore = [
'contracts/strategies/_poc/qv-hackathon/SchemaResolver.sol',
'contracts/strategies/_poc/direct-grants-simple/DirectGrantsSimpleStrategy.sol'
'contracts/strategies/deprecated/_poc/qv-hackathon/SchemaResolver.sol',
'contracts/strategies/deprecated/_poc/direct-grants-simple/DirectGrantsSimpleStrategy.sol'
]

[rpc_endpoints]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"/////// deploy-test ///////": "echo 'deploy test scripts'",
"create-profile": "npx hardhat run scripts/test/createProfile.ts --network",
"create-pool": "npx hardhat run scripts/test/createPool.ts --network",
"smock": "smock-foundry --contracts contracts/core"
"smock": "smock-foundry --contracts contracts/core --contracts test/utils/mocks/"
},
"devDependencies": {
"@matterlabs/hardhat-zksync-deploy": "^1.2.1",
Expand Down
Loading
Loading