-
Notifications
You must be signed in to change notification settings - Fork 54
/
FeeOracleV1.sol
222 lines (185 loc) · 6.85 KB
/
FeeOracleV1.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
// 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 { IFeeOracleV1 } from "../interfaces/IFeeOracleV1.sol";
/**
* @title FeeOracleV1
* @notice A simple fee oracle with a fixed fee, controlled by an admin account
* Used by OmniPortal to calculate xmsg fees
*/
contract FeeOracleV1 is IFeeOracle, IFeeOracleV1, OwnableUpgradeable {
/**
* @notice Base gas limit for each xmsg.
*/
uint64 public baseGasLimit;
/**
* @notice Base protocol fee for each xmsg.
*/
uint256 public protocolFee;
/**
* @notice Address allowed to set gas prices and to-native conversion rates.
*/
address public manager;
/**
* @notice Fee parameters for a specific chain, by chain ID.
*/
mapping(uint64 => IFeeOracleV1.ChainFeeParams) internal _feeParams;
/**
* @notice Denominator for conversion rate calculations.
*/
uint256 public constant CONVERSION_RATE_DENOM = 1e6;
modifier onlyManager() {
require(msg.sender == manager, "FeeOracleV1: not manager");
_;
}
constructor() {
_disableInitializers();
}
function initialize(
address owner_,
address manager_,
uint64 baseGasLimit_,
uint256 protocolFee_,
ChainFeeParams[] calldata params
) public initializer {
__Ownable_init(owner_);
_setManager(manager_);
_setBaseGasLimit(baseGasLimit_);
_setProtocolFee(protocolFee_);
_bulkSetFeeParams(params);
}
/// @inheritdoc IFeeOracle
function version() external pure override returns (uint64) {
return 1;
}
/// @inheritdoc IFeeOracle
function feeFor(uint64 destChainId, bytes calldata data, uint64 gasLimit) external view returns (uint256) {
IFeeOracleV1.ChainFeeParams storage execP = _feeParams[destChainId];
IFeeOracleV1.ChainFeeParams storage dataP = _feeParams[execP.postsTo];
uint256 execGasPrice = execP.gasPrice * execP.toNativeRate / CONVERSION_RATE_DENOM;
uint256 dataGasPrice = dataP.gasPrice * dataP.toNativeRate / CONVERSION_RATE_DENOM;
require(execGasPrice > 0, "FeeOracleV1: no fee params");
require(dataGasPrice > 0, "FeeOracleV1: no fee params");
// 16 gas per non-zero byte, assume non-zero bytes
// TODO: given we mostly support rollups that post data to L1, it may be cheaper for users to count
// non-zero bytes (consuming L2 execution gas) to reduce their L1 data fee
uint256 dataGas = data.length * 16;
return protocolFee + (baseGasLimit + gasLimit) * execGasPrice + (dataGas * dataGasPrice);
}
/**
* @notice Returns the fee parameters for a destination chain.
*/
function feeParams(uint64 chainId) external view returns (ChainFeeParams memory) {
return _feeParams[chainId];
}
/**
* @notice Returns the gas price for a destination chain.
*/
function gasPriceOn(uint64 chainId) external view returns (uint256) {
return _feeParams[chainId].gasPrice;
}
/**
* @notice Returns the to-native conversion rate for a destination chain.
*/
function toNativeRate(uint64 chainId) external view returns (uint256) {
return _feeParams[chainId].toNativeRate;
}
/**
* @notice Returns the chainId of the chain that the given destination chain posts tx data to.
* For rollups, this is L1.
*/
function postsTo(uint64 chainId) external view returns (uint64) {
return _feeParams[chainId].postsTo;
}
/**
* @notice Set the fee parameters for a list of destination chains.
*/
function bulkSetFeeParams(ChainFeeParams[] calldata params) external onlyManager {
_bulkSetFeeParams(params);
}
/**
* @notice Set the gas price for a destination chain.
*/
function setGasPrice(uint64 chainId, uint256 gasPrice) external onlyManager {
_setGasPrice(chainId, gasPrice);
}
/**
* @notice Set the to native conversion rate for a destination chain.
*/
function setToNativeRate(uint64 chainId, uint256 rate) external onlyManager {
_setToNativeRate(chainId, rate);
}
/**
* @notice Set the base gas limit for each xmsg.
*/
function setBaseGasLimit(uint64 gasLimit) external onlyOwner {
_setBaseGasLimit(gasLimit);
}
/**
* @notice Set the base protocol fee for each xmsg.
*/
function setProtocolFee(uint256 fee) external onlyOwner {
_setProtocolFee(fee);
}
/**
* @notice Set the manager admin account.
*/
function setManager(address manager_) external onlyOwner {
require(manager_ != address(0), "FeeOracleV1: no zero manager");
_setManager(manager_);
}
/**
* @notice Set the fee parameters for a list of destination chains.
*/
function _bulkSetFeeParams(ChainFeeParams[] calldata params) internal {
for (uint256 i = 0; i < params.length; i++) {
ChainFeeParams memory p = params[i];
require(p.gasPrice > 0, "FeeOracleV1: no zero gas price");
require(p.toNativeRate > 0, "FeeOracleV1: no zero rate");
require(p.chainId != 0, "FeeOracleV1: no zero chain id");
require(p.postsTo != 0, "FeeOracleV1: no zero postsTo");
_feeParams[p.chainId] = p;
emit FeeParamsSet(p.chainId, p.postsTo, p.gasPrice, p.toNativeRate);
}
}
/**
* @notice Set the gas price for a destination chain.
*/
function _setGasPrice(uint64 chainId, uint256 gasPrice) internal {
require(gasPrice > 0, "FeeOracleV1: no zero gas price");
require(chainId != 0, "FeeOracleV1: no zero chain id");
_feeParams[chainId].gasPrice = gasPrice;
emit GasPriceSet(chainId, gasPrice);
}
/**
* @notice Set the to-native conversion rate for a destination chain.
*/
function _setToNativeRate(uint64 chainId, uint256 rate) internal {
require(rate > 0, "FeeOracleV1: no zero rate");
require(chainId != 0, "FeeOracleV1: no zero chain id");
_feeParams[chainId].toNativeRate = rate;
emit ToNativeRateSet(chainId, rate);
}
/**
* @notice Set the base gas limit for each xmsg.
*/
function _setBaseGasLimit(uint64 gasLimit) internal {
baseGasLimit = gasLimit;
emit BaseGasLimitSet(gasLimit);
}
/**
* @notice Set the base protocol fee for each xmsg.
*/
function _setProtocolFee(uint256 fee) internal {
protocolFee = fee;
emit ProtocolFeeSet(fee);
}
/**
* @notice Set the manager admin account.
*/
function _setManager(address manager_) internal {
manager = manager_;
emit ManagerSet(manager_);
}
}