Skip to content

Commit

Permalink
partially implenent unstake (#418)
Browse files Browse the repository at this point in the history
  • Loading branch information
doerfli committed Jul 2, 2024
1 parent 4023c64 commit e664e06
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 4 deletions.
22 changes: 20 additions & 2 deletions contracts/pool/BundleService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,26 @@ contract BundleService is
virtual
restricted()
{
// TODO: implement
revert();
InstanceStore instanceStore = instance.getInstanceStore();
(
Amount balanceAmount,
Amount lockedAmount,
Amount feeAmount
) = instanceStore.getAmounts(bundleNftId);

// TODO: should the available amount include the fees or not?
Amount availableAmount = balanceAmount - (lockedAmount + feeAmount);

if (availableAmount < amount) {
revert ErrorBundleServiceStakeAmountExceedsLimit(amount, availableAmount);
}

_componentService.decreaseBundleBalance(
instanceStore,
bundleNftId,
amount,
// TODO: if above includes fees, how to split this?
AmountLib.zero());
}


Expand Down
2 changes: 2 additions & 0 deletions contracts/pool/IBundleService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ interface IBundleService is IService {
error ErrorBundleServiceFeesWithdrawAmountIsZero();
error ErrorBundleServiceWalletAllowanceTooSmall(address wallet, address tokenHandler, uint256 allowance, uint256 amount);

error ErrorBundleServiceStakeAmountExceedsLimit(Amount amount, Amount limit);

event LogBundleServiceFeesWithdrawn(NftId bundleNftId, address recipient, address tokenAddress, Amount amount);

/// @dev create a new bundle for the specified attributes
Expand Down
46 changes: 44 additions & 2 deletions contracts/pool/PoolService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,50 @@ contract PoolService is
restricted()
returns(Amount netAmount)
{
// TODO: implement
revert();
(NftId poolNftId,, IInstance instance) = _getAndVerifyActiveComponent(POOL());
InstanceReader instanceReader = instance.getInstanceReader();
IBundle.BundleInfo memory bundleInfo = instanceReader.getBundleInfo(bundleNftId);

if (bundleInfo.poolNftId != poolNftId) {
revert ErrorPoolServiceBundlePoolMismatch(bundleNftId, poolNftId);
}

// call bundle service for bookkeeping and additional checks
_bundleService.unstake(instance, bundleNftId, amount);

(
Amount performanceFeeAmount,
Amount netAmount
) = FeeLib.calculateFee(
_getPerformanceFee(instanceReader, poolNftId),
amount);

// update pool bookkeeping - performance fees stay in the pool, but as fees
_componentService.decreasePoolBalance(
instanceStore,
poolNftId,
amount,
Amount.zero());

_componentService.increasePoolBalance(
instanceStore,
poolNftId,
Amount.zero(),
performanceFeeAmount);

// TODO: transfer net amount to bundle owner (allowance)

// TODO: log event
}

function _getPerformanceFee(InstanceReader instanceReader, NftId poolNftId)
internal
virtual
view
returns (Fee memory performanceFee)
{
NftId productNftId = instanceReader.getPoolInfo(poolNftId).productNftId;
return instanceReader.getPoolInfo(productNftId).performanceFee;
}

function processSale(
Expand Down

0 comments on commit e664e06

Please sign in to comment.