Skip to content

Commit

Permalink
Merge pull request #813 from molecula451/facet-clock
Browse files Browse the repository at this point in the history
refactor: update creditclock to facet
  • Loading branch information
rndquu committed Oct 24, 2023
2 parents f091f2b + fad6671 commit 8e774ee
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 291 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {CollectableDustFacet} from "../../../../src/dollar/facets/CollectableDus
import {ChefFacet} from "../../../../src/dollar/facets/ChefFacet.sol";
import {StakingFacet} from "../../../../src/dollar/facets/StakingFacet.sol";
import {StakingFormulasFacet} from "../../../../src/dollar/facets/StakingFormulasFacet.sol";
import {CreditClockFacet} from "../../../../src/dollar/facets/CreditClockFacet.sol";
import {CreditNftManagerFacet} from "../../../../src/dollar/facets/CreditNftManagerFacet.sol";
import {CreditNftRedemptionCalculatorFacet} from "../../../../src/dollar/facets/CreditNftRedemptionCalculatorFacet.sol";
import {CreditRedemptionCalculatorFacet} from "../../../../src/dollar/facets/CreditRedemptionCalculatorFacet.sol";
Expand Down Expand Up @@ -48,6 +49,7 @@ contract DiamondScript is Constants {

bytes4[] selectorsOfDollarMintCalculatorFacet;
bytes4[] selectorsOfDollarMintExcessFacet;
bytes4[] selectorsOfCreditClockFacet;
// contract types of facets to be deployed
Diamond diamond;
DiamondCutFacet dCutFacet;
Expand All @@ -72,6 +74,7 @@ contract DiamondScript is Constants {

DollarMintCalculatorFacet dollarMintCalculatorFacet;
DollarMintExcessFacet dollarMintExcessFacet;
CreditClockFacet creditClockFacet;

string[] facetNames;

Expand All @@ -96,6 +99,7 @@ contract DiamondScript is Constants {

dollarMintCalculatorFacet = new DollarMintCalculatorFacet();
dollarMintExcessFacet = new DollarMintExcessFacet();
creditClockFacet = new CreditClockFacet();

dInit = new DiamondInit();
facetNames = [
Expand All @@ -113,7 +117,8 @@ contract DiamondScript is Constants {
"CreditNftRedemptionCalculatorFacet",
"CreditRedemptionCalculatorFacet",
"DollarMintCalculatorFacet",
"DollarMintExcessFacet"
"DollarMintExcessFacet",
"CreditClockFacet"
];

DiamondInit.Args memory initArgs = DiamondInit.Args({
Expand All @@ -133,7 +138,7 @@ contract DiamondScript is Constants {
initArgs
)
});
IDiamondCut.FacetCut[] memory cuts = new IDiamondCut.FacetCut[](15);
IDiamondCut.FacetCut[] memory cuts = new IDiamondCut.FacetCut[](16);
setFacet(cuts);
// deploy diamond

Expand Down Expand Up @@ -256,6 +261,13 @@ contract DiamondScript is Constants {
functionSelectors: selectorsOfDollarMintExcessFacet
})
);
cuts[15] = (
IDiamondCut.FacetCut({
facetAddress: address(creditClockFacet),
action: IDiamondCut.FacetCutAction.Add,
functionSelectors: selectorsOfCreditClockFacet
})
);
}

function getSelectors() internal {
Expand Down Expand Up @@ -521,5 +533,17 @@ contract DiamondScript is Constants {
selectorsOfDollarMintExcessFacet.push(
(dollarMintExcessFacet.distributeDollars.selector)
);

// Credit Clock Facet
selectorsOfCreditClockFacet.push((creditClockFacet.getRate.selector));
selectorsOfCreditClockFacet.push(
creditClockFacet.setRatePerBlock.selector
);
selectorsOfCreditClockFacet.push(
(creditClockFacet.setManager.selector)
);
selectorsOfCreditClockFacet.push(
(creditClockFacet.getManager.selector)
);
}
}
117 changes: 0 additions & 117 deletions packages/contracts/src/dollar/core/CreditClock.sol

This file was deleted.

43 changes: 43 additions & 0 deletions packages/contracts/src/dollar/facets/CreditClockFacet.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import "abdk/ABDKMathQuad.sol";
import "../libraries/Constants.sol";
import {Modifiers} from "../libraries/LibAppStorage.sol";
import {LibCreditClock} from "../libraries/LibCreditClock.sol";

/**
* @notice CreditClock Facet
*/
contract CreditClockFacet is Modifiers {
/**
* @notice Updates the manager address
* @param _manager New manager address
*/
function setManager(address _manager) external onlyAdmin {
LibCreditClock.setManager(_manager);
}

/**
* @notice Returns the manager address
* @return Manager address
*/
function getManager() external view returns (address) {
return LibCreditClock.getManager();
}

/**
* @notice Sets rate to apply from this block onward
* @param _ratePerBlock ABDKMathQuad new rate per block to apply from this block onward
*/
function setRatePerBlock(bytes16 _ratePerBlock) external onlyAdmin {
LibCreditClock.setRatePerBlock(_ratePerBlock);
}

/**
* @param blockNumber Block number to get the rate for. 0 for current block.
*/
function getRate(uint256 blockNumber) external view {
LibCreditClock.getRate(blockNumber);
}
}
109 changes: 109 additions & 0 deletions packages/contracts/src/dollar/libraries/LibCreditClock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import "abdk/ABDKMathQuad.sol";
import {LibAccessControl} from "./LibAccessControl.sol";
import {IAccessControl} from "../interfaces/IAccessControl.sol";
import "../libraries/Constants.sol";

/// @notice Library for Credit Clock Facet
library LibCreditClock {
using ABDKMathQuad for uint256;
using ABDKMathQuad for bytes16;

/// @notice Emitted when depreciation rate per block is updated
event SetRatePerBlock(
uint256 rateStartBlock,
bytes16 rateStartValue,
bytes16 ratePerBlock
);

/// @notice Storage slot used to store data for this library
bytes32 constant CREDIT_CLOCK_STORAGE_POSITION =
bytes32(
uint256(keccak256("ubiquity.contracts.credit.clock.storage")) - 1
);

/// @notice Struct used as a storage for the current library
struct CreditClockData {
IAccessControl accessControl;
uint256 rateStartBlock;
bytes16 rateStartValue;
bytes16 ratePerBlock;
bytes16 one;
}

/**
* @notice Returns struct used as a storage for this library
* @return data Struct used as a storage
*/
function creditClockStorage()
internal
pure
returns (CreditClockData storage data)
{
bytes32 position = CREDIT_CLOCK_STORAGE_POSITION;
assembly {
data.slot := position
}
}

/**
* @notice Updates the manager address
* @param _manager New manager address
*/
function setManager(address _manager) internal {
creditClockStorage().accessControl = IAccessControl(_manager);
}

/**
* @notice Returns the manager address
* @return Manager address
*/
function getManager() internal view returns (address) {
return address(creditClockStorage().accessControl);
}

/**
* @notice Sets rate to apply from this block onward
* @param _ratePerBlock ABDKMathQuad new rate per block to apply from this block onward
*/
function setRatePerBlock(bytes16 _ratePerBlock) internal {
CreditClockData storage data = creditClockStorage();
data.rateStartValue = getRate(block.number);
data.rateStartBlock = block.number;
data.ratePerBlock = _ratePerBlock;

emit SetRatePerBlock(
data.rateStartBlock,
data.rateStartValue,
data.ratePerBlock
);
}

/**
* @notice Calculates `rateStartValue * (1 / ((1 + ratePerBlock)^blockNumber - rateStartBlock)))`
* @param blockNumber Block number to get the rate for. 0 for current block.
* @return rate ABDKMathQuad rate calculated for the block number
*/
function getRate(uint256 blockNumber) internal view returns (bytes16 rate) {
CreditClockData storage data = creditClockStorage();
if (blockNumber == 0) {
blockNumber = block.number;
} else {
if (blockNumber < block.number) {
revert("CreditClock: block number must not be in the past.");
}
}
// slither-disable-next-line divide-before-multiply
rate = data.rateStartValue.mul(
data.one.div(
// b ^ n == 2^(n*log²(b))
(blockNumber - data.rateStartBlock)
.fromUInt()
.mul(data.one.add(data.ratePerBlock).log_2())
.pow_2()
)
);
}
}
2 changes: 1 addition & 1 deletion packages/contracts/test/diamond/DiamondTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ contract TestDiamond is DiamondTestSetup {
}

function testHasMultipleFacets() public {
assertEq(facetAddressList.length, 18);
assertEq(facetAddressList.length, 19);
}

function testFacetsHaveCorrectSelectors() public {
Expand Down
Loading

1 comment on commit 8e774ee

@ubiquibot
Copy link

@ubiquibot ubiquibot bot commented on 8e774ee Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.