1+ pragma solidity 0.8.12 ;
2+ // Copyright BigchainDB GmbH and Ocean Protocol contributors
3+ // SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
4+ // Code is Apache-2.0 and docs are CC-BY-4.0
5+ import '../interfaces/IERC20.sol ' ;
6+ import '@openzeppelin/contracts/access/Ownable.sol ' ;
7+ import '../utils/SafeERC20.sol ' ;
8+ import "@openzeppelin/contracts/utils/math/SafeMath.sol " ;
9+
10+
11+ /**
12+ * @title EnterpriseFeeCollector
13+ * @dev Ocean Protocol Enterprise Fee Collector contract
14+ */
15+ contract EnterpriseFeeCollector is Ownable {
16+ using SafeERC20 for IERC20 ;
17+ using SafeMath for uint256 ;
18+ uint256 private constant BASE = 1e18 ;
19+ address payable collector;
20+ struct Token {
21+ bool allowed;
22+ uint256 minFee;
23+ uint256 maxFee;
24+ uint256 feePercentage; // ie: 1e15 = 0.1%
25+ }
26+ // maps an exchangeId to an exchange
27+ mapping (address => Token) public tokenList;
28+ /**
29+ * @dev constructor
30+ * Called prior contract deployment. set the controller address and
31+ * the contract owner address
32+ * @param newCollector the fee collector address.
33+ * @param OwnerAddress the contract owner address
34+ */
35+ constructor (
36+ address payable newCollector ,
37+ address OwnerAddress
38+ )
39+ Ownable ()
40+ {
41+ require (
42+ newCollector != address (0 )&&
43+ OwnerAddress != address (0 ),
44+ 'OPFCommunityFeeCollector: collector address or owner is invalid address '
45+ );
46+ collector = newCollector;
47+ transferOwnership (OwnerAddress);
48+ }
49+ /**
50+ * @dev fallback function
51+ * this is a default fallback function in which receives
52+ * the collected ether.
53+ */
54+ fallback () external payable {}
55+
56+ /**
57+ * @dev receive function
58+ * this is a default receive function in which receives
59+ * the collected ether.
60+ */
61+ receive () external payable {}
62+
63+ /**
64+ * @dev withdrawETH
65+ * transfers all the accumlated ether the collector address
66+ */
67+ function withdrawETH ()
68+ external
69+ payable
70+ {
71+ (bool sent , ) = collector.call {value: address (this ).balance}("" );
72+ require (sent, "Failed to send Ether " );
73+ }
74+
75+ /**
76+ * @dev withdrawToken
77+ * transfers all the accumlated tokens the collector address
78+ * @param tokenAddress the token contract address
79+ */
80+ function withdrawToken (
81+ address tokenAddress
82+ )
83+ external
84+ {
85+ require (
86+ tokenAddress != address (0 ),
87+ 'OPFCommunityFeeCollector: invalid token contract address '
88+ );
89+
90+ IERC20 (tokenAddress).safeTransfer (
91+ collector,
92+ IERC20 (tokenAddress).balanceOf (address (this ))
93+ );
94+ }
95+
96+ /**
97+ * @dev changeCollector
98+ * change the current collector address. Only owner can do that.
99+ * @param newCollector the new collector address
100+ */
101+ function changeCollector (
102+ address payable newCollector
103+ )
104+ external
105+ onlyOwner
106+ {
107+ require (
108+ newCollector != address (0 ),
109+ 'OPFCommunityFeeCollector: invalid collector address '
110+ );
111+ collector = newCollector;
112+ }
113+
114+ /**
115+ * @dev isTokenAllowed
116+ * checks if the token is allowed to be used in the fee calculation.
117+ * @param tokenAddress the token contract address
118+ * @return true if the token is allowed, false otherwise
119+ */
120+
121+ function isTokenAllowed (address tokenAddress )
122+ external
123+ view
124+ returns (bool )
125+ {
126+ return tokenList[tokenAddress].allowed;
127+ }
128+ /**
129+ * @dev getToken
130+ * returns the token details.
131+ * @param tokenAddress the token contract address
132+ * @return Token struct containing the token details
133+ */
134+ function getToken (address tokenAddress )
135+ external
136+ view
137+ returns (Token memory )
138+ {
139+ return tokenList[tokenAddress];
140+ }
141+
142+ /**
143+ * @dev updateToken
144+ * updateToken a token in the allowed list.
145+ * @param tokenAddress the token contract address
146+ * @param minFee the minimum fee for the token
147+ * @param maxFee the maximum fee for the token
148+ * @param feePercentage the fee percentage for the token
149+ */
150+ function updateToken (
151+ address tokenAddress ,
152+ uint256 minFee ,
153+ uint256 maxFee ,
154+ uint256 feePercentage ,
155+ bool allowed
156+ )
157+ external
158+ onlyOwner
159+ {
160+ require (
161+ tokenAddress != address (0 ),
162+ 'OPFCommunityFeeCollector: invalid token contract address '
163+ );
164+ require (
165+ minFee < maxFee,
166+ 'OPFCommunityFeeCollector: minFee should be less than maxFee '
167+ );
168+ require (
169+ feePercentage > 0 && feePercentage <= BASE,
170+ 'OPFCommunityFeeCollector: feePercentage should be greater than 0 and less than or equal to 1e18 '
171+ );
172+ tokenList[tokenAddress] = Token ({
173+ allowed: allowed,
174+ minFee: minFee,
175+ maxFee: maxFee,
176+ feePercentage: feePercentage
177+ });
178+ }
179+
180+ function calculateFee (address tokenAddress , uint256 amount )
181+ external
182+ view
183+ returns (uint256 )
184+ {
185+ uint256 fee;
186+ if (tokenList[tokenAddress].feePercentage> 0 )
187+ fee= amount.mul (tokenList[tokenAddress].feePercentage).div (BASE);
188+ else
189+ fee= 0 ;
190+ if (fee < tokenList[tokenAddress].minFee) {
191+ return tokenList[tokenAddress].minFee;
192+ }
193+ if (fee > tokenList[tokenAddress].maxFee) {
194+ return tokenList[tokenAddress].maxFee;
195+ }
196+ return fee;
197+ }
198+
199+ }
0 commit comments