Skip to content

Commit

Permalink
feat: initialize in base strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
0xOneTony committed Oct 25, 2024
1 parent beb07ac commit 7a8beb5
Show file tree
Hide file tree
Showing 13 changed files with 27 additions and 104 deletions.
16 changes: 16 additions & 0 deletions contracts/strategies/BaseStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ abstract contract BaseStrategy is IBaseStrategy {
_STRATEGY_ID = keccak256(abi.encode(_strategyName));
}

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

/// @notice Initialize the strategy
/// @param __poolId The pool id
/// @param _data custom data to be used to initialize the strategy
function initialize(uint256 __poolId, bytes memory _data) external virtual override {
__BaseStrategy_init(__poolId);
_initializeStrategy(__poolId, _data);

emit Initialized(__poolId, _data);
}

/// ====================================
/// =========== Modifiers ==============
/// ====================================
Expand Down Expand Up @@ -294,6 +308,8 @@ abstract contract BaseStrategy is IBaseStrategy {
/// @param _sender The address of the sender
function _afterDistribute(address[] memory _recipientIds, bytes memory _data, address _sender) internal virtual {}

function _initializeStrategy(uint256 _poolId, bytes memory _data) internal virtual {}

/// @notice Strategies should be able to receive native token
/// @dev By default onlyAllo should be able to call this to fund the pool
/// @dev In case of a strategy that needs to receive native token from other sources, this function should be overridden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,6 @@ contract DirectAllocationStrategy is BaseStrategy, Errors {

constructor(address _allo) BaseStrategy(_allo, "DirectAllocation") {}

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

/// @notice Initialize the strategy
/// @param _poolId The pool id
/// @param _data The data to initialize the strategy
function initialize(uint256 _poolId, bytes memory _data) external virtual override {
__BaseStrategy_init(_poolId);
emit Initialized(_poolId, _data);
}

/// ====================================
/// ============ Internal ==============
/// ====================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ contract DonationVotingOffchain is BaseStrategy, RecipientsExtension, Allocation
/// address[] _allowedTokens,
/// bool _isUsingAllocationMetadata
/// )
function initialize(uint256 _poolId, bytes memory _data) external virtual override {
function _initializeStrategy(uint256 _poolId, bytes memory _data) internal override {
// Decode _data and initialize the specific strategy data
(
RecipientInitializeData memory _recipientExtensionInitializeData,
uint64 _allocationStartTime,
Expand All @@ -142,13 +143,12 @@ contract DonationVotingOffchain is BaseStrategy, RecipientsExtension, Allocation
bool _isUsingAllocationMetadata
) = abi.decode(_data, (RecipientInitializeData, uint64, uint64, uint64, address[], bool));

// Custom initialization logic
withdrawalCooldown = _withdrawalCooldown;

__BaseStrategy_init(_poolId);
// Initialize other extensions using the decoded data
__RecipientsExtension_init(_recipientExtensionInitializeData);
__AllocationExtension_init(_allowedTokens, _allocationStartTime, _allocationEndTime, _isUsingAllocationMetadata);

emit Initialized(_poolId, _data);
}

/// ===============================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ contract DonationVotingOnchain is BaseStrategy, RecipientsExtension, AllocationE
/// address _allocationToken,
/// bool _isUsingAllocationMetadata
/// )
function initialize(uint256 _poolId, bytes memory _data) external virtual override {
function _initializeStrategy(uint256 _poolId, bytes memory _data) internal virtual override {
(
RecipientInitializeData memory _recipientExtensionInitializeData,
uint64 _allocationStartTime,
Expand All @@ -99,13 +99,10 @@ contract DonationVotingOnchain is BaseStrategy, RecipientsExtension, AllocationE

withdrawalCooldown = _withdrawalCooldown;

__BaseStrategy_init(_poolId);
__RecipientsExtension_init(_recipientExtensionInitializeData);
address[] memory _allowedTokens = new address[](1);
_allowedTokens[0] = _allocationToken;
__AllocationExtension_init(_allowedTokens, _allocationStartTime, _allocationEndTime, _isUsingAllocationMetadata);

emit Initialized(_poolId, _data);
}

/// ====================================
Expand Down
13 changes: 0 additions & 13 deletions contracts/strategies/examples/easy-rpgf/EasyRPGF.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ contract EasyRPGF is BaseStrategy, Errors {

constructor(address _allo) BaseStrategy(_allo, "EasyRPGF") {}

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

/// @notice Initialize the strategy
/// @param _poolId The pool id
/// @param _data Not used
function initialize(uint256 _poolId, bytes memory _data) external override {
__BaseStrategy_init(_poolId);

emit Initialized(_poolId, "");
}

/// ====================================
/// ============ Internal ==============
/// ====================================
Expand Down
6 changes: 1 addition & 5 deletions contracts/strategies/examples/quadratic-voting/QVSimple.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ contract QVSimple is BaseStrategy, RecipientsExtension, AllocatorsAllowlistExten
/// @notice Initialize the strategy
/// @param _poolId The pool id
/// @param _data The data to initialize the strategy (Must include RecipientInitializeData and QVSimpleInitializeData)
function initialize(uint256 _poolId, bytes memory _data) external virtual override {
__BaseStrategy_init(_poolId);

function _initializeStrategy(uint256 _poolId, bytes memory _data) internal virtual override {
(
IRecipientsExtension.RecipientInitializeData memory _recipientInitializeData,
QVSimpleInitializeData memory _qvSimpleInitializeData
Expand All @@ -84,8 +82,6 @@ contract QVSimple is BaseStrategy, RecipientsExtension, AllocatorsAllowlistExten
);

maxVoiceCreditsPerAllocator = _qvSimpleInitializeData.maxVoiceCreditsPerAllocator;

emit Initialized(_poolId, _data);
}

/// ======================
Expand Down
4 changes: 1 addition & 3 deletions contracts/strategies/examples/rfp/RFPSimple.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ contract RFPSimple is BaseStrategy, MilestonesExtension, RecipientsExtension {
/// @param _poolId ID of the pool
/// @param _data The data to be decoded
/// @custom:data (RecipientInitializeData _recipientExtensionInitializeData, uint256 _maxBid)
function initialize(uint256 _poolId, bytes memory _data) external virtual override {
function _initializeStrategy(uint256 _poolId, bytes memory _data) internal virtual override {
(RecipientInitializeData memory _recipientExtensionInitializeData, uint256 _maxBid) =
abi.decode(_data, (RecipientInitializeData, uint256));
__BaseStrategy_init(_poolId);
__RecipientsExtension_init(_recipientExtensionInitializeData);
__MilestonesExtension_init(_maxBid);
emit Initialized(_poolId, _data);
}

/// ====================================
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,12 @@ contract SQFSuperfluid is
/// @dev This will revert if the strategy is already initialized and 'msg.sender' is not the 'Allo' contract.
/// @param _poolId ID of the pool
/// @param _data The data to be decoded
function initialize(uint256 _poolId, bytes memory _data) external override {
function _initializeStrategy(uint256 _poolId, bytes memory _data) internal override {
(
RecipientInitializeData memory _recipientExtensionInitializeData,
SQFSuperfluidInitializeParams memory _sqfSuperfluidInitializeParams
) = abi.decode(_data, (RecipientInitializeData, SQFSuperfluidInitializeParams));

// Initialize the BaseStrategy with the '_poolId'
__BaseStrategy_init(_poolId);

// Initialize the RecipientsExtension
__RecipientsExtension_init(_recipientExtensionInitializeData);

Expand Down
48 changes: 0 additions & 48 deletions test/mocks/MockBaseStrategy.sol

This file was deleted.

6 changes: 1 addition & 5 deletions test/mocks/MockGatingExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ import {BaseStrategy} from "strategies/BaseStrategy.sol";
import {EASGatingExtension} from "strategies/extensions/gating/EASGatingExtension.sol";
import {NFTGatingExtension} from "strategies/extensions/gating/NFTGatingExtension.sol";
import {TokenGatingExtension} from "strategies/extensions/gating/TokenGatingExtension.sol";
import {MockBaseStrategy} from "./MockBaseStrategy.sol";

contract MockGatingExtension is EASGatingExtension, NFTGatingExtension, TokenGatingExtension {
constructor(address _allo, string memory _strategyName) BaseStrategy(_allo, _strategyName) {}

function initialize(uint256 _poolId, bytes memory _data) public {
__BaseStrategy_init(_poolId);

function _initializeStrategy(uint256 _poolId, bytes memory _data) internal override {
address _eas = abi.decode(_data, (address));

__EASGatingExtension_init(_eas);
emit Initialized(_poolId, _data);
}

// this is called via allo.sol to register recipients
Expand Down
4 changes: 0 additions & 4 deletions test/mocks/smock-mocks/MockBaseStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import {BaseStrategy} from "contracts/strategies/BaseStrategy.sol";
contract MockBaseStrategy is BaseStrategy {
constructor(address _allo, string memory _strategyName) BaseStrategy(_allo, _strategyName) {}

function initialize(uint256 _poolId, bytes memory) external override {
__BaseStrategy_init(_poolId);
}

function __BaseStrategy_init(uint256 _poolId) internal virtual override {
super.__BaseStrategy_init(_poolId);
}
Expand Down
2 changes: 1 addition & 1 deletion test/mocks/smock-mocks/MockMilestonesExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract MockMilestonesExtension is BaseStrategy, IMilestonesExtension, Mileston

constructor(address _allo, string memory _strategyName) BaseStrategy(_allo, _strategyName) {}

function initialize(uint256 _poolId, bytes memory _data) external {
function initialize(uint256 _poolId, bytes memory _data) external override {
__BaseStrategy_init(_poolId);

uint256 _maxBid = abi.decode(_data, (uint256));
Expand Down
2 changes: 1 addition & 1 deletion test/mocks/smock-mocks/MockRecipientsExtension.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract MockRecipientsExtension is BaseStrategy, RecipientsExtension {
BaseStrategy(_allo, _strategyName)
{}

function initialize(uint256 _poolId, bytes memory _data) external {
function initialize(uint256 _poolId, bytes memory _data) external override {
__BaseStrategy_init(_poolId);

RecipientInitializeData memory _initializeData = abi.decode(_data, (RecipientInitializeData));
Expand Down

0 comments on commit 7a8beb5

Please sign in to comment.