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

component lock (#590) #597

Merged
merged 9 commits into from
Aug 15, 2024
Merged
263 changes: 263 additions & 0 deletions contracts/accounting/AccountingService.sol
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();
}


}
38 changes: 38 additions & 0 deletions contracts/accounting/AccountingServiceManager.sol
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(

Check warning on line 13 in contracts/accounting/AccountingServiceManager.sol

View workflow job for this annotation

GitHub Actions / Build and test (Hardhat)

Explicitly mark visibility in function (Set ignoreConstructors to true if using solidity >=0.7.0)
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;
}
}
45 changes: 45 additions & 0 deletions contracts/accounting/IAccountingService.sol
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;

}
8 changes: 8 additions & 0 deletions contracts/accounting/README.adoc
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}}
Loading
Loading