Skip to content

Commit

Permalink
test: add some coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
amarinkovic committed May 17, 2024
1 parent d58adcd commit 0bdf481
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 24 deletions.
13 changes: 0 additions & 13 deletions src/libs/LibHelpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { LibConstants as LC } from "./LibConstants.sol";

/// @notice Pure functions
library LibHelpers {
function _getIdForObjectAtIndex(uint256 _index) internal pure returns (bytes32) {
return keccak256(abi.encodePacked(_index));
}

function _getIdForAddress(address _addr) internal pure returns (bytes32) {
return bytes32(bytes20(_addr));
}
Expand All @@ -25,15 +21,6 @@ library LibHelpers {
return bottom12Bytes == 0;
}

function _checkUpper12BytesAreEmpty(bytes32 value) internal pure returns (bool) {
bytes32 mask = 0xFFFFFFFFFFFFFFFFFFFFFFFF0000000000000000000000000000000000000000;

bytes32 upper12Bytes = value & mask;

// returns true if upper 12 bytes are empty
return upper12Bytes == 0;
}

function _getAddressFromId(bytes32 _id) internal pure returns (address) {
if (!_checkBottom12BytesAreEmpty(_id)) {
revert("Invalid address based ID");
Expand Down
4 changes: 4 additions & 0 deletions test/T01Deployment.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ contract T01DeploymentTest is D03ProtocolDefaults {
nayms.facetAddresses();
}

function testIsInitialized() public {
assertTrue(nayms.isDiamondInitialized(), "Diamond should be initialised");
}

function testInitDiamond() public skipWhenForking {
InitDiamondFixture fixture = new InitDiamondFixture();

Expand Down
6 changes: 5 additions & 1 deletion test/T01GovernanceUpgrades.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ contract T01GovernanceUpgrades is D03ProtocolDefaults, MockAccounts {
f0[0] = TestFacet.sayHello.selector;
cut[0] = IDiamondCut.FacetCut({ facetAddress: address(testFacetAddress), action: IDiamondCut.FacetCutAction.Add, functionSelectors: f0 });

vm.warp(7 days + STARTING_BLOCK_TIMESTAMP + 1);
uint256 expirationPeriod = 7 days;
assertEq(nayms.getUpgradeExpiration(), expirationPeriod, "upgrade expiration should be 7 days");
vm.warp(expirationPeriod + STARTING_BLOCK_TIMESTAMP + 1);

// try to call diamondCut() without scheduling
bytes32 upgradeId = LibGovernance._calculateUpgradeId(cut, address(0), "");
Expand All @@ -77,8 +79,10 @@ contract T01GovernanceUpgrades is D03ProtocolDefaults, MockAccounts {
cut[0] = IDiamondCut.FacetCut({ facetAddress: address(testFacetAddress), action: IDiamondCut.FacetCutAction.Add, functionSelectors: f0 });

bytes32 upgradeId = LibGovernance._calculateUpgradeId(cut, address(0), "");
assertEq(nayms.calculateUpgradeId(cut, address(0), ""), upgradeId, "Upgrade ID should match");

nayms.createUpgrade(upgradeId);

changePrank(owner);
nayms.diamondCut(cut, address(0), "");

Expand Down
8 changes: 0 additions & 8 deletions test/T01LibHelpers.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,10 @@ import { Vm } from "forge-std/Vm.sol";
import { LibHelpers } from "../src/libs/LibHelpers.sol";

contract T01LibHelpers is Test {
function testGetIdForObjectAtIndexFuzz(uint256 i) public {
assertEq(LibHelpers._getIdForObjectAtIndex(i), keccak256(abi.encodePacked(i)));
}

function testGetIdForAddressFuzz(address a) public {
assertEq(LibHelpers._getIdForAddress(a), bytes32(bytes20(a)));
}

function testGetSenderId() public {
assertEq(LibHelpers._getSenderId(), LibHelpers._getIdForAddress(msg.sender));
}

function testGetAddressFromIdFuzz(bytes32 id) public {
bytes32 mask = 0x0000000000000000000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF;
bytes32 bottom12Bytes = id & mask;
Expand Down
10 changes: 9 additions & 1 deletion test/T03TokenizedVault.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ contract T03TokenizedVaultTest is D03ProtocolDefaults, MockAccounts {

// deposit to entity1
changePrank(address(signer1));

vm.expectRevert(abi.encodePacked(ExternalDepositAmountCannotBeZero.selector));
nayms.externalDeposit(wethAddress, 0);

writeTokenBalance(signer1, naymsAddress, wethAddress, depositAmount);
nayms.externalDeposit(wethAddress, externalDepositAmount);
assertEq(weth.balanceOf(signer1), depositAmount - externalDepositAmount, "signer1 WETH balance after externalDeposit should DECREASE (transfer)");
Expand Down Expand Up @@ -249,6 +253,10 @@ contract T03TokenizedVaultTest is D03ProtocolDefaults, MockAccounts {
nayms.assignRole(account0Id, entity1, LC.ROLE_ENTITY_COMPTROLLER_WITHDRAW);

changePrank(signer1);

vm.expectRevert(abi.encodePacked(ExternalWithdrawAmountCannotBeZero.selector));
nayms.externalWithdrawFromEntity(entity1, account0, wethAddress, 0);

nayms.externalWithdrawFromEntity(entity1, account0, wethAddress, 100);

assertEq(weth.balanceOf(account0), account0WethBalanceAccount0 + 100, "account0 got WETH");
Expand Down Expand Up @@ -1214,7 +1222,7 @@ contract T03TokenizedVaultTest is D03ProtocolDefaults, MockAccounts {
nayms.withdrawDividend(eCharlie, entityId, nWETH);
assertEq(nayms.internalBalanceOf(eCharlie, nWETH), balanceBefore + 3e18);
}

function testRebasingTokenInterest() public {
bytes32 acc0EntityId = nayms.getEntity(account0Id);
nayms.assignRole(em.id, acc0EntityId, LC.ROLE_ENTITY_MANAGER);
Expand Down
28 changes: 27 additions & 1 deletion test/T06Staking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { StakingFixture } from "test/fixtures/StakingFixture.sol";
import { DummyToken } from "./utils/DummyToken.sol";
import { LibTokenizedVaultStaking } from "src/libs/LibTokenizedVaultStaking.sol";

import { IntervalRewardPayedOutAlready, InvalidTokenRewardAmount, InvalidStakingAmount, InvalidStaker } from "src/shared/CustomErrors.sol";
import { IntervalRewardPayedOutAlready, InvalidTokenRewardAmount, InvalidStakingAmount, InvalidStaker, EntityDoesNotExist, StakingAlreadyStarted } from "src/shared/CustomErrors.sol";

function makeId2(bytes12 _objecType, bytes20 randomBytes) pure returns (bytes32) {
return bytes32((_objecType)) | (bytes32(randomBytes));
Expand Down Expand Up @@ -153,6 +153,32 @@ contract T06Staking is D03ProtocolDefaults {
vm.stopPrank();
}

function test_initStaking() public {
StakingConfig memory config;

startPrank(sa);

vm.expectRevert(abi.encodeWithSelector(EntityDoesNotExist.selector, bytes32(0)));
nayms.initStaking(bytes32(0), config);
vm.stopPrank();

initStaking(block.timestamp + 1);

config = StakingConfig({
tokenId: NAYMSID,
initDate: block.timestamp + 2,
a: A, // Amplification factor
r: R, // Boost decay factor
divider: SCALE_FACTOR,
interval: I // Amount of time per interval in seconds
});

startPrank(sa);
vm.expectRevert(abi.encodeWithSelector(StakingAlreadyStarted.selector, nlf.entityId, config.tokenId));
nayms.initStaking(nlf.entityId, config);
vm.stopPrank();
}

function test_vtokenId() public {
c.log(" ~ vTokenID Test ~".green());

Expand Down

0 comments on commit 0bdf481

Please sign in to comment.