Skip to content

Commit

Permalink
reduce distribution service size
Browse files Browse the repository at this point in the history
  • Loading branch information
rapidddenis committed Mar 22, 2024
1 parent e1a3431 commit 2058724
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 12 deletions.
61 changes: 50 additions & 11 deletions contracts/instance/service/DistributionService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ contract DistributionService is
distribution.linkToRegisteredNftId();
distributionNftId = distributionInfo.nftId;

InstanceStore instanceStore = instance.getInstanceStore();
instanceStore.createDistributionSetup(distributionNftId, distribution.getSetupInfo());
instance.getInstanceStore().createDistributionSetup(distributionNftId, distribution.getSetupInfo());
// TODO move to distribution?
bytes4[][] memory selectors = new bytes4[][](2);
selectors[0] = new bytes4[](1);
Expand Down Expand Up @@ -290,7 +289,7 @@ contract DistributionService is
revert ErrorIDistributionServiceReferralInvalid(distributionNftId, referralId);
}

(, IInstance instance) = _getAndVerifyDistribution(distributionNftId);
(, IInstance instance) = _getAndVerifyDistributionAndInstance(distributionNftId);
InstanceReader reader = instance.getInstanceReader();
InstanceStore store = instance.getInstanceStore();
IDistribution.ReferralInfo memory referralInfo = reader.getReferralInfo(referralId);
Expand Down Expand Up @@ -332,7 +331,7 @@ contract DistributionService is
view
returns (IPolicy.Premium memory finalPremium)
{
(, IInstance instance) = _getAndVerifyDistribution(distributionNftId);
(, IInstance instance) = _getAndVerifyDistributionAndInstance(distributionNftId);
InstanceReader reader = instance.getInstanceReader();

// first calculate all fixed and variable fees for the distribution - this will defined the fullPremium
Expand Down Expand Up @@ -393,7 +392,7 @@ contract DistributionService is

// TODO: zero should return false
function referralIsValid(NftId distributionNftId, ReferralId referralId) public view returns (bool isValid) {
(address distributionAddress, IInstance instance) = _getAndVerifyDistribution(distributionNftId);
(address distributionAddress, IInstance instance) = _getAndVerifyDistributionAndInstance(distributionNftId);
IDistribution.ReferralInfo memory info = instance.getInstanceReader().getReferralInfo(referralId);

if (info.distributorNftId.eqz()) {
Expand All @@ -404,7 +403,7 @@ contract DistributionService is
isValid = isValid && info.usedReferrals < info.maxReferrals;
}

function _getAndVerifyCallingDistribution()
/*function _getAndVerifyCallingDistribution()
internal
view
returns(
Expand All @@ -430,10 +429,11 @@ contract DistributionService is
internal
view
returns(
address distributionAddress,
address distributionAddress, // TODO always returns 0
IInstance instance
)
{
// TODO check info.objectType == DISTRIBUTION()?
IRegistry.ObjectInfo memory info = getRegistry().getObjectInfo(distributionNftId);
IRegistry.ObjectInfo memory parentInfo = getRegistry().getObjectInfo(info.parentNftId);
if (parentInfo.objectType != INSTANCE()) {
Expand All @@ -453,18 +453,57 @@ contract DistributionService is
)
{
objectAddress = msg.sender;
objectNftId = getRegistry().getNftId(objectAddress);
if ( objectNftId.eqz()) {
IRegistry.ObjectInfo memory info = getRegistry().getObjectInfo(objectAddress);
objectNftId = info.nftId;
objectType = info.objectType;
if(objectNftId.eqz()) {
revert ErrorIServiceCallerUnknown(objectAddress);
}
IRegistry.ObjectInfo memory info = getRegistry().getObjectInfo(objectNftId);
objectType = info.objectType;
IRegistry.ObjectInfo memory parentInfo = getRegistry().getObjectInfo(info.parentNftId);
if (parentInfo.objectType != INSTANCE()) {
revert ErrorIDistributionServiceParentNftIdNotInstance(objectNftId, info.parentNftId);
}
instance = IInstance(parentInfo.objectAddress);
}*/




function _getAndVerifyDistributionAndInstance(NftId distributionNftId)
internal
view
returns(
address distributionAddress,
IInstance instance
)
{
IRegistry.ObjectInfo memory info = getRegistry().getObjectInfo(distributionNftId);

if(info.objectType != DISTRIBUTION()) {
revert ErrorIDistributionServiceNotDistribution(distributionNftId);
}

IRegistry.ObjectInfo memory parentInfo = getRegistry().getObjectInfo(info.parentNftId);
if (parentInfo.objectType != INSTANCE()) {
revert ErrorIDistributionServiceParentNftIdNotInstance(distributionNftId, info.parentNftId);
}
instance = IInstance(parentInfo.objectAddress);
distributionAddress = info.objectAddress;
}

function _getAndVerifyCallingDistribution()
internal
view
returns(
address distributionAddress,
NftId distributionNftId,
IInstance instance
)
{
distributionNftId = getRegistry().getNftId(msg.sender);
(distributionAddress, instance) = _getAndVerifyDistributionAndInstance(distributionNftId);
}

}
2 changes: 1 addition & 1 deletion contracts/instance/service/IDistributionService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {Timestamp} from "../../types/Timestamp.sol";

interface IDistributionService is IService {
error ErrorIDistributionServiceParentNftIdNotInstance(NftId nftId, NftId parentNftId);
error ErrorIDistributionServiceCallerNotDistributor(address caller);
error ErrorIDistributionServiceNotDistribution(NftId nftId);
error ErrorIDistributionServiceInvalidReferralId(ReferralId referralId);
error ErrorIDistributionServiceMaxReferralsExceeded(uint256 maxReferrals);
error ErrorIDistributionServiceDiscountTooLow(uint256 minDiscountPercentage, uint256 discountPercentage);
Expand Down

0 comments on commit 2058724

Please sign in to comment.