|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 2 | +pragma solidity ^0.8.19; |
| 3 | + |
| 4 | +import "forge-std/Test.sol"; |
| 5 | + |
| 6 | +import {BaseStrategyUpgradeable} from "../src/BaseStrategyUpgradeable.sol"; |
| 7 | +import {IAllo} from "allo-v2-contracts/core/interfaces/IAllo.sol"; |
| 8 | +import {IStrategy} from "allo-v2-contracts/core/interfaces/IStrategy.sol"; |
| 9 | +import {UNAUTHORIZED, INVALID, ALREADY_INITIALIZED, NOT_INITIALIZED, POOL_INACTIVE, POOL_ACTIVE} |
| 10 | + from "allo-v2-contracts/core/libraries/Errors.sol"; |
| 11 | + |
| 12 | +contract MockAllo { |
| 13 | + mapping(uint256 => mapping(address => bool)) public managers; |
| 14 | + |
| 15 | + function setPoolManager(uint256 poolId, address manager, bool allowed) external { |
| 16 | + managers[poolId][manager] = allowed; |
| 17 | + } |
| 18 | + |
| 19 | + function isPoolManager(uint256 poolId, address account) external view returns (bool) { |
| 20 | + return managers[poolId][account]; |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +contract BaseStrategyUpgradeableHarness is BaseStrategyUpgradeable { |
| 25 | + function initializeHarness(address allo_, string memory name_, address owner_) external initializer { |
| 26 | + init(allo_, name_, owner_); |
| 27 | + } |
| 28 | + |
| 29 | + // IStrategy implementation stubs for compilation |
| 30 | + function initialize(uint256 poolId_, bytes memory data) external override { |
| 31 | + __BaseStrategy_init(poolId_); |
| 32 | + emit Initialized(poolId_, data); |
| 33 | + } |
| 34 | + |
| 35 | + function registerRecipient(bytes memory, address) external payable override returns (address) { |
| 36 | + return address(0); |
| 37 | + } |
| 38 | + |
| 39 | + function allocate(bytes memory, address) external payable override {} |
| 40 | + |
| 41 | + function distribute(address[] memory, bytes memory, address) external override {} |
| 42 | + |
| 43 | + function getPoolAmount() external view override returns (uint256) { |
| 44 | + return poolAmount; |
| 45 | + } |
| 46 | + |
| 47 | + function callBaseStrategyInit(uint256 poolId_) external { |
| 48 | + __BaseStrategy_init(poolId_); |
| 49 | + } |
| 50 | + |
| 51 | + // Exposed helpers to reach internal checks |
| 52 | + function exposedCheckOnlyAllo() external view { |
| 53 | + _checkOnlyAllo(); |
| 54 | + } |
| 55 | + |
| 56 | + function exposedCheckOnlyPoolManager(address sender) external view { |
| 57 | + _checkOnlyPoolManager(sender); |
| 58 | + } |
| 59 | + |
| 60 | + function exposedCheckOnlyActivePool() external view { |
| 61 | + _checkOnlyActivePool(); |
| 62 | + } |
| 63 | + |
| 64 | + function exposedCheckInactivePool() external view { |
| 65 | + _checkInactivePool(); |
| 66 | + } |
| 67 | + |
| 68 | + function exposedCheckOnlyInitialized() external view { |
| 69 | + _checkOnlyInitialized(); |
| 70 | + } |
| 71 | + |
| 72 | + function exposedSetPoolActive(bool active) external { |
| 73 | + _setPoolActive(active); |
| 74 | + } |
| 75 | + |
| 76 | + function exposedIsPoolActive() external view returns (bool) { |
| 77 | + return _isPoolActive(); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +contract BaseStrategyUpgradeableTest is Test { |
| 82 | + BaseStrategyUpgradeableHarness internal strategy; |
| 83 | + MockAllo internal allo; |
| 84 | + address internal owner = makeAddr("owner"); |
| 85 | + address internal manager = makeAddr("manager"); |
| 86 | + address internal rando = makeAddr("rando"); |
| 87 | + |
| 88 | + function setUp() public { |
| 89 | + allo = new MockAllo(); |
| 90 | + strategy = new BaseStrategyUpgradeableHarness(); |
| 91 | + strategy.initializeHarness(address(allo), "TEST_STRATEGY", owner); |
| 92 | + } |
| 93 | + |
| 94 | + function test_initSetsOwnerAndAllo() public { |
| 95 | + assertEq(address(strategy.getAllo()), address(allo)); |
| 96 | + assertEq(strategy.owner(), owner); |
| 97 | + assertEq(strategy.getPoolId(), 0); |
| 98 | + } |
| 99 | + |
| 100 | + function test_onlyAlloGuard() public { |
| 101 | + vm.expectRevert(abi.encodeWithSelector(UNAUTHORIZED.selector)); |
| 102 | + strategy.exposedCheckOnlyAllo(); |
| 103 | + |
| 104 | + vm.prank(address(allo)); |
| 105 | + strategy.exposedCheckOnlyAllo(); |
| 106 | + } |
| 107 | + |
| 108 | + function test_BaseStrategyInit_validatesCallerAndPoolId() public { |
| 109 | + vm.prank(address(allo)); |
| 110 | + vm.expectRevert(abi.encodeWithSelector(INVALID.selector)); |
| 111 | + strategy.callBaseStrategyInit(0); |
| 112 | + |
| 113 | + vm.prank(address(allo)); |
| 114 | + strategy.callBaseStrategyInit(1); |
| 115 | + assertEq(strategy.getPoolId(), 1); |
| 116 | + |
| 117 | + vm.prank(address(allo)); |
| 118 | + vm.expectRevert(abi.encodeWithSelector(ALREADY_INITIALIZED.selector)); |
| 119 | + strategy.callBaseStrategyInit(2); |
| 120 | + } |
| 121 | + |
| 122 | + function test_onlyInitializedGuard() public { |
| 123 | + vm.expectRevert(abi.encodeWithSelector(NOT_INITIALIZED.selector)); |
| 124 | + strategy.exposedCheckOnlyInitialized(); |
| 125 | + |
| 126 | + vm.prank(address(allo)); |
| 127 | + strategy.callBaseStrategyInit(7); |
| 128 | + |
| 129 | + strategy.exposedCheckOnlyInitialized(); |
| 130 | + } |
| 131 | + |
| 132 | + function test_onlyPoolManagerGuard() public { |
| 133 | + vm.prank(address(allo)); |
| 134 | + strategy.callBaseStrategyInit(3); |
| 135 | + |
| 136 | + vm.expectRevert(abi.encodeWithSelector(UNAUTHORIZED.selector)); |
| 137 | + strategy.exposedCheckOnlyPoolManager(manager); |
| 138 | + |
| 139 | + allo.setPoolManager(3, manager, true); |
| 140 | + strategy.exposedCheckOnlyPoolManager(manager); |
| 141 | + } |
| 142 | + |
| 143 | + function test_poolActiveStateAndGuards() public { |
| 144 | + vm.expectRevert(abi.encodeWithSelector(POOL_INACTIVE.selector)); |
| 145 | + strategy.exposedCheckOnlyActivePool(); |
| 146 | + |
| 147 | + strategy.exposedCheckInactivePool(); |
| 148 | + assertFalse(strategy.exposedIsPoolActive()); |
| 149 | + |
| 150 | + vm.expectEmit(false, false, false, true); |
| 151 | + emit IStrategy.PoolActive(true); |
| 152 | + strategy.exposedSetPoolActive(true); |
| 153 | + |
| 154 | + assertTrue(strategy.exposedIsPoolActive()); |
| 155 | + |
| 156 | + vm.expectRevert(abi.encodeWithSelector(POOL_ACTIVE.selector)); |
| 157 | + strategy.exposedCheckInactivePool(); |
| 158 | + |
| 159 | + strategy.exposedCheckOnlyActivePool(); |
| 160 | + } |
| 161 | +} |
0 commit comments