zellic-audit
Initial commit
f998fcd
raw
history blame
26.4 kB
// This contract is part of Zellic’s smart contract dataset, which is a collection of publicly available contract code gathered as of March 2023.
/**
* SourceUnit: /Users/patrickmckelvy/code/defi/os/alchemix/alops/submodules/v2-foundry/src/adapters/yearn/YearnTokenAdapter.sol
*/
////// SPDX-License-Identifier-FLATTEN-SUPPRESS-WARNING: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* ////IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool);
}
/**
* SourceUnit: /Users/patrickmckelvy/code/defi/os/alchemix/alops/submodules/v2-foundry/src/adapters/yearn/YearnTokenAdapter.sol
*/
pragma solidity >=0.5.0;
////import "../../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
/// @title IERC20Mintable
/// @author Alchemix Finance
interface IERC20Mintable is IERC20 {
/// @notice Mints `amount` tokens to `recipient`.
///
/// @param recipient The address which will receive the minted tokens.
/// @param amount The amount of tokens to mint.
function mint(address recipient, uint256 amount) external;
}
/**
* SourceUnit: /Users/patrickmckelvy/code/defi/os/alchemix/alops/submodules/v2-foundry/src/adapters/yearn/YearnTokenAdapter.sol
*/
pragma solidity >=0.5.0;
////import "../../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
/// @title IERC20Burnable
/// @author Alchemix Finance
interface IERC20Burnable is IERC20 {
/// @notice Burns `amount` tokens from the balance of `msg.sender`.
///
/// @param amount The amount of tokens to burn.
///
/// @return If burning the tokens was successful.
function burn(uint256 amount) external returns (bool);
/// @notice Burns `amount` tokens from `owner`'s balance.
///
/// @param owner The address to burn tokens from.
/// @param amount The amount of tokens to burn.
///
/// @return If burning the tokens was successful.
function burnFrom(address owner, uint256 amount) external returns (bool);
}
/**
* SourceUnit: /Users/patrickmckelvy/code/defi/os/alchemix/alops/submodules/v2-foundry/src/adapters/yearn/YearnTokenAdapter.sol
*/
////// SPDX-License-Identifier-FLATTEN-SUPPRESS-WARNING: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.0;
////import "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*
* _Available since v4.1._
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
function name() external view returns (string memory);
/**
* @dev Returns the symbol of the token.
*/
function symbol() external view returns (string memory);
/**
* @dev Returns the decimals places of the token.
*/
function decimals() external view returns (uint8);
}
/**
* SourceUnit: /Users/patrickmckelvy/code/defi/os/alchemix/alops/submodules/v2-foundry/src/adapters/yearn/YearnTokenAdapter.sol
*/
pragma solidity >=0.5.0;
/// @title IERC20Minimal
/// @author Alchemix Finance
interface IERC20Minimal {
/// @notice An event which is emitted when tokens are transferred between two parties.
///
/// @param owner The owner of the tokens from which the tokens were transferred.
/// @param recipient The recipient of the tokens to which the tokens were transferred.
/// @param amount The amount of tokens which were transferred.
event Transfer(address indexed owner, address indexed recipient, uint256 amount);
/// @notice An event which is emitted when an approval is made.
///
/// @param owner The address which made the approval.
/// @param spender The address which is allowed to transfer tokens on behalf of `owner`.
/// @param amount The amount of tokens that `spender` is allowed to transfer.
event Approval(address indexed owner, address indexed spender, uint256 amount);
/// @notice Gets the current total supply of tokens.
///
/// @return The total supply.
function totalSupply() external view returns (uint256);
/// @notice Gets the balance of tokens that an account holds.
///
/// @param account The account address.
///
/// @return The balance of the account.
function balanceOf(address account) external view returns (uint256);
/// @notice Gets the allowance that an owner has allotted for a spender.
///
/// @param owner The owner address.
/// @param spender The spender address.
///
/// @return The number of tokens that `spender` is allowed to transfer on behalf of `owner`.
function allowance(address owner, address spender) external view returns (uint256);
/// @notice Transfers `amount` tokens from `msg.sender` to `recipient`.
///
/// @notice Emits a {Transfer} event.
///
/// @param recipient The address which will receive the tokens.
/// @param amount The amount of tokens to transfer.
///
/// @return If the transfer was successful.
function transfer(address recipient, uint256 amount) external returns (bool);
/// @notice Approves `spender` to transfer `amount` tokens on behalf of `msg.sender`.
///
/// @notice Emits a {Approval} event.
///
/// @param spender The address which is allowed to transfer tokens on behalf of `msg.sender`.
/// @param amount The amount of tokens that `spender` is allowed to transfer.
///
/// @return If the approval was successful.
function approve(address spender, uint256 amount) external returns (bool);
/// @notice Transfers `amount` tokens from `owner` to `recipient` using an approval that `owner` gave to `msg.sender`.
///
/// @notice Emits a {Approval} event.
/// @notice Emits a {Transfer} event.
///
/// @param owner The address to transfer tokens from.
/// @param recipient The address that will receive the tokens.
/// @param amount The amount of tokens to transfer.
///
/// @return If the transfer was successful.
function transferFrom(address owner, address recipient, uint256 amount) external returns (bool);
}
/**
* SourceUnit: /Users/patrickmckelvy/code/defi/os/alchemix/alops/submodules/v2-foundry/src/adapters/yearn/YearnTokenAdapter.sol
*/
pragma solidity ^0.8.13;
////import "../../lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
////import "../../lib/openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";
////import "../interfaces/IERC20Burnable.sol";
////import "../interfaces/IERC20Mintable.sol";
/// @title TokenUtils
/// @author Alchemix Finance
library TokenUtils {
/// @notice An error used to indicate that a call to an ERC20 contract failed.
///
/// @param target The target address.
/// @param success If the call to the token was a success.
/// @param data The resulting data from the call. This is error data when the call was not a success. Otherwise,
/// this is malformed data when the call was a success.
error ERC20CallFailed(address target, bool success, bytes data);
/// @dev A safe function to get the decimals of an ERC20 token.
///
/// @dev Reverts with a {CallFailed} error if execution of the query fails or returns an unexpected value.
///
/// @param token The target token.
///
/// @return The amount of decimals of the token.
function expectDecimals(address token) internal view returns (uint8) {
(bool success, bytes memory data) = token.staticcall(
abi.encodeWithSelector(IERC20Metadata.decimals.selector)
);
if (token.code.length == 0 || !success || data.length < 32) {
revert ERC20CallFailed(token, success, data);
}
return abi.decode(data, (uint8));
}
/// @dev Gets the balance of tokens held by an account.
///
/// @dev Reverts with a {CallFailed} error if execution of the query fails or returns an unexpected value.
///
/// @param token The token to check the balance of.
/// @param account The address of the token holder.
///
/// @return The balance of the tokens held by an account.
function safeBalanceOf(address token, address account) internal view returns (uint256) {
(bool success, bytes memory data) = token.staticcall(
abi.encodeWithSelector(IERC20.balanceOf.selector, account)
);
if (token.code.length == 0 || !success || data.length < 32) {
revert ERC20CallFailed(token, success, data);
}
return abi.decode(data, (uint256));
}
/// @dev Transfers tokens to another address.
///
/// @dev Reverts with a {CallFailed} error if execution of the transfer failed or returns an unexpected value.
///
/// @param token The token to transfer.
/// @param recipient The address of the recipient.
/// @param amount The amount of tokens to transfer.
function safeTransfer(address token, address recipient, uint256 amount) internal {
(bool success, bytes memory data) = token.call(
abi.encodeWithSelector(IERC20.transfer.selector, recipient, amount)
);
if (token.code.length == 0 || !success || (data.length != 0 && !abi.decode(data, (bool)))) {
revert ERC20CallFailed(token, success, data);
}
}
/// @dev Approves tokens for the smart contract.
///
/// @dev Reverts with a {CallFailed} error if execution of the approval fails or returns an unexpected value.
///
/// @param token The token to approve.
/// @param spender The contract to spend the tokens.
/// @param value The amount of tokens to approve.
function safeApprove(address token, address spender, uint256 value) internal {
(bool success, bytes memory data) = token.call(
abi.encodeWithSelector(IERC20.approve.selector, spender, value)
);
if (token.code.length == 0 || !success || (data.length != 0 && !abi.decode(data, (bool)))) {
revert ERC20CallFailed(token, success, data);
}
}
/// @dev Transfer tokens from one address to another address.
///
/// @dev Reverts with a {CallFailed} error if execution of the transfer fails or returns an unexpected value.
///
/// @param token The token to transfer.
/// @param owner The address of the owner.
/// @param recipient The address of the recipient.
/// @param amount The amount of tokens to transfer.
function safeTransferFrom(address token, address owner, address recipient, uint256 amount) internal {
(bool success, bytes memory data) = token.call(
abi.encodeWithSelector(IERC20.transferFrom.selector, owner, recipient, amount)
);
if (token.code.length == 0 || !success || (data.length != 0 && !abi.decode(data, (bool)))) {
revert ERC20CallFailed(token, success, data);
}
}
/// @dev Mints tokens to an address.
///
/// @dev Reverts with a {CallFailed} error if execution of the mint fails or returns an unexpected value.
///
/// @param token The token to mint.
/// @param recipient The address of the recipient.
/// @param amount The amount of tokens to mint.
function safeMint(address token, address recipient, uint256 amount) internal {
(bool success, bytes memory data) = token.call(
abi.encodeWithSelector(IERC20Mintable.mint.selector, recipient, amount)
);
if (token.code.length == 0 || !success || (data.length != 0 && !abi.decode(data, (bool)))) {
revert ERC20CallFailed(token, success, data);
}
}
/// @dev Burns tokens.
///
/// Reverts with a `CallFailed` error if execution of the burn fails or returns an unexpected value.
///
/// @param token The token to burn.
/// @param amount The amount of tokens to burn.
function safeBurn(address token, uint256 amount) internal {
(bool success, bytes memory data) = token.call(
abi.encodeWithSelector(IERC20Burnable.burn.selector, amount)
);
if (token.code.length == 0 || !success || (data.length != 0 && !abi.decode(data, (bool)))) {
revert ERC20CallFailed(token, success, data);
}
}
/// @dev Burns tokens from its total supply.
///
/// @dev Reverts with a {CallFailed} error if execution of the burn fails or returns an unexpected value.
///
/// @param token The token to burn.
/// @param owner The owner of the tokens.
/// @param amount The amount of tokens to burn.
function safeBurnFrom(address token, address owner, uint256 amount) internal {
(bool success, bytes memory data) = token.call(
abi.encodeWithSelector(IERC20Burnable.burnFrom.selector, owner, amount)
);
if (token.code.length == 0 || !success || (data.length != 0 && !abi.decode(data, (bool)))) {
revert ERC20CallFailed(token, success, data);
}
}
}
/**
* SourceUnit: /Users/patrickmckelvy/code/defi/os/alchemix/alops/submodules/v2-foundry/src/adapters/yearn/YearnTokenAdapter.sol
*/
////// SPDX-License-Identifier-FLATTEN-SUPPRESS-WARNING: MIT
pragma solidity >=0.5.0;
////import "../../IERC20Minimal.sol";
////import "../../../../lib/openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol";
/// @title IYearnVaultV2
/// @author Yearn Finance
interface IYearnVaultV2 is IERC20Metadata {
struct StrategyParams {
uint256 performanceFee;
uint256 activation;
uint256 debtRatio;
uint256 minDebtPerHarvest;
uint256 maxDebtPerHarvest;
uint256 lastReport;
uint256 totalDebt;
uint256 totalGain;
uint256 totalLoss;
bool enforceChangeLimit;
uint256 profitLimitRatio;
uint256 lossLimitRatio;
address customCheck;
}
function apiVersion() external pure returns (string memory);
function permit(
address owner,
address spender,
uint256 amount,
uint256 expiry,
bytes calldata signature
) external returns (bool);
// NOTE: Vyper produces multiple signatures for a given function with "default" args
function deposit() external returns (uint256);
function deposit(uint256 amount) external returns (uint256);
function deposit(uint256 amount, address recipient) external returns (uint256);
// NOTE: Vyper produces multiple signatures for a given function with "default" args
function withdraw() external returns (uint256);
function withdraw(uint256 maxShares) external returns (uint256);
function withdraw(uint256 maxShares, address recipient) external returns (uint256);
function withdraw(
uint256 maxShares,
address recipient,
uint256 maxLoss
) external returns (uint256);
function token() external view returns (address);
function strategies(address _strategy) external view returns (StrategyParams memory);
function pricePerShare() external view returns (uint256);
function totalAssets() external view returns (uint256);
function depositLimit() external view returns (uint256);
function maxAvailableShares() external view returns (uint256);
/// @notice View how much the Vault would increase this Strategy's borrow limit, based on its present performance
/// (since its last report). Can be used to determine expectedReturn in your Strategy.
function creditAvailable() external view returns (uint256);
/// @notice View how much the Vault would like to pull back from the Strategy, based on its present performance
/// (since its last report). Can be used to determine expectedReturn in your Strategy.
function debtOutstanding() external view returns (uint256);
/// @notice View how much the Vault expect this Strategy to return at the current block, based on its present
/// performance (since its last report). Can be used to determine expectedReturn in your Strategy.
function expectedReturn() external view returns (uint256);
/// @notice This is the main contact point where the Strategy interacts with the Vault. It is critical that this call
/// is handled as intended by the Strategy. Therefore, this function will be called by BaseStrategy to make
/// sure the integration is correct.
function report(
uint256 _gain,
uint256 _loss,
uint256 _debtPayment
) external returns (uint256);
/// @notice This function should only be used in the scenario where the Strategy is being retired but no migration of
/// the positions are possible, or in the extreme scenario that the Strategy needs to be put into
/// "Emergency Exit" mode in order for it to exit as quickly as possible. The latter scenario could be for any
/// reason that is considered "critical" that the Strategy exits its position as fast as possible, such as a
/// sudden change in market conditions leading to losses, or an imminent failure in an external dependency.
function revokeStrategy() external;
/// @notice View the governance address of the Vault to assert privileged functions can only be called by governance.
/// The Strategy serves the Vault, so it is subject to governance defined by the Vault.
function governance() external view returns (address);
/// @notice View the management address of the Vault to assert privileged functions can only be called by management.
/// The Strategy serves the Vault, so it is subject to management defined by the Vault.
function management() external view returns (address);
/// @notice View the guardian address of the Vault to assert privileged functions can only be called by guardian. The
/// Strategy serves the Vault, so it is subject to guardian defined by the Vault.
function guardian() external view returns (address);
}
/**
* SourceUnit: /Users/patrickmckelvy/code/defi/os/alchemix/alops/submodules/v2-foundry/src/adapters/yearn/YearnTokenAdapter.sol
*/
pragma solidity >=0.5.0;
/// @title ITokenAdapter
/// @author Alchemix Finance
interface ITokenAdapter {
/// @notice Gets the current version.
///
/// @return The version.
function version() external view returns (string memory);
/// @notice Gets the address of the yield token that this adapter supports.
///
/// @return The address of the yield token.
function token() external view returns (address);
/// @notice Gets the address of the underlying token that the yield token wraps.
///
/// @return The address of the underlying token.
function underlyingToken() external view returns (address);
/// @notice Gets the number of underlying tokens that a single whole yield token is redeemable
/// for.
///
/// @return The price.
function price() external view returns (uint256);
/// @notice Wraps `amount` underlying tokens into the yield token.
///
/// @param amount The amount of the underlying token to wrap.
/// @param recipient The address which will receive the yield tokens.
///
/// @return amountYieldTokens The amount of yield tokens minted to `recipient`.
function wrap(uint256 amount, address recipient)
external
returns (uint256 amountYieldTokens);
/// @notice Unwraps `amount` yield tokens into the underlying token.
///
/// @param amount The amount of yield-tokens to redeem.
/// @param recipient The recipient of the resulting underlying-tokens.
///
/// @return amountUnderlyingTokens The amount of underlying tokens unwrapped to `recipient`.
function unwrap(uint256 amount, address recipient)
external
returns (uint256 amountUnderlyingTokens);
}
/**
* SourceUnit: /Users/patrickmckelvy/code/defi/os/alchemix/alops/submodules/v2-foundry/src/adapters/yearn/YearnTokenAdapter.sol
*/
pragma solidity ^0.8.13;
/// @notice An error used to indicate that an action could not be completed because either the `msg.sender` or
/// `msg.origin` is not authorized.
error Unauthorized();
/// @notice An error used to indicate that an action could not be completed because the contract either already existed
/// or entered an illegal condition which is not recoverable from.
error IllegalState();
/// @notice An error used to indicate that an action could not be completed because of an illegal argument was passed
/// to the function.
error IllegalArgument();
/**
* SourceUnit: /Users/patrickmckelvy/code/defi/os/alchemix/alops/submodules/v2-foundry/src/adapters/yearn/YearnTokenAdapter.sol
*/
pragma solidity ^0.8.11;
////import {IllegalState} from "../../base/Errors.sol";
////import "../../interfaces/ITokenAdapter.sol";
////import "../../interfaces/external/yearn/IYearnVaultV2.sol";
////import "../../libraries/TokenUtils.sol";
/// @title YearnTokenAdapter
/// @author Alchemix Finance
contract YearnTokenAdapter is ITokenAdapter {
uint256 private constant MAXIMUM_SLIPPAGE = 10000;
string public constant override version = "2.1.0";
address public immutable override token;
address public immutable override underlyingToken;
constructor(address _token, address _underlyingToken) {
token = _token;
underlyingToken = _underlyingToken;
}
/// @inheritdoc ITokenAdapter
function price() external view override returns (uint256) {
return IYearnVaultV2(token).pricePerShare();
}
/// @inheritdoc ITokenAdapter
function wrap(uint256 amount, address recipient) external override returns (uint256) {
TokenUtils.safeTransferFrom(underlyingToken, msg.sender, address(this), amount);
TokenUtils.safeApprove(underlyingToken, token, 0);
TokenUtils.safeApprove(underlyingToken, token, amount);
return IYearnVaultV2(token).deposit(amount, recipient);
}
/// @inheritdoc ITokenAdapter
function unwrap(uint256 amount, address recipient) external override returns (uint256) {
TokenUtils.safeTransferFrom(token, msg.sender, address(this), amount);
uint256 balanceBefore = TokenUtils.safeBalanceOf(token, address(this));
uint256 amountWithdrawn = IYearnVaultV2(token).withdraw(amount, recipient, MAXIMUM_SLIPPAGE);
uint256 balanceAfter = TokenUtils.safeBalanceOf(token, address(this));
// If the Yearn vault did not burn all of the shares then revert. This is critical in mathematical operations
// performed by the system because the system always expects that all of the tokens were unwrapped. In Yearn,
// this sometimes does not happen in cases where strategies cannot withdraw all of the requested tokens (an
// example strategy where this can occur is with Compound and AAVE where funds may not be accessible because
// they were lent out).
if (balanceBefore - balanceAfter != amount) {
revert IllegalState();
}
return amountWithdrawn;
}
}