Skip to content

Commit

Permalink
make services upgradeable
Browse files Browse the repository at this point in the history
  • Loading branch information
rapidddenis committed Aug 29, 2024
1 parent 239f4eb commit abd82e6
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 119 deletions.
37 changes: 26 additions & 11 deletions contracts/pool/BundleService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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("etherisc.gif.BundleService.sol")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant BUNDLE_SERVICE_STORAGE_LOCATION_V1 = 0x6548005c3f4340f82f348c576c0ff69f7f529cadd5ad41f96aae61abceeaa300;

struct BundleServiceStorage {
address _registryAddress;
IRegistryService _registryService;
IAccountingService _accountingService;
IComponentService _componentService;
}

function _initialize(
address owner,
Expand All @@ -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);
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -286,7 +294,8 @@ contract BundleService is
}

// effects
_accountingService.increaseBundleBalance(
BundleServiceStorage storage $ = _getBundleServiceStorage();
$._accountingService.increaseBundleBalance(
instanceStore,
bundleNftId,
amount,
Expand Down Expand Up @@ -327,7 +336,8 @@ contract BundleService is
}

// effects
_accountingService.decreaseBundleBalance(
BundleServiceStorage storage $ = _getBundleServiceStorage();
$._accountingService.decreaseBundleBalance(
instanceStore,
bundleNftId,
unstakedAmount,
Expand Down Expand Up @@ -388,6 +398,11 @@ contract BundleService is
instanceStore.decreaseLocked(bundleNftId, collateralAmount);
}

function _getBundleServiceStorage() private pure returns (BundleServiceStorage storage $) {
assembly {
$.slot := BUNDLE_SERVICE_STORAGE_LOCATION_V1
}
}

function _getDomain() internal pure override returns(ObjectType) {
return BUNDLE();
Expand Down
74 changes: 47 additions & 27 deletions contracts/pool/PoolService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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("etherisc.gif.PoolService.sol")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant POOL_SERVICE_STORAGE_LOCATION_V1 = 0x6548005c3f4340f82f348c576c0ff69f7f529cadd5ad41f96aae61abceeaa300;

struct PoolServiceStorage {
IAccountingService _accountingService;
IBundleService _bundleService;
IComponentService _componentService;
IStaking _staking;
}

function _initialize(
address owner,
Expand All @@ -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);
}
Expand Down Expand Up @@ -93,9 +99,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,
Expand Down Expand Up @@ -207,13 +214,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);

Expand Down Expand Up @@ -246,16 +254,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,
Expand Down Expand Up @@ -352,13 +361,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,
Expand Down Expand Up @@ -399,14 +409,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);
Expand Down Expand Up @@ -446,20 +457,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,
Expand All @@ -468,7 +480,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);
Expand Down Expand Up @@ -559,10 +571,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
Expand Down Expand Up @@ -593,15 +606,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);
Expand Down Expand Up @@ -748,6 +763,11 @@ contract PoolService is
// instance = IInstance(instanceAddress);
// }

function _getPoolServiceStorage() private pure returns (PoolServiceStorage storage $) {
assembly {
$.slot := POOL_SERVICE_STORAGE_LOCATION_V1
}
}

function _getDomain() internal pure override returns(ObjectType) {
return POOL();
Expand Down
35 changes: 25 additions & 10 deletions contracts/product/ApplicationService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ contract ApplicationService is
ComponentVerifyingService,
IApplicationService
{
IDistributionService private _distributionService;
IPricingService private _pricingService;
IRegistryService private _registryService;
// keccak256(abi.encode(uint256(keccak256("etherisc.gif.ApplicationService.sol")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant APPLICATION_SERVICE_STORAGE_LOCATION_V1 = 0x6548005c3f4340f82f348c576c0ff69f7f529cadd5ad41f96aae61abceeaa300;

struct ApplicationServiceStorage {
IDistributionService _distributionService;
IPricingService _pricingService;
IRegistryService _registryService;
}

function _initialize(
address owner,
Expand All @@ -47,9 +52,10 @@ contract ApplicationService is

__Service_init(authority, registry, owner);

_distributionService = IDistributionService(_getServiceAddress(DISTRIBUTION()));
_pricingService = IPricingService(_getServiceAddress(PRICE()));
_registryService = IRegistryService(_getServiceAddress(REGISTRY()));
ApplicationServiceStorage storage $ = _getApplicationServiceStorage();
$._distributionService = IDistributionService(_getServiceAddress(DISTRIBUTION()));
$._pricingService = IPricingService(_getServiceAddress(PRICE()));
$._registryService = IRegistryService(_getServiceAddress(REGISTRY()));

_registerInterface(type(IApplicationService).interfaceId);
}
Expand Down Expand Up @@ -88,7 +94,8 @@ contract ApplicationService is
// check referral with distribution
{
if (productInfo.hasDistribution && ! referralId.eqz()) {
if (!_distributionService.referralIsValid(productInfo.distributionNftId, referralId)) {
ApplicationServiceStorage storage $ = _getApplicationServiceStorage();
if (!$._distributionService.referralIsValid(productInfo.distributionNftId, referralId)) {
revert ErrorApplicationServiceReferralInvalid(productNftId, productInfo.distributionNftId, referralId);
}
}
Expand All @@ -113,7 +120,8 @@ contract ApplicationService is
applicationOwner,
"");

applicationNftId = _registryService.registerPolicy(objectInfo);
ApplicationServiceStorage storage $ = _getApplicationServiceStorage();
applicationNftId = $._registryService.registerPolicy(objectInfo);
}


Expand All @@ -125,7 +133,9 @@ contract ApplicationService is
view
returns (Amount premiumAmount)
{
return _pricingService.calculatePremium(
ApplicationServiceStorage storage $ = _getApplicationServiceStorage();

return $._pricingService.calculatePremium(
info.productNftId,
info.riskId,
info.sumInsuredAmount,
Expand Down Expand Up @@ -266,7 +276,12 @@ contract ApplicationService is
}

// internal functions

function _getApplicationServiceStorage() private pure returns (ApplicationServiceStorage storage $) {
// solhint-disable-next-line no-inline-assembly
assembly {
$.slot := APPLICATION_SERVICE_STORAGE_LOCATION_V1
}
}

function _getDomain() internal pure override returns(ObjectType) {
return APPLICATION();
Expand Down
Loading

0 comments on commit abd82e6

Please sign in to comment.