-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathDebtEngineModule.sol
More file actions
110 lines (100 loc) · 4.54 KB
/
DebtEngineModule.sol
File metadata and controls
110 lines (100 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// SPDX-License-Identifier: MPL-2.0
pragma solidity ^0.8.20;
/* ==== Engine === */
import {IDebtEngine, ICMTATDebt, ICMTATCreditEvents} from "../../../interfaces/engine/IDebtEngine.sol";
/* ==== Module === */
import {IDebtEngineModule} from "../../../interfaces/modules/IDebtEngineModule.sol";
/**
* @title Debt Engine module
* @dev
*
* Retrieve debt and creditEvents information from a debtEngine (external contract)
*/
abstract contract DebtEngineModule is IDebtEngineModule {
/* ============ State Variables ============ */
bytes32 public constant DEBT_ENGINE_ROLE = keccak256("DEBT_ENGINE_ROLE");
/* ============ ERC-7201 ============ */
// keccak256(abi.encode(uint256(keccak256("CMTAT.storage.DebtEngineModule")) - 1)) & ~bytes32(uint256(0xff))
bytes32 private constant DebtEngineModuleStorageLocation = 0xcd6e7f8fdfee4389651c62f4d8dd0b8f0f4b97b1582a8419b0c53664203c6d00;
/* ==== ERC-7201 State Variables === */
struct DebtModuleStorage {
IDebtEngine _debtEngine;
}
/* ============ Modifier ============ */
modifier onlyDebtEngineManager {
_authorizeDebtEngineManagement();
_;
}
/*//////////////////////////////////////////////////////////////
PUBLIC/EXTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/
/* ============ State Restricted Functions ============ */
/**
* @notice Sets a new external DebtEngine contract to delegate debt logic.
* @dev Only callable by accounts with the `DEBT_ROLE`.
* Emits a {DebtEngine} event upon successful update.
* @param debtEngine_ The address of the new DebtEngine contract.
* @custom:access-control
* - the caller must have the `DEBT_ROLE`.
*/
function setDebtEngine(
IDebtEngine debtEngine_
) public virtual override(IDebtEngineModule) onlyDebtEngineManager {
DebtModuleStorage storage $ = _getDebtEngineModuleStorage();
require(address($._debtEngine) != address(debtEngine_), CMTAT_DebtEngineModule_SameValue());
_setDebtEngine($, debtEngine_);
}
/* ============ View functions ============ */
/**
* @notice Returns the current credit events information.
* @dev Delegates to the external DebtEngine if set; otherwise returns the base implementation from DebtModule.
* @return creditEvents_ The current credit events structure.
* @inheritdoc ICMTATCreditEvents
*/
function creditEvents() public view virtual override(ICMTATCreditEvents) returns(CreditEvents memory creditEvents_){
DebtModuleStorage storage $ = _getDebtEngineModuleStorage();
if(address($._debtEngine) != address(0)){
creditEvents_ = $._debtEngine.creditEvents();
}
}
/**
* @notice Returns the current debt information.
* @dev Delegates to the external DebtEngine if set; otherwise returns the base implementation from DebtModule.
* @return debtInformation_ The current debt data structure.
* @inheritdoc ICMTATDebt
*/
function debt() public view virtual override(ICMTATDebt) returns(DebtInformation memory debtInformation_){
DebtModuleStorage storage $ = _getDebtEngineModuleStorage();
if(address($._debtEngine) != address(0)){
debtInformation_ = $._debtEngine.debt();
}
}
/**
* @notice Returns the address of the currently active DebtEngine.
* @return debtEngine_ The contract address of the debt engine in use.
*/
function debtEngine() public view virtual override(IDebtEngineModule) returns (IDebtEngine debtEngine_) {
DebtModuleStorage storage $ = _getDebtEngineModuleStorage();
return $._debtEngine;
}
/*//////////////////////////////////////////////////////////////
INTERNAL/PRIVATE FUNCTIONS
//////////////////////////////////////////////////////////////*/
function _setDebtEngine(
DebtModuleStorage storage $, IDebtEngine debtEngine_
) internal {
$._debtEngine = debtEngine_;
emit DebtEngine(debtEngine_);
}
/*//////////////////////////////////////////////////////////////
INTERNAL/PRIVATE FUNCTIONS
//////////////////////////////////////////////////////////////*/
/* ==== Access Control ==== */
function _authorizeDebtEngineManagement() internal virtual;
/* ============ ERC-7201 ============ */
function _getDebtEngineModuleStorage() internal pure returns (DebtModuleStorage storage $) {
assembly {
$.slot := DebtEngineModuleStorageLocation
}
}
}