@oldzeppelin/contract 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/.docker/Dockerfile +17 -0
  2. package/.dockerignore +7 -0
  3. package/.env.sample +24 -0
  4. package/.gitlab-ci.yml +51 -0
  5. package/.gitmodules +15 -0
  6. package/.prettierrc +10 -0
  7. package/.solcover.js +4 -0
  8. package/.vscode/settings.json +23 -0
  9. package/LICENSE.MD +51 -0
  10. package/README.md +135 -0
  11. package/contracts/arbitrum/contracts/controllers/UniswapV2ControllerArbitrum.sol +37 -0
  12. package/contracts/arbitrum/contracts/controllers/UniswapV3ControllerArbitrum.sol +46 -0
  13. package/contracts/arbitrum/contracts/oracle/PriceOracleArbitrum.sol +51 -0
  14. package/contracts/main/contracts/controllers/Controller.sol +61 -0
  15. package/contracts/main/contracts/controllers/IController.sol +81 -0
  16. package/contracts/main/contracts/controllers/OneInchV5Controller.sol +332 -0
  17. package/contracts/main/contracts/controllers/UnoswapV2Controller.sol +789 -0
  18. package/contracts/main/contracts/controllers/UnoswapV3Controller.sol +1018 -0
  19. package/contracts/main/contracts/core/CoreWhitelist.sol +192 -0
  20. package/contracts/main/contracts/core/ICoreWhitelist.sol +92 -0
  21. package/contracts/main/contracts/core/IUFarmCore.sol +95 -0
  22. package/contracts/main/contracts/core/UFarmCore.sol +402 -0
  23. package/contracts/main/contracts/fund/FundFactory.sol +59 -0
  24. package/contracts/main/contracts/fund/IUFarmFund.sol +68 -0
  25. package/contracts/main/contracts/fund/UFarmFund.sol +504 -0
  26. package/contracts/main/contracts/oracle/ChainlinkedOracle.sol +71 -0
  27. package/contracts/main/contracts/oracle/IChainlinkAggregator.sol +18 -0
  28. package/contracts/main/contracts/oracle/IPriceOracle.sol +55 -0
  29. package/contracts/main/contracts/oracle/PriceOracle.sol +20 -0
  30. package/contracts/main/contracts/oracle/PriceOracleCore.sol +212 -0
  31. package/contracts/main/contracts/oracle/WstETHOracle.sol +64 -0
  32. package/contracts/main/contracts/permissions/Permissions.sol +54 -0
  33. package/contracts/main/contracts/permissions/UFarmPermissionsModel.sol +136 -0
  34. package/contracts/main/contracts/pool/IPoolAdmin.sol +57 -0
  35. package/contracts/main/contracts/pool/IUFarmPool.sol +304 -0
  36. package/contracts/main/contracts/pool/PerformanceFeeLib.sol +81 -0
  37. package/contracts/main/contracts/pool/PoolAdmin.sol +437 -0
  38. package/contracts/main/contracts/pool/PoolFactory.sol +74 -0
  39. package/contracts/main/contracts/pool/PoolWhitelist.sol +70 -0
  40. package/contracts/main/contracts/pool/UFarmPool.sol +959 -0
  41. package/contracts/main/shared/AssetController.sol +194 -0
  42. package/contracts/main/shared/ECDSARecover.sol +91 -0
  43. package/contracts/main/shared/NZGuard.sol +99 -0
  44. package/contracts/main/shared/SafeOPS.sol +128 -0
  45. package/contracts/main/shared/UFarmCoreLink.sol +83 -0
  46. package/contracts/main/shared/UFarmErrors.sol +16 -0
  47. package/contracts/main/shared/UFarmMathLib.sol +80 -0
  48. package/contracts/main/shared/UFarmOwnableUUPS.sol +59 -0
  49. package/contracts/main/shared/UFarmOwnableUUPSBeacon.sol +34 -0
  50. package/contracts/test/Block.sol +15 -0
  51. package/contracts/test/InchSwapTestProxy.sol +292 -0
  52. package/contracts/test/MockPoolAdmin.sol +8 -0
  53. package/contracts/test/MockUFarmPool.sol +8 -0
  54. package/contracts/test/MockV3wstETHstETHAgg.sol +128 -0
  55. package/contracts/test/MockedWETH9.sol +72 -0
  56. package/contracts/test/OneInchToUFarmTestEnv.sol +466 -0
  57. package/contracts/test/StableCoin.sol +25 -0
  58. package/contracts/test/UFarmMockSequencerUptimeFeed.sol +44 -0
  59. package/contracts/test/UFarmMockV3Aggregator.sol +145 -0
  60. package/contracts/test/UUPSBlock.sol +19 -0
  61. package/contracts/test/ufarmLocal/MulticallV3.sol +220 -0
  62. package/contracts/test/ufarmLocal/controllers/UniswapV2ControllerUFarm.sol +27 -0
  63. package/contracts/test/ufarmLocal/controllers/UniswapV3ControllerUFarm.sol +43 -0
  64. package/deploy/100_test_env_setup.ts +483 -0
  65. package/deploy/20_deploy_uniV2.ts +48 -0
  66. package/deploy/21_create_pairs_uniV2.ts +149 -0
  67. package/deploy/22_deploy_mocked_aggregators.ts +123 -0
  68. package/deploy/22_deploy_wsteth_oracle.ts +65 -0
  69. package/deploy/23_deploy_uniV3.ts +80 -0
  70. package/deploy/24_create_pairs_uniV3.ts +140 -0
  71. package/deploy/25_deploy_oneInch.ts +38 -0
  72. package/deploy/2_deploy_multicall.ts +34 -0
  73. package/deploy/30_deploy_price_oracle.ts +33 -0
  74. package/deploy/3_deploy_lido.ts +114 -0
  75. package/deploy/40_deploy_pool_beacon.ts +19 -0
  76. package/deploy/41_deploy_poolAdmin_beacon.ts +19 -0
  77. package/deploy/42_deploy_ufarmcore.ts +29 -0
  78. package/deploy/43_deploy_fund_beacon.ts +19 -0
  79. package/deploy/4_deploy_tokens.ts +76 -0
  80. package/deploy/50_deploy_poolFactory.ts +35 -0
  81. package/deploy/51_deploy_fundFactory.ts +29 -0
  82. package/deploy/60_init_contracts.ts +101 -0
  83. package/deploy/61_whitelist_tokens.ts +18 -0
  84. package/deploy/70_deploy_uniV2Controller.ts +70 -0
  85. package/deploy/71_deploy_uniV3Controller.ts +67 -0
  86. package/deploy/72_deploy_oneInchController.ts +25 -0
  87. package/deploy/79_whitelist_controllers.ts +125 -0
  88. package/deploy/ufarm/arbitrum/1_prepare_env.ts +82 -0
  89. package/deploy/ufarm/arbitrum/2_deploy_ufarm.ts +178 -0
  90. package/deploy/ufarm/arbitrum-sepolia/1000_prepare_arb_sepolia_env.ts +308 -0
  91. package/deploy-config.json +112 -0
  92. package/deploy-data/oracles.csv +32 -0
  93. package/deploy-data/protocols.csv +10 -0
  94. package/deploy-data/tokens.csv +32 -0
  95. package/docker-compose.yml +67 -0
  96. package/hardhat.config.ts +449 -0
  97. package/index.js +93 -0
  98. package/package.json +82 -0
  99. package/scripts/_deploy_helpers.ts +992 -0
  100. package/scripts/_deploy_network_options.ts +49 -0
  101. package/scripts/activatePool.ts +51 -0
  102. package/scripts/createPool.ts +62 -0
  103. package/scripts/deploy_1inch_proxy.ts +98 -0
  104. package/scripts/pool-data.ts +420 -0
  105. package/scripts/post-deploy.sh +24 -0
  106. package/scripts/setUniV2Rate.ts +252 -0
  107. package/scripts/swapOneInchV5.ts +94 -0
  108. package/scripts/swapUniswapV2.ts +65 -0
  109. package/scripts/swapUniswapV3.ts +71 -0
  110. package/scripts/test.ts +61 -0
  111. package/scripts/typings-copy-artifacts.ts +83 -0
  112. package/tasks/boostPool.ts +39 -0
  113. package/tasks/createFund.ts +44 -0
  114. package/tasks/deboostPool.ts +48 -0
  115. package/tasks/grantUFarmPermissions.ts +57 -0
  116. package/tasks/index.ts +7 -0
  117. package/tasks/mintUSDT.ts +62 -0
  118. package/test/Periphery.test.ts +640 -0
  119. package/test/PriceOracle.test.ts +82 -0
  120. package/test/TestCases.MD +109 -0
  121. package/test/UFarmCore.test.ts +331 -0
  122. package/test/UFarmFund.test.ts +406 -0
  123. package/test/UFarmPool.test.ts +4736 -0
  124. package/test/_fixtures.ts +783 -0
  125. package/test/_helpers.ts +2195 -0
  126. package/test/_oneInchTestData.ts +632 -0
  127. package/tsconfig.json +12 -0
@@ -0,0 +1,192 @@
1
+ // SPDX-License-Identifier: BUSL-1.1
2
+
3
+ pragma solidity ^0.8.0;
4
+
5
+ import {EnumerableSet} from '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';
6
+ import {NZGuard} from '../../shared/NZGuard.sol';
7
+ import {UFarmErrors} from '../../shared/UFarmErrors.sol';
8
+ import {ICoreWhitelist} from './ICoreWhitelist.sol';
9
+ import {ERC20} from '@openzeppelin/contracts/token/ERC20/ERC20.sol';
10
+
11
+ /**
12
+ * @title TokenWhitelist
13
+ * @author https://ufarm.digital/
14
+ * @notice Keeps track of all tokens that can be used in the system.
15
+ */
16
+ abstract contract CoreWhitelist is ICoreWhitelist, NZGuard, UFarmErrors {
17
+ using EnumerableSet for EnumerableSet.AddressSet;
18
+ using EnumerableSet for EnumerableSet.Bytes32Set;
19
+
20
+ mapping(bytes32 => address) public controllers;
21
+ mapping(address => AssetWithPriceFeed) private _tokenInfo;
22
+
23
+ EnumerableSet.AddressSet private __tokens;
24
+ EnumerableSet.Bytes32Set private __protocols;
25
+
26
+ event TokenAdded(AssetWithPriceFeed assetInfo);
27
+ event TokenRemoved(address indexed token);
28
+ event ProtocolAdded(bytes32 indexed protocol, address indexed controller);
29
+ event ProtocolUpdated(bytes32 indexed protocol, address indexed controller);
30
+ event ProtocolRemoved(bytes32 indexed protocol, address indexed controller);
31
+
32
+ error DecimalsMismatch();
33
+ error TokenAlreadyAdded(address token);
34
+ error TokenNotRemoved(address token);
35
+ error ProtocolNotAllowed(bytes32 protocol);
36
+
37
+ /**
38
+ * @inheritdoc ICoreWhitelist
39
+ */
40
+ function tokenInfo(
41
+ address token
42
+ ) external view override returns (AssetWithPriceFeed memory info) {
43
+ return _tokenInfo[token];
44
+ }
45
+
46
+ /**
47
+ * @inheritdoc ICoreWhitelist
48
+ */
49
+ function whitelistProtocolsWithControllers(
50
+ bytes32[] memory _protocolNames,
51
+ address[] memory _protocolControllers
52
+ ) public virtual override {
53
+ uint256 controllersLength = _protocolControllers.length;
54
+
55
+ if (_protocolNames.length != controllersLength) revert ArraysLengthMismatch();
56
+ _nonEmptyArray(controllersLength);
57
+
58
+ for (uint256 i; i < controllersLength; ++i) {
59
+ address controller = _protocolControllers[i];
60
+ bytes32 protocol = _protocolNames[i];
61
+ if (controller == address(0)) revert ZeroAddress();
62
+ if (protocol == bytes32(0)) revert ZeroValue();
63
+
64
+ _whitelistProtocol(protocol, controller);
65
+ }
66
+ }
67
+
68
+ /**
69
+ * @inheritdoc ICoreWhitelist
70
+ */
71
+ function updateProtocolsControllers(
72
+ bytes32[] memory _protocolNames,
73
+ address[] memory _protocolControllers
74
+ ) public virtual override {
75
+ uint256 controllersLength = _protocolControllers.length;
76
+
77
+ if (_protocolNames.length != controllersLength) revert ArraysLengthMismatch();
78
+ _nonEmptyArray(controllersLength);
79
+
80
+ address controller;
81
+ bytes32 protocol;
82
+ for (uint256 i; i < controllersLength; ++i) {
83
+ controller = _protocolControllers[i];
84
+ protocol = _protocolNames[i];
85
+ _updateProtocol(protocol, controller);
86
+ }
87
+ }
88
+
89
+ /**
90
+ * @inheritdoc ICoreWhitelist
91
+ */
92
+ function blacklistProtocols(bytes32[] memory _protocols) public virtual override {
93
+ uint256 protocolsLength = _protocols.length;
94
+ _nonEmptyArray(protocolsLength);
95
+
96
+ for (uint256 i; i < protocolsLength; ++i) {
97
+ bytes32 protocol = _protocols[i];
98
+ _blacklistProtocol(protocol);
99
+ }
100
+ }
101
+
102
+ /**
103
+ * @inheritdoc ICoreWhitelist
104
+ */
105
+ function getWhitelistedProtocols()
106
+ external
107
+ view
108
+ virtual
109
+ override
110
+ returns (bytes32[] memory protocols)
111
+ {
112
+ return __protocols.values();
113
+ }
114
+
115
+ /**
116
+ * @inheritdoc ICoreWhitelist
117
+ */
118
+ function isTokenWhitelisted(address token) public view returns (bool) {
119
+ return __tokens.contains(token);
120
+ }
121
+
122
+ function _whitelistTokens(AssetWithPriceFeed[] calldata tokens) internal {
123
+ uint256 length = tokens.length;
124
+ _nonEmptyArray(length);
125
+ address token;
126
+ uint8 decimals;
127
+ AssetWithPriceFeed memory assetInfo;
128
+ for (uint256 i; i < length; ++i) {
129
+ assetInfo = tokens[i];
130
+ token = assetInfo.assetAddr;
131
+ if (__tokens.add(token)) {
132
+ decimals = ERC20(token).decimals();
133
+ if (decimals != assetInfo.assetDec) revert DecimalsMismatch();
134
+ _tokenInfo[token] = assetInfo;
135
+ emit TokenAdded(assetInfo);
136
+ } else revert TokenAlreadyAdded(token);
137
+ }
138
+ }
139
+
140
+ function _blacklistTokens(address[] memory tokens) internal {
141
+ uint256 length = tokens.length;
142
+ _nonEmptyArray(length);
143
+ address token;
144
+ for (uint256 i; i < length; ++i) {
145
+ token = tokens[i];
146
+ if (__tokens.remove(token)) {
147
+ emit TokenRemoved(token);
148
+ } else revert TokenNotRemoved(token);
149
+ }
150
+ }
151
+
152
+ /**
153
+ * @inheritdoc ICoreWhitelist
154
+ */
155
+ function isProtocolWhitelisted(bytes32 _protocol) external view virtual override returns (bool) {
156
+ return _isProtocolWhitelisted(_protocol);
157
+ }
158
+
159
+ function _isProtocolWhitelisted(bytes32 _protocol) internal view returns (bool) {
160
+ return __protocols.contains(_protocol);
161
+ }
162
+
163
+ function _getWhitelistedProtocols() internal view returns (bytes32[] memory protocols) {
164
+ return __protocols.values();
165
+ }
166
+
167
+ function _whitelistProtocol(bytes32 _protocol, address _controller) internal {
168
+ if (__protocols.add(_protocol)) {
169
+ controllers[_protocol] = _controller;
170
+ emit ProtocolAdded(_protocol, _controller);
171
+ } else revert ActionAlreadyDone();
172
+ }
173
+
174
+ function _updateProtocol(
175
+ bytes32 _protocol,
176
+ address _controller
177
+ ) internal nonZeroBytes32(_protocol) {
178
+ if (__protocols.contains(_protocol)) {
179
+ controllers[_protocol] = _controller;
180
+ emit ProtocolUpdated(_protocol, _controller);
181
+ } else revert ProtocolNotAllowed(_protocol);
182
+ }
183
+
184
+ function _blacklistProtocol(bytes32 _protocol) internal nonZeroBytes32(_protocol) {
185
+ if (__protocols.remove(_protocol)) {
186
+ delete controllers[_protocol];
187
+ emit ProtocolRemoved(_protocol, controllers[_protocol]);
188
+ } else revert ActionAlreadyDone();
189
+ }
190
+
191
+ uint256[50] private __gap;
192
+ }
@@ -0,0 +1,92 @@
1
+ // SPDX-License-Identifier: BUSL-1.1
2
+
3
+ pragma solidity ^0.8.0;
4
+
5
+ interface ICoreWhitelist {
6
+ struct FeedWithDecimal {
7
+ address feedAddr;
8
+ uint8 feedDec;
9
+ }
10
+ struct AssetWithPriceFeed {
11
+ address assetAddr;
12
+ uint8 assetDec;
13
+ FeedWithDecimal priceFeed;
14
+ }
15
+
16
+ /**
17
+ * @notice Returns contoller address for the protocol
18
+ * @param protocol - protocol to check
19
+ * @return controller - controller address
20
+ */
21
+ function controllers(bytes32 protocol) external view returns (address);
22
+
23
+ /**
24
+ * @notice Returns array of whitelisted protocols
25
+ * @return protocols - array of whitelisted protocols
26
+ */
27
+ function getWhitelistedProtocols() external view returns (bytes32[] memory protocols);
28
+
29
+ /**
30
+ * @notice Checks if contracts can use token
31
+ * @param token - token to check
32
+ * @return bool - `true` if token is whitelisted, `false` otherwise
33
+ */
34
+ function isTokenWhitelisted(address token) external view returns (bool);
35
+
36
+ /**
37
+ * @notice Returns token info
38
+ * @param token - token to check
39
+ * @return info - token info
40
+ */
41
+ function tokenInfo(address token) external view returns (AssetWithPriceFeed memory info);
42
+
43
+ /**
44
+ * @notice Checks if protocol is whitelisted and controller is allowed to use it
45
+ * @param protocol - protocol to check
46
+ * @return bool - `true` if protocol is whitelisted, `false` otherwise
47
+ */
48
+ function isProtocolWhitelisted(bytes32 protocol) external view returns (bool);
49
+
50
+ /**
51
+ * @notice Whitelists tokens for the system
52
+ * @dev Emits `TokenAdded` event for each token
53
+ *
54
+ * @param tokens - array of tokens to whitelist
55
+ */
56
+
57
+ function whitelistTokens(AssetWithPriceFeed[] calldata tokens) external;
58
+
59
+ /**
60
+ * @notice Blacklists tokens from the system
61
+ * @dev Emits TokenRemoved event for each token
62
+ *
63
+ * @param tokens Array of tokens to blacklist
64
+ */
65
+ function blacklistTokens(address[] memory tokens) external;
66
+
67
+ /**
68
+ * @notice Whitelists protocols for the system and sets their controllers
69
+ * @param _protocolNames Array of protocols names
70
+ * @param _protocolControllers Array of new protocols controllers addresses
71
+ */
72
+ function whitelistProtocolsWithControllers(
73
+ bytes32[] memory _protocolNames,
74
+ address[] memory _protocolControllers
75
+ ) external;
76
+
77
+ /**
78
+ * @notice Updates the existing protocols controllers addresses
79
+ * @param _protocolNames Array of protocols names
80
+ * @param _protocolControllers Array of new protocols controllers addresses
81
+ */
82
+ function updateProtocolsControllers(
83
+ bytes32[] memory _protocolNames,
84
+ address[] memory _protocolControllers
85
+ ) external;
86
+
87
+ /**
88
+ * @notice Removes protocols addresses from the controllers mapping
89
+ * @param _protocols Array of protocols names
90
+ */
91
+ function blacklistProtocols(bytes32[] memory _protocols) external;
92
+ }
@@ -0,0 +1,95 @@
1
+ // SPDX-License-Identifier: BUSL-1.1
2
+
3
+ pragma solidity ^0.8.0;
4
+
5
+ import {IFundFactory} from '../fund/FundFactory.sol';
6
+ import {IPoolFactory} from '../pool/PoolFactory.sol';
7
+ import {ICoreWhitelist} from './ICoreWhitelist.sol';
8
+
9
+ interface IUFarmCore is ICoreWhitelist {
10
+ /**
11
+ * @dev Returns the total number of funds in the UFarm ecosystem.
12
+ */
13
+ function fundsCount() external view returns (uint256);
14
+
15
+ /**
16
+ * @dev Retrieves the address of a specific fund by its index.
17
+ */
18
+ function getFund(uint256) external view returns (address);
19
+
20
+ /**
21
+ * @dev Checks if a given address is a recognized fund in the UFarm ecosystem.
22
+ */
23
+ function isFund(address) external view returns (bool);
24
+
25
+ /**
26
+ * @dev Returns the address of the price oracle used in the UFarm ecosystem.
27
+ */
28
+ function priceOracle() external view returns (address);
29
+
30
+ /**
31
+ * @dev Returns the instance of the FundFactory contract used by UFarm.
32
+ */
33
+ function fundFactory() external view returns (IFundFactory);
34
+
35
+ /**
36
+ * @dev Returns the instance of the PoolFactory contract used by UFarm.
37
+ */
38
+ function poolFactory() external view returns (IPoolFactory);
39
+
40
+ /**
41
+ * @dev Checks whether the UFarm protocol is currently paused.
42
+ */
43
+ function isPaused() external view returns (bool);
44
+
45
+ /**
46
+ * @dev Toggles the paused state of the UFarm protocol.
47
+ */
48
+ function switchPause() external;
49
+
50
+ /**
51
+ * @dev Retrieves the current protocol commission rate, expressed as a mantissa.
52
+ */
53
+ function protocolCommission() external view returns (uint256);
54
+
55
+ /**
56
+ * @dev Returns the minimum deposit amount required for a fund to be active.
57
+ */
58
+ function minimumFundDeposit() external view returns (uint256);
59
+
60
+ /// GETTERS
61
+
62
+ /**
63
+ * @dev Callable only by the UFarm member with `Moderator` permission
64
+ * @param _fundManager - address of the fund manager
65
+ * @param _applicationId - internal id of the application
66
+ */
67
+ function createFund(address _fundManager, bytes32 _applicationId) external returns (address fund);
68
+
69
+ /**
70
+ * @dev Sets the minimum fund deposit amount required for activating a Pool.
71
+ * @param _minimumFundDeposit The minimum deposit amount in USDT.
72
+ */
73
+ function setMinimumFundDeposit(uint256 _minimumFundDeposit) external;
74
+
75
+ /**
76
+ * @dev Sets the protocol commission rate.
77
+ * @param _protocolCommission The commission rate, expressed as a mantissa.
78
+ */
79
+ function setProtocolCommission(uint256 _protocolCommission) external;
80
+
81
+ /**
82
+ * @dev Updates the permissions for a specified UFarm member.
83
+ * @param _member The address of the member whose permissions are to be updated.
84
+ * @param _newPermissionsMask The new permissions mask to be applied.
85
+ */
86
+ function updatePermissions(address _member, uint256 _newPermissionsMask) external;
87
+
88
+ /**
89
+ * @notice Allows managers to withdraw assets from the contract
90
+ * @dev Used to withdraw protocol commission. Arrays length should be equal
91
+ * @param _tokens - array of tokens to withdraw
92
+ * @param _amounts - array of amounts to withdraw
93
+ */
94
+ function withdrawAssets(address[] calldata _tokens, uint256[] calldata _amounts) external;
95
+ }