@@ -31,10 +31,15 @@ contract PoolService is
3131 Service ,
3232 IPoolService
3333{
34- IAccountingService private _accountingService;
35- IBundleService internal _bundleService;
36- IComponentService internal _componentService;
37- IStaking private _staking;
34+ // keccak256(abi.encode(uint256(keccak256("etherisc.gif.PoolService@3.0.0")) - 1)) & ~bytes32(uint256(0xff));
35+ bytes32 public constant POOL_SERVICE_STORAGE_LOCATION_V3_0 = 0x99b25f02200e1434f4531a215abb8fd8c59ad15f6abb6b06ffa8223eb7162700 ;
36+
37+ struct PoolServiceStorage {
38+ IAccountingService _accountingService;
39+ IBundleService _bundleService;
40+ IComponentService _componentService;
41+ IStaking _staking;
42+ }
3843
3944 function _initialize (
4045 address owner ,
@@ -51,10 +56,11 @@ contract PoolService is
5156
5257 __Service_init (authority, registry, owner);
5358
54- _accountingService = IAccountingService (_getServiceAddress (ACCOUNTING ()));
55- _bundleService = IBundleService (_getServiceAddress (BUNDLE ()));
56- _componentService = IComponentService (_getServiceAddress (COMPONENT ()));
57- _staking = IStaking (getRegistry ().getStakingAddress ());
59+ PoolServiceStorage storage $ = _getPoolServiceStorage ();
60+ $._accountingService = IAccountingService (_getServiceAddress (ACCOUNTING ()));
61+ $._bundleService = IBundleService (_getServiceAddress (BUNDLE ()));
62+ $._componentService = IComponentService (_getServiceAddress (COMPONENT ()));
63+ $._staking = IStaking (getRegistry ().getStakingAddress ());
5864
5965 _registerInterface (type (IPoolService).interfaceId);
6066 }
@@ -90,9 +96,10 @@ contract PoolService is
9096 // TODO get performance fee for pool (#477)
9197
9298 // releasing collateral in bundle
93- (Amount unstakedAmount , Amount feeAmount ) = _bundleService.close (instance, bundleNftId);
99+ PoolServiceStorage storage $ = _getPoolServiceStorage ();
100+ (Amount unstakedAmount , Amount feeAmount ) = $._bundleService.close (instance, bundleNftId);
94101
95- _accountingService.decreasePoolBalance (
102+ $. _accountingService.decreasePoolBalance (
96103 instance.getInstanceStore (),
97104 poolNftId,
98105 unstakedAmount + feeAmount,
@@ -205,13 +212,14 @@ contract PoolService is
205212 amount);
206213
207214 // do all the book keeping
208- _accountingService.increasePoolBalance (
215+ PoolServiceStorage storage $ = _getPoolServiceStorage ();
216+ $._accountingService.increasePoolBalance (
209217 instanceStore,
210218 poolNftId,
211219 netAmount,
212220 feeAmount);
213221
214- _bundleService.stake (instanceReader, instanceStore, bundleNftId, netAmount);
222+ $. _bundleService.stake (instanceReader, instanceStore, bundleNftId, netAmount);
215223
216224 emit LogPoolServiceBundleStaked (instanceNftId, poolNftId, bundleNftId, amount, netAmount);
217225
@@ -244,16 +252,17 @@ contract PoolService is
244252 ) = PoolLib.checkAndGetPoolInfo (getRegistry (), msg .sender , bundleNftId);
245253
246254 // call bundle service for bookkeeping and additional checks
247- Amount unstakedAmount = _bundleService.unstake (instanceStore, bundleNftId, amount);
255+ PoolServiceStorage storage $ = _getPoolServiceStorage ();
256+ Amount unstakedAmount = $._bundleService.unstake (instanceStore, bundleNftId, amount);
248257
249258 // Important: from now on work only with unstakedAmount as it is the only reliable amount.
250259 // if amount was max, this was set to the available amount
251260
252261 // TODO: handle performance fees (issue #477)
253262 netAmount = unstakedAmount;
254263
255- // update pool bookkeeping - performance fees stay in the pool, but as fees
256- _accountingService.decreasePoolBalance (
264+ // update pool bookkeeping - performance fees stay in the pool, but as fees
265+ $. _accountingService.decreasePoolBalance (
257266 instanceStore,
258267 poolNftId,
259268 unstakedAmount,
@@ -350,13 +359,14 @@ contract PoolService is
350359 Amount bundleNetAmount = premium.netPremiumAmount;
351360
352361 InstanceStore instanceStore = instance.getInstanceStore ();
353- _accountingService.increasePoolBalance (
362+ PoolServiceStorage storage $ = _getPoolServiceStorage ();
363+ $._accountingService.increasePoolBalance (
354364 instanceStore,
355365 poolNftId,
356366 bundleNetAmount + bundleFeeAmount,
357367 poolFeeAmount);
358368
359- _accountingService.increaseBundleBalanceForPool (
369+ $. _accountingService.increaseBundleBalanceForPool (
360370 instanceStore,
361371 bundleNftId,
362372 bundleNetAmount,
@@ -399,14 +409,15 @@ contract PoolService is
399409 sumInsuredAmount);
400410
401411 // lock collateral amount from involved bundle
402- _bundleService.lockCollateral (
412+ PoolServiceStorage storage $ = _getPoolServiceStorage ();
413+ $._bundleService.lockCollateral (
403414 instance,
404415 applicationNftId,
405416 bundleNftId,
406417 localCollateralAmount);
407418
408419 // update value locked with staking service
409- _staking.increaseTotalValueLocked (
420+ $. _staking.increaseTotalValueLocked (
410421 instance.getNftId (),
411422 token,
412423 totalCollateralAmount);
@@ -455,20 +466,21 @@ contract PoolService is
455466
456467 // effects
457468 NftId poolNftId = getRegistry ().getParentNftId (bundleNftId);
469+ PoolServiceStorage storage $ = _getPoolServiceStorage ();
458470
459- _accountingService.decreasePoolBalance (
471+ $. _accountingService.decreasePoolBalance (
460472 instanceStore,
461473 poolNftId,
462474 payoutAmount,
463475 AmountLib.zero ());
464476
465- _accountingService.decreaseBundleBalanceForPool (
477+ $. _accountingService.decreaseBundleBalanceForPool (
466478 instanceStore,
467479 bundleNftId,
468480 payoutAmount,
469481 AmountLib.zero ());
470482
471- _bundleService.releaseCollateral (
483+ $. _bundleService.releaseCollateral (
472484 instanceStore,
473485 policyNftId,
474486 bundleNftId,
@@ -477,7 +489,7 @@ contract PoolService is
477489 // update value locked with staking service
478490 TokenHandler poolTokenHandler = instanceReader.getTokenHandler (poolNftId);
479491
480- _staking.decreaseTotalValueLocked (
492+ $. _staking.decreaseTotalValueLocked (
481493 instanceReader.getInstanceNftId (),
482494 address (poolTokenHandler.TOKEN ()),
483495 payoutAmount);
@@ -494,7 +506,7 @@ contract PoolService is
494506 payoutBeneficiary);
495507
496508 if (processingFeeAmount.gtz ()) {
497- _accountingService.increaseProductFeesForPool (
509+ $. _accountingService.increaseProductFeesForPool (
498510 instanceStore,
499511 productNftId,
500512 processingFeeAmount);
@@ -541,10 +553,11 @@ contract PoolService is
541553 // decrease fee counters by withdrawnAmount
542554 {
543555 InstanceStore store = instance.getInstanceStore ();
556+ PoolServiceStorage storage $ = _getPoolServiceStorage ();
544557 // decrease fee amount of the bundle
545- _accountingService.decreaseBundleBalanceForPool (store, bundleNftId, AmountLib.zero (), withdrawnAmount);
558+ $. _accountingService.decreaseBundleBalanceForPool (store, bundleNftId, AmountLib.zero (), withdrawnAmount);
546559 // decrease pool balance
547- _accountingService.decreasePoolBalance (store, poolNftId, withdrawnAmount, AmountLib.zero ());
560+ $. _accountingService.decreasePoolBalance (store, poolNftId, withdrawnAmount, AmountLib.zero ());
548561 }
549562
550563 // interactions
@@ -575,15 +588,17 @@ contract PoolService is
575588
576589 Amount remainingCollateralAmount = policyInfo.sumInsuredAmount - policyInfo.claimAmount;
577590
578- _bundleService.releaseCollateral (
591+ // effects
592+ PoolServiceStorage storage $ = _getPoolServiceStorage ();
593+ $._bundleService.releaseCollateral (
579594 instance.getInstanceStore (),
580595 policyNftId,
581596 policyInfo.bundleNftId,
582597 remainingCollateralAmount);
583598
584599 // update value locked with staking service
585600 InstanceReader instanceReader = instance.getInstanceReader ();
586- _staking.decreaseTotalValueLocked (
601+ $. _staking.decreaseTotalValueLocked (
587602 instanceReader.getInstanceNftId (),
588603 address (instanceReader.getToken (policyInfo.productNftId)),
589604 remainingCollateralAmount);
@@ -607,6 +622,12 @@ contract PoolService is
607622 return PoolLib.getAndVerifyActivePool (getRegistry (), msg .sender );
608623 }
609624
625+ function _getPoolServiceStorage () private pure returns (PoolServiceStorage storage $) {
626+ assembly {
627+ $.slot := POOL_SERVICE_STORAGE_LOCATION_V3_0
628+ }
629+ }
630+
610631
611632 function _getDomain () internal pure override returns (ObjectType) {
612633 return POOL ();
0 commit comments