Skip to content

Commit

Permalink
Merge pull request #50 from tetu-io/dev-dvpublic
Browse files Browse the repository at this point in the history
try/catch in TetuVoterSimplified
  • Loading branch information
belbix authored Dec 19, 2023
2 parents 0ef7227 + d6447f0 commit 512323c
Show file tree
Hide file tree
Showing 4 changed files with 18,186 additions and 4,770 deletions.
11 changes: 9 additions & 2 deletions contracts/test/MockLiquidator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ contract MockLiquidator is ITetuLiquidator {
uint internal price = 100_000 * 1e18;
string internal error = "";
uint internal routeLength = 1;
bool internal useTokensToCalculatePrice;

function setPrice(uint value) external {
price = value;
}

function setUseTokensToCalculatePrice(bool value) external {
useTokensToCalculatePrice = value;
}

function setError(string memory value) external {
error = value;
}
Expand All @@ -23,8 +28,10 @@ contract MockLiquidator is ITetuLiquidator {
routeLength = value;
}

function getPrice(address, address, uint) external view override returns (uint) {
return price;
function getPrice(address, address, uint tokens) external view override returns (uint) {
return useTokensToCalculatePrice
? price * tokens
: price;
}

function getPriceForRoute(PoolData[] memory, uint) external view override returns (uint) {
Expand Down
16 changes: 13 additions & 3 deletions contracts/ve/TetuVoterSimplified.sol
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,25 @@ contract TetuVoterSimplified is ReentrancyGuard, ControllableV3, IVoter {
require(amount != 0, "zero amount");

IController c = IController(controller());
ITetuLiquidator liquidator = ITetuLiquidator(c.liquidator());
address _token = token;
address _gauge = gauge;

IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
emit NotifyReward(msg.sender, amount);

// gauge is able to revert if reward amount is too small
// in this case let's rollback transferring rewards to all vaults,
// and keep rewards on balance up to the next attempt
try TetuVoterSimplified(address(this))._notifyRewardAmount(c, _token, _gauge, amount) {} catch {}
}

/// @notice Try to send all available rewards to vaults
/// @dev We need this external function to be able to call it inside try/catch
/// and revert transferring of rewards to all vaults simultaneously if transferring to any vault reverts
function _notifyRewardAmount(IController c, address _token, address _gauge, uint amount) external {
// any sender is allowed, no limitations
ITetuLiquidator liquidator = ITetuLiquidator(c.liquidator());

amount = IERC20(_token).balanceOf(address(this));

uint length = c.vaultsListLength();
Expand All @@ -168,8 +180,6 @@ contract TetuVoterSimplified is ReentrancyGuard, ControllableV3, IVoter {
tvlSum += tvlInTokenValue;
}



for (uint i; i < length; ++i) {
uint ratio = tvlInTokenValues[i] * 1e18 / tvlSum;
uint toDistro = amount * ratio / 1e18;
Expand Down
Loading

0 comments on commit 512323c

Please sign in to comment.