-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #597 from etherisc/feature/component-lock
component lock (#590)
- Loading branch information
Showing
50 changed files
with
953 additions
and
420 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,263 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.20; | ||
|
||
import {Amount, AmountLib} from "../type/Amount.sol"; | ||
import {Fee} from "../type/Fee.sol"; | ||
import {IAccountingService} from "./IAccountingService.sol"; | ||
import {InstanceStore} from "../instance/InstanceStore.sol"; | ||
|
||
import {Amount, AmountLib} from "../type/Amount.sol"; | ||
import {Fee} from "../type/Fee.sol"; | ||
import {NftId} from "../type/NftId.sol"; | ||
import {ObjectType, ACCOUNTING, BUNDLE, DISTRIBUTION, DISTRIBUTOR, POOL, PRODUCT} from "../type/ObjectType.sol"; | ||
import {Service} from "../shared/Service.sol"; | ||
|
||
|
||
contract AccountingService is | ||
Service, | ||
IAccountingService | ||
{ | ||
bool private constant INCREASE = true; | ||
bool private constant DECREASE = false; | ||
|
||
function _initialize( | ||
address owner, | ||
bytes memory data | ||
) | ||
internal | ||
virtual override | ||
initializer() | ||
{ | ||
( | ||
address registryAddress, | ||
address authority | ||
) = abi.decode(data, (address, address)); | ||
|
||
_initializeService(registryAddress, authority, owner); | ||
|
||
_registerInterface(type(IAccountingService).interfaceId); | ||
} | ||
|
||
function decreaseComponentFees( | ||
InstanceStore instanceStore, | ||
NftId componentNftId, | ||
Amount feeAmount | ||
) | ||
external | ||
virtual | ||
restricted() | ||
{ | ||
_changeTargetBalance(DECREASE, instanceStore, componentNftId, AmountLib.zero(), feeAmount); | ||
} | ||
|
||
|
||
function increaseProductFees( | ||
InstanceStore instanceStore, | ||
NftId productNftId, | ||
Amount feeAmount | ||
) | ||
external | ||
virtual | ||
restricted() | ||
{ | ||
_checkNftType(productNftId, PRODUCT()); | ||
_changeTargetBalance(INCREASE, instanceStore, productNftId, AmountLib.zero(), feeAmount); | ||
} | ||
|
||
|
||
function decreaseProductFees(InstanceStore instanceStore, NftId productNftId, Amount feeAmount) | ||
external | ||
virtual | ||
restricted() | ||
{ | ||
_checkNftType(productNftId, PRODUCT()); | ||
_changeTargetBalance(DECREASE, instanceStore, productNftId, AmountLib.zero(), feeAmount); | ||
} | ||
|
||
function increaseDistributionBalance( | ||
InstanceStore instanceStore, | ||
NftId distributionNftId, | ||
Amount amount, | ||
Amount feeAmount | ||
) | ||
external | ||
virtual | ||
restricted() | ||
{ | ||
_checkNftType(distributionNftId, DISTRIBUTION()); | ||
_changeTargetBalance(INCREASE, instanceStore, distributionNftId, amount, feeAmount); | ||
} | ||
|
||
|
||
function decreaseDistributionBalance( | ||
InstanceStore instanceStore, | ||
NftId distributionNftId, | ||
Amount amount, | ||
Amount feeAmount | ||
) | ||
external | ||
virtual | ||
restricted() | ||
{ | ||
_checkNftType(distributionNftId, DISTRIBUTION()); | ||
_changeTargetBalance(DECREASE, instanceStore, distributionNftId, amount, feeAmount); | ||
} | ||
|
||
function increaseDistributorBalance( | ||
InstanceStore instanceStore, | ||
NftId distributorNftId, | ||
Amount amount, | ||
Amount feeAmount | ||
) | ||
external | ||
virtual | ||
restricted() | ||
{ | ||
_checkNftType(distributorNftId, DISTRIBUTOR()); | ||
_changeTargetBalance(INCREASE, instanceStore, distributorNftId, amount, feeAmount); | ||
} | ||
|
||
function decreaseDistributorBalance( | ||
InstanceStore instanceStore, | ||
NftId distributorNftId, | ||
Amount amount, | ||
Amount feeAmount | ||
) | ||
external | ||
virtual | ||
restricted() | ||
{ | ||
_checkNftType(distributorNftId, DISTRIBUTOR()); | ||
_changeTargetBalance(DECREASE, instanceStore, distributorNftId, amount, feeAmount); | ||
} | ||
|
||
function increasePoolBalance( | ||
InstanceStore instanceStore, | ||
NftId poolNftId, | ||
Amount amount, | ||
Amount feeAmount | ||
) | ||
public | ||
virtual | ||
restricted() | ||
{ | ||
_checkNftType(poolNftId, POOL()); | ||
_changeTargetBalance(INCREASE, instanceStore, poolNftId, amount, feeAmount); | ||
} | ||
|
||
function decreasePoolBalance( | ||
InstanceStore instanceStore, | ||
NftId poolNftId, | ||
Amount amount, | ||
Amount feeAmount | ||
) | ||
public | ||
virtual | ||
// TODO re-enable once role granting is stable and fixed | ||
// restricted() | ||
{ | ||
_checkNftType(poolNftId, POOL()); | ||
_changeTargetBalance(DECREASE, instanceStore, poolNftId, amount, feeAmount); | ||
} | ||
|
||
function increaseBundleBalance( | ||
InstanceStore instanceStore, | ||
NftId bundleNftId, | ||
Amount amount, | ||
Amount feeAmount | ||
) | ||
external | ||
virtual | ||
restricted() | ||
{ | ||
_checkNftType(bundleNftId, BUNDLE()); | ||
_changeTargetBalance(INCREASE, instanceStore, bundleNftId, amount, feeAmount); | ||
} | ||
|
||
function decreaseBundleBalance( | ||
InstanceStore instanceStore, | ||
NftId bundleNftId, | ||
Amount amount, | ||
Amount feeAmount | ||
) | ||
external | ||
virtual | ||
restricted() | ||
{ | ||
_checkNftType(bundleNftId, BUNDLE()); | ||
_changeTargetBalance(DECREASE, instanceStore, bundleNftId, amount, feeAmount); | ||
} | ||
|
||
function increaseBundleBalanceForPool( | ||
InstanceStore instanceStore, | ||
NftId bundleNftId, | ||
Amount amount, | ||
Amount feeAmount | ||
) | ||
external | ||
virtual | ||
restricted() | ||
{ | ||
_checkNftType(bundleNftId, BUNDLE()); | ||
_changeTargetBalance(INCREASE, instanceStore, bundleNftId, amount, feeAmount); | ||
} | ||
|
||
function decreaseBundleBalanceForPool( | ||
InstanceStore instanceStore, | ||
NftId bundleNftId, | ||
Amount amount, | ||
Amount feeAmount | ||
) | ||
external | ||
virtual | ||
restricted() | ||
{ | ||
_checkNftType(bundleNftId, BUNDLE()); | ||
_changeTargetBalance(DECREASE, instanceStore, bundleNftId, amount, feeAmount); | ||
} | ||
|
||
|
||
//-------- internal functions ------------------------------------------// | ||
|
||
function _changeTargetBalance( | ||
bool increase, | ||
InstanceStore instanceStore, | ||
NftId targetNftId, | ||
Amount amount, | ||
Amount feeAmount | ||
) | ||
internal | ||
virtual | ||
{ | ||
Amount totalAmount = amount + feeAmount; | ||
|
||
if(increase) { | ||
if(totalAmount.gtz()) { instanceStore.increaseBalance(targetNftId, totalAmount); } | ||
if(feeAmount.gtz()) { instanceStore.increaseFees(targetNftId, feeAmount); } | ||
} else { | ||
if(totalAmount.gtz()) { instanceStore.decreaseBalance(targetNftId, totalAmount); } | ||
if(feeAmount.gtz()) { instanceStore.decreaseFees(targetNftId, feeAmount); } | ||
} | ||
} | ||
|
||
|
||
function _logUpdateFee(NftId productNftId, string memory name, Fee memory feeBefore, Fee memory feeAfter) | ||
internal | ||
virtual | ||
{ | ||
emit LogComponentServiceUpdateFee( | ||
productNftId, | ||
name, | ||
feeBefore.fractionalFee, | ||
feeBefore.fixedFee, | ||
feeAfter.fractionalFee, | ||
feeAfter.fixedFee | ||
); | ||
} | ||
|
||
function _getDomain() internal pure virtual override returns(ObjectType) { | ||
return ACCOUNTING(); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.20; | ||
|
||
import {AccountingService} from "./AccountingService.sol"; | ||
import {IVersionable} from "../upgradeability/IVersionable.sol"; | ||
import {ProxyManager} from "../upgradeability/ProxyManager.sol"; | ||
|
||
contract AccountingServiceManager is ProxyManager { | ||
|
||
AccountingService private _accountingService; | ||
|
||
/// @dev initializes proxy manager with service implementation | ||
constructor( | ||
address authority, | ||
address registry, | ||
bytes32 salt | ||
) | ||
{ | ||
AccountingService svc = new AccountingService(); | ||
bytes memory data = abi.encode(registry, authority); | ||
IVersionable versionable = initialize( | ||
registry, | ||
address(svc), | ||
data, | ||
salt); | ||
|
||
_accountingService = AccountingService(address(versionable)); | ||
} | ||
|
||
//--- view functions ----------------------------------------------------// | ||
function getAccountingService() | ||
external | ||
view | ||
returns (AccountingService) | ||
{ | ||
return _accountingService; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.20; | ||
|
||
|
||
import {Amount} from "../type/Amount.sol"; | ||
import {InstanceStore} from "../instance/InstanceStore.sol"; | ||
import {IService} from "../shared/IService.sol"; | ||
import {NftId} from "../type/NftId.sol"; | ||
import {UFixed} from "../type/UFixed.sol"; | ||
|
||
/// @dev component base class | ||
/// component examples are staking, product, distribution, pool and oracle | ||
interface IAccountingService is | ||
IService | ||
{ | ||
event LogComponentServiceUpdateFee( | ||
NftId nftId, | ||
string feeName, | ||
UFixed previousFractionalFee, | ||
uint256 previousFixedFee, | ||
UFixed newFractionalFee, | ||
uint256 newFixedFee | ||
); | ||
|
||
function decreaseComponentFees(InstanceStore instanceStore, NftId componentNftId, Amount feeAmount) external; | ||
|
||
function increaseProductFees(InstanceStore instanceStore, NftId productNftId, Amount feeAmount) external; | ||
function decreaseProductFees(InstanceStore instanceStore, NftId productNftId, Amount feeAmount) external; | ||
|
||
function increaseDistributionBalance(InstanceStore instanceStore, NftId distributionNftId, Amount amount, Amount feeAmount) external; | ||
function decreaseDistributionBalance(InstanceStore instanceStore, NftId distributionNftId, Amount amount, Amount feeAmount) external; | ||
|
||
function increaseDistributorBalance(InstanceStore instanceStore, NftId distributorNftId, Amount amount, Amount feeAmount) external; | ||
function decreaseDistributorBalance(InstanceStore instanceStore, NftId distributorNftId, Amount amount, Amount feeAmount) external; | ||
|
||
function increasePoolBalance(InstanceStore instanceStore, NftId poolNftId, Amount amount, Amount feeAmount) external; | ||
function decreasePoolBalance(InstanceStore instanceStore, NftId poolNftId, Amount amount, Amount feeAmount) external; | ||
|
||
function increaseBundleBalance(InstanceStore instanceStore, NftId bundleNftId, Amount amount, Amount feeAmount) external; | ||
function decreaseBundleBalance(InstanceStore instanceStore, NftId bundleNftId, Amount amount, Amount feeAmount) external; | ||
|
||
function increaseBundleBalanceForPool(InstanceStore instanceStore, NftId bundleNftId, Amount amount, Amount feeAmount) external; | ||
function decreaseBundleBalanceForPool(InstanceStore instanceStore, NftId bundleNftId, Amount amount, Amount feeAmount) external; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
= Accounting | ||
|
||
Contains contracts related to accounting and financial operations. | ||
|
||
== Contracts | ||
|
||
{{AccountingService}} | ||
{{AccountingServiceManager}} |
Oops, something went wrong.