-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRangeValidationModule.sol
More file actions
174 lines (153 loc) · 5.88 KB
/
Copy pathRangeValidationModule.sol
File metadata and controls
174 lines (153 loc) · 5.88 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.27;
import {IOwnable} from '../dependencies/IOwnable.sol';
import {IRangeValidationModule} from '../../interfaces/IRangeValidationModule.sol';
import {IAgentHub} from '../../interfaces/IAgentHub.sol';
/**
* @title RangeValidationModule
* @author BGD Labs
* @notice Contract shared across agents to validate range and also configure range per agent with access control
*/
contract RangeValidationModule is IRangeValidationModule {
uint256 internal constant BPS_MAX = 100_00;
struct AgentConfig {
/// @notice the default range config for the agent
RangeConfig defaultConfig;
/// @notice the market specific range config for the agent
mapping(address => RangeConfig) marketConfig;
}
mapping(bytes32 configId => AgentConfig) internal _rangeConfigs;
modifier onlyHubOwnerOrAgentAdmin(address agentHub, uint256 agentId) {
require(
msg.sender == IAgentHub(agentHub).getAgentAdmin(agentId) ||
msg.sender == IOwnable(agentHub).owner(),
OnlyHubOwnerOrAgentAdmin(msg.sender)
);
_;
}
/// @inheritdoc IRangeValidationModule
function validate(
address agentHub,
uint256 agentId,
address market,
RangeValidationInput calldata input
) public view returns (bool) {
bytes32 configId = _getRangeConfigId(agentHub, agentId, input.updateType);
AgentConfig storage agentConfig = _rangeConfigs[configId];
RangeConfig memory config = agentConfig.marketConfig[market];
// if config is not set for a specific market, fallback to the default configuration
if (config.maxIncrease == 0 && config.maxDecrease == 0) {
config = agentConfig.defaultConfig;
}
return _validateRange(input.from, input.to, config);
}
/// @inheritdoc IRangeValidationModule
function validate(
address agentHub,
uint256 agentId,
address market,
RangeValidationInput[] calldata input
) external view returns (bool) {
for (uint256 i = 0; i < input.length; i++) {
if (!validate(agentHub, agentId, market, input[i])) return false;
}
return true;
}
/// @inheritdoc IRangeValidationModule
function setDefaultRangeConfig(
address agentHub,
uint256 agentId,
string calldata updateType,
RangeConfig calldata config
) external onlyHubOwnerOrAgentAdmin(agentHub, agentId) {
require(
!config.isDecreaseRelative || config.maxDecrease <= 100_00,
InvalidMaxRelativeDecrease(config.maxDecrease)
);
bytes32 configId = _getRangeConfigId(agentHub, agentId, updateType);
_rangeConfigs[configId].defaultConfig = config;
emit DefaultRangeConfigSet(agentHub, agentId, updateType, config);
}
/// @inheritdoc IRangeValidationModule
function setRangeConfigByMarket(
address agentHub,
uint256 agentId,
address market,
string calldata updateType,
RangeConfig calldata config
) external onlyHubOwnerOrAgentAdmin(agentHub, agentId) {
require(
!config.isDecreaseRelative || config.maxDecrease <= 100_00,
InvalidMaxRelativeDecrease(config.maxDecrease)
);
bytes32 configId = _getRangeConfigId(agentHub, agentId, updateType);
_rangeConfigs[configId].marketConfig[market] = config;
emit MarketRangeConfigSet(agentHub, agentId, market, updateType, config);
}
/// @inheritdoc IRangeValidationModule
function getDefaultRangeConfig(
address agentHub,
uint256 agentId,
string calldata updateType
) external view returns (RangeConfig memory) {
bytes32 configId = _getRangeConfigId(agentHub, agentId, updateType);
return _rangeConfigs[configId].defaultConfig;
}
/// @inheritdoc IRangeValidationModule
function getRangeConfigByMarket(
address agentHub,
uint256 agentId,
address market,
string calldata updateType
) external view returns (RangeConfig memory) {
bytes32 configId = _getRangeConfigId(agentHub, agentId, updateType);
return _rangeConfigs[configId].marketConfig[market];
}
/**
* @notice method to get the range configuration id, unique to each configuration
* @param agentHub address of the agentHub contract of the agent
* @param agentId id of the agent configured on the agentHub
* @param updateType updateType for which to get configuration id
* @return id unique to each configuration, which is used to set and get range configuration
*/
function _getRangeConfigId(
address agentHub,
uint256 agentId,
string calldata updateType
) internal pure returns (bytes32) {
return keccak256(abi.encode(agentHub, agentId, updateType));
}
/**
* @notice Ensures the risk param update is within the allowed range
* @param from current risk param value
* @param to new updated risk param value
* @param config struct storing the RangeConfig used to validate
* @return true if the risk param value is in the configured range
*/
function _validateRange(
uint256 from,
uint256 to,
RangeConfig memory config
) internal pure returns (bool) {
uint256 diff;
uint256 maxChange;
bool isRelativeChange;
if (from < to) {
diff = to - from;
maxChange = config.maxIncrease;
isRelativeChange = config.isIncreaseRelative;
} else {
diff = from - to;
maxChange = config.maxDecrease;
isRelativeChange = config.isDecreaseRelative;
}
// maxDiff denotes the max permitted difference, if the maxChange is relative in value, we
// calculate the max permitted difference using the maxChange and the from value, otherwise
// if the maxChange is absolute in value the max permitted difference is the maxChange itself
uint256 maxDiff = isRelativeChange ? (maxChange * from) / BPS_MAX : maxChange;
// in case of relative change, `maxDiff` is rounded down due to the nature of the division operator
// in solidity, so in case of precision loss maxDiff is always smaller so we always return in favour that
// the update is not in valid range
return diff <= maxDiff;
}
}