zellic-audit
Initial commit
f998fcd
raw
history blame
50.2 kB
{
"language": "Solidity",
"sources": {
"@openzeppelin/contracts/access/Ownable.sol": {
"content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)\n\npragma solidity ^0.8.0;\n\nimport \"../utils/Context.sol\";\n\n/**\n * @dev Contract module which provides a basic access control mechanism, where\n * there is an account (an owner) that can be granted exclusive access to\n * specific functions.\n *\n * By default, the owner account will be the one that deploys the contract. This\n * can later be changed with {transferOwnership}.\n *\n * This module is used through inheritance. It will make available the modifier\n * `onlyOwner`, which can be applied to your functions to restrict their use to\n * the owner.\n */\nabstract contract Ownable is Context {\n address private _owner;\n\n event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);\n\n /**\n * @dev Initializes the contract setting the deployer as the initial owner.\n */\n constructor() {\n _transferOwnership(_msgSender());\n }\n\n /**\n * @dev Throws if called by any account other than the owner.\n */\n modifier onlyOwner() {\n _checkOwner();\n _;\n }\n\n /**\n * @dev Returns the address of the current owner.\n */\n function owner() public view virtual returns (address) {\n return _owner;\n }\n\n /**\n * @dev Throws if the sender is not the owner.\n */\n function _checkOwner() internal view virtual {\n require(owner() == _msgSender(), \"Ownable: caller is not the owner\");\n }\n\n /**\n * @dev Leaves the contract without owner. It will not be possible to call\n * `onlyOwner` functions anymore. Can only be called by the current owner.\n *\n * NOTE: Renouncing ownership will leave the contract without an owner,\n * thereby removing any functionality that is only available to the owner.\n */\n function renounceOwnership() public virtual onlyOwner {\n _transferOwnership(address(0));\n }\n\n /**\n * @dev Transfers ownership of the contract to a new account (`newOwner`).\n * Can only be called by the current owner.\n */\n function transferOwnership(address newOwner) public virtual onlyOwner {\n require(newOwner != address(0), \"Ownable: new owner is the zero address\");\n _transferOwnership(newOwner);\n }\n\n /**\n * @dev Transfers ownership of the contract to a new account (`newOwner`).\n * Internal function without access restriction.\n */\n function _transferOwnership(address newOwner) internal virtual {\n address oldOwner = _owner;\n _owner = newOwner;\n emit OwnershipTransferred(oldOwner, newOwner);\n }\n}\n"
},
"@openzeppelin/contracts/utils/Context.sol": {
"content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Provides information about the current execution context, including the\n * sender of the transaction and its data. While these are generally available\n * via msg.sender and msg.data, they should not be accessed in such a direct\n * manner, since when dealing with meta-transactions the account sending and\n * paying for execution may not be the actual sender (as far as an application\n * is concerned).\n *\n * This contract is only required for intermediate, library-like contracts.\n */\nabstract contract Context {\n function _msgSender() internal view virtual returns (address) {\n return msg.sender;\n }\n\n function _msgData() internal view virtual returns (bytes calldata) {\n return msg.data;\n }\n}\n"
},
"@openzeppelin/contracts/utils/math/SafeMath.sol": {
"content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v4.6.0) (utils/math/SafeMath.sol)\n\npragma solidity ^0.8.0;\n\n// CAUTION\n// This version of SafeMath should only be used with Solidity 0.8 or later,\n// because it relies on the compiler's built in overflow checks.\n\n/**\n * @dev Wrappers over Solidity's arithmetic operations.\n *\n * NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler\n * now has built in overflow checking.\n */\nlibrary SafeMath {\n /**\n * @dev Returns the addition of two unsigned integers, with an overflow flag.\n *\n * _Available since v3.4._\n */\n function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n unchecked {\n uint256 c = a + b;\n if (c < a) return (false, 0);\n return (true, c);\n }\n }\n\n /**\n * @dev Returns the subtraction of two unsigned integers, with an overflow flag.\n *\n * _Available since v3.4._\n */\n function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n unchecked {\n if (b > a) return (false, 0);\n return (true, a - b);\n }\n }\n\n /**\n * @dev Returns the multiplication of two unsigned integers, with an overflow flag.\n *\n * _Available since v3.4._\n */\n function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n unchecked {\n // Gas optimization: this is cheaper than requiring 'a' not being zero, but the\n // benefit is lost if 'b' is also tested.\n // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522\n if (a == 0) return (true, 0);\n uint256 c = a * b;\n if (c / a != b) return (false, 0);\n return (true, c);\n }\n }\n\n /**\n * @dev Returns the division of two unsigned integers, with a division by zero flag.\n *\n * _Available since v3.4._\n */\n function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n unchecked {\n if (b == 0) return (false, 0);\n return (true, a / b);\n }\n }\n\n /**\n * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.\n *\n * _Available since v3.4._\n */\n function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {\n unchecked {\n if (b == 0) return (false, 0);\n return (true, a % b);\n }\n }\n\n /**\n * @dev Returns the addition of two unsigned integers, reverting on\n * overflow.\n *\n * Counterpart to Solidity's `+` operator.\n *\n * Requirements:\n *\n * - Addition cannot overflow.\n */\n function add(uint256 a, uint256 b) internal pure returns (uint256) {\n return a + b;\n }\n\n /**\n * @dev Returns the subtraction of two unsigned integers, reverting on\n * overflow (when the result is negative).\n *\n * Counterpart to Solidity's `-` operator.\n *\n * Requirements:\n *\n * - Subtraction cannot overflow.\n */\n function sub(uint256 a, uint256 b) internal pure returns (uint256) {\n return a - b;\n }\n\n /**\n * @dev Returns the multiplication of two unsigned integers, reverting on\n * overflow.\n *\n * Counterpart to Solidity's `*` operator.\n *\n * Requirements:\n *\n * - Multiplication cannot overflow.\n */\n function mul(uint256 a, uint256 b) internal pure returns (uint256) {\n return a * b;\n }\n\n /**\n * @dev Returns the integer division of two unsigned integers, reverting on\n * division by zero. The result is rounded towards zero.\n *\n * Counterpart to Solidity's `/` operator.\n *\n * Requirements:\n *\n * - The divisor cannot be zero.\n */\n function div(uint256 a, uint256 b) internal pure returns (uint256) {\n return a / b;\n }\n\n /**\n * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),\n * reverting when dividing by zero.\n *\n * Counterpart to Solidity's `%` operator. This function uses a `revert`\n * opcode (which leaves remaining gas untouched) while Solidity uses an\n * invalid opcode to revert (consuming all remaining gas).\n *\n * Requirements:\n *\n * - The divisor cannot be zero.\n */\n function mod(uint256 a, uint256 b) internal pure returns (uint256) {\n return a % b;\n }\n\n /**\n * @dev Returns the subtraction of two unsigned integers, reverting with custom message on\n * overflow (when the result is negative).\n *\n * CAUTION: This function is deprecated because it requires allocating memory for the error\n * message unnecessarily. For custom revert reasons use {trySub}.\n *\n * Counterpart to Solidity's `-` operator.\n *\n * Requirements:\n *\n * - Subtraction cannot overflow.\n */\n function sub(\n uint256 a,\n uint256 b,\n string memory errorMessage\n ) internal pure returns (uint256) {\n unchecked {\n require(b <= a, errorMessage);\n return a - b;\n }\n }\n\n /**\n * @dev Returns the integer division of two unsigned integers, reverting with custom message on\n * division by zero. The result is rounded towards zero.\n *\n * Counterpart to Solidity's `/` operator. Note: this function uses a\n * `revert` opcode (which leaves remaining gas untouched) while Solidity\n * uses an invalid opcode to revert (consuming all remaining gas).\n *\n * Requirements:\n *\n * - The divisor cannot be zero.\n */\n function div(\n uint256 a,\n uint256 b,\n string memory errorMessage\n ) internal pure returns (uint256) {\n unchecked {\n require(b > 0, errorMessage);\n return a / b;\n }\n }\n\n /**\n * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),\n * reverting with custom message when dividing by zero.\n *\n * CAUTION: This function is deprecated because it requires allocating memory for the error\n * message unnecessarily. For custom revert reasons use {tryMod}.\n *\n * Counterpart to Solidity's `%` operator. This function uses a `revert`\n * opcode (which leaves remaining gas untouched) while Solidity uses an\n * invalid opcode to revert (consuming all remaining gas).\n *\n * Requirements:\n *\n * - The divisor cannot be zero.\n */\n function mod(\n uint256 a,\n uint256 b,\n string memory errorMessage\n ) internal pure returns (uint256) {\n unchecked {\n require(b > 0, errorMessage);\n return a % b;\n }\n }\n}\n"
},
"contracts/Recipes/Interfaces/IERC20.sol": {
"content": "pragma solidity ^0.8.0;\r\n\r\n/**\r\n * @dev Interface of the ERC20 standard as defined in the EIP.\r\n */\r\ninterface IERC20 {\r\n /**\r\n * @dev Returns the amount of tokens in existence.\r\n */\r\n function totalSupply() external view returns (uint256);\r\n\r\n /**\r\n * @dev Returns the amount of tokens owned by `account`.\r\n */\r\n function balanceOf(address account) external view returns (uint256);\r\n\r\n /**\r\n * @dev Moves `amount` tokens from the caller's account to `recipient`.\r\n *\r\n * Returns a boolean value indicating whether the operation succeeded.\r\n *\r\n * Emits a {Transfer} event.\r\n */\r\n function transfer(address recipient, uint256 amount) external returns (bool);\r\n\r\n /**\r\n * @dev Returns the remaining number of tokens that `spender` will be\r\n * allowed to spend on behalf of `owner` through {transferFrom}. This is\r\n * zero by default.\r\n *\r\n * This value changes when {approve} or {transferFrom} are called.\r\n */\r\n function allowance(address owner, address spender) external view returns (uint256);\r\n\r\n /**\r\n * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.\r\n *\r\n * Returns a boolean value indicating whether the operation succeeded.\r\n *\r\n * IMPORTANT: Beware that changing an allowance with this method brings the risk\r\n * that someone may use both the old and the new allowance by unfortunate\r\n * transaction ordering. One possible solution to mitigate this race\r\n * condition is to first reduce the spender's allowance to 0 and set the\r\n * desired value afterwards:\r\n * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\r\n *\r\n * Emits an {Approval} event.\r\n */\r\n function approve(address spender, uint256 amount) external returns (bool);\r\n\r\n /**\r\n * @dev Moves `amount` tokens from `sender` to `recipient` using the\r\n * allowance mechanism. `amount` is then deducted from the caller's\r\n * allowance.\r\n *\r\n * Returns a boolean value indicating whether the operation succeeded.\r\n *\r\n * Emits a {Transfer} event.\r\n */\r\n function transferFrom(\r\n address sender,\r\n address recipient,\r\n uint256 amount\r\n ) external returns (bool);\r\n\r\n /**\r\n * @dev Emitted when `value` tokens are moved from one account (`from`) to\r\n * another (`to`).\r\n *\r\n * Note that `value` may be zero.\r\n */\r\n event Transfer(address indexed from, address indexed to, uint256 value);\r\n\r\n /**\r\n * @dev Emitted when the allowance of a `spender` for an `owner` is set by\r\n * a call to {approve}. `value` is the new allowance.\r\n */\r\n event Approval(address indexed owner, address indexed spender, uint256 value);\r\n}"
},
"contracts/Recipes/Interfaces/ILendingLogic.sol": {
"content": "pragma solidity ^0.8.1;\r\n\r\ninterface ILendingLogic {\r\n /**\r\n @notice Get the APR based on underlying token.\r\n @param _token Address of the underlying token\r\n @return Interest with 18 decimals\r\n */\r\n function getAPRFromUnderlying(address _token) external view returns(uint256);\r\n\r\n /**\r\n @notice Get the APR based on wrapped token.\r\n @param _token Address of the wrapped token\r\n @return Interest with 18 decimals\r\n */\r\n function getAPRFromWrapped(address _token) external view returns(uint256);\r\n\r\n /**\r\n @notice Get the calls needed to lend.\r\n @param _underlying Address of the underlying token\r\n @param _amount Amount of the underlying token\r\n @return targets Addresses of the contracts to call\r\n @return data Calldata of the calls\r\n */\r\n function lend(address _underlying, uint256 _amount, address _tokenHolder) external view returns(address[] memory targets, bytes[] memory data);\r\n\r\n /**\r\n @notice Get the calls needed to unlend\r\n @param _wrapped Address of the wrapped token\r\n @param _amount Amount of the underlying tokens\r\n @return targets Addresses of the contracts to call\r\n @return data Calldata of the calls\r\n */\r\n function unlend(address _wrapped, uint256 _amount, address _tokenHolder) external view returns(address[] memory targets, bytes[] memory data);\r\n\r\n /**\r\n @notice Get the underlying wrapped exchange rate\r\n @param _wrapped Address of the wrapped token\r\n @return The exchange rate\r\n */\r\n function exchangeRate(address _wrapped) external returns(uint256);\r\n\r\n /**\r\n @notice Get the underlying wrapped exchange rate in a view (non state changing) way\r\n @param _wrapped Address of the wrapped token\r\n @return The exchange rate\r\n */\r\n function exchangeRateView(address _wrapped) external view returns(uint256);\r\n}"
},
"contracts/Recipes/Interfaces/ILendingRegistry.sol": {
"content": "pragma solidity ^0.8.1;\r\n\r\ninterface ILendingRegistry {\r\n // Maps wrapped token to protocol\r\n function wrappedToProtocol(address _wrapped) external view returns(bytes32);\r\n // Maps wrapped token to underlying\r\n function wrappedToUnderlying(address _wrapped) external view returns(address);\r\n function underlyingToProtocolWrapped(address _underlying, bytes32 protocol) external view returns (address);\r\n function protocolToLogic(bytes32 _protocol) external view returns (address);\r\n\r\n /**\r\n @notice Set which protocl a wrapped token belongs to\r\n @param _wrapped Address of the wrapped token\r\n @param _protocol Bytes32 key of the protocol\r\n */\r\n function setWrappedToProtocol(address _wrapped, bytes32 _protocol) external;\r\n\r\n /**\r\n @notice Set what is the underlying for a wrapped token\r\n @param _wrapped Address of the wrapped token\r\n @param _underlying Address of the underlying token\r\n */\r\n function setWrappedToUnderlying(address _wrapped, address _underlying) external;\r\n\r\n /**\r\n @notice Set the logic contract for the protocol\r\n @param _protocol Bytes32 key of the procol\r\n @param _logic Address of the lending logic contract for that protocol\r\n */\r\n function setProtocolToLogic(bytes32 _protocol, address _logic) external;\r\n /**\r\n @notice Set the wrapped token for the underlying deposited in this protocol\r\n @param _underlying Address of the unerlying token\r\n @param _protocol Bytes32 key of the protocol\r\n @param _wrapped Address of the wrapped token\r\n */\r\n function setUnderlyingToProtocolWrapped(address _underlying, bytes32 _protocol, address _wrapped) external;\r\n\r\n /**\r\n @notice Get tx data to lend the underlying amount in a specific protocol\r\n @param _underlying Address of the underlying token\r\n @param _amount Amount to lend\r\n @param _protocol Bytes32 key of the protocol\r\n @return targets Addresses of the contracts to call\r\n @return data Calldata for the calls\r\n */\r\n function getLendTXData(address _underlying, uint256 _amount, bytes32 _protocol) external view returns(address[] memory targets, bytes[] memory data);\r\n\r\n /**\r\n @notice Get the tx data to unlend the wrapped amount\r\n @param _wrapped Address of the wrapped token\r\n @param _amount Amount of wrapped token to unlend\r\n @return targets Addresses of the contracts to call\r\n @return data Calldata for the calls\r\n */\r\n function getUnlendTXData(address _wrapped, uint256 _amount) external view returns(address[] memory targets, bytes[] memory data);\r\n}"
},
"contracts/Recipes/Interfaces/IPie.sol": {
"content": "pragma solidity ^0.8.1;\r\n\r\nimport \"./IERC20.sol\";\r\n\r\ninterface IPie is IERC20 {\r\n function joinPool(uint256 _amount) external;\r\n function exitPool(uint256 _amount) external;\r\n function calcTokensForAmount(uint256 _amount) external view returns(address[] memory tokens, uint256[] memory amounts);\r\n}"
},
"contracts/Recipes/Interfaces/IPieRegistry.sol": {
"content": "pragma solidity ^0.8.1;\r\ninterface IPieRegistry {\r\n function inRegistry(address _pool) external view returns(bool);\r\n function entries(uint256 _index) external view returns(address);\r\n function addSmartPool(address _smartPool) external;\r\n function removeSmartPool(uint256 _index) external;\r\n}"
},
"contracts/Recipes/Interfaces/IUniV3Router.sol": {
"content": "pragma solidity ^0.8.1;\r\n\r\ninterface uniV3Router {\r\n\r\n struct ExactInputSingleParams {\r\n address tokenIn;\r\n address tokenOut;\r\n uint24 fee;\r\n address recipient;\r\n uint256 deadline;\r\n uint256 amountIn;\r\n uint256 amountOutMinimum;\r\n uint160 sqrtPriceLimitX96;\r\n }\r\n\r\n struct ExactOutputSingleParams {\r\n address tokenIn;\r\n address tokenOut;\r\n uint24 fee;\r\n address recipient;\r\n uint256 deadline;\r\n uint256 amountOut;\r\n uint256 amountInMaximum;\r\n uint160 sqrtPriceLimitX96;\r\n }\r\n\r\n struct ExactInputParams {\r\n bytes path;\r\n address recipient;\r\n uint256 deadline;\r\n uint256 amountIn;\r\n uint256 amountOutMinimum;\r\n }\r\n\r\n struct ExactOutputParams {\r\n bytes path;\r\n address recipient;\r\n uint256 deadline;\r\n uint256 amountOut;\r\n uint256 amountInMaximum;\r\n }\r\n\r\n function exactInputSingle(ExactInputSingleParams memory params) external returns (uint256 amountOut);\r\n\r\n function exactOutputSingle(ExactOutputSingleParams calldata params) external;\r\n\r\n function exactOutput(ExactOutputParams memory params) external returns (uint256 amountIn);\r\n}\r\n\r\ninterface uniOracle {\r\n function quoteExactOutputSingle(\r\n address tokenIn,\r\n address tokenOut,\r\n uint24 fee,\r\n uint256 amountOut,\r\n uint160 sqrtPriceLimitX96\r\n ) external returns (uint256 amountIn);\r\n}\r\n"
},
"contracts/Recipes/Interfaces/IWETH.sol": {
"content": "pragma solidity ^0.8.1;\r\n\r\nimport \"./IERC20.sol\";\r\n\r\ninterface IWETH is IERC20 {\r\n function deposit() external payable;\r\n function withdraw(uint) external;\r\n function decimals() external view returns(uint8);\r\n function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);\r\n function transfer(address recipient, uint256 amount) external returns (bool);\r\n}"
},
"contracts/Recipes/OpenZeppelin/Address.sol": {
"content": "pragma solidity ^0.8.0;\r\n\r\n/**\r\n * @dev Collection of functions related to the address type\r\n */\r\nlibrary Address {\r\n /**\r\n * @dev Returns true if `account` is a contract.\r\n *\r\n * [IMPORTANT]\r\n * ====\r\n * It is unsafe to assume that an address for which this function returns\r\n * false is an externally-owned account (EOA) and not a contract.\r\n *\r\n * Among others, `isContract` will return false for the following\r\n * types of addresses:\r\n *\r\n * - an externally-owned account\r\n * - a contract in construction\r\n * - an address where a contract will be created\r\n * - an address where a contract lived, but was destroyed\r\n * ====\r\n */\r\n function isContract(address account) internal view returns (bool) {\r\n // This method relies on extcodesize, which returns 0 for contracts in\r\n // construction, since the code is only stored at the end of the\r\n // constructor execution.\r\n\r\n uint256 size;\r\n assembly {\r\n size := extcodesize(account)\r\n }\r\n return size > 0;\r\n }\r\n\r\n /**\r\n * @dev Replacement for Solidity's `transfer`: sends `amount` wei to\r\n * `recipient`, forwarding all available gas and reverting on errors.\r\n *\r\n * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost\r\n * of certain opcodes, possibly making contracts go over the 2300 gas limit\r\n * imposed by `transfer`, making them unable to receive funds via\r\n * `transfer`. {sendValue} removes this limitation.\r\n *\r\n * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].\r\n *\r\n * IMPORTANT: because control is transferred to `recipient`, care must be\r\n * taken to not create reentrancy vulnerabilities. Consider using\r\n * {ReentrancyGuard} or the\r\n * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].\r\n */\r\n function sendValue(address payable recipient, uint256 amount) internal {\r\n require(address(this).balance >= amount, \"Address: insufficient balance\");\r\n\r\n (bool success, ) = recipient.call{value: amount}(\"\");\r\n require(success, \"Address: unable to send value, recipient may have reverted\");\r\n }\r\n\r\n /**\r\n * @dev Performs a Solidity function call using a low level `call`. A\r\n * plain `call` is an unsafe replacement for a function call: use this\r\n * function instead.\r\n *\r\n * If `target` reverts with a revert reason, it is bubbled up by this\r\n * function (like regular Solidity function calls).\r\n *\r\n * Returns the raw returned data. To convert to the expected return value,\r\n * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].\r\n *\r\n * Requirements:\r\n *\r\n * - `target` must be a contract.\r\n * - calling `target` with `data` must not revert.\r\n *\r\n * _Available since v3.1._\r\n */\r\n function functionCall(address target, bytes memory data) internal returns (bytes memory) {\r\n return functionCall(target, data, \"Address: low-level call failed\");\r\n }\r\n\r\n /**\r\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with\r\n * `errorMessage` as a fallback revert reason when `target` reverts.\r\n *\r\n * _Available since v3.1._\r\n */\r\n function functionCall(\r\n address target,\r\n bytes memory data,\r\n string memory errorMessage\r\n ) internal returns (bytes memory) {\r\n return functionCallWithValue(target, data, 0, errorMessage);\r\n }\r\n\r\n /**\r\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\r\n * but also transferring `value` wei to `target`.\r\n *\r\n * Requirements:\r\n *\r\n * - the calling contract must have an ETH balance of at least `value`.\r\n * - the called Solidity function must be `payable`.\r\n *\r\n * _Available since v3.1._\r\n */\r\n function functionCallWithValue(\r\n address target,\r\n bytes memory data,\r\n uint256 value\r\n ) internal returns (bytes memory) {\r\n return functionCallWithValue(target, data, value, \"Address: low-level call with value failed\");\r\n }\r\n\r\n /**\r\n * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but\r\n * with `errorMessage` as a fallback revert reason when `target` reverts.\r\n *\r\n * _Available since v3.1._\r\n */\r\n function functionCallWithValue(\r\n address target,\r\n bytes memory data,\r\n uint256 value,\r\n string memory errorMessage\r\n ) internal returns (bytes memory) {\r\n require(address(this).balance >= value, \"Address: insufficient balance for call\");\r\n require(isContract(target), \"Address: call to non-contract\");\r\n\r\n (bool success, bytes memory returndata) = target.call{value: value}(data);\r\n return verifyCallResult(success, returndata, errorMessage);\r\n }\r\n\r\n /**\r\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\r\n * but performing a static call.\r\n *\r\n * _Available since v3.3._\r\n */\r\n function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {\r\n return functionStaticCall(target, data, \"Address: low-level static call failed\");\r\n }\r\n\r\n /**\r\n * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],\r\n * but performing a static call.\r\n *\r\n * _Available since v3.3._\r\n */\r\n function functionStaticCall(\r\n address target,\r\n bytes memory data,\r\n string memory errorMessage\r\n ) internal view returns (bytes memory) {\r\n require(isContract(target), \"Address: static call to non-contract\");\r\n\r\n (bool success, bytes memory returndata) = target.staticcall(data);\r\n return verifyCallResult(success, returndata, errorMessage);\r\n }\r\n\r\n /**\r\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\r\n * but performing a delegate call.\r\n *\r\n * _Available since v3.4._\r\n */\r\n function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {\r\n return functionDelegateCall(target, data, \"Address: low-level delegate call failed\");\r\n }\r\n\r\n /**\r\n * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],\r\n * but performing a delegate call.\r\n *\r\n * _Available since v3.4._\r\n */\r\n function functionDelegateCall(\r\n address target,\r\n bytes memory data,\r\n string memory errorMessage\r\n ) internal returns (bytes memory) {\r\n require(isContract(target), \"Address: delegate call to non-contract\");\r\n\r\n (bool success, bytes memory returndata) = target.delegatecall(data);\r\n return verifyCallResult(success, returndata, errorMessage);\r\n }\r\n\r\n /**\r\n * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the\r\n * revert reason using the provided one.\r\n *\r\n * _Available since v4.3._\r\n */\r\n function verifyCallResult(\r\n bool success,\r\n bytes memory returndata,\r\n string memory errorMessage\r\n ) internal pure returns (bytes memory) {\r\n if (success) {\r\n return returndata;\r\n } else {\r\n // Look for revert reason and bubble it up if present\r\n if (returndata.length > 0) {\r\n // The easiest way to bubble the revert reason is using memory via assembly\r\n\r\n assembly {\r\n let returndata_size := mload(returndata)\r\n revert(add(32, returndata), returndata_size)\r\n }\r\n } else {\r\n revert(errorMessage);\r\n }\r\n }\r\n }\r\n}"
},
"contracts/Recipes/OpenZeppelin/SafeERC20.sol": {
"content": "pragma solidity ^0.8.0;\r\n\r\nimport \"../Interfaces/IERC20.sol\";\r\nimport \"./Address.sol\";\r\n\r\n/**\r\n * @title SafeERC20\r\n * @dev Wrappers around ERC20 operations that throw on failure (when the token\r\n * contract returns false). Tokens that return no value (and instead revert or\r\n * throw on failure) are also supported, non-reverting calls are assumed to be\r\n * successful.\r\n * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,\r\n * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.\r\n */\r\nlibrary SafeERC20 {\r\n using Address for address;\r\n\r\n function safeTransfer(\r\n IERC20 token,\r\n address to,\r\n uint256 value\r\n ) internal {\r\n _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));\r\n }\r\n\r\n function safeTransferFrom(\r\n IERC20 token,\r\n address from,\r\n address to,\r\n uint256 value\r\n ) internal {\r\n _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));\r\n }\r\n\r\n /**\r\n * @dev Deprecated. This function has issues similar to the ones found in\r\n * {IERC20-approve}, and its usage is discouraged.\r\n *\r\n * Whenever possible, use {safeIncreaseAllowance} and\r\n * {safeDecreaseAllowance} instead.\r\n */\r\n function safeApprove(\r\n IERC20 token,\r\n address spender,\r\n uint256 value\r\n ) internal {\r\n // safeApprove should only be called when setting an initial allowance,\r\n // or when resetting it to zero. To increase and decrease it, use\r\n // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'\r\n require(\r\n (value == 0) || (token.allowance(address(this), spender) == 0),\r\n \"SafeERC20: approve from non-zero to non-zero allowance\"\r\n );\r\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));\r\n }\r\n\r\n function safeIncreaseAllowance(\r\n IERC20 token,\r\n address spender,\r\n uint256 value\r\n ) internal {\r\n uint256 newAllowance = token.allowance(address(this), spender) + value;\r\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));\r\n }\r\n\r\n function safeDecreaseAllowance(\r\n IERC20 token,\r\n address spender,\r\n uint256 value\r\n ) internal {\r\n unchecked {\r\n uint256 oldAllowance = token.allowance(address(this), spender);\r\n require(oldAllowance >= value, \"SafeERC20: decreased allowance below zero\");\r\n uint256 newAllowance = oldAllowance - value;\r\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));\r\n }\r\n }\r\n\r\n /**\r\n * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement\r\n * on the return value: the return value is optional (but if data is returned, it must not be false).\r\n * @param token The token targeted by the call.\r\n * @param data The call data (encoded using abi.encode or one of its variants).\r\n */\r\n function _callOptionalReturn(IERC20 token, bytes memory data) private {\r\n // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since\r\n // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that\r\n // the target address contains contract code and also asserts for success in the low-level call.\r\n\r\n bytes memory returndata = address(token).functionCall(data, \"SafeERC20: low-level call failed\");\r\n if (returndata.length > 0) {\r\n // Return data is optional\r\n require(abi.decode(returndata, (bool)), \"SafeERC20: ERC20 operation did not succeed\");\r\n }\r\n }\r\n}"
},
"contracts/Recipes/SimpleUniRecipeETH.sol": {
"content": "// SPDX-License-Identifier: MIT\r\npragma solidity ^0.8.7;\r\n\r\nimport \"./OpenZeppelin/SafeERC20.sol\";\r\nimport \"@openzeppelin/contracts/utils/math/SafeMath.sol\";\r\nimport \"@openzeppelin/contracts/access/Ownable.sol\";\r\nimport \"./Interfaces/IWETH.sol\";\r\nimport \"./Interfaces/ILendingRegistry.sol\";\r\nimport \"./Interfaces/ILendingLogic.sol\";\r\nimport \"./Interfaces/IPieRegistry.sol\";\r\nimport \"./Interfaces/IPie.sol\";\r\nimport \"./Interfaces/IUniV3Router.sol\";\r\n\r\npragma experimental ABIEncoderV2;\r\n\r\n/**\r\n * @title SimpleUniRecipe contract for BaoFinance's Baskets Protocol (PieDAO fork)\r\n *\r\n * @author vex\r\n */\r\ncontract SimpleUniRecipeETH is Ownable {\r\n using SafeERC20 for IERC20;\r\n using SafeMath for uint256;\r\n\r\n // -------------------------------\r\n // CONSTANTS\r\n // -------------------------------\r\n\r\n IERC20 constant DAI = IERC20(0x6B175474E89094C44Da98b954EedeAC495271d0F);\r\n IWETH constant WETH = IWETH(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);\r\n ILendingRegistry public immutable lendingRegistry;\r\n IPieRegistry public immutable basketRegistry;\r\n\r\n // -------------------------------\r\n // VARIABLES\r\n // -------------------------------\r\n\r\n uniV3Router public uniRouter;\r\n uniOracle public oracle;\r\n\r\n /**\r\n * Create a new StableUniRecipe.\r\n *\r\n * @param _lendingRegistry LendingRegistry address\r\n * @param _pieRegistry PieRegistry address\r\n * @param _uniV3Router Uniswap V3 Router address\r\n */\r\n constructor(\r\n address _lendingRegistry,\r\n address _pieRegistry,\r\n address _uniV3Router,\r\n address _uniOracle\r\n ) {\r\n require(_lendingRegistry != address(0), \"LENDING_MANAGER_ZERO\");\r\n require(_pieRegistry != address(0), \"PIE_REGISTRY_ZERO\");\r\n\r\n lendingRegistry = ILendingRegistry(_lendingRegistry);\r\n basketRegistry = IPieRegistry(_pieRegistry);\r\n\r\n uniRouter = uniV3Router(_uniV3Router);\r\n oracle = uniOracle(_uniOracle);\r\n\r\n // Approve max WETH spending on Uni Router\r\n WETH.approve(address(uniRouter), type(uint256).max);\r\n }\r\n\r\n // -------------------------------\r\n // PUBLIC FUNCTIONS\r\n // -------------------------------\r\n\r\n /**\r\n * External bake function.\r\n * Mints `_mintAmount` basket tokens with as little of `_maxInput` as possible.\r\n *\r\n * @param _basket Address of basket token to mint\r\n * @param _maxInput Max DAI to use to mint _mintAmount basket tokens\r\n * @param _mintAmount Target amount of basket tokens to mint\r\n * @return inputAmountUsed Amount of DAI used to mint the basket token\r\n * @return outputAmount Amount of basket tokens minted\r\n */\r\n function bake(\r\n address _basket,\r\n uint256 _maxInput,\r\n uint256 _mintAmount\r\n ) external returns (uint256 inputAmountUsed, uint256 outputAmount) {\r\n // Transfer WETH to the Recipe\r\n WETH.transferFrom(msg.sender, address(this), _maxInput);\r\n\r\n // Bake _mintAmount basket tokens\r\n outputAmount = _bake(_basket, _mintAmount);\r\n\r\n // Transfer remaining WETH to msg.sender\r\n uint256 remainingInputBalance = WETH.balanceOf(address(this));\r\n if (remainingInputBalance > 0) {\r\n WETH.transfer(msg.sender, remainingInputBalance);\r\n }\r\n inputAmountUsed = _maxInput - remainingInputBalance;\r\n\r\n // Transfer minted basket tokens to msg.sender\r\n IERC20(_basket).safeTransfer(msg.sender, outputAmount);\r\n }\r\n\r\n /**\r\n * Bake a basket with ETH.\r\n *\r\n * Wraps the ETH that was sent, swaps it for DAI on UniV3, and continues the baking\r\n * process as normal.\r\n *\r\n * @param _basket Basket token to mint\r\n * @param _mintAmount Target amount of basket tokens to mint\r\n */\r\n function toBasket(\r\n address _basket,\r\n uint256 _mintAmount\r\n ) external payable returns (uint256 inputAmountUsed, uint256 outputAmount) {\r\n // Wrap ETH\r\n WETH.deposit{value : msg.value}();\r\n\r\n // Bake basket\r\n outputAmount = _bake(_basket, _mintAmount);\r\n\r\n // Send remaining funds back to msg.sender\r\n uint256 wethBalance = WETH.balanceOf(address(this));\r\n if (wethBalance > 0) {\r\n inputAmountUsed = msg.value - wethBalance;\r\n\r\n WETH.withdraw(wethBalance);\r\n WETH.transfer(msg.sender, wethBalance);\r\n }\r\n\r\n // Transfer minted baskets to msg.sender\r\n IERC20(_basket).safeTransfer(msg.sender, outputAmount);\r\n }\r\n\r\n /**\r\n * Get the price of `_amount` basket tokens in DAI\r\n *\r\n * @param _basket Basket token to get the price of\r\n * @param _amount Amount of basket tokens to get price of\r\n * @return _price Price of `_amount` basket tokens in DAI\r\n */\r\n function getPrice(address _basket, uint256 _amount) public returns (uint256 _price) {\r\n // Check that _basket is a valid basket\r\n require(basketRegistry.inRegistry(_basket));\r\n\r\n // Loop through all the tokens in the basket and get their prices on UniSwap V3\r\n (address[] memory tokens, uint256[] memory amounts) = IPie(_basket).calcTokensForAmount(_amount);\r\n address _token;\r\n address _underlying;\r\n uint256 _amount;\r\n for (uint256 i; i < tokens.length; ++i) {\r\n _token = tokens[i];\r\n _amount = amounts[i].add(1);\r\n\r\n // If the amount equals zero, revert.\r\n assembly {\r\n if iszero(_amount) {\r\n revert(0, 0)\r\n }\r\n }\r\n\r\n _underlying = lendingRegistry.wrappedToUnderlying(_token);\r\n if (_underlying != address(0)) {\r\n _amount = mulDivDown(\r\n _amount,\r\n getLendingLogicFromWrapped(_token).exchangeRateView(_token),\r\n 1e18\r\n );\r\n _token = _underlying;\r\n }\r\n\r\n // If the token is WETH, we don't need to perform a swap before lending.\r\n _price += _token == address(WETH) ? _amount : _quoteExactOutput(address(WETH), _token, _amount, 500);\r\n }\r\n return _price;\r\n }\r\n\r\n /**\r\n * Get the price of `_amount` basket tokens in ETH\r\n *\r\n * @param _basket Basket token to get the price of\r\n * @param _amount Amount of basket tokens to get price of\r\n * @return _price Price of `_amount` basket tokens in ETH\r\n */\r\n function getPriceUSD(address _basket, uint256 _amount) external returns (uint256 _price) {\r\n _price = _quoteExactOutput(\r\n address(DAI),\r\n address(WETH),\r\n getPrice(_basket, _amount),\r\n 500\r\n );\r\n }\r\n\r\n // -------------------------------\r\n // INTERNAL FUNCTIONS\r\n // -------------------------------\r\n\r\n /**\r\n * Internal bake function.\r\n * Checks if _outputToken is a valid basket, mints _mintAmount basketTokens, and returns the real\r\n * amount minted.\r\n *\r\n * @param _basket Address of basket token to mint\r\n * @param _mintAmount Target amount of basket tokens to mint\r\n * @return outputAmount Amount of basket tokens minted\r\n */\r\n function _bake(address _basket, uint256 _mintAmount) internal returns (uint256 outputAmount) {\r\n require(basketRegistry.inRegistry(_basket));\r\n\r\n swapAndJoin(_basket, _mintAmount);\r\n\r\n outputAmount = IERC20(_basket).balanceOf(address(this));\r\n }\r\n\r\n /**\r\n * Swap for the underlying assets of a basket using only Uni V3 and mint _outputAmount basket tokens.\r\n *\r\n * @param _basket Basket to pull underlying assets from\r\n * @param _mintAmount Target amount of basket tokens to mint\r\n */\r\n function swapAndJoin(address _basket, uint256 _mintAmount) internal {\r\n IPie basket = IPie(_basket);\r\n (address[] memory tokens, uint256[] memory amounts) = basket.calcTokensForAmount(_mintAmount);\r\n\r\n // Instantiate empty variables that will be assigned multiple times in the loop, less memory allocation\r\n address _token;\r\n address underlying;\r\n uint256 _amount;\r\n uint256 underlyingAmount;\r\n ILendingLogic lendingLogic;\r\n\r\n for (uint256 i; i < tokens.length; ++i) {\r\n _token = tokens[i];\r\n _amount = amounts[i].add(1);\r\n\r\n // If the token is registered in the lending registry, swap to\r\n // its underlying token and lend it.\r\n underlying = lendingRegistry.wrappedToUnderlying(_token);\r\n\r\n if (underlying == address(0) && _token != address(WETH)) {\r\n _swap_out_amount(\r\n address(WETH),\r\n _token,\r\n _amount,\r\n 500\r\n );\r\n } else {\r\n // Get underlying amount according to the exchange rate\r\n lendingLogic = getLendingLogicFromWrapped(_token);\r\n underlyingAmount = mulDivDown(_amount, lendingLogic.exchangeRate(_token), 1e18);\r\n\r\n // Swap for the underlying asset on UniV3\r\n // If the token is DAI, no need to swap\r\n if (underlying != address(WETH)) {\r\n _swap_out_amount(\r\n address(WETH),\r\n underlying,\r\n underlyingAmount,\r\n 500\r\n );\r\n }\r\n\r\n // Execute lending transactions\r\n (address[] memory targets, bytes[] memory data) = lendingLogic.lend(underlying, underlyingAmount, address(this));\r\n for (uint256 j; j < targets.length; ++j) {\r\n (bool success,) = targets[j].call{value : 0}(data[j]);\r\n require(success, \"CALL_FAILED\");\r\n }\r\n }\r\n IERC20(_token).approve(_basket, _amount);\r\n }\r\n basket.joinPool(_mintAmount);\r\n }\r\n\r\n /**\r\n * Swap `_from` -> `_to` and receive exactly `_amountOut` of `_to` on UniV3\r\n *\r\n * @param _from Address of token to swap from\r\n * @param _to Address of token to swap to\r\n * @param _amountOut Exact amount of `_to` to receive\r\n * @param _fee UniV3 pool fee\r\n */\r\n function _swap_out_amount(\r\n address _from,\r\n address _to,\r\n uint256 _amountOut,\r\n uint24 _fee\r\n ) internal {\r\n uniRouter.exactOutputSingle(\r\n uniV3Router.ExactOutputSingleParams(\r\n _from,\r\n _to,\r\n _fee,\r\n address(this),\r\n 0,\r\n _amountOut,\r\n type(uint256).max,\r\n 0\r\n )\r\n );\r\n }\r\n\r\n /**\r\n * Swap `_from` -> `_to` given an an amount of 'from' token to be swaped on UniV3\r\n *\r\n * @param _from Address of token to swap from\r\n * @param _to Address of token to swap to\r\n * @param _amountIn Exact amount of `_from` to sell\r\n * @param _fee UniV3 pool fee\r\n */\r\n function _swap_in_amount(\r\n address _from,\r\n address _to,\r\n uint256 _amountIn,\r\n uint24 _fee\r\n ) internal returns (uint256) {\r\n return uniRouter.exactInputSingle(\r\n uniV3Router.ExactInputSingleParams(\r\n _from,\r\n _to,\r\n _fee,\r\n address(this),\r\n 0,\r\n _amountIn,\r\n 0,\r\n 0\r\n )\r\n );\r\n }\r\n\r\n /**\r\n * Quote an exact input swap on UniV3\r\n *\r\n * @param _from Token to swap from\r\n * @param _to Token to swap to\r\n * @param _amountOut Exact amount of `_to` tokens to be received for `_amountIn` `_from` tokens\r\n * @return _amountIn Amount to send in order to receive `_amountOut` `to` tokens\r\n */\r\n function _quoteExactOutput(\r\n address _from,\r\n address _to,\r\n uint256 _amountOut,\r\n uint24 _fee\r\n ) internal returns (uint256 _amountIn) {\r\n try oracle.quoteExactOutputSingle(_from, _to, _fee, _amountOut, 0) returns (uint256 _p) {\r\n _amountIn = _p;\r\n } catch {\r\n _amountIn = type(uint256).max;\r\n }\r\n }\r\n\r\n /**\r\n * Get the lending logic of a wrapped token\r\n *\r\n * @param _wrapped Address of wrapped token\r\n * @return ILendingLogic - Lending logic associated with `_wrapped`\r\n */\r\n function getLendingLogicFromWrapped(address _wrapped) internal view returns (ILendingLogic) {\r\n return ILendingLogic(\r\n lendingRegistry.protocolToLogic(\r\n lendingRegistry.wrappedToProtocol(\r\n _wrapped\r\n )\r\n )\r\n );\r\n }\r\n\r\n /**\r\n * Yoinked from the geniuses behind solmate\r\n * https://github.com/Rari-Capital/solmate/blob/main/src/utils/FixedPointMathLib.sol\r\n *\r\n * (x*y)/z\r\n */\r\n function mulDivDown(\r\n uint256 x,\r\n uint256 y,\r\n uint256 denominator\r\n ) internal pure returns (uint256 z) {\r\n assembly {\r\n // Store x * y in z for now.\r\n z := mul(x, y)\r\n\r\n // Equivalent to require(denominator != 0 && (x == 0 || (x * y) / x == y))\r\n if iszero(and(iszero(iszero(denominator)), or(iszero(x), eq(div(z, x), y)))) {\r\n revert(0, 0)\r\n }\r\n\r\n // Divide z by the denominator.\r\n z := div(z, denominator)\r\n }\r\n }\r\n\r\n // -------------------------------\r\n // ADMIN FUNCTIONS\r\n // -------------------------------\r\n\r\n /**\r\n * Update the Uni V3 Router\r\n *\r\n * @param _newRouter New Uni V3 Router address\r\n */\r\n function updateUniRouter(address _newRouter) external onlyOwner {\r\n // Update stored Uni V3 exchange\r\n uniRouter = uniV3Router(_newRouter);\r\n\r\n // Re-approve WETH\r\n WETH.approve(_newRouter, 0);\r\n WETH.approve(_newRouter, type(uint256).max);\r\n\r\n }\r\n\r\n /**\r\n * Update the Uni V3 Oracle\r\n *\r\n * @param _newOracle New Uni V3 Oracle address\r\n */\r\n function updateUniOracle(address _newOracle) external onlyOwner {\r\n oracle = uniOracle(_newOracle);\r\n }\r\n\r\n receive() external payable {}\r\n}"
}
},
"settings": {
"optimizer": {
"enabled": false,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"evm.bytecode",
"evm.deployedBytecode",
"devdoc",
"userdoc",
"metadata",
"abi"
]
}
},
"libraries": {}
}
}