Skip to content

Commit

Permalink
migrate DistributionService from ComponentVerifyingService to Service (
Browse files Browse the repository at this point in the history
  • Loading branch information
doerfli committed Sep 3, 2024
1 parent bc7a257 commit 2f1157b
Showing 1 changed file with 30 additions and 19 deletions.
49 changes: 30 additions & 19 deletions contracts/distribution/DistributionService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {IComponents} from "../instance/module/IComponents.sol";
import {IPolicy} from "../instance/module/IPolicy.sol";

import {Amount, AmountLib} from "../type/Amount.sol";
import {ComponentVerifyingService} from "../shared/ComponentVerifyingService.sol";
import {ContractLib} from "../shared/ContractLib.sol";
import {Service} from "../shared/Service.sol";
import {DistributorType, DistributorTypeLib} from "../type/DistributorType.sol";
import {NftId, NftIdLib} from "../type/NftId.sol";
import {KEEP_STATE} from "../type/StateId.sol";
Expand All @@ -27,7 +28,7 @@ import {UFixed} from "../type/UFixed.sol";


contract DistributionService is
ComponentVerifyingService,
Service,
IDistributionService
{
IAccountingService private _accountingService;
Expand Down Expand Up @@ -74,10 +75,10 @@ contract DistributionService is
restricted()
returns (DistributorType distributorType)
{
(NftId distributionNftId,, IInstance instance) = _getAndVerifyActiveComponent(DISTRIBUTION());
(NftId distributionNftId, IInstance instance) = _getAndVerifyActiveDistribution(getRegistry());

{
NftId productNftId = _getProductNftId(distributionNftId);
NftId productNftId = getRegistry().getParentNftId(distributionNftId);
IComponents.FeeInfo memory feeInfo = instance.getInstanceReader().getFeeInfo(productNftId);

UFixed variableDistributionFees = feeInfo.distributionFee.fractionalFee;
Expand Down Expand Up @@ -120,7 +121,7 @@ contract DistributionService is
restricted()
returns (NftId distributorNftId)
{
(NftId distributionNftId,, IInstance instance) = _getAndVerifyActiveComponent(DISTRIBUTION());
(NftId distributionNftId, IInstance instance) = _getAndVerifyActiveDistribution(getRegistry());
_checkDistributionType(instance.getInstanceReader(), distributorType, distributionNftId);

distributorNftId = _registryService.registerDistributor(
Expand Down Expand Up @@ -153,7 +154,7 @@ contract DistributionService is
restricted()
{
_checkNftType(distributorNftId, DISTRIBUTOR());
(NftId distributionNftId,, IInstance instance) = _getAndVerifyActiveComponent(DISTRIBUTION());
(NftId distributionNftId, IInstance instance) = _getAndVerifyActiveDistribution(getRegistry());
_checkDistributionType(instance.getInstanceReader(), newDistributorType, distributionNftId);

IDistribution.DistributorInfo memory distributorInfo = instance.getInstanceReader().getDistributorInfo(distributorNftId);
Expand All @@ -177,7 +178,7 @@ contract DistributionService is
onlyNftOfType(distributorNftId, DISTRIBUTOR())
returns (ReferralId referralId)
{
(NftId distributionNftId,, IInstance instance) = _getAndVerifyActiveComponent(DISTRIBUTION());
(NftId distributionNftId, IInstance instance) = _getAndVerifyActiveDistribution(getRegistry());

if (bytes(code).length == 0) {
revert ErrorDistributionServiceInvalidReferral(code);
Expand Down Expand Up @@ -239,8 +240,7 @@ contract DistributionService is
{
if (referralIsValid(distributionNftId, referralId)) {
IRegistry registry = getRegistry();
IRegistry.ObjectInfo memory distributionInfo = registry.getObjectInfo(distributionNftId);
IInstance instance = _getInstanceForComponent(registry, distributionInfo.parentNftId);
IInstance instance = IInstance(ContractLib.getInstanceForComponent(getRegistry(), distributionNftId));

// update book keeping for referral info
IDistribution.ReferralInfo memory referralInfo = instance.getInstanceReader().getReferralInfo(referralId);
Expand All @@ -260,8 +260,7 @@ contract DistributionService is
onlyNftOfType(distributionNftId, DISTRIBUTION())
{
IRegistry registry = getRegistry();
IRegistry.ObjectInfo memory distributionInfo = registry.getObjectInfo(distributionNftId);
IInstance instance = _getInstanceForComponent(registry, distributionInfo.parentNftId);
IInstance instance = IInstance(ContractLib.getInstanceForComponent(registry, distributionNftId));
InstanceReader reader = instance.getInstanceReader();
InstanceStore store = instance.getInstanceStore();

Expand Down Expand Up @@ -298,7 +297,7 @@ contract DistributionService is
onlyNftOfType(distributorNftId, DISTRIBUTOR())
returns (Amount withdrawnAmount)
{
(NftId distributionNftId,, IInstance instance) = _getAndVerifyActiveComponent(DISTRIBUTION());
(NftId distributionNftId, IInstance instance) = _getAndVerifyActiveDistribution(getRegistry());
InstanceReader reader = instance.getInstanceReader();

IComponents.ComponentInfo memory distributionInfo = reader.getComponentInfo(distributionNftId);
Expand Down Expand Up @@ -344,8 +343,7 @@ contract DistributionService is
}

IRegistry registry = getRegistry();
IRegistry.ObjectInfo memory distributionInfo = registry.getObjectInfo(distributionNftId);
IInstance instance = _getInstanceForComponent(registry, distributionInfo.parentNftId);
IInstance instance = IInstance(ContractLib.getInstanceForComponent(registry, distributionNftId));
IDistribution.ReferralInfo memory info = instance.getInstanceReader().getReferralInfo(referralId);

if (info.distributorNftId.eqz()) {
Expand Down Expand Up @@ -373,14 +371,27 @@ contract DistributionService is
}
}

function _getInstanceForDistribution(NftId distributionNftId)
function _getAndVerifyActiveDistribution(
IRegistry registry
)
internal
view
returns(IInstance instance)
returns (
NftId didstributionNftId,
IInstance instance
)
{
NftId instanceNftId = getRegistry().getParentNftId(distributionNftId);
address instanceAddress = getRegistry().getObjectAddress(instanceNftId);
return IInstance(instanceAddress);
(
IRegistry.ObjectInfo memory info,
address instanceAddress
) = ContractLib.getAndVerifyComponent(
registry,
msg.sender,
DISTRIBUTION(),
true); // only active pools

didstributionNftId = info.nftId;
instance = IInstance(instanceAddress);
}

function _getDomain() internal pure override returns(ObjectType) {
Expand Down

0 comments on commit 2f1157b

Please sign in to comment.