forked from ubiquity/ubiquity-dollar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ubiquity#813 from molecula451/facet-clock
refactor: update creditclock to facet
- Loading branch information
Showing
8 changed files
with
258 additions
and
291 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
109
packages/contracts/src/dollar/libraries/LibCreditClock.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.