Skip to content

Commit

Permalink
add unit tests (#476)
Browse files Browse the repository at this point in the history
  • Loading branch information
doerfli committed Jul 9, 2024
1 parent ef44499 commit fa320a8
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 12 deletions.
30 changes: 18 additions & 12 deletions contracts/pool/PoolService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {IStaking} from "../staking/IStaking.sol";

import {Amount, AmountLib} from "../type/Amount.sol";
import {Fee, FeeLib} from "../type/Fee.sol";
import {NftId, NftIdLib} from "../type/NftId.sol";
import {NftId} from "../type/NftId.sol";
import {ObjectType, POOL, BUNDLE, COMPONENT, INSTANCE, REGISTRY} from "../type/ObjectType.sol";
import {RoleId, PUBLIC_ROLE} from "../type/RoleId.sol";
import {Fee, FeeLib} from "../type/Fee.sol";
Expand Down Expand Up @@ -75,11 +75,9 @@ contract PoolService is
{
(NftId poolNftId,, IInstance instance) = _getAndVerifyActiveComponent(POOL());
InstanceReader instanceReader = instance.getInstanceReader();
IComponents.PoolInfo memory poolInfo = instanceReader.getPoolInfo(poolNftId);

IComponents.ComponentInfo memory componentInfo = instanceReader.getComponentInfo(poolNftId);
IComponents.PoolInfo memory poolInfo = abi.decode(componentInfo.data, (IComponents.PoolInfo));
Amount previousMaxBalanceAmount = poolInfo.maxBalanceAmount;

poolInfo.maxBalanceAmount = maxBalanceAmount;
instance.getInstanceStore().updatePool(poolNftId, poolInfo, KEEP_STATE());

Expand Down Expand Up @@ -126,7 +124,14 @@ contract PoolService is
_getStakingFee(instance.getInstanceReader(), poolNftId),
stakingAmount);

// TODO: (staking amount + existing pool balance) must be be > maxCapitalAmount
{
InstanceReader instanceReader = instance.getInstanceReader();
IComponents.PoolInfo memory poolInfo = instanceReader.getPoolInfo(poolNftId);
Amount currentPoolBalance = instanceReader.getBalanceAmount(poolNftId);
if (currentPoolBalance + netStakedAmount > poolInfo.maxBalanceAmount) {
revert ErrorPoolServiceMaxBalanceAmountExceeded(poolNftId, poolInfo.maxBalanceAmount, currentPoolBalance, netStakedAmount);
}
}

bundleNftId = _bundleService.create(
instance,
Expand Down Expand Up @@ -196,19 +201,20 @@ contract PoolService is
revert ErrorPoolServiceBundlePoolMismatch(bundleNftId, poolNftId);
}

Amount currentPoolBalance = instanceReader.getBalanceAmount(poolNftId);
if (amount + currentPoolBalance > poolInfo.maxBalanceAmount) {
revert ErrorPoolServiceMaxBalanceAmountExceeded(poolNftId, poolInfo.maxBalanceAmount, currentPoolBalance, amount);
}

// calculate fees
Amount feeAmount;
(
Amount feeAmount,
Amount netAmount
feeAmount,
netAmount
) = FeeLib.calculateFee(
_getStakingFee(instanceReader, poolNftId),
amount);

Amount currentPoolBalance = instanceReader.getBalanceAmount(poolNftId);
if (currentPoolBalance + netAmount > poolInfo.maxBalanceAmount) {
revert ErrorPoolServiceMaxBalanceAmountExceeded(poolNftId, poolInfo.maxBalanceAmount, currentPoolBalance, netAmount);
}

// do all the bookkeeping
_componentService.increasePoolBalance(
instance.getInstanceStore(),
Expand Down
63 changes: 63 additions & 0 deletions test/TestPool.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {IBundle} from "../contracts/instance/module/IBundle.sol";
import {IComponents} from "../contracts/instance/module/IComponents.sol";
import {IKeyValueStore} from "../contracts/shared/IKeyValueStore.sol";
import {ILifecycle} from "../contracts/shared/ILifecycle.sol";
import {IPoolService} from "../contracts/pool/IPoolService.sol";
import {Key32} from "../contracts/type/Key32.sol";
import {NftId, NftIdLib} from "../contracts/type/NftId.sol";
import {ObjectType, BUNDLE} from "../contracts/type/ObjectType.sol";
Expand Down Expand Up @@ -201,6 +202,68 @@ contract TestPool is GifTest {
"unexpected activatedAt");
}

function test_PoolCreateBundle_twoBundlesMaxBalanceExceeded() public {
// GIVEN
_prepareProduct(false);

vm.startPrank(poolOwner);
pool.setMaxBalanceAmount(AmountLib.toAmount(5000));
vm.stopPrank();

// WHEN
vm.startPrank(investor);

Seconds lifetime = SecondsLib.toSeconds(604800);
Fee memory zeroFee = FeeLib.zero();

// THEN
vm.expectRevert(abi.encodeWithSelector(
IPoolService.ErrorPoolServiceMaxBalanceAmountExceeded.selector,
poolNftId,
AmountLib.toAmount(5000),
AmountLib.toAmount(0),
AmountLib.toAmount(10000)));

// WHEN
pool.createBundle(
zeroFee,
10000,
lifetime,
""
);
}

function test_PoolCreateBundle_maxBalanceExceeded() public {
// GIVEN
_prepareProduct(true);

vm.startPrank(poolOwner);
pool.setMaxBalanceAmount(AmountLib.toAmount(15000));
vm.stopPrank();

// WHEN
vm.startPrank(investor);

Seconds lifetime = SecondsLib.toSeconds(604800);
Fee memory zeroFee = FeeLib.zero();

// THEN
vm.expectRevert(abi.encodeWithSelector(
IPoolService.ErrorPoolServiceMaxBalanceAmountExceeded.selector,
poolNftId,
AmountLib.toAmount(10000),
AmountLib.toAmount(10000),
AmountLib.toAmount(10000)));

// WHEN
pool.createBundle(
zeroFee,
10000,
lifetime,
""
);
}

function test_PoolCreateBundle_withStakingFee() public {
// GIVEN
initialStakingFee = FeeLib.percentageFee(10);
Expand Down

0 comments on commit fa320a8

Please sign in to comment.