-
Notifications
You must be signed in to change notification settings - Fork 8
/
FeeManagerBase.sol
68 lines (58 loc) · 1.82 KB
/
FeeManagerBase.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
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
abstract contract FeeManagerBase {
uint16 constant MAX_BP = 10_000;
uint16 _feeBP; // the percentage of gas fee in basis point;
// 1356a63b
error BP_Not_Valid();
// 1a72054d
error Insuccifient_Funds();
// c40a532b
error Withdrawal_Failed();
/// @dev access-controlled
function setBp(uint16 _newBp) public virtual {
if (_newBp > MAX_BP) {
revert BP_Not_Valid();
}
_feeBP = _newBp;
}
function getBp() public view returns (uint16) {
return _feeBP;
}
function withdraw(address beneficiary, uint256 amount) public virtual {
if (amount > address(this).balance) {
revert Insuccifient_Funds();
}
_refund(beneficiary, amount);
}
modifier collectFee() {
uint256 txFee;
if (_feeBP > 0) {
uint256 gasBefore = gasleft();
_;
uint256 gasAfter = gasleft();
txFee = ((gasBefore - gasAfter) * tx.gasprice * _feeBP) / MAX_BP;
if (msg.value < txFee) {
revert Insuccifient_Funds();
}
} else {
_;
}
// refund excess
if (msg.value > 0) {
uint256 excess = msg.value - txFee;
if (excess > 0) {
// refund the sender, rather than the caller
// @dev may fail subsequent call(s), if the caller were a contract
// that might need to make subsequent calls requiring ETh transfers
_refund(tx.origin, excess);
}
}
}
function _refund(address recipient, uint256 amount) private {
(bool success,) = recipient.call{value: amount}("");
if (!success) {
revert Withdrawal_Failed();
}
}
}