Skip to content

Commit

Permalink
refactor: split balance manager from withdrawable features
Browse files Browse the repository at this point in the history
  • Loading branch information
geolffreym committed Sep 15, 2024
1 parent 31bc502 commit 8dbe7c7
Show file tree
Hide file tree
Showing 27 changed files with 80 additions and 635 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ node_modules
/typechain
/typechain-types

excluded/*
# solidity-coverage files
/coverage
/coverage.json
Expand Down
22 changes: 15 additions & 7 deletions contracts/RightsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,16 @@ contract RightsManager is

// initialize dependencies for RM
IRepository repo = IRepository(repository);
address wvc = repo.getContract(T.ContractTypes.WVC);
address treasuryAddress = repo.getContract(T.ContractTypes.TRE);
address syndicationAddress = repo.getContract(T.ContractTypes.SYN);
address referendumAddress = repo.getContract(T.ContractTypes.REF);

syndication = IRegistrableVerifiable(syndicationAddress);
referendum = IReferendumVerifiable(referendumAddress);

__Fees_init(initialFee, address(0));
// TODO aca WVC
__Fees_init(initialFee, wvc);
__Treasurer_init(treasuryAddress);
}

Expand Down Expand Up @@ -203,6 +205,14 @@ contract RightsManager is
_setTreasuryAddress(newTreasuryAddress);
}

/// @inheritdoc IBalanceManager
/// @notice Returns the contract's balance for the specified currency.
/// @param currency The address of the token to check the balance of (address(0) for native currency).
/// @return The balance of the contract in the specified currency.
function getBalance(address currency) external view returns (uint256) {
return address(this).balanceOf(currency);
}

/// @inheritdoc IDisburser
/// @notice Disburses funds from the contract to a specified address.
/// @param amount The amount of currencies to disburse.
Expand All @@ -217,7 +227,7 @@ contract RightsManager is
emit FeesDisbursed(treasury, amount, currency);
}

/// @inheritdoc IFundsManager
/// @inheritdoc IBalanceManagerWithdrawable
/// @notice Withdraws tokens from the contract to a specified recipient's address.
/// @dev This implementation enforce th withdraw of token only for distributors by registered manager.
/// @param recipient The address that will receive the withdrawn tokens.
Expand All @@ -229,9 +239,7 @@ contract RightsManager is
address currency
) external onlyValidCurrency(currency) onlyActiveDistributor(recipient) {
IDistributor distributor = IDistributor(recipient);
uint256 available = getLedgerEntry(recipient, currency);

if (available < amount)
if (getLedgerBalance(recipient, currency) < amount)
revert NoFundsToWithdraw("Insuficient funds to withdraw.");

if (_msgSender() != distributor.getManager())
Expand Down Expand Up @@ -331,7 +339,7 @@ contract RightsManager is
address currency,
address custodial
)
external
public
onlySupportedCurrency(currency)
onlyActiveDistributor(custodial)
returns (uint256 tFees, uint256 dFees)
Expand Down Expand Up @@ -417,7 +425,7 @@ contract RightsManager is
if (!success) revert NoDeal(reason);

_closeDeal(dealProof); // inactivate the deal after success..
_sumLedgerEntry(distributor, deal.fees, deal.currency);
_sumLedgerEntry(deal.custodial, deal.fees, deal.currency);
_registerPolicy(deal.account, policyAddress);
emit AccessGranted(deal.account, dealProof, policyAddress);
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/Treasury.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pragma solidity ^0.8.24;
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "contracts/base/upgradeable/GovernableUpgradeable.sol";
import "contracts/interfaces/IBalanceManagerWithdrawable.sol";
import "contracts/interfaces/IFeesManager.sol";
import "contracts/interfaces/IFundsManager.sol";

// TODO payment splitter
// https://docs.openzeppelin.com/contracts/4.x/api/finance#PaymentSplitter
Expand All @@ -18,7 +18,7 @@ contract Treasury is
Initializable,
UUPSUpgradeable,
GovernableUpgradeable,
IFundsManager
IBalanceManagerWithdrawable
{
/// @dev Constructor that disables initializers to prevent the implementation contract from being initialized.
/// @notice This constructor prevents the implementation contract from being initialized.
Expand Down
2 changes: 1 addition & 1 deletion contracts/base/upgradeable/LedgerUpgradeable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ abstract contract LedgerUpgradeable is Initializable, ILedger {
/// @notice Retrieves the registered currency amount for the specified account.
/// @param account The address of the account.
/// @param currency The currency to retrieve ledger amount.
function getLedgerEntry(
function getLedgerBalance(
address account,
address currency
) public view returns (uint256) {
Expand Down
10 changes: 8 additions & 2 deletions contracts/interfaces/IDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ pragma solidity ^0.8.24;

import "./IFeesManager.sol";
import "./ICurrencyManager.sol";
import "./IFundsManager.sol";
import "./IBalanceManager.sol";
import "./IBalanceManagerWithdrawable.sol";

interface IDistributor is IFeesManager, ICurrencyManager, IFundsManager {
interface IDistributor is
IFeesManager,
ICurrencyManager,
IBalanceManager,
IBalanceManagerWithdrawable
{
/// @notice Set the endpoint of the distributor.
/// @dev This function can only be called by the owner of the contract.
/// @param _endpoint The new distributor's endpoint.
Expand Down
17 changes: 10 additions & 7 deletions contracts/interfaces/IFeesManager.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

/// @title IFeesManager Interface
/// @notice This interface defines functions for managing and retrieving treasury fees associated with different currencies.

Check failure on line 5 in contracts/interfaces/IFeesManager.sol

View workflow job for this annotation

GitHub Actions / solhint linting

Line length must be no more than 120 but current length is 124
/// @dev This interface is intended to be implemented by contracts that manage platform or treasury fees for multiple currencies.

Check failure on line 6 in contracts/interfaces/IFeesManager.sol

View workflow job for this annotation

GitHub Actions / solhint linting

Line length must be no more than 120 but current length is 129
interface IFeesManager {

/// @notice Sets a new treasury fee.
/// @param newTreasuryFee The new treasury fee %.
/// @param currency The currency to associate fees with. Use address(0) for the native coin.
/// @notice Sets a new treasury fee for a specific currency.
/// @param newTreasuryFee The new treasury fee percentage (e.g., 5 for 5%).
/// @param currency The address of the currency to associate fees with. Use address(0) for the native coin.
/// @notice Only the owner can call this function.
function setFees(uint256 newTreasuryFee, address currency) external;

/// @notice Returns the current treasury fee %.
/// @param currency The address of the currency for which to retrieve the fees fee.
/// @return The treasury fee.
/// @notice Returns the current treasury fee percentage for a specific currency.
/// @param currency The address of the currency for which to retrieve the treasury fee. Use address(0) for the native coin.

Check failure on line 16 in contracts/interfaces/IFeesManager.sol

View workflow job for this annotation

GitHub Actions / solhint linting

Line length must be no more than 120 but current length is 127
/// @return The treasury fee percentage.
function getFees(address currency) external view returns (uint256);
}
}
16 changes: 0 additions & 16 deletions contracts/interfaces/IFundsManager.sol

This file was deleted.

8 changes: 3 additions & 5 deletions contracts/interfaces/IGovernable.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// SPDX-License-Identifier: MIT
// NatSpec format convention - https://docs.soliditylang.org/en/v0.5.10/natspec-format.html

pragma solidity ^0.8.24;
/**
* @title IGovernable
* @dev Interface for managing governance and emergency admin functionalities.
*/

/// @title IGovernable
/// @dev Interface for managing governance and emergency admin functionalities.
interface IGovernable {
/// @notice Sets the privileged governance role.
/// @custom:permissions Governance.
Expand Down
10 changes: 6 additions & 4 deletions contracts/interfaces/ILedger.sol
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

/// @title ILedger Interface
/// @notice This interface defines the functionality for retrieving ledger entries for accounts.
/// @dev The interface is intended to be implemented by contracts that track registered fund amounts for specific accounts and currencies.

Check failure on line 6 in contracts/interfaces/ILedger.sol

View workflow job for this annotation

GitHub Actions / solhint linting

Line length must be no more than 120 but current length is 138
interface ILedger {

/// @notice Retrieves the registered currency amount for the specified account.
/// @param account The address of the account.
/// @param currency The currency to retrieve ledger amount.
/// @return The amount of registered fund for the account.
function getLedgerEntry(
/// @param currency The address of the currency to retrieve ledger amount (use address(0) for the native currency).
/// @return The amount of registered fund for the account in the specified currency.
function getLedgerBalance(
address account,
address currency
) external view returns (uint256);
Expand Down
3 changes: 2 additions & 1 deletion contracts/interfaces/IPolicy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ interface IPolicy {

/// @notice Returns the business/strategy model implemented by the policy.
/// @return A detailed description of the subscription policy as bytes.
function description() external pure override returns (bytes memory);
/// @dev A bytes string encoded packed is expected on the return.
function description() external pure returns (bytes memory);

/// @notice Retrieves the access terms for a specific account and content ID.
/// @param account The address of the account for which access terms are being retrieved.
Expand Down
8 changes: 5 additions & 3 deletions contracts/interfaces/IRightsManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@ pragma solidity ^0.8.24;
import "./ITreasurer.sol";
import "./IFeesManager.sol";
import "./IDisburser.sol";
import "./IContentVault.sol";
import "./IRightsCustodial.sol";
import "./IBalanceManager.sol";
import "./IRightsDealBroker.sol";
import "./IRightsPolicyAuditor.sol";
import "./IRightsPolicyController.sol";
import "./IRightsCustodialGranter.sol";
import "./IRightsAccessController.sol";
import "./IFundsManager.sol";
import "./IContentVault.sol";
import "./IBalanceManagerWithdrawable.sol";

interface IRightsManager is
ITreasurer,
IDisburser,
IFeesManager,
IFundsManager,
IBalanceManager,
IRightsCustodial,
IRightsDealBroker,
IRightsAccessController,
IRightsCustodialGranter,
IRightsPolicyController,
IBalanceManagerWithdrawable,
IRightsPolicyAuditor
{
/// @notice Checks if the content is eligible for distribution by the content holder's custodial.
Expand Down
2 changes: 2 additions & 0 deletions contracts/interfaces/ISyndicatable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "./ITreasurer.sol";
import "./IDisburser.sol";
import "./IFeesManager.sol";
import "./IRegistrable.sol";
import "./IBalanceManager.sol";
import "./IRegistrableRevokable.sol";
import "./IRegistrableVerifiable.sol";

Expand All @@ -19,6 +20,7 @@ interface ISyndicatable is
IDisburser,
IRegistrable,
IFeesManager,
IBalanceManager,
IRegistrableRevokable,
IRegistrableVerifiable
{
Expand Down
Loading

0 comments on commit 8dbe7c7

Please sign in to comment.