Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify storage locations #649

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,14 @@ hh run scripts/find_missing_virtual_methods.ts.ts

Checks all public/external methods in services and components for methods that are not marked as `virtual` but should be.

### find upgradeable contracts storage locations

```bash
grep -o -h -E '([A-Z0-9_]+_LOCATION_V[0-9]+_[0-9]+\s*=\s*0x[A-Ha-h0-9]+)' -r contracts
```

Prints addresses for each storage struct of each release of each upgradeable contract

## Forge

### Commands
Expand Down
6 changes: 3 additions & 3 deletions contracts/distribution/Distribution.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
InstanceLinkedComponent,
IDistributionComponent
{
// keccak256(abi.encode(uint256(keccak256("etherisc.storage.Distribution")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant DISTRIBUTION_STORAGE_LOCATION_V1 = 0xaab7c5ea03d290056d6c060e0833d3ebcbe647f7694616a2ec52738a64b2f900;
// keccak256(abi.encode(uint256(keccak256("etherisc.gif.Distribution@3.0.0")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant DISTRIBUTION_STORAGE_LOCATION_V3_0 = 0xf05be9ae88f1a13816beae3bf4177e164907f5d055d6c38ea0a1fabc08c6c500;

struct DistributionStorage {
IComponentService _componentService;
Expand Down Expand Up @@ -103,7 +103,7 @@
}


function __Distribution_init(

Check warning on line 106 in contracts/distribution/Distribution.sol

View workflow job for this annotation

GitHub Actions / Build and test (Hardhat)

Function name must be in mixedCase
address registry,
NftId productNftId,
IAuthorization authorization,
Expand Down Expand Up @@ -239,7 +239,7 @@

function _getDistributionStorage() private pure returns (DistributionStorage storage $) {
assembly {
$.slot := DISTRIBUTION_STORAGE_LOCATION_V1
$.slot := DISTRIBUTION_STORAGE_LOCATION_V3_0
}
}
}
47 changes: 32 additions & 15 deletions contracts/distribution/DistributionService.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,18 @@ contract DistributionService is
Service,
IDistributionService
{
IAccountingService private _accountingService;
IComponentService private _componentService;
IInstanceService private _instanceService;
IRegistryService private _registryService;

// TODO make all storage slot constants private?
// keccak256(abi.encode(uint256(keccak256("[email protected]")) - 1)) & ~bytes32(uint256(0xff));
bytes32 public constant DISTRIBUTION_SERVICE_STORAGE_LOCATION_V3_0 = 0xabfd9ba715b54654d8f0ef9f71a2076277bb1487677f05f4685d1bba29453600;

struct DistributionServiceStorage {
IAccountingService _accountingService;
IComponentService _componentService;
IInstanceService _instanceService;
IRegistryService _registryService;
}


function _initialize(
address owner,
bytes memory data
Expand All @@ -53,10 +60,11 @@ contract DistributionService is

__Service_init(authority, registry, owner);

_accountingService = IAccountingService(_getServiceAddress(ACCOUNTING()));
_componentService = IComponentService(_getServiceAddress(COMPONENT()));
_instanceService = IInstanceService(_getServiceAddress(INSTANCE()));
_registryService = IRegistryService(_getServiceAddress(REGISTRY()));
DistributionServiceStorage storage $ = _getDistributionServiceStorage();
$._accountingService = IAccountingService(_getServiceAddress(ACCOUNTING()));
$._componentService = IComponentService(_getServiceAddress(COMPONENT()));
$._instanceService = IInstanceService(_getServiceAddress(INSTANCE()));
$._registryService = IRegistryService(_getServiceAddress(REGISTRY()));

_registerInterface(type(IDistributionService).interfaceId);
}
Expand Down Expand Up @@ -129,7 +137,8 @@ contract DistributionService is
(NftId distributionNftId, IInstance instance) = _getAndVerifyActiveDistribution();
_checkDistributionType(instance.getInstanceReader(), distributorType, distributionNftId);

distributorNftId = _registryService.registerDistributor(
DistributionServiceStorage storage $ = _getDistributionServiceStorage();
distributorNftId = $._registryService.registerDistributor(
IRegistry.ObjectInfo(
NftIdLib.zero(),
distributionNftId,
Expand Down Expand Up @@ -279,25 +288,27 @@ contract DistributionService is
// get distribution owner fee amount
Amount distributionOwnerFee = premium.distributionOwnerFeeFixAmount + premium.distributionOwnerFeeVarAmount;

DistributionServiceStorage storage $ = _getDistributionServiceStorage();

// update referral/distributor info if applicable
if (referralIsValid(distributionNftId, referralId)) {

// increase distribution balance by commission amount and distribution owner fee
Amount commissionAmount = premium.commissionAmount;
_accountingService.increaseDistributionBalance(store, distributionNftId, commissionAmount, distributionOwnerFee);
$._accountingService.increaseDistributionBalance(store, distributionNftId, commissionAmount, distributionOwnerFee);

// update book keeping for referral info
IDistribution.ReferralInfo memory referralInfo = reader.getReferralInfo(referralId);

_accountingService.increaseDistributorBalance(store, referralInfo.distributorNftId, AmountLib.zero(), commissionAmount);
$._accountingService.increaseDistributorBalance(store, referralInfo.distributorNftId, AmountLib.zero(), commissionAmount);

// update book keeping for distributor info
IDistribution.DistributorInfo memory distributorInfo = reader.getDistributorInfo(referralInfo.distributorNftId);
distributorInfo.numPoliciesSold += 1;
store.updateDistributor(referralInfo.distributorNftId, distributorInfo, KEEP_STATE());
} else {
// increase distribution balance by distribution owner fee
_accountingService.increaseDistributionBalance(store, distributionNftId, AmountLib.zero(), distributionOwnerFee);
$._accountingService.increaseDistributionBalance(store, distributionNftId, AmountLib.zero(), distributionOwnerFee);
}

emit LogDistributionServiceSaleProcessed(distributionNftId, referralId);
Expand Down Expand Up @@ -331,11 +342,12 @@ contract DistributionService is

// decrease fee counters by withdrawnAmount and update distributor info
{
DistributionServiceStorage storage $ = _getDistributionServiceStorage();
InstanceStore store = instance.getInstanceStore();
// decrease fee counter for distribution balance
_accountingService.decreaseDistributionBalance(store, distributionNftId, withdrawnAmount, AmountLib.zero());
$._accountingService.decreaseDistributionBalance(store, distributionNftId, withdrawnAmount, AmountLib.zero());
// decrease fee counter for distributor fee
_accountingService.decreaseDistributorBalance(store, distributorNftId, AmountLib.zero(), withdrawnAmount);
$._accountingService.decreaseDistributorBalance(store, distributorNftId, AmountLib.zero(), withdrawnAmount);
}

// transfer amount to distributor
Expand Down Expand Up @@ -441,6 +453,11 @@ contract DistributionService is
return PoolLib.getAndVerifyActiveComponent(getRegistry(), msg.sender, DISTRIBUTION());
}

function _getDistributionServiceStorage() private pure returns (DistributionServiceStorage storage $) {
assembly {
$.slot := DISTRIBUTION_SERVICE_STORAGE_LOCATION_V3_0
}
}

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