Skip to content

Commit

Permalink
Merge pull request #3 from tetu-io/wombex_strategy
Browse files Browse the repository at this point in the history
Wombex strategy
  • Loading branch information
belbix authored Sep 28, 2023
2 parents 70683bc + 8e60f37 commit a64050f
Show file tree
Hide file tree
Showing 36 changed files with 3,833 additions and 105 deletions.
35 changes: 35 additions & 0 deletions contracts/impl/radiant/Radiant2SupplyStrategy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: ISC
/**
* By using this software, you understand, acknowledge and accept that Tetu
* and/or the underlying software are provided “as is” and “as available”
* basis and without warranties or representations of any kind either expressed
* or implied. Any use of this open source software released under the ISC
* Internet Systems Consortium license is done at your own risk to the fullest
* extent permissible pursuant to applicable law any and all liability as well
* as all warranties, including any fitness for a particular purpose with respect
* to Tetu and/or the underlying software and the use thereof are disclaimed.
*/
pragma solidity 0.8.4;

import "../../strategies/radiant/Radiant2StrategyBase.sol";

contract Radiant2SupplyStrategy is Radiant2StrategyBase {

function initialize(
address controller_,
address underlying_,
address vault_,
uint buybackRatio_
) external initializer {
require(ISmartVault(vault_).underlying() == underlying_, "!underlying");

Radiant2StrategyBase.initializeStrategy(
controller_,
underlying_,
vault_,
buybackRatio_
);
}


}
37 changes: 37 additions & 0 deletions contracts/impl/wombex/WombexSupplyStrategy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: ISC
/**
* By using this software, you understand, acknowledge and accept that Tetu
* and/or the underlying software are provided “as is” and “as available”
* basis and without warranties or representations of any kind either expressed
* or implied. Any use of this open source software released under the ISC
* Internet Systems Consortium license is done at your own risk to the fullest
* extent permissible pursuant to applicable law any and all liability as well
* as all warranties, including any fitness for a particular purpose with respect
* to Tetu and/or the underlying software and the use thereof are disclaimed.
*/
pragma solidity 0.8.4;

import "../../strategies/wombex/WombexStrategyBase.sol";

contract WombexSupplyStrategy is WombexStrategyBase {

function initialize(
address controller_,
address underlying_,
address lpToken_,
address wmxLP_,
address vault_,
uint buybackRatio_
) external initializer {
require(ISmartVault(vault_).underlying() == underlying_, "!underlying");

WombexStrategyBase.initializeStrategy(
controller_,
underlying_,
lpToken_,
wmxLP_,
vault_,
buybackRatio_
);
}
}
20 changes: 11 additions & 9 deletions contracts/strategies/UniversalLendStrategy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ abstract contract UniversalLendStrategy is ProxyStrategyBase {
function _getActualPoolBalance() internal virtual returns (uint);

/// @dev Perform only withdraw action, without changing local balance
function _withdrawFromPoolWithoutChangeLocalBalance(uint amount, uint poolBalance) internal virtual returns (bool withdrewAll);
function _withdrawFromPoolWithoutChangeLocalBalance(uint amount, uint poolBalance) internal virtual returns (bool withdrewAll, uint withdrawnAmount);

/// @dev Withdraw all and set localBalance to zero
function _withdrawAllFromPool() internal virtual;
Expand Down Expand Up @@ -118,11 +118,11 @@ abstract contract UniversalLendStrategy is ProxyStrategyBase {
/// @dev Withdraw underlying from the pool
function withdrawAndClaimFromPool(uint256 amount_) internal override {
uint poolBalance = _doHardWork(true, false);
bool withdrewAll = _withdrawFromPoolWithoutChangeLocalBalance(amount_, poolBalance);
(bool withdrewAll, uint withdrawn) = _withdrawFromPoolWithoutChangeLocalBalance(amount_, poolBalance);
if (withdrewAll) {
localBalance = 0;
} else {
localBalance > amount_ ? localBalance -= amount_ : localBalance = 0;
localBalance > amount_ ? localBalance -= withdrawn : localBalance = 0;
}
}

Expand Down Expand Up @@ -198,24 +198,26 @@ abstract contract UniversalLendStrategy is ProxyStrategyBase {
require(_localBalance < _DUST || profit < poolBalance / 20, 'Too huge profit');

uint toBuybacks = _calcToBuyback(profit, _localBalance);
uint remaining = profit - toBuybacks;
if (remaining != 0) {
localBalance += remaining;
}

if (toBuybacks > _DUST) {
// if no users, withdraw all and send to controller for remove dust from this contract
if (toBuybacks == poolBalance) {
_withdrawAllFromPool();
localBalance = 0;
IERC20(u).safeTransfer(address(c), IERC20(u).balanceOf(address(this)));
} else {
bool withdrewAll = _withdrawFromPoolWithoutChangeLocalBalance(toBuybacks, poolBalance);
(bool withdrewAll, uint withdrawnAmount) = _withdrawFromPoolWithoutChangeLocalBalance(toBuybacks, poolBalance);
if (withdrewAll) {
localBalance = 0;
}
IERC20(u).safeTransfer(_PERF_FEE_TREASURY, toBuybacks);
IERC20(u).safeTransfer(_PERF_FEE_TREASURY, withdrawnAmount);
uint remaining = profit - withdrawnAmount; // need to use real withdrawn amount instead of toBuybacks
if (remaining != 0) {
localBalance += remaining;
}
}
}

}
IBookkeeper(c.bookkeeper()).registerStrategyEarned(0);
return _getActualPoolBalance();
Expand Down
132 changes: 132 additions & 0 deletions contracts/strategies/radiant/Radiant2StrategyBase.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// SPDX-License-Identifier: ISC
/**
* By using this software, you understand, acknowledge and accept that Tetu
* and/or the underlying software are provided “as is” and “as available”
* basis and without warranties or representations of any kind either expressed
* or implied. Any use of this open source software released under the ISC
* Internet Systems Consortium license is done at your own risk to the fullest
* extent permissible pursuant to applicable law any and all liability as well
* as all warranties, including any fitness for a particular purpose with respect
* to Tetu and/or the underlying software and the use thereof are disclaimed.
*/
pragma solidity 0.8.4;

import "../UniversalLendStrategy.sol";
import "../../third_party/aave/IAToken.sol";
import "../../third_party/aave/ILendingPool.sol";
import "../../third_party/aave/IAaveIncentivesController.sol";
import "../../third_party/aave/IProtocolDataProvider.sol";

/// @title Contract for RadiantV2 simple supply strategy simplified
/// @author belbix, Aleh
abstract contract Radiant2StrategyBase is UniversalLendStrategy {
using SafeERC20 for IERC20;

/// ******************************************************
/// Constants and variables
/// ******************************************************

/// @notice Version of the contract
/// @dev Should be incremented when contract changed
string public constant VERSION = "1.0.1";

IStrategy.Platform public constant override platform = IStrategy.Platform.SLOT_48; //todo change
/// @notice Strategy type for statistical purposes
string public constant override STRATEGY_NAME = "Radiant2StrategyBase";

ILendingPool public constant AAVE_LENDING_POOL = ILendingPool(0xd50Cf00b6e600Dd036Ba8eF475677d816d6c4281);
IProtocolDataProvider public constant AAVE_DATA_PROVIDER = IProtocolDataProvider(0x2f9D57E97C3DFED8676e605BC504a48E0c5917E9);


/// ******************************************************
/// Initialization
/// ******************************************************

/// @notice Initialize contract after setup it as proxy implementation
function initializeStrategy(
address controller_,
address underlying_,
address vault_,
uint buybackRatio_
) public initializer {
UniversalLendStrategy.initializeLendStrategy(
controller_,
underlying_,
vault_,
buybackRatio_,
new address[](0)
);

address aToken;
(aToken,,) = AAVE_DATA_PROVIDER.getReserveTokensAddresses(underlying_);
require(IAToken(aToken).UNDERLYING_ASSET_ADDRESS() == _underlying(), "Wrong underlying");
}

/// ******************************************************
/// Views
/// ******************************************************

/// @notice Invested assets in the pool
function _rewardPoolBalance() internal override view returns (uint) {
return localBalance;
}

/// @notice Return approximately amount of reward tokens ready to claim
function readyToClaim() external pure override returns (uint[] memory) {
uint[] memory rewards = new uint256[](1);
return rewards;
}

/// @notice TVL of the underlying in the pool
function poolTotalAmount() external view override returns (uint256) {
address aToken;
(aToken,,) = AAVE_DATA_PROVIDER.getReserveTokensAddresses(_underlying());
return IERC20(_underlying()).balanceOf(aToken);
}

/// ******************************************************
/// Internal logic implementation
/// ******************************************************


/// @dev Refresh rates and return actual deposited balance in underlying tokens
function _getActualPoolBalance() internal view override returns (uint) {
(uint suppliedUnderlying,,,,,,,,) = AAVE_DATA_PROVIDER.getUserReserveData(_underlying(), address(this));
return suppliedUnderlying;
}

/// @dev Deposit to pool and increase local balance
function _simpleDepositToPool(uint amount) internal override {
address u = _underlying();
_approveIfNeeds(u, amount, address(AAVE_LENDING_POOL));
AAVE_LENDING_POOL.deposit(u, amount, address(this), 0);
}

/// @dev Perform only withdraw action, without changing local balance
function _withdrawFromPoolWithoutChangeLocalBalance(uint amount, uint poolBalance) internal override returns (bool withdrewAll, uint withdrawnAmount) {
address u = _underlying();
uint underlyingBalanceBefore = IERC20(u).balanceOf(address(this));
if (amount < poolBalance) {
withdrewAll = false;
AAVE_LENDING_POOL.withdraw(u, amount, address(this));
} else {
withdrewAll = true;
AAVE_LENDING_POOL.withdraw(u, type(uint).max, address(this));
}
uint underlyingBalanceAfter = IERC20(u).balanceOf(address(this));
withdrawnAmount = underlyingBalanceAfter - underlyingBalanceBefore;
}

/// @dev Withdraw all and set localBalance to zero
function _withdrawAllFromPool() internal override {
AAVE_LENDING_POOL.withdraw(_underlying(), type(uint).max, address(this));
}

/// @dev Claim distribution rewards
function _claimReward() internal override {
// no rewards for the simple supply
}

//slither-disable-next-line unused-state
uint256[49] private ______gap;
}
12 changes: 8 additions & 4 deletions contracts/strategies/venus/VenusSupplyStrategyBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract contract VenusSupplyStrategyBase is UniversalLendStrategy {

/// @notice Version of the contract
/// @dev Should be incremented when contract changed
string public constant VERSION = "1.0.1";
string public constant VERSION = "1.0.2";

IStrategy.Platform public constant override platform = IStrategy.Platform.SLOT_47; // todo change
/// @notice Strategy type for statistical purposes
Expand Down Expand Up @@ -109,14 +109,18 @@ abstract contract VenusSupplyStrategyBase is UniversalLendStrategy {
}

/// @dev Perform only withdraw action, without changing local balance
function _withdrawFromPoolWithoutChangeLocalBalance(uint amount, uint poolBalance) internal override returns (bool withdrewAll) {
function _withdrawFromPoolWithoutChangeLocalBalance(uint amount, uint poolBalance) internal override returns (bool withdrewAll, uint withdrawnAmount) {
address u = _underlying();
uint underlyingBalanceBefore = IERC20(u).balanceOf(address(this));
if (amount < poolBalance) {
vToken.redeemUnderlying(amount);
return false;
withdrewAll = false;
} else {
vToken.redeemUnderlying(amount);
return true;
withdrewAll = true;
}
uint underlyingBalanceAfter = IERC20(u).balanceOf(address(this));
withdrawnAmount = underlyingBalanceAfter - underlyingBalanceBefore;
}

/// @dev Withdraw all and set localBalance to zero
Expand Down
Loading

0 comments on commit a64050f

Please sign in to comment.