-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc7a257
commit 36bc878
Showing
19 changed files
with
285 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,10 +31,15 @@ contract BundleService is | |
|
||
string public constant NAME = "BundleService"; | ||
|
||
address private _registryAddress; | ||
IRegistryService private _registryService; | ||
IAccountingService private _accountingService; | ||
IComponentService private _componentService; | ||
// keccak256(abi.encode(uint256(keccak256("[email protected]")) - 1)) & ~bytes32(uint256(0xff)); | ||
bytes32 public constant BUNDLE_SERVICE_STORAGE_LOCATION_V3_0 = 0x46b8ce3500c11673bb6f380e5e46ed88536ecfaa352cb594644cb469c5a14d00; | ||
|
||
struct BundleServiceStorage { | ||
address _registryAddress; | ||
IRegistryService _registryService; | ||
IAccountingService _accountingService; | ||
IComponentService _componentService; | ||
} | ||
|
||
function _initialize( | ||
address owner, | ||
|
@@ -51,9 +56,10 @@ contract BundleService is | |
|
||
__Service_init(authority, registry, owner); | ||
|
||
_registryService = IRegistryService(_getServiceAddress(REGISTRY())); | ||
_accountingService = IAccountingService(_getServiceAddress(ACCOUNTING())); | ||
_componentService = IComponentService(_getServiceAddress(COMPONENT())); | ||
BundleServiceStorage storage $ = _getBundleServiceStorage(); | ||
$._registryService = IRegistryService(_getServiceAddress(REGISTRY())); | ||
$._accountingService = IAccountingService(_getServiceAddress(ACCOUNTING())); | ||
$._componentService = IComponentService(_getServiceAddress(COMPONENT())); | ||
|
||
_registerInterface(type(IBundleService).interfaceId); | ||
} | ||
|
@@ -99,7 +105,8 @@ contract BundleService is | |
(NftId poolNftId,, IInstance instance) = _getAndVerifyActiveComponent(POOL()); | ||
|
||
// register bundle with registry | ||
bundleNftId = _registryService.registerBundle( | ||
BundleServiceStorage storage $ = _getBundleServiceStorage(); | ||
bundleNftId = $._registryService.registerBundle( | ||
IRegistry.ObjectInfo( | ||
NftIdLib.zero(), | ||
poolNftId, | ||
|
@@ -258,7 +265,8 @@ contract BundleService is | |
Amount balanceAmountWithFees = instanceReader.getBalanceAmount(bundleNftId); | ||
feeAmount = instanceReader.getFeeAmount(bundleNftId); | ||
unstakedAmount = balanceAmountWithFees - feeAmount; | ||
_accountingService.decreaseBundleBalance(instanceStore, bundleNftId, unstakedAmount, feeAmount); | ||
BundleServiceStorage storage $ = _getBundleServiceStorage(); | ||
$._accountingService.decreaseBundleBalance(instanceStore, bundleNftId, unstakedAmount, feeAmount); | ||
} | ||
} | ||
|
||
|
@@ -286,7 +294,8 @@ contract BundleService is | |
} | ||
|
||
// effects | ||
_accountingService.increaseBundleBalance( | ||
BundleServiceStorage storage $ = _getBundleServiceStorage(); | ||
$._accountingService.increaseBundleBalance( | ||
instanceStore, | ||
bundleNftId, | ||
amount, | ||
|
@@ -327,7 +336,8 @@ contract BundleService is | |
} | ||
|
||
// effects | ||
_accountingService.decreaseBundleBalance( | ||
BundleServiceStorage storage $ = _getBundleServiceStorage(); | ||
$._accountingService.decreaseBundleBalance( | ||
instanceStore, | ||
bundleNftId, | ||
unstakedAmount, | ||
|
@@ -388,6 +398,11 @@ contract BundleService is | |
instanceStore.decreaseLocked(bundleNftId, collateralAmount); | ||
} | ||
|
||
function _getBundleServiceStorage() private pure returns (BundleServiceStorage storage $) { | ||
assembly { | ||
$.slot := BUNDLE_SERVICE_STORAGE_LOCATION_V3_0 | ||
} | ||
} | ||
|
||
function _getDomain() internal pure override returns(ObjectType) { | ||
return BUNDLE(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,10 +36,15 @@ contract PoolService is | |
Service, | ||
IPoolService | ||
{ | ||
IAccountingService private _accountingService; | ||
IBundleService internal _bundleService; | ||
IComponentService internal _componentService; | ||
IStaking private _staking; | ||
// keccak256(abi.encode(uint256(keccak256("[email protected]")) - 1)) & ~bytes32(uint256(0xff)); | ||
bytes32 public constant POOL_SERVICE_STORAGE_LOCATION_V3_0 = 0x99b25f02200e1434f4531a215abb8fd8c59ad15f6abb6b06ffa8223eb7162700; | ||
|
||
struct PoolServiceStorage { | ||
IAccountingService _accountingService; | ||
IBundleService _bundleService; | ||
IComponentService _componentService; | ||
IStaking _staking; | ||
} | ||
|
||
function _initialize( | ||
address owner, | ||
|
@@ -56,10 +61,11 @@ contract PoolService is | |
|
||
__Service_init(authority, registry, owner); | ||
|
||
_accountingService = IAccountingService(_getServiceAddress(ACCOUNTING())); | ||
_bundleService = IBundleService(_getServiceAddress(BUNDLE())); | ||
_componentService = IComponentService(_getServiceAddress(COMPONENT())); | ||
_staking = IStaking(getRegistry().getStakingAddress()); | ||
PoolServiceStorage storage $ = _getPoolServiceStorage(); | ||
$._accountingService = IAccountingService(_getServiceAddress(ACCOUNTING())); | ||
$._bundleService = IBundleService(_getServiceAddress(BUNDLE())); | ||
$._componentService = IComponentService(_getServiceAddress(COMPONENT())); | ||
$._staking = IStaking(getRegistry().getStakingAddress()); | ||
|
||
_registerInterface(type(IPoolService).interfaceId); | ||
} | ||
|
@@ -95,9 +101,10 @@ contract PoolService is | |
// TODO get performance fee for pool (#477) | ||
|
||
// releasing collateral in bundle | ||
(Amount unstakedAmount, Amount feeAmount) = _bundleService.close(instance, bundleNftId); | ||
PoolServiceStorage storage $ = _getPoolServiceStorage(); | ||
(Amount unstakedAmount, Amount feeAmount) = $._bundleService.close(instance, bundleNftId); | ||
|
||
_accountingService.decreasePoolBalance( | ||
$._accountingService.decreasePoolBalance( | ||
instance.getInstanceStore(), | ||
poolNftId, | ||
unstakedAmount + feeAmount, | ||
|
@@ -210,13 +217,14 @@ contract PoolService is | |
amount); | ||
|
||
// do all the book keeping | ||
_accountingService.increasePoolBalance( | ||
PoolServiceStorage storage $ = _getPoolServiceStorage(); | ||
$._accountingService.increasePoolBalance( | ||
instanceStore, | ||
poolNftId, | ||
netAmount, | ||
feeAmount); | ||
|
||
_bundleService.stake(instanceReader, instanceStore, bundleNftId, netAmount); | ||
$._bundleService.stake(instanceReader, instanceStore, bundleNftId, netAmount); | ||
|
||
emit LogPoolServiceBundleStaked(instanceNftId, poolNftId, bundleNftId, amount, netAmount); | ||
|
||
|
@@ -249,16 +257,17 @@ contract PoolService is | |
) = PoolLib.checkAndGetPoolInfo(getRegistry(), msg.sender, bundleNftId); | ||
|
||
// call bundle service for bookkeeping and additional checks | ||
Amount unstakedAmount = _bundleService.unstake(instanceStore, bundleNftId, amount); | ||
PoolServiceStorage storage $ = _getPoolServiceStorage(); | ||
Amount unstakedAmount = $._bundleService.unstake(instanceStore, bundleNftId, amount); | ||
|
||
// Important: from now on work only with unstakedAmount as it is the only reliable amount. | ||
// if amount was max, this was set to the available amount | ||
|
||
// TODO: handle performance fees (issue #477) | ||
netAmount = unstakedAmount; | ||
|
||
// update pool bookkeeping - performance fees stay in the pool, but as fees | ||
_accountingService.decreasePoolBalance( | ||
// update pool bookkeeping - performance fees stay in the pool, but as fees | ||
$._accountingService.decreasePoolBalance( | ||
instanceStore, | ||
poolNftId, | ||
unstakedAmount, | ||
|
@@ -355,13 +364,14 @@ contract PoolService is | |
Amount bundleNetAmount = premium.netPremiumAmount; | ||
|
||
InstanceStore instanceStore = instance.getInstanceStore(); | ||
_accountingService.increasePoolBalance( | ||
PoolServiceStorage storage $ = _getPoolServiceStorage(); | ||
$._accountingService.increasePoolBalance( | ||
instanceStore, | ||
poolNftId, | ||
bundleNetAmount + bundleFeeAmount, | ||
poolFeeAmount); | ||
|
||
_accountingService.increaseBundleBalanceForPool( | ||
$._accountingService.increaseBundleBalanceForPool( | ||
instanceStore, | ||
bundleNftId, | ||
bundleNetAmount, | ||
|
@@ -402,14 +412,15 @@ contract PoolService is | |
sumInsuredAmount); | ||
|
||
// lock collateral amount from involved bundle | ||
_bundleService.lockCollateral( | ||
PoolServiceStorage storage $ = _getPoolServiceStorage(); | ||
$._bundleService.lockCollateral( | ||
instance, | ||
applicationNftId, | ||
bundleNftId, | ||
localCollateralAmount); | ||
|
||
// update value locked with staking service | ||
_staking.increaseTotalValueLocked( | ||
$._staking.increaseTotalValueLocked( | ||
instance.getNftId(), | ||
token, | ||
totalCollateralAmount); | ||
|
@@ -449,20 +460,21 @@ contract PoolService is | |
|
||
// effects | ||
NftId poolNftId = getRegistry().getParentNftId(bundleNftId); | ||
PoolServiceStorage storage $ = _getPoolServiceStorage(); | ||
|
||
_accountingService.decreasePoolBalance( | ||
$._accountingService.decreasePoolBalance( | ||
instanceStore, | ||
poolNftId, | ||
payoutAmount, | ||
AmountLib.zero()); | ||
|
||
_accountingService.decreaseBundleBalanceForPool( | ||
$._accountingService.decreaseBundleBalanceForPool( | ||
instanceStore, | ||
bundleNftId, | ||
payoutAmount, | ||
AmountLib.zero()); | ||
|
||
_bundleService.releaseCollateral( | ||
$._bundleService.releaseCollateral( | ||
instanceStore, | ||
policyNftId, | ||
bundleNftId, | ||
|
@@ -471,7 +483,7 @@ contract PoolService is | |
// update value locked with staking service | ||
TokenHandler poolTokenHandler = instanceReader.getTokenHandler(poolNftId); | ||
|
||
_staking.decreaseTotalValueLocked( | ||
$._staking.decreaseTotalValueLocked( | ||
instanceReader.getInstanceNftId(), | ||
address(poolTokenHandler.TOKEN()), | ||
payoutAmount); | ||
|
@@ -519,10 +531,11 @@ contract PoolService is | |
// decrease fee counters by withdrawnAmount | ||
{ | ||
InstanceStore store = instance.getInstanceStore(); | ||
PoolServiceStorage storage $ = _getPoolServiceStorage(); | ||
// decrease fee amount of the bundle | ||
_accountingService.decreaseBundleBalanceForPool(store, bundleNftId, AmountLib.zero(), withdrawnAmount); | ||
$._accountingService.decreaseBundleBalanceForPool(store, bundleNftId, AmountLib.zero(), withdrawnAmount); | ||
// decrease pool balance | ||
_accountingService.decreasePoolBalance(store, poolNftId, withdrawnAmount, AmountLib.zero()); | ||
$._accountingService.decreasePoolBalance(store, poolNftId, withdrawnAmount, AmountLib.zero()); | ||
} | ||
|
||
// interactions | ||
|
@@ -553,15 +566,17 @@ contract PoolService is | |
|
||
Amount remainingCollateralAmount = policyInfo.sumInsuredAmount - policyInfo.claimAmount; | ||
|
||
_bundleService.releaseCollateral( | ||
// effects | ||
PoolServiceStorage storage $ = _getPoolServiceStorage(); | ||
$._bundleService.releaseCollateral( | ||
instance.getInstanceStore(), | ||
policyNftId, | ||
policyInfo.bundleNftId, | ||
remainingCollateralAmount); | ||
|
||
// update value locked with staking service | ||
InstanceReader instanceReader = instance.getInstanceReader(); | ||
_staking.decreaseTotalValueLocked( | ||
$._staking.decreaseTotalValueLocked( | ||
instanceReader.getInstanceNftId(), | ||
address(instanceReader.getToken(policyInfo.productNftId)), | ||
remainingCollateralAmount); | ||
|
@@ -580,6 +595,12 @@ contract PoolService is | |
return PoolLib.getAndVerifyActivePool(getRegistry(), msg.sender); | ||
} | ||
|
||
function _getPoolServiceStorage() private pure returns (PoolServiceStorage storage $) { | ||
assembly { | ||
$.slot := POOL_SERVICE_STORAGE_LOCATION_V3_0 | ||
} | ||
} | ||
|
||
|
||
function _getDomain() internal pure override returns(ObjectType) { | ||
return POOL(); | ||
|
Oops, something went wrong.