/
FarmingCenter.sol
315 lines (266 loc) · 12.1 KB
/
FarmingCenter.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
// SPDX-License-Identifier: BUSL-1.1
pragma solidity =0.7.6;
pragma abicoder v2;
import './interfaces/IFarmingCenter.sol';
import './interfaces/IFarmingCenterVault.sol';
import '@cryptoalgebra/core/contracts/interfaces/IAlgebraPool.sol';
import '@cryptoalgebra/core/contracts/interfaces/IERC20Minimal.sol';
import '@cryptoalgebra/periphery/contracts/interfaces/INonfungiblePositionManager.sol';
import '@cryptoalgebra/periphery/contracts/base/Multicall.sol';
import '@cryptoalgebra/periphery/contracts/base/ERC721Permit.sol';
import './base/PeripheryPayments.sol';
import './libraries/IncentiveId.sol';
/// @title Algebra main farming contract
/// @dev Manages farmings and performs entry, exit and other actions.
contract FarmingCenter is IFarmingCenter, ERC721Permit, Multicall, PeripheryPayments {
IAlgebraLimitFarming public immutable override limitFarming;
IAlgebraEternalFarming public immutable override eternalFarming;
INonfungiblePositionManager public immutable override nonfungiblePositionManager;
IFarmingCenterVault public immutable override farmingCenterVault;
/// @dev The ID of the next token that will be minted. Skips 0
uint256 private _nextId = 1;
/// @dev saves addresses of virtual pools for pool
mapping(address => VirtualPoolAddresses) private _virtualPoolAddresses;
/// @dev deposits[tokenId] => Deposit
mapping(uint256 => Deposit) public override deposits;
mapping(uint256 => L2Nft) public override l2Nfts;
/// @notice Represents the deposit of a liquidity NFT
struct Deposit {
uint256 L2TokenId;
uint32 numberOfFarms;
bool inLimitFarming;
address owner;
}
/// @notice Represents the nft layer 2
struct L2Nft {
uint96 nonce; // the nonce for permits
address operator; // the address that is approved for spending this token
uint256 tokenId;
}
constructor(
IAlgebraLimitFarming _limitFarming,
IAlgebraEternalFarming _eternalFarming,
INonfungiblePositionManager _nonfungiblePositionManager,
IFarmingCenterVault _farmingCenterVault
)
ERC721Permit('Algebra Farming NFT-V2', 'ALGB-FARM', '2')
PeripheryPayments(INonfungiblePositionManager(_nonfungiblePositionManager).WNativeToken())
{
limitFarming = _limitFarming;
eternalFarming = _eternalFarming;
nonfungiblePositionManager = _nonfungiblePositionManager;
farmingCenterVault = _farmingCenterVault;
}
function checkAuthorizationForToken(uint256 tokenId) private view {
require(_isApprovedOrOwner(msg.sender, tokenId), 'Not approved');
}
/// @notice Upon receiving a Algebra ERC721, creates the token deposit setting owner to `from`.
/// @inheritdoc IERC721Receiver
function onERC721Received(
address,
address from,
uint256 tokenId,
bytes calldata
) external override returns (bytes4) {
require(msg.sender == address(nonfungiblePositionManager), 'not an Algebra nft');
uint256 id = _nextId;
Deposit storage newDeposit = deposits[tokenId];
(newDeposit.L2TokenId, newDeposit.owner) = (id, from);
l2Nfts[id].tokenId = tokenId;
_mint(from, id);
_nextId = id + 1;
emit DepositTransferred(tokenId, address(0), from);
return this.onERC721Received.selector;
}
function _getTokenBalanceOfVault(address token) private view returns (uint256 balance) {
return IERC20Minimal(token).balanceOf(address(farmingCenterVault));
}
/// @inheritdoc IFarmingCenter
function enterFarming(
IncentiveKey memory key,
uint256 tokenId,
uint256 tokensLocked,
bool isLimit
) external override {
Deposit storage _deposit = deposits[tokenId];
checkAuthorizationForToken(_deposit.L2TokenId);
(uint32 numberOfFarms, bool inLimitFarming) = (_deposit.numberOfFarms, _deposit.inLimitFarming);
numberOfFarms++;
IAlgebraFarming _farming;
if (isLimit) {
require(!inLimitFarming, 'token already farmed');
inLimitFarming = true;
_farming = IAlgebraFarming(limitFarming);
} else _farming = IAlgebraFarming(eternalFarming);
(_deposit.numberOfFarms, _deposit.inLimitFarming) = (numberOfFarms, inLimitFarming);
bytes32 incentiveId = IncentiveId.compute(key);
(, , , , , address multiplierToken, ) = _farming.incentives(incentiveId);
if (tokensLocked > 0) {
uint256 balanceBefore = _getTokenBalanceOfVault(multiplierToken);
TransferHelper.safeTransferFrom(multiplierToken, msg.sender, address(farmingCenterVault), tokensLocked);
uint256 balanceAfter = _getTokenBalanceOfVault(multiplierToken);
require(balanceAfter > balanceBefore, 'Insufficient tokens locked');
tokensLocked = balanceAfter - balanceBefore;
farmingCenterVault.lockTokens(tokenId, incentiveId, tokensLocked);
}
_farming.enterFarming(key, tokenId, tokensLocked);
}
/// @inheritdoc IFarmingCenter
function exitFarming(
IncentiveKey memory key,
uint256 tokenId,
bool isLimit
) external override {
Deposit storage deposit = deposits[tokenId];
checkAuthorizationForToken(deposit.L2TokenId);
IAlgebraFarming _farming;
deposit.numberOfFarms -= 1;
deposit.owner = msg.sender;
if (isLimit) {
deposit.inLimitFarming = false;
_farming = IAlgebraFarming(limitFarming);
} else _farming = IAlgebraFarming(eternalFarming);
_farming.exitFarming(key, tokenId, msg.sender);
bytes32 incentiveId = IncentiveId.compute(key);
(, , , , , address multiplierToken, ) = _farming.incentives(incentiveId);
if (multiplierToken != address(0)) {
farmingCenterVault.claimTokens(multiplierToken, msg.sender, tokenId, incentiveId);
}
}
/// @inheritdoc IFarmingCenter
function collect(INonfungiblePositionManager.CollectParams memory params)
external
override
returns (uint256 amount0, uint256 amount1)
{
checkAuthorizationForToken(deposits[params.tokenId].L2TokenId);
if (params.recipient == address(0)) {
params.recipient = address(this);
}
return nonfungiblePositionManager.collect(params);
}
/// @inheritdoc IFarmingCenter
function collectRewards(IncentiveKey memory key, uint256 tokenId)
external
override
returns (uint256 reward, uint256 bonusReward)
{
checkAuthorizationForToken(deposits[tokenId].L2TokenId);
address _virtualPool = _virtualPoolAddresses[address(key.pool)].eternalVirtualPool;
if (_virtualPool != address(0)) {
IAlgebraVirtualPool(_virtualPool).increaseCumulative(uint32(block.timestamp));
}
(reward, bonusReward) = eternalFarming.collectRewards(key, tokenId, msg.sender);
}
function _claimRewardFromFarming(
IAlgebraFarming _farming,
IERC20Minimal rewardToken,
address to,
uint256 amountRequested
) internal returns (uint256 reward) {
return _farming.claimRewardFrom(rewardToken, msg.sender, to, amountRequested);
}
/// @inheritdoc IFarmingCenter
function claimReward(
IERC20Minimal rewardToken,
address to,
uint256 amountRequestedIncentive,
uint256 amountRequestedEternal
) external override returns (uint256 reward) {
if (amountRequestedIncentive != 0) {
reward = _claimRewardFromFarming(limitFarming, rewardToken, to, amountRequestedIncentive);
}
if (amountRequestedEternal != 0) {
reward += _claimRewardFromFarming(eternalFarming, rewardToken, to, amountRequestedEternal);
}
}
/// @inheritdoc IFarmingCenter
function connectVirtualPool(IAlgebraPool pool, address newVirtualPool) external override {
bool isLimitFarming = msg.sender == address(limitFarming);
require(isLimitFarming || msg.sender == address(eternalFarming), 'only farming can call this');
VirtualPoolAddresses storage virtualPools = _virtualPoolAddresses[address(pool)];
address newIncentive;
if (pool.activeIncentive() == address(0)) {
newIncentive = newVirtualPool; // turn on pool directly
} else {
if (newVirtualPool == address(0)) {
// turn on directly another pool if it exists
newIncentive = isLimitFarming ? virtualPools.eternalVirtualPool : virtualPools.limitVirtualPool;
} else {
newIncentive = address(this); // turn on via "proxy"
}
}
pool.setIncentive(newIncentive);
if (isLimitFarming) {
virtualPools.limitVirtualPool = newVirtualPool;
} else {
virtualPools.eternalVirtualPool = newVirtualPool;
}
}
/// @inheritdoc IFarmingCenter
function withdrawToken(
uint256 tokenId,
address to,
bytes memory data
) external override {
require(to != address(this), 'cannot withdraw to farming');
Deposit storage deposit = deposits[tokenId];
uint256 l2TokenId = deposit.L2TokenId;
checkAuthorizationForToken(l2TokenId);
require(deposit.numberOfFarms == 0, 'cannot withdraw token while farmd');
delete l2Nfts[l2TokenId];
_burn(l2TokenId);
delete deposits[tokenId];
emit DepositTransferred(tokenId, msg.sender, address(0));
nonfungiblePositionManager.safeTransferFrom(address(this), to, tokenId, data);
}
/**
* @dev This function is called by the main pool when an initialized tick is crossed and two farmings are active at same time.
* @param nextTick The crossed tick
* @param zeroToOne The direction
*/
function cross(int24 nextTick, bool zeroToOne) external override {
VirtualPoolAddresses storage _virtualPoolAddressesForPool = _virtualPoolAddresses[msg.sender];
IAlgebraVirtualPool(_virtualPoolAddressesForPool.eternalVirtualPool).cross(nextTick, zeroToOne);
IAlgebraVirtualPool(_virtualPoolAddressesForPool.limitVirtualPool).cross(nextTick, zeroToOne);
}
/**
* @dev This function is called by the main pool at start of the swap if two farmings are active at same time.
* @param blockTimestamp The current block timestamp, truncated
* @return status The current farming(s) status
*/
function increaseCumulative(uint32 blockTimestamp) external override returns (Status status) {
VirtualPoolAddresses storage _virtualPoolAddressesForPool = _virtualPoolAddresses[msg.sender];
Status eternalStatus = IAlgebraVirtualPool(_virtualPoolAddressesForPool.eternalVirtualPool).increaseCumulative(
blockTimestamp
);
Status limitStatus = IAlgebraVirtualPool(_virtualPoolAddressesForPool.limitVirtualPool).increaseCumulative(
blockTimestamp
);
if (eternalStatus == Status.ACTIVE || limitStatus == Status.ACTIVE) {
return Status.ACTIVE;
} else if (limitStatus == Status.NOT_STARTED) {
return Status.NOT_STARTED;
}
return Status.NOT_EXIST;
}
function virtualPoolAddresses(address pool) external view override returns (address limitVP, address eternalVP) {
(limitVP, eternalVP) = (
_virtualPoolAddresses[pool].limitVirtualPool,
_virtualPoolAddresses[pool].eternalVirtualPool
);
}
function _getAndIncrementNonce(uint256 tokenId) internal override returns (uint256) {
return uint256(l2Nfts[tokenId].nonce++);
}
/// @inheritdoc IERC721
function getApproved(uint256 tokenId) public view override(ERC721, IERC721) returns (address) {
require(_exists(tokenId), 'ERC721: approved query for nonexistent token');
return l2Nfts[tokenId].operator;
}
/// @dev Overrides _approve to use the operator in the position, which is packed with the position permit nonce
function _approve(address to, uint256 tokenId) internal override(ERC721) {
l2Nfts[tokenId].operator = to;
emit Approval(ownerOf(tokenId), to, tokenId);
}
}