From c0b755ca823d8bc3818460e6958f071c177df40c Mon Sep 17 00:00:00 2001 From: byshape Date: Tue, 24 Oct 2023 19:00:14 +0100 Subject: [PATCH] Renamed `FarmAccounting` and `UserAccounting` libraries --- README.md | 6 +-- contracts/FarmingPlugin.sol | 10 ++--- contracts/FarmingPool.sol | 8 ++-- contracts/MultiFarmingPlugin.sol | 8 ++-- contracts/interfaces/IFarmingPlugin.sol | 4 +- contracts/interfaces/IFarmingPool.sol | 4 +- contracts/interfaces/IMultiFarmingPlugin.sol | 4 +- .../Accounting.sol} | 6 +-- .../Allocation.sol} | 6 +-- contracts/{ => libraries}/FarmingLib.sol | 40 +++++++++---------- test/FarmingPlugin.js | 6 +-- 11 files changed, 51 insertions(+), 51 deletions(-) rename contracts/{accounting/UserAccounting.sol => libraries/Accounting.sol} (94%) rename contracts/{accounting/FarmAccounting.sol => libraries/Allocation.sol} (91%) rename contracts/{ => libraries}/FarmingLib.sol (75%) diff --git a/README.md b/README.md index b63893e..c6bfff3 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ contract AMMPoolToken is ERC20Plugins { Storage access: -- [1 storage slot](https://github.com/1inch/farming/blob/master/contracts/accounting/FarmAccounting.sol#L14-L16) for farming params, updated only on farming restarting: +- [1 storage slot](https://github.com/1inch/farming/blob/master/contracts/libraries/Allocation.sol#L13-L15) for farming params, updated only when farming is restarted or stopped: ```solidity uint40 public finished; @@ -121,14 +121,14 @@ Storage access: uint184 public reward; ``` -- [1 storage slot](https://github.com/1inch/farming/blob/master/contracts/accounting/UserAccounting.sol#L7-L8) for farming state, updated only on changing number of farming tokens: +- [1 storage slot](https://github.com/1inch/farming/blob/master/contracts/libraries/Accounting.sol#L9-L10) for farming state, updated only on changing number of farming tokens: ```solidity uint40 public checkpoint; uint216 public farmedPerTokenStored; ``` -- [1 storage slot](https://github.com/1inch/farming/blob/master/contracts/accounting/UserAccounting.sol#L9) per each farmer, updated on deposits/withdrawals (kudos to [@snjax](https://github.com/snjax)): +- [1 storage slot](https://github.com/1inch/farming/blob/master/contracts/libraries/Accounting.sol#L11) per each farmer, updated on deposits/withdrawals (kudos to [@snjax](https://github.com/snjax)): ```solidity mapping(address => int256) public corrections; diff --git a/contracts/FarmingPlugin.sol b/contracts/FarmingPlugin.sol index a4aac7d..a14fd99 100644 --- a/contracts/FarmingPlugin.sol +++ b/contracts/FarmingPlugin.sol @@ -11,12 +11,12 @@ import { IERC20Plugins } from "@1inch/token-plugins/contracts/interfaces/IERC20P import { IFarmingPlugin } from "./interfaces/IFarmingPlugin.sol"; import { Distributor } from "./Distributor.sol"; -import { FarmingLib, FarmAccounting } from "./FarmingLib.sol"; +import { FarmingLib, Allocation } from "./libraries/FarmingLib.sol"; contract FarmingPlugin is Plugin, IFarmingPlugin, Distributor { using SafeERC20 for IERC20; using FarmingLib for FarmingLib.Info; - using FarmAccounting for FarmAccounting.Info; + using Allocation for Allocation.Info; using Address for address payable; error ZeroFarmableTokenAddress(); @@ -37,8 +37,8 @@ contract FarmingPlugin is Plugin, IFarmingPlugin, Distributor { emit FarmCreated(address(farmableToken_), address(rewardsToken_)); } - function farmInfo() public view returns(FarmAccounting.Info memory) { - return _farm.farmInfo; + function farmInfo() public view returns(Allocation.Info memory) { + return _farm.allocationInfo; } function totalSupply() public view returns(uint256) { @@ -91,7 +91,7 @@ contract FarmingPlugin is Plugin, IFarmingPlugin, Distributor { payable(_distributor).sendValue(amount); } else { if (token_ == rewardsToken) { - if (rewardsToken.balanceOf(address(this)) < _farm.farmInfo.balance + amount) revert InsufficientFunds(); + if (rewardsToken.balanceOf(address(this)) < _farm.allocationInfo.balance + amount) revert InsufficientFunds(); } token_.safeTransfer(_distributor, amount); } diff --git a/contracts/FarmingPool.sol b/contracts/FarmingPool.sol index 4c0db93..879a870 100644 --- a/contracts/FarmingPool.sol +++ b/contracts/FarmingPool.sol @@ -10,7 +10,7 @@ import { SafeERC20 } from "@1inch/solidity-utils/contracts/libraries/SafeERC20.s import { IFarmingPool } from "./interfaces/IFarmingPool.sol"; import { Distributor } from "./Distributor.sol"; -import { FarmAccounting, FarmingLib } from "./FarmingLib.sol"; +import { Allocation, FarmingLib } from "./libraries/FarmingLib.sol"; contract FarmingPool is IFarmingPool, Distributor, ERC20 { using SafeERC20 for IERC20; @@ -48,8 +48,8 @@ contract FarmingPool is IFarmingPool, Distributor, ERC20 { return IERC20Metadata(address(stakingToken)).decimals(); } - function farmInfo() public view returns(FarmAccounting.Info memory) { - return _farm.farmInfo; + function farmInfo() public view returns(Allocation.Info memory) { + return _farm.allocationInfo; } function startFarming(uint256 amount, uint256 period) public virtual onlyDistributor { @@ -104,7 +104,7 @@ contract FarmingPool is IFarmingPool, Distributor, ERC20 { if (token == stakingToken) { if (stakingToken.balanceOf(address(this)) < totalSupply() + amount) revert InsufficientFunds(); } else if (token == rewardsToken) { - if (rewardsToken.balanceOf(address(this)) < _farm.farmInfo.balance + amount) revert InsufficientFunds(); + if (rewardsToken.balanceOf(address(this)) < _farm.allocationInfo.balance + amount) revert InsufficientFunds(); } token.safeTransfer(_distributor, amount); diff --git a/contracts/MultiFarmingPlugin.sol b/contracts/MultiFarmingPlugin.sol index 0b3a24d..c6541d2 100644 --- a/contracts/MultiFarmingPlugin.sol +++ b/contracts/MultiFarmingPlugin.sol @@ -12,7 +12,7 @@ import { IERC20Plugins } from "@1inch/token-plugins/contracts/interfaces/IERC20P import { IMultiFarmingPlugin } from "./interfaces/IMultiFarmingPlugin.sol"; import { Distributor } from "./Distributor.sol"; -import { FarmAccounting, FarmingLib } from "./FarmingLib.sol"; +import { Allocation, FarmingLib } from "./libraries/FarmingLib.sol"; contract MultiFarmingPlugin is Plugin, IMultiFarmingPlugin, Distributor { using SafeERC20 for IERC20; @@ -46,8 +46,8 @@ contract MultiFarmingPlugin is Plugin, IMultiFarmingPlugin, Distributor { return _rewardsTokens.items.get(); } - function farmInfo(IERC20 rewardsToken) public view returns(FarmAccounting.Info memory) { - return _farms[rewardsToken].farmInfo; + function farmInfo(IERC20 rewardsToken) public view returns(Allocation.Info memory) { + return _farms[rewardsToken].allocationInfo; } function totalSupply() public view returns(uint256) { @@ -132,7 +132,7 @@ contract MultiFarmingPlugin is Plugin, IMultiFarmingPlugin, Distributor { payable(_distributor).sendValue(amount); } else { if (_rewardsTokens.contains(address(token_))) { - if (token_.balanceOf(address(this)) < _farms[token_].farmInfo.balance + amount) revert InsufficientFunds(); + if (token_.balanceOf(address(this)) < _farms[token_].allocationInfo.balance + amount) revert InsufficientFunds(); } token_.safeTransfer(_distributor, amount); } diff --git a/contracts/interfaces/IFarmingPlugin.sol b/contracts/interfaces/IFarmingPlugin.sol index 2bbb9af..1eebcd6 100644 --- a/contracts/interfaces/IFarmingPlugin.sol +++ b/contracts/interfaces/IFarmingPlugin.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.0; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { IPlugin } from "@1inch/token-plugins/contracts/interfaces/IPlugin.sol"; -import { FarmAccounting } from "../accounting/FarmAccounting.sol"; +import { Allocation } from "../libraries/Allocation.sol"; interface IFarmingPlugin is IPlugin { event FarmCreated(address token, address reward); @@ -12,7 +12,7 @@ interface IFarmingPlugin is IPlugin { // View functions function totalSupply() external view returns(uint256); - function farmInfo() external view returns(FarmAccounting.Info memory); + function farmInfo() external view returns(Allocation.Info memory); function farmed(address account) external view returns(uint256); // User functions diff --git a/contracts/interfaces/IFarmingPool.sol b/contracts/interfaces/IFarmingPool.sol index e4ac90d..037df64 100644 --- a/contracts/interfaces/IFarmingPool.sol +++ b/contracts/interfaces/IFarmingPool.sol @@ -3,13 +3,13 @@ pragma solidity ^0.8.0; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; -import { FarmAccounting } from "../accounting/FarmAccounting.sol"; +import { Allocation } from "../libraries/Allocation.sol"; interface IFarmingPool is IERC20 { event RewardUpdated(uint256 reward, uint256 duration); // View functions - function farmInfo() external view returns(FarmAccounting.Info memory); + function farmInfo() external view returns(Allocation.Info memory); function farmed(address account) external view returns(uint256); // User functions diff --git a/contracts/interfaces/IMultiFarmingPlugin.sol b/contracts/interfaces/IMultiFarmingPlugin.sol index 38d6a55..f3aa5f6 100644 --- a/contracts/interfaces/IMultiFarmingPlugin.sol +++ b/contracts/interfaces/IMultiFarmingPlugin.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.0; import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import { IPlugin } from "@1inch/token-plugins/contracts/interfaces/IPlugin.sol"; -import { FarmAccounting } from "../accounting/FarmAccounting.sol"; +import { Allocation } from "../libraries/Allocation.sol"; interface IMultiFarmingPlugin is IPlugin { event FarmCreated(address token, address reward); @@ -12,7 +12,7 @@ interface IMultiFarmingPlugin is IPlugin { // View functions function totalSupply() external view returns(uint256); - function farmInfo(IERC20 rewardsToken) external view returns(FarmAccounting.Info memory); + function farmInfo(IERC20 rewardsToken) external view returns(Allocation.Info memory); function farmed(IERC20 rewardsToken, address account) external view returns(uint256); // User functions diff --git a/contracts/accounting/UserAccounting.sol b/contracts/libraries/Accounting.sol similarity index 94% rename from contracts/accounting/UserAccounting.sol rename to contracts/libraries/Accounting.sol index 735596a..cc6b0de 100644 --- a/contracts/accounting/UserAccounting.sol +++ b/contracts/libraries/Accounting.sol @@ -2,9 +2,9 @@ pragma solidity ^0.8.0; -import { FarmAccounting } from "./FarmAccounting.sol"; +import { Allocation } from "./Allocation.sol"; -library UserAccounting { +library Accounting { struct Info { uint40 checkpoint; uint216 farmedPerTokenStored; @@ -30,7 +30,7 @@ library UserAccounting { function farmed(Info storage info, address account, uint256 balance, uint256 fpt) internal view returns(uint256) { // balance * fpt is less than 168 bit - return uint256(int256(balance * fpt) - info.corrections[account]) / FarmAccounting._SCALE; + return uint256(int256(balance * fpt) - info.corrections[account]) / Allocation._SCALE; } function eraseFarmed(Info storage info, address account, uint256 balance, uint256 fpt) internal { diff --git a/contracts/accounting/FarmAccounting.sol b/contracts/libraries/Allocation.sol similarity index 91% rename from contracts/accounting/FarmAccounting.sol rename to contracts/libraries/Allocation.sol index 78e2a6e..dd90ffb 100644 --- a/contracts/accounting/FarmAccounting.sol +++ b/contracts/libraries/Allocation.sol @@ -4,7 +4,7 @@ pragma solidity ^0.8.0; import { Math } from "@openzeppelin/contracts/utils/math/Math.sol"; -library FarmAccounting { +library Allocation { error ZeroDuration(); error DurationTooLarge(); error AmountTooLarge(); @@ -31,7 +31,7 @@ library FarmAccounting { } } - function startFarming(Info storage info, uint256 amount, uint256 period) internal returns(uint256) { + function allocate(Info storage info, uint256 amount, uint256 period) internal returns(uint256) { if (period == 0) revert ZeroDuration(); if (period > type(uint32).max) revert DurationTooLarge(); @@ -52,7 +52,7 @@ library FarmAccounting { return amount; } - function stopFarming(Info storage info) internal returns(uint256 leftover) { + function terminateAllocation(Info storage info) internal returns(uint256 leftover) { leftover = info.reward - farmedSinceCheckpointScaled(info, info.finished - info.duration) / _SCALE; (info.finished, info.duration, info.reward, info.balance) = ( uint40(block.timestamp), diff --git a/contracts/FarmingLib.sol b/contracts/libraries/FarmingLib.sol similarity index 75% rename from contracts/FarmingLib.sol rename to contracts/libraries/FarmingLib.sol index 16ecc6b..3be40c9 100644 --- a/contracts/FarmingLib.sol +++ b/contracts/libraries/FarmingLib.sol @@ -2,20 +2,20 @@ pragma solidity ^0.8.0; -import { FarmAccounting } from "./accounting/FarmAccounting.sol"; -import { UserAccounting } from "./accounting/UserAccounting.sol"; +import { Allocation } from "./Allocation.sol"; +import { Accounting } from "./Accounting.sol"; /// @title FarmingLib -/// @dev A library for farming logic, using FarmAccounting and UserAccounting. +/// @dev A library for farming logic, using Allocation and Accounting. library FarmingLib { - using FarmAccounting for FarmAccounting.Info; - using UserAccounting for UserAccounting.Info; + using Allocation for Allocation.Info; + using Accounting for Accounting.Info; using FarmingLib for FarmingLib.Info; - /// @dev Struct containing farm and user detailed info for farming operations. See {FarmAccounting.Info} and {UserAccounting.Info} for. + /// @dev Struct containing allocation and accounting detailed info for farming operations. See {Allocation.Info} and {Accounting.Info} for. struct Data { - FarmAccounting.Info farmInfo; - UserAccounting.Info userInfo; + Allocation.Info allocationInfo; + Accounting.Info accountingInfo; } /// @dev Struct containing the total supply function and a data slot for EVM storage. @@ -60,8 +60,8 @@ library FarmingLib { */ function startFarming(Info memory self, uint256 amount, uint256 period) internal returns(uint256 reward) { Data storage data = self.getData(); - data.userInfo.updateFarmedPerToken(_farmedPerToken(self)); - reward = data.farmInfo.startFarming(amount, period); + data.accountingInfo.updateFarmedPerToken(_farmedPerToken(self)); + reward = data.allocationInfo.allocate(amount, period); } /** @@ -71,8 +71,8 @@ library FarmingLib { */ function stopFarming(Info memory self) internal returns(uint256 leftover) { Data storage data = self.getData(); - data.userInfo.updateFarmedPerToken(_farmedPerToken(self)); - leftover = data.farmInfo.stopFarming(); + data.accountingInfo.updateFarmedPerToken(_farmedPerToken(self)); + leftover = data.allocationInfo.terminateAllocation(); } /** @@ -83,7 +83,7 @@ library FarmingLib { * @return result The farmed amount. */ function farmed(Info memory self, address account, uint256 balance) internal view returns(uint256) { - return self.getData().userInfo.farmed(account, balance, _farmedPerToken(self)); + return self.getData().accountingInfo.farmed(account, balance, _farmedPerToken(self)); } /** @@ -96,10 +96,10 @@ library FarmingLib { function claim(Info memory self, address account, uint256 balance) internal returns(uint256 amount) { Data storage data = self.getData(); uint256 fpt = _farmedPerToken(self); - amount = data.userInfo.farmed(account, balance, fpt); + amount = data.accountingInfo.farmed(account, balance, fpt); if (amount > 0) { - data.userInfo.eraseFarmed(account, balance, fpt); - data.farmInfo.claim(amount); + data.accountingInfo.eraseFarmed(account, balance, fpt); + data.allocationInfo.claim(amount); } } @@ -111,14 +111,14 @@ library FarmingLib { * @param amount The amount to transfer. */ function updateBalances(Info memory self, address from, address to, uint256 amount) internal { - self.getData().userInfo.updateBalances(from, to, amount, _farmedPerToken(self)); + self.getData().accountingInfo.updateBalances(from, to, amount, _farmedPerToken(self)); } function _farmedPerToken(Info memory self) private view returns (uint256) { - return self.getData().userInfo.farmedPerToken(_infoToContext(self), _lazyGetSupply, _lazyGetFarmed); + return self.getData().accountingInfo.farmedPerToken(_infoToContext(self), _lazyGetSupply, _lazyGetFarmed); } - // UserAccounting bindings + // Accounting bindings function _lazyGetSupply(bytes32 context) private view returns(uint256) { Info memory self = _contextToInfo(context); @@ -127,7 +127,7 @@ library FarmingLib { function _lazyGetFarmed(bytes32 context, uint256 checkpoint) private view returns(uint256) { Info memory self = _contextToInfo(context); - return self.getData().farmInfo.farmedSinceCheckpointScaled(checkpoint); + return self.getData().allocationInfo.farmedSinceCheckpointScaled(checkpoint); } function _contextToInfo(bytes32 context) private pure returns(Info memory self) { diff --git a/test/FarmingPlugin.js b/test/FarmingPlugin.js index 1b0fffa..567f05e 100644 --- a/test/FarmingPlugin.js +++ b/test/FarmingPlugin.js @@ -334,14 +334,14 @@ describe('FarmingPlugin', function () { const duration = BigInt(60 * 60 * 24); await farm.startFarming(1000, duration); await time.increaseTo((await farm.farmInfo()).finished + 1n); - + const balanceWalletBefore = await gift.balanceOf(wallet1); const balanceFarmBefore = await gift.balanceOf(farm); - + const distributor = await farm.distributor(); expect(wallet1.address).to.equal(distributor); await farm.stopFarming(); - + expect(await gift.balanceOf(wallet1)).to.be.equal(balanceWalletBefore); expect(await gift.balanceOf(farm)).to.be.equal(balanceFarmBefore); expect((await farm.farmInfo()).reward).to.be.equal(0);