Skip to content

Commit

Permalink
Introduce ERC20Farm remaining after claim for non-precise implementat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
k06a committed Nov 27, 2021
1 parent b778663 commit 84fd5ed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
7 changes: 5 additions & 2 deletions contracts/ERC20Farm.sol
Expand Up @@ -17,9 +17,12 @@ contract ERC20Farm is IERC20Farm, FarmAccounting {
FarmAccounting(stakingToken_, rewardsToken_)
{} // solhint-disable-line no-empty-blocks

function claimFor(address account, uint256 amount) external override {
function claimFor(address account, uint256 amount) external override returns(uint256 remaining) {
require(msg.sender == address(stakingToken), "ERC20: Access denied");
rewardsToken.safeTransfer(account, amount);

uint256 claimable = Math.min(amount, rewardsToken.balanceOf(address(this)));
rewardsToken.safeTransfer(account, claimable);
return amount - claimable;
}

function _updateFarmingState() internal override {
Expand Down
16 changes: 10 additions & 6 deletions contracts/ERC20Farmable.sol
Expand Up @@ -83,12 +83,16 @@ abstract contract ERC20Farmable is ERC20, IERC20Farmable {
uint256 balance = balanceOf(msg.sender);


try farm_.claimFor(msg.sender, _farmed(farm_, msg.sender, balance, fpt)) {
if (_userFarms[msg.sender].contains(address(farm_))) {
userCorrection[farm_][msg.sender] = -int256(balance * fpt);
}
else {
userCorrection[farm_][msg.sender] = 0;
try farm_.claimFor(msg.sender, _farmed(farm_, msg.sender, balance, fpt)) returns(uint256 remaining) {
if (remaining <= 1e36) {
if (_userFarms[msg.sender].contains(address(farm_))) {
userCorrection[farm_][msg.sender] = -int256(balance * fpt + remaining * 1e18);
}
else {
userCorrection[farm_][msg.sender] = -int256(remaining * 1e18);
}
} else {
emit Error("farm.claimFor() result overflowed");
}
}
catch {
Expand Down
2 changes: 1 addition & 1 deletion contracts/interfaces/IERC20Farm.sol
Expand Up @@ -5,5 +5,5 @@ pragma solidity ^0.8.0;
import "./IFarmAccounting.sol";

interface IERC20Farm is IFarmAccounting {
function claimFor(address account, uint256 amount) external;
function claimFor(address account, uint256 amount) external returns(uint256 remaining);
}

0 comments on commit 84fd5ed

Please sign in to comment.