Skip to content

Commit

Permalink
Merge pull request #1 from WatchItDev/gated/access
Browse files Browse the repository at this point in the history
Gated/access
  • Loading branch information
geolffreym authored Aug 26, 2024
2 parents c4fc27c + 3184eff commit 9e0a8c4
Show file tree
Hide file tree
Showing 35 changed files with 706 additions and 602 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ node_modules
/coverage.json
# aderyn report
report.md
cache_forge/
lib/
out/

# Hardhat Ignition default folder for deployments against a local node
ignition/deployments/chain-31337
1 change: 0 additions & 1 deletion cache_forge/solidity-files-cache.json

This file was deleted.

66 changes: 47 additions & 19 deletions contracts/Distributor.sol
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts-upgradeable/utils/introspection/ERC165Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

import "contracts/base/upgradeable/CurrencyManagerUpgradeable.sol";
import "contracts/base/upgradeable/FeesManagerUpgradeable.sol";
import "contracts/libraries/TreasuryHelper.sol";
import "contracts/libraries/MathHelper.sol";
import "contracts/libraries/FeesHelper.sol";
import "contracts/interfaces/IDistributor.sol";

/// @title Content Distributor contract.
Expand All @@ -25,21 +26,18 @@ contract Distributor is
CurrencyManagerUpgradeable,
IDistributor
{
using Math for uint256;
using TreasuryHelper for address;
using MathHelper for uint256;
using FeesHelper for uint256;

/// @notice The URL to the distribution.
/// Since this is a contract considered as implementation for beacon proxy,
/// we need to reserve a gap for endpoint to avoid memory layout getting mixed up.
/// https://docs.openzeppelin.com/upgrades-plugins/1.x/writing-upgradeable
string private endpoint;
mapping(address => uint256) private floor;
string private endpoint;
uint256 flattenFactor; // To smooth or flatten the increase in fees as demand grows.

/// @notice Event emitted when the endpoint is updated.
/// @param oldEndpoint The old endpoint.
/// @param newEndpoint The new endpoint.
event EndpointUpdated(string oldEndpoint, string newEndpoint);

/// @notice Error to be thrown when an invalid endpoint is provided.
error InvalidEndpoint();

Expand All @@ -54,6 +52,8 @@ contract Distributor is
__Fees_init(0, address(0));

if (bytes(_endpoint).length == 0) revert InvalidEndpoint();
// balanced factors
flattenFactor = 10;
endpoint = _endpoint;
}

Expand All @@ -72,11 +72,11 @@ contract Distributor is
}

/// @inheritdoc IDistributor
/// @notice Updates the distribution endpoint URL.
/// @notice Set the distribution endpoint URL.
/// @param _endpoint The new endpoint URL to be set.
/// @dev This function reverts if the provided endpoint is an empty string.
/// @dev Emits an {EndpointUpdated} event.
function updateEndpoint(string calldata _endpoint) external onlyOwner {
function setEndpoint(string calldata _endpoint) external onlyOwner {
if (bytes(_endpoint).length == 0) revert InvalidEndpoint();
string memory oldEndpoint = endpoint;
endpoint = _endpoint;
Expand Down Expand Up @@ -110,6 +110,13 @@ contract Distributor is
_addCurrency(address(0));
}

/// @notice Sets the scaling and flattening factors used to calculate fees.
/// @dev This function allows the administrator to adjust how sensitive the fees are to changes in demand.
/// @param flatten The flattening factor that controls how gradual or smooth the fee increase is.
function setFactors(uint256 flatten) public onlyOwner {
flattenFactor = flatten;
}

/// @inheritdoc IDistributor
/// @notice Sets the minimum floor value for fees associated with a specific currency.
/// @dev This function can only be called by the owner and for supported currencies.
Expand All @@ -121,22 +128,43 @@ contract Distributor is
) external onlyOwner onlySupportedCurrency(currency) {
floor[currency] = minimum;
}


// flatten = flatten * (1-(1/ln(demanda))

/// @notice Calculates an adjusted floor value based on the logarithm of custodials.
/// @dev The function adjusts the base floor by adding a proportion
/// that scales with the logarithm of the custodials.
/// This ensures that the floor value increases gradually as custodials grow.
/// @param baseFloor The initial base floor value to be adjusted.
/// @param demand The number of custodials, which influences the adjustment.
function _getAdjustedFloor(
uint256 baseFloor,
uint256 demand
) internal view returns (uint256) {
if (baseFloor == 0) return 0;
// Economies of scale.
// Calculate the logarithm of custodials, adding 1 to avoid taking log(0)
// fees + (fees * (log2(demand) / flatten))
uint256 safeOp = (demand == 0 ? (demand + 1) : demand);
return baseFloor + (baseFloor * (safeOp.log2() / flattenFactor));
}

/// @inheritdoc IDistributor
/// @notice Proposes a fee to the distributor by adjusting it according to a predefined floor value.
/// @param fees The initial fee amount proposed.
/// @param currency The currency in which the fees are proposed.
/// @return acceptedFees The final fee amount after adjustment, ensuring it meets the floor value.
/// @notice Adjusts the proposed fee amount for the distributor according to the custodial charge.
/// @param fees The initial fee amount proposed by the distributor.
/// @param currency The currency in which the fees are denominated.
/// @param demand The amount of content under the distributor's custody.
/// @return The final fee amount after adjustment, ensuring it meets or exceeds the minimum floor value.
function negotiate(
uint256 fees,
address currency
address currency,
uint256 demand
) external view returns (uint256) {
uint256 bps = getFees(currency);
uint256 proposedFees = fees.perOf(bps);
uint256 acceptedFees = proposedFees < floor[currency]
? floor[currency]
: proposedFees;
return acceptedFees;
uint256 adjustedFloor = _getAdjustedFloor(floor[currency], demand);
return proposedFees < adjustedFloor ? adjustedFloor : proposedFees;
}

/// @inheritdoc IERC165
Expand Down
2 changes: 2 additions & 0 deletions contracts/Referendum.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ contract Referendum is
address initiator,
// TODO aca en lugar de ser un tipo, podria ser bytes con standard format,
/// para permitir enviar datos escalables..
/// TODO podria ser tambien que los datos que establezcan sean los de IP story
/// para determinas las condiciones como geofencing, etc.. ver que condiciones establecer IP
T.ContentParams calldata params
) public {
if (initiator == address(0)) revert InvalidSubmissionInitiator();
Expand Down
2 changes: 1 addition & 1 deletion contracts/Repository.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "contracts/interfaces/IRepository.sol";
import "contracts/libraries/Types.sol";

/// @title Repository Contract
/// @title Repository Contract (Registry)
/// @notice Manages the addresses of different contract types and their versions.
/// @dev This contract uses the UUPS upgradeable pattern and AccessControl for role-based access control.
contract Repository is
Expand Down
Loading

0 comments on commit 9e0a8c4

Please sign in to comment.