Skip to content

Commit

Permalink
check for valid vault id and valid token address returned from api
Browse files Browse the repository at this point in the history
  • Loading branch information
ewansheldon committed Jan 3, 2025
1 parent 88b2d9f commit 8ee0921
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
46 changes: 30 additions & 16 deletions contracts/AutoRedemption.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {AutomationCompatibleInterface} from
import {ConfirmedOwner} from "@chainlink/contracts/src/v0.8/shared/access/ConfirmedOwner.sol";
import {IRedeemable} from "contracts/interfaces/IRedeemable.sol";
import {ISmartVaultManager} from "contracts/interfaces/ISmartVaultManager.sol";
import {ISmartVault} from "contracts/interfaces/ISmartVault.sol";
import {IUniswapV3Pool} from "contracts/interfaces/IUniswapV3Pool.sol";
import {IQuoter} from "contracts/interfaces/IQuoter.sol";
import {LiquidityMath} from "src/uniswap/LiquidityMath.sol";
Expand Down Expand Up @@ -145,6 +146,17 @@ contract AutoRedemption is AutomationCompatibleInterface, FunctionsClient, Confi
} catch {}
}

function validData(ISmartVaultManager.SmartVaultData memory _vaultData, address _token) private returns (bool) {
if (_vaultData.status.vaultAddress == address(0)) return false;
for (uint256 i = 0; i < _vaultData.status.collateral.length; i++) {
if (_vaultData.status.collateral[i].token.addr == _token) return true;
}
ISmartVault.YieldPair[] memory _yieldPairs = ISmartVault(_vaultData.status.vaultAddress).yieldAssets();
for (uint256 i = 0; i < _yieldPairs.length; i++) {
if (_yieldPairs[i].hypervisor == _token) return true;
}
}

function runAutoRedemption(bytes memory response)
private
returns (address _smartVault, address _collateralToken, uint256 _usdsRedeemed)
Expand All @@ -155,24 +167,26 @@ contract AutoRedemption is AutomationCompatibleInterface, FunctionsClient, Confi
try ISmartVaultManager(smartVaultManager).vaultData(_tokenID) returns (
ISmartVaultManager.SmartVaultData memory _vaultData
) {
if (_USDsTargetAmount > _vaultData.status.minted) _USDsTargetAmount = _vaultData.status.minted;
_smartVault = _vaultData.status.vaultAddress;
if (_tokenID <= lastLegacyVaultID) {
_usdsRedeemed = legacyAutoRedemption(_smartVault, _token, _USDsTargetAmount);
} else {
address _hypervisor;
if (hypervisorCollaterals[_token] != address(0)) {
_hypervisor = _token;
_token = hypervisorCollaterals[_hypervisor];
if (validData(_vaultData, _token)) {
if (_USDsTargetAmount > _vaultData.status.minted) _USDsTargetAmount = _vaultData.status.minted;
_smartVault = _vaultData.status.vaultAddress;
if (_tokenID <= lastLegacyVaultID) {
_usdsRedeemed = legacyAutoRedemption(_smartVault, _token, _USDsTargetAmount);
} else {
address _hypervisor;
if (hypervisorCollaterals[_token] != address(0)) {
_hypervisor = _token;
_token = hypervisorCollaterals[_hypervisor];
}
bytes memory _collateralToUSDsPath = swapPaths[_token].input;
try IRedeemable(_smartVault).autoRedemption(
swapRouter, quoter, _token, _collateralToUSDsPath, _USDsTargetAmount, _hypervisor
) returns (uint256 _redeemed) {
_usdsRedeemed = _redeemed;
} catch {}
}
bytes memory _collateralToUSDsPath = swapPaths[_token].input;
try IRedeemable(_smartVault).autoRedemption(
swapRouter, quoter, _token, _collateralToUSDsPath, _USDsTargetAmount, _hypervisor
) returns (uint256 _redeemed) {
_usdsRedeemed = _redeemed;
} catch {}
_collateralToken = _token;
}
_collateralToken = _token;
} catch {}
}
}
Expand Down
8 changes: 0 additions & 8 deletions contracts/SmartVaultV4.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,6 @@ contract SmartVaultV4 is ISmartVault, IRedeemable {
uint256 private minted;
bool private liquidated;

struct YieldPair {
address hypervisor;
address token0;
uint256 amount0;
address token1;
uint256 amount1;
}

event CollateralRemoved(bytes32 symbol, uint256 amount, address to);
event AssetRemoved(address token, uint256 amount, address to);
event USDsMinted(address to, uint256 amount, uint256 fee);
Expand Down
9 changes: 9 additions & 0 deletions contracts/interfaces/ISmartVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,17 @@ interface ISmartVault {
bytes32 vaultType;
}

struct YieldPair {
address hypervisor;
address token0;
uint256 amount0;
address token1;
uint256 amount1;
}

function status() external view returns (Status memory);
function undercollateralised() external view returns (bool);
function setOwner(address _newOwner) external;
function liquidate(address _liquidator) external;
function yieldAssets() external view returns (YieldPair[] memory _yieldPairs);
}
8 changes: 0 additions & 8 deletions contracts/test_utils/SmartVaultV4Legacy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ contract SmartVaultV4Legacy is ISmartVault, IRedeemableLegacy {
uint256 private minted;
bool private liquidated;

struct YieldPair {
address hypervisor;
address token0;
uint256 amount0;
address token1;
uint256 amount1;
}

event CollateralRemoved(bytes32 symbol, uint256 amount, address to);
event AssetRemoved(address token, uint256 amount, address to);
event USDsMinted(address to, uint256 amount, uint256 fee);
Expand Down

0 comments on commit 8ee0921

Please sign in to comment.