-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathFeeOracleV2.sol
390 lines (325 loc) · 12.6 KB
/
FeeOracleV2.sol
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity =0.8.24;
import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import { IFeeOracle } from "../interfaces/IFeeOracle.sol";
import { IFeeOracleV2 } from "../interfaces/IFeeOracleV2.sol";
/**
* @title FeeOracleV2
* @notice A simple fee oracle with a fixed fee, controlled by an admin account
* Used by OmniPortal to calculate xmsg fees
*/
contract FeeOracleV2 is IFeeOracle, IFeeOracleV2, OwnableUpgradeable {
/**
* @notice Base protocol fee for each xmsg.
*/
uint96 public protocolFee;
/**
* @notice Address allowed to set gas prices and to-native conversion rates.
*/
address public manager;
/**
* @notice Conversion rate from `gasToken` to this chain's native token (normalized by CONVERSION_RATE_DENOM)
*/
mapping(uint16 gasToken => uint256) public tokenToNativeRate;
/**
* @notice Fee parameters for a specific chain, by chain ID.
*/
mapping(uint64 chainId => IFeeOracleV2.FeeParams) internal _feeParams;
/**
* @notice Data cost parameters for a specific chain, by data cost ID.
*/
mapping(uint64 dataCostId => IFeeOracleV2.DataCostParams) internal _dataCostParams;
/**
* @notice Denominator for conversion rate calculations.
*/
uint256 public constant CONVERSION_RATE_DENOM = 1e6;
modifier onlyManager() {
if (msg.sender != manager) revert IFeeOracleV2.NotManager();
_;
}
constructor() {
_disableInitializers();
}
function initialize(
address owner_,
address manager_,
uint96 protocolFee_,
FeeParams[] calldata feeParams_,
DataCostParams[] calldata dataCostParams_,
ToNativeRateParams[] calldata toNativeRateParams_
) public initializer {
__Ownable_init(owner_);
_setManager(manager_);
_setProtocolFee(protocolFee_);
_bulkSetFeeParams(feeParams_);
_bulkSetDataCostParams(dataCostParams_);
_bulkSetToNativeRate(toNativeRateParams_);
}
/// @inheritdoc IFeeOracle
function version() external pure override returns (uint64) {
return 2;
}
/// @inheritdoc IFeeOracle
function feeFor(uint64 destChainId, bytes calldata data, uint64 gasLimit) external view returns (uint256) {
IFeeOracleV2.FeeParams storage p = _feeParams[destChainId];
IFeeOracleV2.DataCostParams storage d = _dataCostParams[p.dataCostId];
uint256 _execGasPrice = p.gasPrice * tokenToNativeRate[p.gasToken] / CONVERSION_RATE_DENOM;
uint256 _dataGasPrice = d.gasPrice * tokenToNativeRate[d.gasToken] / CONVERSION_RATE_DENOM;
if (_execGasPrice == 0) revert IFeeOracleV2.NoFeeParams();
if (_dataGasPrice == 0) revert IFeeOracleV2.NoFeeParams();
// 16 gas per non-zero byte, assume non-zero bytes
uint256 dataGas = (d.baseBytes + data.length) * d.gasPerByte;
return protocolFee + (p.baseGasLimit + gasLimit) * _execGasPrice + (dataGas * _dataGasPrice);
}
/**
* @notice Returns the fee parameters for a destination chain.
*/
function feeParams(uint64 chainId) external view returns (FeeParams memory) {
return _feeParams[chainId];
}
/**
* @notice Returns the data cost parameters for a data cost ID.
*/
function dataCostParams(uint64 dataCostId) external view returns (DataCostParams memory) {
return _dataCostParams[dataCostId];
}
/**
* @notice Returns the gas price for a destination chain.
*/
function execGasPrice(uint64 chainId) external view returns (uint64) {
return _feeParams[chainId].gasPrice;
}
/**
* @notice Returns the data gas price for a data cost ID.
*/
function dataGasPrice(uint64 dataCostId) external view returns (uint64) {
return _dataCostParams[dataCostId].gasPrice;
}
/**
* @notice Returns the base gas limit for a destination chain.
*/
function baseGasLimit(uint64 chainId) external view returns (uint32) {
return _feeParams[chainId].baseGasLimit;
}
/**
* @notice Returns the base bytes buffer for a data cost ID.
*/
function baseBytes(uint64 dataCostId) external view returns (uint32) {
return _dataCostParams[dataCostId].baseBytes;
}
/**
* @notice Returns the gas token for a destination chain.
*/
function execGasToken(uint64 chainId) external view returns (uint16) {
return _feeParams[chainId].gasToken;
}
/**
* @notice Returns the gas token for a data cost ID.
*/
function dataGasToken(uint64 dataCostId) external view returns (uint16) {
return _dataCostParams[dataCostId].gasToken;
}
/**
* @notice Returns the data cost ID for a destination chain.
*/
function execDataCostId(uint64 chainId) external view returns (uint64) {
return _feeParams[chainId].dataCostId;
}
/**
* @notice Returns the gas per byte for a data cost ID.
*/
function dataGasPerByte(uint64 dataCostId) external view returns (uint64) {
return _dataCostParams[dataCostId].gasPerByte;
}
/**
* @notice Returns the to-native conversion rate for a destination chain.
*/
function toNativeRate(uint64 chainId) external view returns (uint256) {
return tokenToNativeRate[_feeParams[chainId].gasToken];
}
/**
* @notice Set the fee parameters for a list of destination chains.
*/
function bulkSetFeeParams(FeeParams[] calldata params) external onlyManager {
_bulkSetFeeParams(params);
}
/**
* @notice Set the data cost parameters for a list of data cost IDs.
*/
function bulkSetDataCostParams(DataCostParams[] calldata params) external onlyManager {
_bulkSetDataCostParams(params);
}
/**
* @notice Set the to-native conversion rate for a list of gas tokens.
*/
function bulkSetToNativeRate(ToNativeRateParams[] calldata params) external onlyManager {
_bulkSetToNativeRate(params);
}
/**
* @notice Set the execution gas price for a destination chain.
*/
function setExecGasPrice(uint64 chainId, uint64 gasPrice) external onlyManager {
_setExecGasPrice(chainId, gasPrice);
}
/**
* @notice Set the data gas price for a data cost ID.
*/
function setDataGasPrice(uint64 dataCostId, uint64 gasPrice) external onlyManager {
_setDataGasPrice(dataCostId, gasPrice);
}
/**
* @notice Set the base gas limit for a destination chain.
*/
function setBaseGasLimit(uint64 chainId, uint32 newBaseGasLimit) external onlyManager {
_setBaseGasLimit(chainId, newBaseGasLimit);
}
/**
* @notice Set the base bytes buffer for a data cost ID.
*/
function setBaseBytes(uint64 dataCostId, uint32 newBaseBytes) external onlyManager {
_setBaseBytes(dataCostId, newBaseBytes);
}
/**
* @notice Set the data cost ID for a destination chain.
*/
function setDataCostId(uint64 chainId, uint64 dataCostId) external onlyManager {
_setDataCostId(chainId, dataCostId);
}
/**
* @notice Set the gas per byte for a data cost ID.
*/
function setGasPerByte(uint64 dataCostId, uint64 gasPerByte) external onlyManager {
_setGasPerByte(dataCostId, gasPerByte);
}
/**
* @notice Set the to native conversion rate for a gas token.
*/
function setToNativeRate(uint16 gasToken, uint256 nativeRate) external onlyManager {
_setToNativeRate(gasToken, nativeRate);
}
/**
* @notice Set the base protocol fee for each xmsg.
*/
function setProtocolFee(uint96 fee) external onlyOwner {
_setProtocolFee(fee);
}
/**
* @notice Set the manager admin account.
*/
function setManager(address manager_) external onlyOwner {
if (manager_ == address(0)) revert IFeeOracleV2.ZeroAddress();
_setManager(manager_);
}
/**
* @notice Set the fee parameters for a list of destination chains.
*/
function _bulkSetFeeParams(FeeParams[] calldata params) internal {
for (uint256 i = 0; i < params.length; i++) {
FeeParams memory p = params[i];
if (p.gasToken == 0) revert IFeeOracleV2.ZeroGasToken();
if (p.chainId == 0) revert IFeeOracleV2.ZeroChainId();
if (p.gasPrice == 0) revert IFeeOracleV2.ZeroGasPrice();
if (p.dataCostId == 0) revert IFeeOracleV2.ZeroDataCostId();
_feeParams[p.chainId] = p;
emit FeeParamsSet(p.gasToken, p.baseGasLimit, p.chainId, p.gasPrice, p.dataCostId);
}
}
/**
* @notice Set the data cost parameters for a list of data cost IDs.
*/
function _bulkSetDataCostParams(DataCostParams[] calldata params) internal {
for (uint256 i = 0; i < params.length; i++) {
DataCostParams memory d = params[i];
if (d.gasToken == 0) revert IFeeOracleV2.ZeroGasToken();
if (d.id == 0) revert IFeeOracleV2.ZeroDataCostId();
if (d.gasPrice == 0) revert IFeeOracleV2.ZeroGasPrice();
if (d.gasPerByte == 0) revert IFeeOracleV2.ZeroGasPerByte();
_dataCostParams[d.id] = d;
emit DataCostParamsSet(d.gasToken, d.baseBytes, d.id, d.gasPrice, d.gasPerByte);
}
}
/**
* @notice Set the to-native conversion rate for a list of gas tokens.
*/
function _bulkSetToNativeRate(ToNativeRateParams[] calldata params) internal {
for (uint256 i = 0; i < params.length; i++) {
ToNativeRateParams memory n = params[i];
_setToNativeRate(n.gasToken, n.nativeRate);
}
}
/**
* @notice Set the execution gas price for a destination chain.
*/
function _setExecGasPrice(uint64 chainId, uint64 gasPrice) internal {
if (gasPrice == 0) revert IFeeOracleV2.ZeroGasPrice();
if (chainId == 0) revert IFeeOracleV2.ZeroChainId();
_feeParams[chainId].gasPrice = gasPrice;
emit ExecGasPriceSet(chainId, gasPrice);
}
/**
* @notice Set the data gas price for a destination chain.
*/
function _setDataGasPrice(uint64 dataCostId, uint64 gasPrice) internal {
if (gasPrice == 0) revert IFeeOracleV2.ZeroGasPrice();
if (dataCostId == 0) revert IFeeOracleV2.ZeroDataCostId();
_dataCostParams[dataCostId].gasPrice = gasPrice;
emit DataGasPriceSet(dataCostId, gasPrice);
}
/**
* @notice Set the base gas limit for a destination chain.
*/
function _setBaseGasLimit(uint64 chainId, uint32 newBaseGasLimit) internal {
if (chainId == 0) revert IFeeOracleV2.ZeroChainId();
_feeParams[chainId].baseGasLimit = newBaseGasLimit;
emit BaseGasLimitSet(chainId, newBaseGasLimit);
}
/**
* @notice Set the base bytes buffer for a data cost ID.
*/
function _setBaseBytes(uint64 dataCostId, uint32 newBaseBytes) internal {
if (dataCostId == 0) revert IFeeOracleV2.ZeroDataCostId();
_dataCostParams[dataCostId].baseBytes = newBaseBytes;
emit BaseBytesSet(dataCostId, newBaseBytes);
}
/**
* @notice Set the data cost ID for a destination chain.
*/
function _setDataCostId(uint64 chainId, uint64 dataCostId) internal {
if (chainId == 0) revert IFeeOracleV2.ZeroChainId();
if (dataCostId == 0) revert IFeeOracleV2.ZeroDataCostId();
_feeParams[chainId].dataCostId = dataCostId;
emit DataCostIdSet(chainId, dataCostId);
}
/**
* @notice Set the gas per byte for a data cost ID.
*/
function _setGasPerByte(uint64 dataCostId, uint64 gasPerByte) internal {
if (dataCostId == 0) revert IFeeOracleV2.ZeroDataCostId();
if (gasPerByte == 0) revert IFeeOracleV2.ZeroGasPerByte();
_dataCostParams[dataCostId].gasPerByte = gasPerByte;
emit GasPerByteSet(dataCostId, gasPerByte);
}
/**
* @notice Set the to-native conversion rate for a gas token.
*/
function _setToNativeRate(uint16 gasToken, uint256 nativeRate) internal {
if (nativeRate == 0) revert IFeeOracleV2.ZeroNativeRate();
if (gasToken == 0) revert IFeeOracleV2.ZeroGasToken();
tokenToNativeRate[gasToken] = nativeRate;
emit ToNativeRateSet(gasToken, nativeRate);
}
/**
* @notice Set the base protocol fee for each xmsg.
*/
function _setProtocolFee(uint96 fee) internal {
protocolFee = fee;
emit ProtocolFeeSet(fee);
}
/**
* @notice Set the manager admin account.
*/
function _setManager(address manager_) internal {
manager = manager_;
emit ManagerSet(manager_);
}
}