Skip to content

Commit

Permalink
tests: distribution financial rentability tests
Browse files Browse the repository at this point in the history
  • Loading branch information
geolffreym committed Aug 26, 2024
1 parent c694168 commit 3184eff
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 42 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.

16 changes: 7 additions & 9 deletions contracts/Distributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ contract Distributor is

mapping(address => uint256) private floor;
string private endpoint;
uint256 scalingFactor; // To sharpen or intensify the increase in fees as demand grows.
uint256 flattenFactor; // To smooth or flatten the increase in fees as demand grows.

/// @notice Event emitted when the endpoint is updated.
Expand All @@ -54,7 +53,6 @@ contract Distributor is

if (bytes(_endpoint).length == 0) revert InvalidEndpoint();
// balanced factors
scalingFactor = 10;
flattenFactor = 10;
endpoint = _endpoint;
}
Expand Down Expand Up @@ -113,12 +111,9 @@ contract Distributor is
}

/// @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
/// (scaling factor) and how flattened the fee increases are (flattening factor).
/// @param scale The scaling factor that controls how aggressively fees increase with demand.
/// @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 scale, uint256 flatten) public onlyOwner {
scalingFactor = scale;
function setFactors(uint256 flatten) public onlyOwner {
flattenFactor = flatten;
}

Expand All @@ -133,6 +128,9 @@ 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
Expand All @@ -147,9 +145,9 @@ contract Distributor is
if (baseFloor == 0) return 0;
// Economies of scale.
// Calculate the logarithm of custodials, adding 1 to avoid taking log(0)
// fees + ((log2(demand) * scale) / flatten)
// fees + (fees * (log2(demand) / flatten))
uint256 safeOp = (demand == 0 ? (demand + 1) : demand);
return baseFloor + ((safeOp.log2() * scalingFactor) / flattenFactor);
return baseFloor + (baseFloor * (safeOp.log2() / flattenFactor));
}

/// @inheritdoc IDistributor
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// NatSpec format convention - https://docs.soliditylang.org/en/v0.5.10/natspec-format.html
pragma solidity ^0.8.24;

import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "contracts/interfaces/IRightsDelegable.sol";
import "contracts/libraries/Types.sol";
Expand All @@ -13,11 +14,12 @@ abstract contract RightsManagerDelegationUpgradeable is
Initializable,
IRightsDelegable
{
using EnumerableSet for EnumerableSet.UintSet;
/// @custom:storage-location erc7201:rightsmanagerdelegationupgradeable
/// @dev Storage struct for managing rights delegation to policies.
struct RightsStorage {
/// @dev Mapping to store the delegated rights for each policy (address) and content ID.
mapping(address => mapping(uint256 => bool)) _delegation;
mapping(address => EnumerableSet.UintSet) _delegation;
}

/// @dev Error thrown when rights have not been delegated to the specified grantee for the given content ID.
Expand All @@ -30,11 +32,9 @@ abstract contract RightsManagerDelegationUpgradeable is
bytes32 private constant DELEGATION_RIGHTS_SLOT =
0x8de86c03e9c907c4cfd46ee06d6593a7fc5fdfb6903523c8213ef37d380b3b00;

/**
* @notice Internal function to access the rights storage.
* @dev Uses inline assembly to assign the correct storage slot to the RightsStorage struct.
* @return $ The storage struct containing the rights delegation data.
*/
/// @notice Internal function to access the rights storage.
/// @dev Uses inline assembly to assign the correct storage slot to the RightsStorage struct.
/// @return $ The storage struct containing the rights delegation data.
function _getRightsStorage()
private
pure
Expand All @@ -45,37 +45,45 @@ abstract contract RightsManagerDelegationUpgradeable is
}
}

/// @dev Modifier to ensure that the given policy contract has been delegated
/// @dev Modifier to ensure that the given policy contract has been delegated
/// the rights for the specific content ID.
/// @param grantee The address of the account or contract to delegate rights to.
/// @param contentId The content ID to check for delegation.
/// Reverts if the rights have not been delegated for the content ID.
modifier onlyWhenRightsDelegated(address grantee, uint256 contentId) {
RightsStorage storage $ = _getRightsStorage();
if (!$._delegation[grantee][contentId])
if (!$._delegation[grantee].contains(contentId))
revert InvalidNotRightsDelegated(grantee, contentId);
_;
}

/**
* @notice Delegates rights for a specific content ID to a grantee.
* @dev This function stores the delegation details in the RightsStorage struct.
* @param grantee The address of the account or contract to delegate rights to.
* @param contentId The content ID for which rights are being delegated.
*/
/// @notice Retrieves all content IDs for which rights have been delegated to a grantee.
/// @dev This function returns an array of content IDs that the specified grantee
/// has been delegated rights for. It fetches the data from the RightsStorage struct.
/// @param grantee The address of the account or contract whose delegated rights are being queried.
/// @return An array of content IDs that have been delegated to the specified grantee.
function getDelegatedRights(
address grantee
) public returns (uint256[] memory) {
RightsStorage storage $ = _getRightsStorage();
return $._delegation[grantee].values();
}

/// @notice Delegates rights for a specific content ID to a grantee.
/// @dev This function stores the delegation details in the RightsStorage struct.
/// @param grantee The address of the account or contract to delegate rights to.
/// @param contentId The content ID for which rights are being delegated.
function _delegateRights(address grantee, uint256 contentId) internal {
RightsStorage storage $ = _getRightsStorage();
$._delegation[grantee][contentId] = true;
$._delegation[grantee].add(contentId);
}

/**
* @notice Revokes the delegation of rights for a grantee.
* @dev This function removes the rights delegation from the RightsStorage struct.
* @param grantee The address of the account or contract whose delegation is being revoked.
* @param contentId The content ID for which rights are being delegated.
*/
/// @notice Revokes the delegation of rights for a grantee.
/// @dev This function removes the rights delegation from the RightsStorage struct.
/// @param grantee The address of the account or contract whose delegation is being revoked.
/// @param contentId The content ID for which rights are being delegated.
function _revokeRights(address grantee, uint256 contentId) internal {
RightsStorage storage $ = _getRightsStorage();
$._delegation[grantee][contentId] = false;
$._delegation[grantee].remove(contentId);
}
}
6 changes: 3 additions & 3 deletions contracts/interfaces/IDistributor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ interface IDistributor is IFeesManager, ICurrencyManager {
/// @param minimum The minimum fee that can be proposed for the given currency.
function setFloor(address currency, uint256 minimum) external;

/// @notice Sets the scaling and flattening factors used to adjust fees.
/// @param scale The scaling factor that controls how aggressively fees increase with demand.
/// @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 scale, uint256 flatten) external;
function setFactors(uint256 flatten) external;

/// @notice Retrieves the endpoint of the distributor.
/// @dev This function allows users to view the current endpoint of the distributor.
Expand Down
8 changes: 7 additions & 1 deletion contracts/interfaces/IRightsDelegable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ interface IRightsDelegable {
function delegateRights(address grantee, uint256 contentId) external;

/// @notice Revokes the delegation of rights for a grantee.
/// @param grantee The address of the account or contract to revoke rights to.
/// @param grantee The address of the account or contract to revoke rights to.
/// @param contentId The content ID for which rights are being revoked.
function revokeRights(address grantee, uint256 contentId) external;

/// @notice Retrieves all content IDs for which rights have been delegated to a grantee.
/// @param grantee The address of the account or contract whose delegated rights are being queried.
/// @return An array of content IDs that have been delegated to the specified grantee.
function getDelegatedRights(
address grantee
) external returns (uint256[] memory);
}
1 change: 0 additions & 1 deletion lib/forge-std
Submodule forge-std deleted from 07263d
32 changes: 27 additions & 5 deletions test/Distributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {

const DISTRIBUTOR_INTERFACE_ID = '0x3a8b2846'

async function getAccounts () {
async function getAccounts() {
return await hre.ethers.getSigners()
}

Expand Down Expand Up @@ -87,7 +87,7 @@ describe('Distributor', function () {
const newEndpoint = 'watchit4.movie'
// initial endpoint set by beacon proxy initialization.
const distributor = await deployAndInitializeDistributorContract('watchit.com')
const updater = await distributor.updateEndpoint(newEndpoint)
const updater = await distributor.setEndpoint(newEndpoint)
await updater.wait()

const distributorEndpoint = await distributor.getEndpoint()
Expand All @@ -97,7 +97,7 @@ describe('Distributor', function () {
it('Should emit a valid EndpointUpdated after update endpoint.', async function () {
const newEndpoint = 'watchit3.movie'
const distributor = await deployAndInitializeDistributorContract('watchit.movie')
const updater = await distributor.updateEndpoint(newEndpoint)
const updater = await distributor.setEndpoint(newEndpoint)
await updater.wait()

const filter = distributor.filters.EndpointUpdated()
Expand All @@ -112,17 +112,39 @@ describe('Distributor', function () {

it('Should fail if `updateEndpoint` is called with invalid empty endpoint.', async function () {
const distributor = await deployAndInitializeDistributorContract('watchit.movie')
await expect(distributor.updateEndpoint('')).to.be.revertedWithCustomError(
await expect(distributor.setEndpoint('')).to.be.revertedWithCustomError(
distributor, 'InvalidEndpoint'
)
})

it('Should fail if `updateEndpoint` is called with invalid owner.', async function () {
const [, secondary] = await switcher(getAccounts)
const distributor = await deployAndInitializeDistributorContract('watchit.movie')
await expect(distributor.connect(secondary).updateEndpoint('check.com')).to.be.revertedWithCustomError(
await expect(distributor.connect(secondary).setEndpoint('check.com')).to.be.revertedWithCustomError(
distributor, 'OwnableUnauthorizedAccount'
)
})
})

describe('Financial', function () {

it("Should calculate expected negotiation based on constant fee .", async function () {

})

it("Should calculate expected negotiation based on variant fee.", async function () {

})

it("Should calculate expected negotiation based on variant fee + flatten factor.", async function () {

})

it("Should calculate expected negotiation auto adjusted flatten factor.", async function () {

})

})


})
12 changes: 12 additions & 0 deletions test/internals/DistributorFinancial.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pragma solidity 0.8.24;

import "forge-std/Test.sol";
import "contracts/base/upgradeable/Distributor.sol";

contract DistributorFinancialTest is Test, Disitributor {

function test_NegotiationPlusConstantFees() {}
function test_NegotiationPlusVariantFees() {}
function test_NegotiationPlusVariantPlusFlattentFactorFees() {}
function test_NegotiationAutoAdjustedFactorFees() {}
}

0 comments on commit 3184eff

Please sign in to comment.