Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Korokhov committed May 16, 2024
1 parent 26a1a7a commit b69a64d
Show file tree
Hide file tree
Showing 10 changed files with 506 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/contracts/Plugin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity 0.8.25;
import {IPlugin} from "src/interfaces/IPlugin.sol";
import {IFactory} from "src/interfaces/IFactory.sol";

contract Plugin is IPlugin {
abstract contract Plugin is IPlugin {
/**
* @inheritdoc IPlugin
*/
Expand Down
4 changes: 4 additions & 0 deletions src/contracts/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,10 @@ contract Vault is MigratableEntity, ERC6372, ReentrancyGuardUpgradeable, AccessC
revert NetworkAlreadyOptedIn();
}

if (maxNetworkLimit_ == 0) {
revert InvalidMaxNetworkLimit();
}

isNetworkOptedIn[msg.sender][resolver] = true;

_networkLimit[msg.sender][resolver].amount = 0;
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/IVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface IVault {
error NotResolver();
error VetoPeriodEnded();
error NetworkAlreadyOptedIn();
error InvalidMaxNetworkLimit();
error NetworkNotOptedIn();
error OperatorAlreadyOptedIn();
error ExceedsMaxNetworkLimit();
Expand Down
39 changes: 39 additions & 0 deletions test/NonMigratablesRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {Test, console2} from "forge-std/Test.sol";

import {NonMigratablesRegistry} from "src/contracts/NonMigratablesRegistry.sol";
import {INonMigratablesRegistry} from "src/interfaces/INonMigratablesRegistry.sol";

contract NonMigratablesRegistryTest is Test {
address owner;
address alice;
uint256 alicePrivateKey;
address bob;
uint256 bobPrivateKey;

INonMigratablesRegistry registry;

function setUp() public {
owner = address(this);
(alice, alicePrivateKey) = makeAddrAndKey("alice");
(bob, bobPrivateKey) = makeAddrAndKey("bob");
}

function test_Create() public {
registry = new NonMigratablesRegistry();

assertEq(registry.isEntity(alice), false);
}

function test_Register() public {
registry = new NonMigratablesRegistry();

vm.startPrank(alice);
registry.register();
vm.stopPrank();

assertEq(registry.isEntity(alice), true);
}
}
55 changes: 55 additions & 0 deletions test/Plugin.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {Test, console2} from "forge-std/Test.sol";

import {NonMigratablesRegistry} from "src/contracts/NonMigratablesRegistry.sol";
import {IPlugin} from "src/interfaces/IPlugin.sol";

import {SimplePlugin} from "./mocks/SimplePlugin.sol";

contract PluginTest is Test {
address owner;
address alice;
uint256 alicePrivateKey;
address bob;
uint256 bobPrivateKey;

NonMigratablesRegistry registry;

SimplePlugin plugin;

function setUp() public {
owner = address(this);
(alice, alicePrivateKey) = makeAddrAndKey("alice");
(bob, bobPrivateKey) = makeAddrAndKey("bob");

registry = new NonMigratablesRegistry();
}

function test_Create(uint256 number) public {
plugin = new SimplePlugin(address(registry));

assertEq(plugin.REGISTRY(), address(registry));
assertEq(plugin.number(alice), 0);

vm.startPrank(alice);
registry.register();
vm.stopPrank();

vm.startPrank(alice);
plugin.setNumber(number);
vm.stopPrank();

assertEq(plugin.number(alice), number);
}

function test_SetNumberRevertNotEntity(uint256 number) public {
plugin = new SimplePlugin(address(registry));

vm.startPrank(alice);
vm.expectRevert(IPlugin.NotEntity.selector);
plugin.setNumber(number);
vm.stopPrank();
}
}
106 changes: 100 additions & 6 deletions test/Vault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ contract VaultTest is Test {
assertEq(vault.isNetworkOptedIn(address(0), address(0)), false);
assertEq(vault.isOperatorOptedIn(address(0)), false);
assertEq(vault.operatorOptOutAt(address(0)), 0);
assertEq(vault.maxNetworkLimit(address(0), address(0)), 0);
assertEq(vault.networkLimit(address(0), address(0)), 0);
(uint256 nextNetworkLimitAmount, uint256 nextNetworkLimitTimestamp) =
vault.nextNetworkLimit(address(0), address(0));
Expand Down Expand Up @@ -1655,7 +1656,10 @@ contract VaultTest is Test {
);
}

function test_OptInNetwork() public {
function test_OptInNetwork(uint256 networkLimit, uint256 maxNetworkLimit) public {
maxNetworkLimit = bound(maxNetworkLimit, 1, type(uint256).max);
vm.assume(networkLimit <= maxNetworkLimit);

string memory metadataURL = "";
uint48 epochDuration = 1;
uint48 slashDuration = 1;
Expand All @@ -1668,17 +1672,19 @@ contract VaultTest is Test {
_registerNetwork(network, bob);

address resolver = address(1);
_optInNetwork(network, resolver, type(uint256).max);
_optInNetwork(network, resolver, maxNetworkLimit);

assertTrue(vault.isNetworkOptedIn(network, resolver));
(uint256 nextNetworkLimitAmount, uint256 nextNetworkLimitTimestamp) = vault.nextNetworkLimit(network, resolver);
assertEq(nextNetworkLimitAmount, 0);
assertEq(nextNetworkLimitTimestamp, 0);
assertEq(vault.networkLimit(network, resolver), 0);
assertEq(vault.maxNetworkLimit(network, resolver), maxNetworkLimit);

_setNetworkLimit(alice, network, resolver, 5);
_setNetworkLimit(alice, network, resolver, networkLimit);

assertEq(vault.networkLimit(network, resolver), 5);
assertEq(vault.networkLimit(network, resolver), networkLimit);
assertEq(vault.maxNetworkLimit(network, resolver), maxNetworkLimit);

blockTimestamp = blockTimestamp + 1;
vm.warp(blockTimestamp);
Expand All @@ -1689,7 +1695,8 @@ contract VaultTest is Test {
(nextNetworkLimitAmount, nextNetworkLimitTimestamp) = vault.nextNetworkLimit(network, resolver);
assertEq(nextNetworkLimitAmount, 0);
assertEq(nextNetworkLimitTimestamp, vault.currentEpochStart() + 2 * vault.epochDuration());
assertEq(vault.networkLimit(network, resolver), 5);
assertEq(vault.networkLimit(network, resolver), networkLimit);
assertEq(vault.maxNetworkLimit(network, resolver), 0);

blockTimestamp = blockTimestamp + 2;
vm.warp(blockTimestamp);
Expand All @@ -1698,6 +1705,7 @@ contract VaultTest is Test {
assertEq(nextNetworkLimitAmount, 0);
assertEq(nextNetworkLimitTimestamp, vault.currentEpochStart());
assertEq(vault.networkLimit(network, resolver), 0);
assertEq(vault.maxNetworkLimit(network, resolver), 0);

_optInNetwork(network, resolver, type(uint256).max);

Expand All @@ -1706,6 +1714,7 @@ contract VaultTest is Test {
assertEq(nextNetworkLimitAmount, 0);
assertEq(nextNetworkLimitTimestamp, 0);
assertEq(vault.networkLimit(network, resolver), 0);
assertEq(vault.maxNetworkLimit(network, resolver), type(uint256).max);

blockTimestamp = blockTimestamp + 3;
vm.warp(blockTimestamp);
Expand All @@ -1714,6 +1723,7 @@ contract VaultTest is Test {
assertEq(nextNetworkLimitAmount, 0);
assertEq(nextNetworkLimitTimestamp, 0);
assertEq(vault.networkLimit(network, resolver), 0);
assertEq(vault.maxNetworkLimit(network, resolver), type(uint256).max);

_optOutNetwork(network, resolver);

Expand All @@ -1722,6 +1732,22 @@ contract VaultTest is Test {
assertEq(nextNetworkLimitAmount, 0);
assertEq(nextNetworkLimitTimestamp, vault.currentEpochStart() + 2 * vault.epochDuration());
assertEq(vault.networkLimit(network, resolver), 0);
assertEq(vault.maxNetworkLimit(network, resolver), 0);
}

function test_OptInNetworkRevertInvalidMaxNetworkLimit() public {
string memory metadataURL = "";
uint48 epochDuration = 1;
uint48 slashDuration = 1;
uint48 vetoDuration = 0;
vault = _getVault(metadataURL, epochDuration, slashDuration, vetoDuration);

address network = bob;
_registerNetwork(network, bob);

address resolver = address(1);
vm.expectRevert(IVault.InvalidMaxNetworkLimit.selector);
_optInNetwork(network, resolver, 0);
}

function test_OptInNetworkRevertNetworkAlreadyOptedIn() public {
Expand Down Expand Up @@ -2025,7 +2051,33 @@ contract VaultTest is Test {
_setNetworkLimit(alice, network, resolver, amount1);
}

function test_SetOperatorLimit1(uint48 epochDuration, uint256 amount1, uint256 amount2, uint256 amount3) public {
function test_SetNetworkLimitRevertExceedsMaxNetworkLimit(
uint48 epochDuration,
uint256 amount1,
uint256 maxNetworkLimit
) public {
epochDuration = uint48(bound(uint256(epochDuration), 1, 100 days));
maxNetworkLimit = bound(maxNetworkLimit, 1, type(uint256).max);
vm.assume(amount1 > maxNetworkLimit);

string memory metadataURL = "";
uint48 slashDuration = 1;
uint48 vetoDuration = 0;
vault = _getVault(metadataURL, epochDuration, slashDuration, vetoDuration);

uint256 blockTimestamp = block.timestamp * block.timestamp / block.timestamp;

address network = bob;
_registerNetwork(network, bob);

address resolver = address(1);
_optInNetwork(network, resolver, maxNetworkLimit);

vm.expectRevert(IVault.ExceedsMaxNetworkLimit.selector);
_setNetworkLimit(alice, network, resolver, amount1);
}

function test_SetOperatorLimit(uint48 epochDuration, uint256 amount1, uint256 amount2, uint256 amount3) public {
epochDuration = uint48(bound(uint256(epochDuration), 1, 100 days));
vm.assume(amount3 < amount2);

Expand Down Expand Up @@ -2507,6 +2559,48 @@ contract VaultTest is Test {
_claimRewards(alice, rewardClaims);
}

function test_ClaimRewardsRevertInvalidHintsLength(uint256 amount, uint256 ditributeAmount) public {
amount = bound(amount, 1, 100 * 10 ** 18);
ditributeAmount = bound(ditributeAmount, 1, 100 * 10 ** 18);

string memory metadataURL = "";
uint48 epochDuration = 1;
uint48 slashDuration = 1;
uint48 vetoDuration = 0;
vault = _getVault(metadataURL, epochDuration, slashDuration, vetoDuration);

address network = bob;
_registerNetwork(network, bob);

uint256 blockTimestamp = block.timestamp * block.timestamp / block.timestamp;

for (uint256 i; i < 10; ++i) {
_deposit(alice, amount);

blockTimestamp = blockTimestamp + 1;
vm.warp(blockTimestamp);
}

IERC20 token = IERC20(new Token("Token"));
token.transfer(bob, 100_000 * 1e18);
vm.startPrank(bob);
token.approve(address(vault), type(uint256).max);
vm.stopPrank();

uint48 timestamp = 3;
_ditributeReward(bob, network, address(token), ditributeAmount, timestamp);

IVault.RewardClaim[] memory rewardClaims = new IVault.RewardClaim[](1);
rewardClaims[0] = IVault.RewardClaim({
token: address(token),
amountIndexes: type(uint256).max,
activeSharesOfHints: new uint32[](2)
});

vm.expectRevert(IVault.InvalidHintsLength.selector);
_claimRewards(alice, rewardClaims);
}

function test_SetHasDepositWhitelist() public {
string memory metadataURL = "";
uint48 epochDuration = 1;
Expand Down
14 changes: 14 additions & 0 deletions test/mocks/SimplePlugin.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {Plugin} from "src/contracts/Plugin.sol";

contract SimplePlugin is Plugin {
mapping(address entity => uint256 value) public number;

constructor(address registry) Plugin(registry) {}

function setNumber(uint256 number_) external onlyEntity {
number[msg.sender] = number_;
}
}
55 changes: 55 additions & 0 deletions test/plugins/MetadataPlugin.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.25;

import {Test, console2} from "forge-std/Test.sol";

import {NonMigratablesRegistry} from "src/contracts/NonMigratablesRegistry.sol";
import {IPlugin} from "src/interfaces/IPlugin.sol";

import {MetadataPlugin} from "src/contracts/plugins/MetadataPlugin.sol";
import {IMetadataPlugin} from "src/interfaces/plugins/IMetadataPlugin.sol";

contract MetadataPluginTest is Test {
address owner;
address alice;
uint256 alicePrivateKey;
address bob;
uint256 bobPrivateKey;

NonMigratablesRegistry registry;

IMetadataPlugin plugin;

function setUp() public {
owner = address(this);
(alice, alicePrivateKey) = makeAddrAndKey("alice");
(bob, bobPrivateKey) = makeAddrAndKey("bob");

registry = new NonMigratablesRegistry();
}

function test_Create(string calldata metadataURL_) public {
plugin = IMetadataPlugin(address(new MetadataPlugin(address(registry))));

assertEq(plugin.metadataURL(alice), "");

vm.startPrank(alice);
registry.register();
vm.stopPrank();

vm.startPrank(alice);
plugin.setMetadataURL(metadataURL_);
vm.stopPrank();

assertEq(plugin.metadataURL(alice), metadataURL_);
}

function test_SetNumberRevertNotEntity(string calldata metadataURL_) public {
plugin = IMetadataPlugin(address(new MetadataPlugin(address(registry))));

vm.startPrank(alice);
vm.expectRevert(IPlugin.NotEntity.selector);
plugin.setMetadataURL(metadataURL_);
vm.stopPrank();
}
}
Loading

0 comments on commit b69a64d

Please sign in to comment.