@yearn-mechanics/yswaps
Version:
YSwaps will swap enabled strategie's funds from harvests
184 lines • 138 kB
JSON
{
"language": "Solidity",
"sources": {
"contracts/CommonErrors.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nlibrary CommonErrors {\n error ZeroAddress();\n error NotAuthorized();\n error ZeroAmount();\n error ZeroSlippage();\n error IncorrectSwapInformation();\n}\n"
},
"contracts/TradeFactory/TradeFactoryAccessManager.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/access/AccessControl.sol';\n\nimport '../CommonErrors.sol';\n\nabstract contract TradeFactoryAccessManager is AccessControl {\n bytes32 public constant MASTER_ADMIN = keccak256('MASTER_ADMIN');\n\n constructor(address _masterAdmin) {\n if (_masterAdmin == address(0)) revert CommonErrors.ZeroAddress();\n _setRoleAdmin(MASTER_ADMIN, MASTER_ADMIN);\n _setupRole(MASTER_ADMIN, _masterAdmin);\n }\n}\n"
},
"@openzeppelin/contracts/access/AccessControl.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\nimport \"./IAccessControl.sol\";\nimport \"../utils/Context.sol\";\nimport \"../utils/Strings.sol\";\nimport \"../utils/introspection/ERC165.sol\";\n\n/**\n * @dev Contract module that allows children to implement role-based access\n * control mechanisms. This is a lightweight version that doesn't allow enumerating role\n * members except through off-chain means by accessing the contract event logs. Some\n * applications may benefit from on-chain enumerability, for those cases see\n * {AccessControlEnumerable}.\n *\n * Roles are referred to by their `bytes32` identifier. These should be exposed\n * in the external API and be unique. The best way to achieve this is by\n * using `public constant` hash digests:\n *\n * ```\n * bytes32 public constant MY_ROLE = keccak256(\"MY_ROLE\");\n * ```\n *\n * Roles can be used to represent a set of permissions. To restrict access to a\n * function call, use {hasRole}:\n *\n * ```\n * function foo() public {\n * require(hasRole(MY_ROLE, msg.sender));\n * ...\n * }\n * ```\n *\n * Roles can be granted and revoked dynamically via the {grantRole} and\n * {revokeRole} functions. Each role has an associated admin role, and only\n * accounts that have a role's admin role can call {grantRole} and {revokeRole}.\n *\n * By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means\n * that only accounts with this role will be able to grant or revoke other\n * roles. More complex role relationships can be created by using\n * {_setRoleAdmin}.\n *\n * WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to\n * grant and revoke this role. Extra precautions should be taken to secure\n * accounts that have been granted it.\n */\nabstract contract AccessControl is Context, IAccessControl, ERC165 {\n struct RoleData {\n mapping(address => bool) members;\n bytes32 adminRole;\n }\n\n mapping(bytes32 => RoleData) private _roles;\n\n bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;\n\n /**\n * @dev Modifier that checks that an account has a specific role. Reverts\n * with a standardized message including the required role.\n *\n * The format of the revert reason is given by the following regular expression:\n *\n * /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/\n *\n * _Available since v4.1._\n */\n modifier onlyRole(bytes32 role) {\n _checkRole(role, _msgSender());\n _;\n }\n\n /**\n * @dev See {IERC165-supportsInterface}.\n */\n function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {\n return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId);\n }\n\n /**\n * @dev Returns `true` if `account` has been granted `role`.\n */\n function hasRole(bytes32 role, address account) public view override returns (bool) {\n return _roles[role].members[account];\n }\n\n /**\n * @dev Revert with a standard message if `account` is missing `role`.\n *\n * The format of the revert reason is given by the following regular expression:\n *\n * /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/\n */\n function _checkRole(bytes32 role, address account) internal view {\n if (!hasRole(role, account)) {\n revert(\n string(\n abi.encodePacked(\n \"AccessControl: account \",\n Strings.toHexString(uint160(account), 20),\n \" is missing role \",\n Strings.toHexString(uint256(role), 32)\n )\n )\n );\n }\n }\n\n /**\n * @dev Returns the admin role that controls `role`. See {grantRole} and\n * {revokeRole}.\n *\n * To change a role's admin, use {_setRoleAdmin}.\n */\n function getRoleAdmin(bytes32 role) public view override returns (bytes32) {\n return _roles[role].adminRole;\n }\n\n /**\n * @dev Grants `role` to `account`.\n *\n * If `account` had not been already granted `role`, emits a {RoleGranted}\n * event.\n *\n * Requirements:\n *\n * - the caller must have ``role``'s admin role.\n */\n function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {\n _grantRole(role, account);\n }\n\n /**\n * @dev Revokes `role` from `account`.\n *\n * If `account` had been granted `role`, emits a {RoleRevoked} event.\n *\n * Requirements:\n *\n * - the caller must have ``role``'s admin role.\n */\n function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {\n _revokeRole(role, account);\n }\n\n /**\n * @dev Revokes `role` from the calling account.\n *\n * Roles are often managed via {grantRole} and {revokeRole}: this function's\n * purpose is to provide a mechanism for accounts to lose their privileges\n * if they are compromised (such as when a trusted device is misplaced).\n *\n * If the calling account had been granted `role`, emits a {RoleRevoked}\n * event.\n *\n * Requirements:\n *\n * - the caller must be `account`.\n */\n function renounceRole(bytes32 role, address account) public virtual override {\n require(account == _msgSender(), \"AccessControl: can only renounce roles for self\");\n\n _revokeRole(role, account);\n }\n\n /**\n * @dev Grants `role` to `account`.\n *\n * If `account` had not been already granted `role`, emits a {RoleGranted}\n * event. Note that unlike {grantRole}, this function doesn't perform any\n * checks on the calling account.\n *\n * [WARNING]\n * ====\n * This function should only be called from the constructor when setting\n * up the initial roles for the system.\n *\n * Using this function in any other way is effectively circumventing the admin\n * system imposed by {AccessControl}.\n * ====\n */\n function _setupRole(bytes32 role, address account) internal virtual {\n _grantRole(role, account);\n }\n\n /**\n * @dev Sets `adminRole` as ``role``'s admin role.\n *\n * Emits a {RoleAdminChanged} event.\n */\n function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {\n bytes32 previousAdminRole = getRoleAdmin(role);\n _roles[role].adminRole = adminRole;\n emit RoleAdminChanged(role, previousAdminRole, adminRole);\n }\n\n function _grantRole(bytes32 role, address account) private {\n if (!hasRole(role, account)) {\n _roles[role].members[account] = true;\n emit RoleGranted(role, account, _msgSender());\n }\n }\n\n function _revokeRole(bytes32 role, address account) private {\n if (hasRole(role, account)) {\n _roles[role].members[account] = false;\n emit RoleRevoked(role, account, _msgSender());\n }\n }\n}\n"
},
"@openzeppelin/contracts/access/IAccessControl.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\n/**\n * @dev External interface of AccessControl declared to support ERC165 detection.\n */\ninterface IAccessControl {\n /**\n * @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole`\n *\n * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite\n * {RoleAdminChanged} not being emitted signaling this.\n *\n * _Available since v3.1._\n */\n event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);\n\n /**\n * @dev Emitted when `account` is granted `role`.\n *\n * `sender` is the account that originated the contract call, an admin role\n * bearer except when using {AccessControl-_setupRole}.\n */\n event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);\n\n /**\n * @dev Emitted when `account` is revoked `role`.\n *\n * `sender` is the account that originated the contract call:\n * - if using `revokeRole`, it is the admin role bearer\n * - if using `renounceRole`, it is the role bearer (i.e. `account`)\n */\n event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);\n\n /**\n * @dev Returns `true` if `account` has been granted `role`.\n */\n function hasRole(bytes32 role, address account) external view returns (bool);\n\n /**\n * @dev Returns the admin role that controls `role`. See {grantRole} and\n * {revokeRole}.\n *\n * To change a role's admin, use {AccessControl-_setRoleAdmin}.\n */\n function getRoleAdmin(bytes32 role) external view returns (bytes32);\n\n /**\n * @dev Grants `role` to `account`.\n *\n * If `account` had not been already granted `role`, emits a {RoleGranted}\n * event.\n *\n * Requirements:\n *\n * - the caller must have ``role``'s admin role.\n */\n function grantRole(bytes32 role, address account) external;\n\n /**\n * @dev Revokes `role` from `account`.\n *\n * If `account` had been granted `role`, emits a {RoleRevoked} event.\n *\n * Requirements:\n *\n * - the caller must have ``role``'s admin role.\n */\n function revokeRole(bytes32 role, address account) external;\n\n /**\n * @dev Revokes `role` from the calling account.\n *\n * Roles are often managed via {grantRole} and {revokeRole}: this function's\n * purpose is to provide a mechanism for accounts to lose their privileges\n * if they are compromised (such as when a trusted device is misplaced).\n *\n * If the calling account had been granted `role`, emits a {RoleRevoked}\n * event.\n *\n * Requirements:\n *\n * - the caller must be `account`.\n */\n function renounceRole(bytes32 role, address account) external;\n}\n"
},
"@openzeppelin/contracts/utils/Context.sol": {
"content": "// SPDX-License-Identifier: MIT\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/Strings.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\n/**\n * @dev String operations.\n */\nlibrary Strings {\n bytes16 private constant _HEX_SYMBOLS = \"0123456789abcdef\";\n\n /**\n * @dev Converts a `uint256` to its ASCII `string` decimal representation.\n */\n function toString(uint256 value) internal pure returns (string memory) {\n // Inspired by OraclizeAPI's implementation - MIT licence\n // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol\n\n if (value == 0) {\n return \"0\";\n }\n uint256 temp = value;\n uint256 digits;\n while (temp != 0) {\n digits++;\n temp /= 10;\n }\n bytes memory buffer = new bytes(digits);\n while (value != 0) {\n digits -= 1;\n buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));\n value /= 10;\n }\n return string(buffer);\n }\n\n /**\n * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.\n */\n function toHexString(uint256 value) internal pure returns (string memory) {\n if (value == 0) {\n return \"0x00\";\n }\n uint256 temp = value;\n uint256 length = 0;\n while (temp != 0) {\n length++;\n temp >>= 8;\n }\n return toHexString(value, length);\n }\n\n /**\n * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.\n */\n function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {\n bytes memory buffer = new bytes(2 * length + 2);\n buffer[0] = \"0\";\n buffer[1] = \"x\";\n for (uint256 i = 2 * length + 1; i > 1; --i) {\n buffer[i] = _HEX_SYMBOLS[value & 0xf];\n value >>= 4;\n }\n require(value == 0, \"Strings: hex length insufficient\");\n return string(buffer);\n }\n}\n"
},
"@openzeppelin/contracts/utils/introspection/ERC165.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\nimport \"./IERC165.sol\";\n\n/**\n * @dev Implementation of the {IERC165} interface.\n *\n * Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check\n * for the additional interface id that will be supported. For example:\n *\n * ```solidity\n * function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {\n * return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);\n * }\n * ```\n *\n * Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation.\n */\nabstract contract ERC165 is IERC165 {\n /**\n * @dev See {IERC165-supportsInterface}.\n */\n function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {\n return interfaceId == type(IERC165).interfaceId;\n }\n}\n"
},
"@openzeppelin/contracts/utils/introspection/IERC165.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Interface of the ERC165 standard, as defined in the\n * https://eips.ethereum.org/EIPS/eip-165[EIP].\n *\n * Implementers can declare support of contract interfaces, which can then be\n * queried by others ({ERC165Checker}).\n *\n * For an implementation, see {ERC165}.\n */\ninterface IERC165 {\n /**\n * @dev Returns true if this contract implements the interface defined by\n * `interfaceId`. See the corresponding\n * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]\n * to learn more about how these ids are created.\n *\n * This function call must use less than 30 000 gas.\n */\n function supportsInterface(bytes4 interfaceId) external view returns (bool);\n}\n"
},
"@openzeppelin/contracts/token/ERC20/ERC20.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\nimport \"./IERC20.sol\";\nimport \"./extensions/IERC20Metadata.sol\";\nimport \"../../utils/Context.sol\";\n\n/**\n * @dev Implementation of the {IERC20} interface.\n *\n * This implementation is agnostic to the way tokens are created. This means\n * that a supply mechanism has to be added in a derived contract using {_mint}.\n * For a generic mechanism see {ERC20PresetMinterPauser}.\n *\n * TIP: For a detailed writeup see our guide\n * https://forum.zeppelin.solutions/t/how-to-implement-erc20-supply-mechanisms/226[How\n * to implement supply mechanisms].\n *\n * We have followed general OpenZeppelin Contracts guidelines: functions revert\n * instead returning `false` on failure. This behavior is nonetheless\n * conventional and does not conflict with the expectations of ERC20\n * applications.\n *\n * Additionally, an {Approval} event is emitted on calls to {transferFrom}.\n * This allows applications to reconstruct the allowance for all accounts just\n * by listening to said events. Other implementations of the EIP may not emit\n * these events, as it isn't required by the specification.\n *\n * Finally, the non-standard {decreaseAllowance} and {increaseAllowance}\n * functions have been added to mitigate the well-known issues around setting\n * allowances. See {IERC20-approve}.\n */\ncontract ERC20 is Context, IERC20, IERC20Metadata {\n mapping(address => uint256) private _balances;\n\n mapping(address => mapping(address => uint256)) private _allowances;\n\n uint256 private _totalSupply;\n\n string private _name;\n string private _symbol;\n\n /**\n * @dev Sets the values for {name} and {symbol}.\n *\n * The default value of {decimals} is 18. To select a different value for\n * {decimals} you should overload it.\n *\n * All two of these values are immutable: they can only be set once during\n * construction.\n */\n constructor(string memory name_, string memory symbol_) {\n _name = name_;\n _symbol = symbol_;\n }\n\n /**\n * @dev Returns the name of the token.\n */\n function name() public view virtual override returns (string memory) {\n return _name;\n }\n\n /**\n * @dev Returns the symbol of the token, usually a shorter version of the\n * name.\n */\n function symbol() public view virtual override returns (string memory) {\n return _symbol;\n }\n\n /**\n * @dev Returns the number of decimals used to get its user representation.\n * For example, if `decimals` equals `2`, a balance of `505` tokens should\n * be displayed to a user as `5.05` (`505 / 10 ** 2`).\n *\n * Tokens usually opt for a value of 18, imitating the relationship between\n * Ether and Wei. This is the value {ERC20} uses, unless this function is\n * overridden;\n *\n * NOTE: This information is only used for _display_ purposes: it in\n * no way affects any of the arithmetic of the contract, including\n * {IERC20-balanceOf} and {IERC20-transfer}.\n */\n function decimals() public view virtual override returns (uint8) {\n return 18;\n }\n\n /**\n * @dev See {IERC20-totalSupply}.\n */\n function totalSupply() public view virtual override returns (uint256) {\n return _totalSupply;\n }\n\n /**\n * @dev See {IERC20-balanceOf}.\n */\n function balanceOf(address account) public view virtual override returns (uint256) {\n return _balances[account];\n }\n\n /**\n * @dev See {IERC20-transfer}.\n *\n * Requirements:\n *\n * - `recipient` cannot be the zero address.\n * - the caller must have a balance of at least `amount`.\n */\n function transfer(address recipient, uint256 amount) public virtual override returns (bool) {\n _transfer(_msgSender(), recipient, amount);\n return true;\n }\n\n /**\n * @dev See {IERC20-allowance}.\n */\n function allowance(address owner, address spender) public view virtual override returns (uint256) {\n return _allowances[owner][spender];\n }\n\n /**\n * @dev See {IERC20-approve}.\n *\n * Requirements:\n *\n * - `spender` cannot be the zero address.\n */\n function approve(address spender, uint256 amount) public virtual override returns (bool) {\n _approve(_msgSender(), spender, amount);\n return true;\n }\n\n /**\n * @dev See {IERC20-transferFrom}.\n *\n * Emits an {Approval} event indicating the updated allowance. This is not\n * required by the EIP. See the note at the beginning of {ERC20}.\n *\n * Requirements:\n *\n * - `sender` and `recipient` cannot be the zero address.\n * - `sender` must have a balance of at least `amount`.\n * - the caller must have allowance for ``sender``'s tokens of at least\n * `amount`.\n */\n function transferFrom(\n address sender,\n address recipient,\n uint256 amount\n ) public virtual override returns (bool) {\n _transfer(sender, recipient, amount);\n\n uint256 currentAllowance = _allowances[sender][_msgSender()];\n require(currentAllowance >= amount, \"ERC20: transfer amount exceeds allowance\");\n unchecked {\n _approve(sender, _msgSender(), currentAllowance - amount);\n }\n\n return true;\n }\n\n /**\n * @dev Atomically increases the allowance granted to `spender` by the caller.\n *\n * This is an alternative to {approve} that can be used as a mitigation for\n * problems described in {IERC20-approve}.\n *\n * Emits an {Approval} event indicating the updated allowance.\n *\n * Requirements:\n *\n * - `spender` cannot be the zero address.\n */\n function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {\n _approve(_msgSender(), spender, _allowances[_msgSender()][spender] + addedValue);\n return true;\n }\n\n /**\n * @dev Atomically decreases the allowance granted to `spender` by the caller.\n *\n * This is an alternative to {approve} that can be used as a mitigation for\n * problems described in {IERC20-approve}.\n *\n * Emits an {Approval} event indicating the updated allowance.\n *\n * Requirements:\n *\n * - `spender` cannot be the zero address.\n * - `spender` must have allowance for the caller of at least\n * `subtractedValue`.\n */\n function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {\n uint256 currentAllowance = _allowances[_msgSender()][spender];\n require(currentAllowance >= subtractedValue, \"ERC20: decreased allowance below zero\");\n unchecked {\n _approve(_msgSender(), spender, currentAllowance - subtractedValue);\n }\n\n return true;\n }\n\n /**\n * @dev Moves `amount` of tokens from `sender` to `recipient`.\n *\n * This internal function is equivalent to {transfer}, and can be used to\n * e.g. implement automatic token fees, slashing mechanisms, etc.\n *\n * Emits a {Transfer} event.\n *\n * Requirements:\n *\n * - `sender` cannot be the zero address.\n * - `recipient` cannot be the zero address.\n * - `sender` must have a balance of at least `amount`.\n */\n function _transfer(\n address sender,\n address recipient,\n uint256 amount\n ) internal virtual {\n require(sender != address(0), \"ERC20: transfer from the zero address\");\n require(recipient != address(0), \"ERC20: transfer to the zero address\");\n\n _beforeTokenTransfer(sender, recipient, amount);\n\n uint256 senderBalance = _balances[sender];\n require(senderBalance >= amount, \"ERC20: transfer amount exceeds balance\");\n unchecked {\n _balances[sender] = senderBalance - amount;\n }\n _balances[recipient] += amount;\n\n emit Transfer(sender, recipient, amount);\n\n _afterTokenTransfer(sender, recipient, amount);\n }\n\n /** @dev Creates `amount` tokens and assigns them to `account`, increasing\n * the total supply.\n *\n * Emits a {Transfer} event with `from` set to the zero address.\n *\n * Requirements:\n *\n * - `account` cannot be the zero address.\n */\n function _mint(address account, uint256 amount) internal virtual {\n require(account != address(0), \"ERC20: mint to the zero address\");\n\n _beforeTokenTransfer(address(0), account, amount);\n\n _totalSupply += amount;\n _balances[account] += amount;\n emit Transfer(address(0), account, amount);\n\n _afterTokenTransfer(address(0), account, amount);\n }\n\n /**\n * @dev Destroys `amount` tokens from `account`, reducing the\n * total supply.\n *\n * Emits a {Transfer} event with `to` set to the zero address.\n *\n * Requirements:\n *\n * - `account` cannot be the zero address.\n * - `account` must have at least `amount` tokens.\n */\n function _burn(address account, uint256 amount) internal virtual {\n require(account != address(0), \"ERC20: burn from the zero address\");\n\n _beforeTokenTransfer(account, address(0), amount);\n\n uint256 accountBalance = _balances[account];\n require(accountBalance >= amount, \"ERC20: burn amount exceeds balance\");\n unchecked {\n _balances[account] = accountBalance - amount;\n }\n _totalSupply -= amount;\n\n emit Transfer(account, address(0), amount);\n\n _afterTokenTransfer(account, address(0), amount);\n }\n\n /**\n * @dev Sets `amount` as the allowance of `spender` over the `owner` s tokens.\n *\n * This internal function is equivalent to `approve`, and can be used to\n * e.g. set automatic allowances for certain subsystems, etc.\n *\n * Emits an {Approval} event.\n *\n * Requirements:\n *\n * - `owner` cannot be the zero address.\n * - `spender` cannot be the zero address.\n */\n function _approve(\n address owner,\n address spender,\n uint256 amount\n ) internal virtual {\n require(owner != address(0), \"ERC20: approve from the zero address\");\n require(spender != address(0), \"ERC20: approve to the zero address\");\n\n _allowances[owner][spender] = amount;\n emit Approval(owner, spender, amount);\n }\n\n /**\n * @dev Hook that is called before any transfer of tokens. This includes\n * minting and burning.\n *\n * Calling conditions:\n *\n * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens\n * will be transferred to `to`.\n * - when `from` is zero, `amount` tokens will be minted for `to`.\n * - when `to` is zero, `amount` of ``from``'s tokens will be burned.\n * - `from` and `to` are never both zero.\n *\n * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].\n */\n function _beforeTokenTransfer(\n address from,\n address to,\n uint256 amount\n ) internal virtual {}\n\n /**\n * @dev Hook that is called after any transfer of tokens. This includes\n * minting and burning.\n *\n * Calling conditions:\n *\n * - when `from` and `to` are both non-zero, `amount` of ``from``'s tokens\n * has been transferred to `to`.\n * - when `from` is zero, `amount` tokens have been minted for `to`.\n * - when `to` is zero, `amount` of ``from``'s tokens have been burned.\n * - `from` and `to` are never both zero.\n *\n * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].\n */\n function _afterTokenTransfer(\n address from,\n address to,\n uint256 amount\n ) internal virtual {}\n}\n"
},
"@openzeppelin/contracts/token/ERC20/IERC20.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Interface of the ERC20 standard as defined in the EIP.\n */\ninterface IERC20 {\n /**\n * @dev Returns the amount of tokens in existence.\n */\n function totalSupply() external view returns (uint256);\n\n /**\n * @dev Returns the amount of tokens owned by `account`.\n */\n function balanceOf(address account) external view returns (uint256);\n\n /**\n * @dev Moves `amount` tokens from the caller's account to `recipient`.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * Emits a {Transfer} event.\n */\n function transfer(address recipient, uint256 amount) external returns (bool);\n\n /**\n * @dev Returns the remaining number of tokens that `spender` will be\n * allowed to spend on behalf of `owner` through {transferFrom}. This is\n * zero by default.\n *\n * This value changes when {approve} or {transferFrom} are called.\n */\n function allowance(address owner, address spender) external view returns (uint256);\n\n /**\n * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * IMPORTANT: Beware that changing an allowance with this method brings the risk\n * that someone may use both the old and the new allowance by unfortunate\n * transaction ordering. One possible solution to mitigate this race\n * condition is to first reduce the spender's allowance to 0 and set the\n * desired value afterwards:\n * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729\n *\n * Emits an {Approval} event.\n */\n function approve(address spender, uint256 amount) external returns (bool);\n\n /**\n * @dev Moves `amount` tokens from `sender` to `recipient` using the\n * allowance mechanism. `amount` is then deducted from the caller's\n * allowance.\n *\n * Returns a boolean value indicating whether the operation succeeded.\n *\n * Emits a {Transfer} event.\n */\n function transferFrom(\n address sender,\n address recipient,\n uint256 amount\n ) external returns (bool);\n\n /**\n * @dev Emitted when `value` tokens are moved from one account (`from`) to\n * another (`to`).\n *\n * Note that `value` may be zero.\n */\n event Transfer(address indexed from, address indexed to, uint256 value);\n\n /**\n * @dev Emitted when the allowance of a `spender` for an `owner` is set by\n * a call to {approve}. `value` is the new allowance.\n */\n event Approval(address indexed owner, address indexed spender, uint256 value);\n}\n"
},
"@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\nimport \"../IERC20.sol\";\n\n/**\n * @dev Interface for the optional metadata functions from the ERC20 standard.\n *\n * _Available since v4.1._\n */\ninterface IERC20Metadata is IERC20 {\n /**\n * @dev Returns the name of the token.\n */\n function name() external view returns (string memory);\n\n /**\n * @dev Returns the symbol of the token.\n */\n function symbol() external view returns (string memory);\n\n /**\n * @dev Returns the decimals places of the token.\n */\n function decimals() external view returns (uint8);\n}\n"
},
"contracts/TradeFactory/TradeFactoryExecutor.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/utils/math/Math.sol';\nimport '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';\nimport '@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol';\nimport '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';\n\nimport '@yearn-contracts/contract-utils/solidity/contracts/utils/Machinery.sol';\n\nimport '../swappers/async/AsyncSwapper.sol';\nimport '../swappers/sync/SyncSwapper.sol';\n\nimport '../OTCPool.sol';\n\nimport './TradeFactoryPositionsHandler.sol';\n\ninterface ITradeFactoryExecutor {\n event SyncTradeExecuted(\n address indexed _strategy,\n address indexed _swapper,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n bytes _data,\n uint256 _receivedAmount\n );\n\n event AsyncTradeExecuted(uint256 indexed _id, uint256 _receivedAmount);\n\n event AsyncTradesMatched(\n uint256 indexed _firstTradeId,\n uint256 indexed _secondTradeId,\n uint256 _consumedFirstTrade,\n uint256 _consumedSecondTrade\n );\n\n event AsyncOTCTradesExecuted(uint256[] _ids, uint256 _rateTokenInToOut);\n\n event AsyncTradeExpired(uint256 indexed _id);\n\n event SwapperAndTokenEnabled(address indexed _swapper, address _token);\n\n error OngoingTrade();\n\n error ExpiredTrade();\n\n error ZeroRate();\n\n function execute(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n bytes calldata _data\n ) external returns (uint256 _receivedAmount);\n\n function execute(\n uint256 _id,\n address _swapper,\n uint256 _minAmountOut,\n bytes calldata _data\n ) external returns (uint256 _receivedAmount);\n\n function expire(uint256 _id) external returns (uint256 _freedAmount);\n\n function execute(uint256[] calldata _ids, uint256 _rateTokenInToOut) external;\n\n function execute(\n uint256 _firstTradeId,\n uint256 _secondTradeId,\n uint256 _consumedFirstTrade,\n uint256 _consumedSecondTrade\n ) external;\n}\n\nabstract contract TradeFactoryExecutor is ITradeFactoryExecutor, TradeFactoryPositionsHandler, Machinery {\n using SafeERC20 for IERC20;\n using EnumerableSet for EnumerableSet.UintSet;\n using EnumerableSet for EnumerableSet.AddressSet;\n\n bytes32 public constant TRADES_SETTLER = keccak256('TRADES_SETTLER');\n\n constructor(address _tradesSettler, address _mechanicsRegistry) Machinery(_mechanicsRegistry) {\n _setRoleAdmin(TRADES_SETTLER, MASTER_ADMIN);\n _setupRole(TRADES_SETTLER, _tradesSettler);\n }\n\n // Machinery\n function setMechanicsRegistry(address _mechanicsRegistry) external virtual override onlyRole(MASTER_ADMIN) {\n _setMechanicsRegistry(_mechanicsRegistry);\n }\n\n function execute(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n bytes calldata _data\n ) external override onlyRole(STRATEGY) returns (uint256 _receivedAmount) {\n address _swapper = strategySyncSwapper[msg.sender];\n if (_tokenIn == address(0) || _tokenOut == address(0)) revert CommonErrors.ZeroAddress();\n if (_amountIn == 0) revert CommonErrors.ZeroAmount();\n if (_maxSlippage == 0) revert CommonErrors.ZeroSlippage();\n IERC20(_tokenIn).safeTransferFrom(msg.sender, _swapper, _amountIn);\n _receivedAmount = ISyncSwapper(_swapper).swap(msg.sender, _tokenIn, _tokenOut, _amountIn, _maxSlippage, _data);\n emit SyncTradeExecuted(msg.sender, _swapper, _tokenIn, _tokenOut, _amountIn, _maxSlippage, _data, _receivedAmount);\n }\n\n // TradeFactoryExecutor\n function execute(\n uint256 _id,\n address _swapper,\n uint256 _minAmountOut,\n bytes calldata _data\n ) external override onlyMechanic returns (uint256 _receivedAmount) {\n if (!_pendingTradesIds.contains(_id)) revert InvalidTrade();\n Trade storage _trade = pendingTradesById[_id];\n if (block.timestamp > _trade._deadline) revert ExpiredTrade();\n if (!_swappers.contains(_swapper)) revert InvalidSwapper();\n IERC20(_trade._tokenIn).safeTransferFrom(_trade._strategy, _swapper, _trade._amountIn);\n _receivedAmount = IAsyncSwapper(_swapper).swap(_trade._strategy, _trade._tokenIn, _trade._tokenOut, _trade._amountIn, _minAmountOut, _data);\n _removePendingTrade(_trade._strategy, _id);\n emit AsyncTradeExecuted(_id, _receivedAmount);\n }\n\n function expire(uint256 _id) external override onlyMechanic returns (uint256 _freedAmount) {\n if (!_pendingTradesIds.contains(_id)) revert InvalidTrade();\n Trade storage _trade = pendingTradesById[_id];\n if (block.timestamp < _trade._deadline) revert OngoingTrade();\n _freedAmount = _trade._amountIn;\n // We have to take tokens from strategy, to decrease the allowance\n IERC20(_trade._tokenIn).safeTransferFrom(_trade._strategy, address(this), _trade._amountIn);\n // Send tokens back to strategy\n IERC20(_trade._tokenIn).safeTransfer(_trade._strategy, _trade._amountIn);\n // Remove trade\n _removePendingTrade(_trade._strategy, _id);\n emit AsyncTradeExpired(_id);\n }\n\n function execute(uint256[] calldata _ids, uint256 _rateTokenInToOut) external override onlyMechanic {\n if (_rateTokenInToOut == 0) revert ZeroRate();\n address _tokenIn = pendingTradesById[_ids[0]]._tokenIn;\n address _tokenOut = pendingTradesById[_ids[0]]._tokenOut;\n uint256 _magnitudeIn = 10**IERC20Metadata(_tokenIn).decimals();\n for (uint256 i; i < _ids.length; i++) {\n Trade storage _trade = pendingTradesById[_ids[i]];\n if (i > 0 && (_trade._tokenIn != _tokenIn || _trade._tokenOut != _tokenOut)) revert InvalidTrade();\n if (block.timestamp > _trade._deadline) revert ExpiredTrade();\n if ((strategyPermissions[_trade._strategy] & _OTC_MASK) != _OTC_MASK) revert CommonErrors.NotAuthorized();\n uint256 _consumedOut = (_trade._amountIn * _rateTokenInToOut) / _magnitudeIn;\n IERC20(_trade._tokenIn).safeTransferFrom(_trade._strategy, IOTCPool(otcPool).governor(), _trade._amountIn);\n IOTCPool(otcPool).take(_trade._tokenOut, _consumedOut, _trade._strategy);\n _removePendingTrade(_trade._strategy, _trade._id);\n }\n emit AsyncOTCTradesExecuted(_ids, _rateTokenInToOut);\n }\n\n function execute(\n uint256 _firstTradeId,\n uint256 _secondTradeId,\n uint256 _consumedFirstTrade,\n uint256 _consumedSecondTrade\n ) external override onlyRole(TRADES_SETTLER) {\n Trade storage _firstTrade = pendingTradesById[_firstTradeId];\n Trade storage _secondTrade = pendingTradesById[_secondTradeId];\n if (_firstTrade._tokenIn != _secondTrade._tokenOut || _firstTrade._tokenOut != _secondTrade._tokenIn) revert InvalidTrade();\n if (block.timestamp > _firstTrade._deadline || block.timestamp > _secondTrade._deadline) revert ExpiredTrade();\n if (\n (strategyPermissions[_firstTrade._strategy] & _COW_MASK) != _COW_MASK ||\n (strategyPermissions[_secondTrade._strategy] & _COW_MASK) != _COW_MASK\n ) revert CommonErrors.NotAuthorized();\n\n IERC20(_firstTrade._tokenIn).safeTransferFrom(_firstTrade._strategy, _secondTrade._strategy, _consumedFirstTrade);\n IERC20(_secondTrade._tokenIn).safeTransferFrom(_secondTrade._strategy, _firstTrade._strategy, _consumedSecondTrade);\n\n if (_consumedFirstTrade != _firstTrade._amountIn) {\n _firstTrade._amountIn -= _consumedFirstTrade;\n } else {\n _removePendingTrade(_firstTrade._strategy, _firstTradeId);\n }\n\n if (_consumedSecondTrade != _secondTrade._amountIn) {\n _secondTrade._amountIn -= _consumedSecondTrade;\n } else {\n _removePendingTrade(_secondTrade._strategy, _secondTradeId);\n }\n\n emit AsyncTradesMatched(_firstTradeId, _secondTradeId, _consumedFirstTrade, _consumedSecondTrade);\n }\n}\n"
},
"@openzeppelin/contracts/utils/math/Math.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Standard math utilities missing in the Solidity language.\n */\nlibrary Math {\n /**\n * @dev Returns the largest of two numbers.\n */\n function max(uint256 a, uint256 b) internal pure returns (uint256) {\n return a >= b ? a : b;\n }\n\n /**\n * @dev Returns the smallest of two numbers.\n */\n function min(uint256 a, uint256 b) internal pure returns (uint256) {\n return a < b ? a : b;\n }\n\n /**\n * @dev Returns the average of two numbers. The result is rounded towards\n * zero.\n */\n function average(uint256 a, uint256 b) internal pure returns (uint256) {\n // (a + b) / 2 can overflow.\n return (a & b) + (a ^ b) / 2;\n }\n\n /**\n * @dev Returns the ceiling of the division of two numbers.\n *\n * This differs from standard division with `/` in that it rounds up instead\n * of rounding down.\n */\n function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {\n // (a + b - 1) / b can overflow on addition, so we distribute.\n return a / b + (a % b == 0 ? 0 : 1);\n }\n}\n"
},
"@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\nimport \"../IERC20.sol\";\nimport \"../../../utils/Address.sol\";\n\n/**\n * @title SafeERC20\n * @dev Wrappers around ERC20 operations that throw on failure (when the token\n * contract returns false). Tokens that return no value (and instead revert or\n * throw on failure) are also supported, non-reverting calls are assumed to be\n * successful.\n * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,\n * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.\n */\nlibrary SafeERC20 {\n using Address for address;\n\n function safeTransfer(\n IERC20 token,\n address to,\n uint256 value\n ) internal {\n _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));\n }\n\n function safeTransferFrom(\n IERC20 token,\n address from,\n address to,\n uint256 value\n ) internal {\n _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));\n }\n\n /**\n * @dev Deprecated. This function has issues similar to the ones found in\n * {IERC20-approve}, and its usage is discouraged.\n *\n * Whenever possible, use {safeIncreaseAllowance} and\n * {safeDecreaseAllowance} instead.\n */\n function safeApprove(\n IERC20 token,\n address spender,\n uint256 value\n ) internal {\n // safeApprove should only be called when setting an initial allowance,\n // or when resetting it to zero. To increase and decrease it, use\n // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'\n require(\n (value == 0) || (token.allowance(address(this), spender) == 0),\n \"SafeERC20: approve from non-zero to non-zero allowance\"\n );\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));\n }\n\n function safeIncreaseAllowance(\n IERC20 token,\n address spender,\n uint256 value\n ) internal {\n uint256 newAllowance = token.allowance(address(this), spender) + value;\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));\n }\n\n function safeDecreaseAllowance(\n IERC20 token,\n address spender,\n uint256 value\n ) internal {\n unchecked {\n uint256 oldAllowance = token.allowance(address(this), spender);\n require(oldAllowance >= value, \"SafeERC20: decreased allowance below zero\");\n uint256 newAllowance = oldAllowance - value;\n _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));\n }\n }\n\n /**\n * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement\n * on the return value: the return value is optional (but if data is returned, it must not be false).\n * @param token The token targeted by the call.\n * @param data The call data (encoded using abi.encode or one of its variants).\n */\n function _callOptionalReturn(IERC20 token, bytes memory data) private {\n // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since\n // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that\n // the target address contains contract code and also asserts for success in the low-level call.\n\n bytes memory returndata = address(token).functionCall(data, \"SafeERC20: low-level call failed\");\n if (returndata.length > 0) {\n // Return data is optional\n require(abi.decode(returndata, (bool)), \"SafeERC20: ERC20 operation did not succeed\");\n }\n }\n}\n"
},
"@openzeppelin/contracts/utils/structs/EnumerableSet.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Library for managing\n * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive\n * types.\n *\n * Sets have the following properties:\n *\n * - Elements are added, removed, and checked for existence in constant time\n * (O(1)).\n * - Elements are enumerated in O(n). No guarantees are made on the ordering.\n *\n * ```\n * contract Example {\n * // Add the library methods\n * using EnumerableSet for EnumerableSet.AddressSet;\n *\n * // Declare a set state variable\n * EnumerableSet.AddressSet private mySet;\n * }\n * ```\n *\n * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)\n * and `uint256` (`UintSet`) are supported.\n */\nlibrary EnumerableSet {\n // To implement this library for multiple types with as little code\n // repetition as possible, we write it in terms of a generic Set type with\n // bytes32 values.\n // The Set implementation uses private functions, and user-facing\n // implementations (such as AddressSet) are just wrappers around the\n // underlying Set.\n // This means that we can only create new EnumerableSets for types that fit\n // in bytes32.\n\n struct Set {\n // Storage of set values\n bytes32[] _values;\n // Position of the value in the `values` array, plus 1 because index 0\n // means a value is not in the set.\n mapping(bytes32 => uint256) _indexes;\n }\n\n /**\n * @dev Add a value to a set. O(1).\n *\n * Returns true if the value was added to the set, that is if it was not\n * already present.\n */\n function _add(Set storage set, bytes32 value) private returns (bool) {\n if (!_contains(set, value)) {\n set._values.push(value);\n // The value is stored at length-1, but we add 1 to all indexes\n // and use 0 as a sentinel value\n set._indexes[value] = set._values.length;\n return true;\n } else {\n return false;\n }\n }\n\n /**\n * @dev Removes a value from a set. O(1).\n *\n * Returns true if the value was removed from the set, that is if it was\n * present.\n */\n function _remove(Set storage set, bytes32 value) private returns (bool) {\n // We read and store the value's index to prevent multiple reads from the same storage slot\n uint256 valueIndex = set._indexes[value];\n\n if (valueIndex != 0) {\n // Equivalent to contains(set, value)\n // To delete an element from the _values array in O(1), we swap the element to delete with the last one in\n // the array, and then remove the last element (sometimes called as 'swap and pop').\n // This modifies the order of the array, as noted in {at}.\n\n uint256 toDeleteIndex = valueIndex - 1;\n uint256 lastIndex = set._values.length - 1;\n\n if (lastIndex != toDeleteIndex) {\n bytes32 lastvalue = set._values[lastIndex];\n\n // Move the last value to the index where the value to delete is\n set._values[toDeleteIndex] = lastvalue;\n // Update the index for the moved value\n set._indexes[lastvalue] = valueIndex; // Replace lastvalue's index to valueIndex\n }\n\n // Delete the slot where the moved value was stored\n set._values.pop();\n\n // Delete the index for the deleted slot\n delete set._indexes[value];\n\n return true;\n } else {\n return false;\n }\n }\n\n /**\n * @dev Returns true if the value is in the set. O(1).\n */\n function _contains(Set storage set, bytes32 value) private view returns (bool) {\n return set._indexes[value] != 0;\n }\n\n /**\n * @dev Returns the number of values on the set. O(1).\n */\n function _length(Set storage set) private view returns (uint256) {\n return set._values.length;\n }\n\n /**\n * @dev Returns the value stored at position `index` in the set. O(1).\n *\n * Note that there are no guarantees on the ordering of values inside the\n * array, and it may change when more values are added or removed.\n *\n * Requirements:\n *\n * - `index` must be strictly less than {length}.\n */\n function _at(Set storage set, uint256 index) private view returns (bytes32) {\n return set._values[index];\n }\n\n /**\n * @dev Return the entire set in an array\n *\n * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\n * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\n * this function has an unbounded cost, and using it as part of a state-changing function may render the function\n * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\n */\n function _values(Set storage set) private view returns (bytes32[] memory) {\n return set._values;\n }\n\n // Bytes32Set\n\n struct Bytes32Set {\n Set _inner;\n }\n\n /**\n * @dev Add a value to a set. O(1).\n *\n * Returns true if the value was added to the set, that is if it was not\n * already present.\n */\n function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {\n return _add(set._inner, value);\n }\n\n /**\n * @dev Removes a value from a set. O(1).\n *\n * Returns true if the value was removed from the set, that is if it was\n * present.\n */\n function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {\n return _remove(set._inner, value);\n }\n\n /**\n * @dev Returns true if the value is in the set. O(1).\n */\n function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {\n return _contains(set._inner, value);\n }\n\n /**\n * @dev Returns the number of values in the set. O(1).\n */\n function length(Bytes32Set storage set) internal view returns (uint256) {\n return _length(set._inner);\n }\n\n /**\n * @dev Returns the value stored at position `index` in the set. O(1).\n *\n * Note that there are no guarantees on the ordering of values inside the\n * array, and it may change when more values are added or removed.\n *\n * Requirements:\n *\n * - `index` must be strictly less than {length}.\n */\n function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {\n return _at(set._inner, index);\n }\n\n /**\n * @dev Return the entire set in an array\n *\n * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\n * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\n * this function has an unbounded cost, and using it as part of a state-changing function may render the function\n * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\n */\n function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {\n return _values(set._inner);\n }\n\n // AddressSet\n\n struct AddressSet {\n Set _inner;\n }\n\n /**\n * @dev Add a value to a set. O(1).\n *\n * Returns true if the value was added to the set, that is if it was not\n * already present.\n */\n function add(AddressSet storage set, address value) internal returns (bool) {\n return _add(set._inner, bytes32(uint256(uint160(value))));\n }\n\n /**\n * @dev Removes a value from a set. O(1).\n *\n * Returns true if the value was removed from the set, that is if it was\n * present.\n */\n function remove(AddressSet storage set, address value) internal returns (bool) {\n return _remove(set._inner, bytes32(uint256(uint160(value))));\n }\n\n /**\n * @dev Returns true if the value is in the set. O(1).\n */\n function contains(AddressSet storage set, address value) internal view returns (bool) {\n return _contains(set._inner, bytes32(uint256(uint160(value))));\n }\n\n /**\n * @dev Returns the number of values in the set. O(1).\n */\n function length(AddressSet storage set) internal view returns (uint256) {\n return _length(set._inner);\n }\n\n /**\n * @dev Returns the value stored at position `index` in the set. O(1).\n *\n * Note that there are no guarantees on the ordering of values inside the\n * array, and it may change when more values are added or removed.\n *\n * Requirements:\n *\n * - `index` must be strictly less than {length}.\n */\n function at(AddressSet storage set, uint256 index) internal view returns (address) {\n return address(uint160(uint256(_at(set._inner, index))));\n }\n\n /**\n * @dev Return the entire set in an array\n *\n * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\n * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\n * this function has an unbounded cost, and using it as part of a state-changing function may render the function\n * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\n */\n function values(AddressSet storage set) internal view returns (address[] memory) {\n bytes32[] memory store = _values(set._inner);\n address[] memory result;\n\n assembly {\n result := store\n }\n\n return result;\n }\n\n // UintSet\n\n struct UintSet {\n Set _inner;\n }\n\n /**\n * @dev Add a value to a set. O(1).\n *\n * Returns true if the value was added to the set, that is if it was not\n * already present.\n */\n function add(UintSet storage set, uint256 value) internal returns (bool) {\n return _add(set._inner, bytes32(value));\n }\n\n /**\n * @dev Removes a value from a set. O(1).\n *\n * Returns true if the value was removed from the set, that is if it was\n * present.\n */\n function remove(UintSet storage set, uint256 value) internal returns (bool) {\n return _remove(set._inner, bytes32(value));\n }\n\n /**\n * @dev Returns true if the value is in the set. O(1).\n */\n function contains(UintSet storage set, uint256 value) internal view returns (bool) {\n return _contains(set._inner, bytes32(value));\n }\n\n /**\n * @dev Returns the number of values on the set. O(1).\n */\n function length(UintSet storage set) internal view returns (uint256) {\n return _length(set._inner);\n }\n\n /**\n * @dev Returns the value stored at position `index` in the set. O(1).\n *\n * Note that there are no guarantees on the ordering of values inside the\n * array, and it may change when more values are added or removed.\n *\n * Requirements:\n *\n * - `index` must be strictly less than {length}.\n */\n function at(UintSet storage set, uint256 index) internal view returns (uint256) {\n return uint256(_at(set._inner, index));\n }\n\n /**\n * @dev Return the entire set in an array\n *\n * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed\n * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that\n * this function has an unbounded cost, and using it as part of a state-changing function may render the function\n * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.\n */\n function values(UintSet storage set) internal view returns (uint256[] memory) {\n bytes32[] memory store = _values(set._inner);\n uint256[] memory result;\n\n assembly {\n result := store\n }\n\n return result;\n }\n}\n"
},
"@yearn-contracts/contract-utils/solidity/contracts/utils/Machinery.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';\nimport '../interfaces/utils/IMachinery.sol';\nimport '../interfaces/mechanics/IMechanicsRegistry.sol';\n\ncontract Machinery is IMachinery {\n using EnumerableSet for EnumerableSet.AddressSet;\n\n IMechanicsRegistry internal _mechanicsRegistry;\n\n constructor(address __mechanicsRegistry) {\n _setMechanicsRegistry(__mechanicsRegistry);\n }\n\n modifier onlyMechanic() {\n require(_mechanicsRegistry.isMechanic(msg.sender), 'Machinery: not mechanic');\n _;\n }\n\n function setMechanicsRegistry(address __mechanicsRegistry) external virtual override {\n _setMechanicsRegistry(__mechanicsRegistry);\n }\n\n function _setMechanicsRegistry(address __mechanicsRegistry) internal {\n _mechanicsRegistry = IMechanicsRegistry(__mechanicsRegistry);\n }\n\n // View helpers\n function mechanicsRegistry() external view override returns (address _mechanicRegistry) {\n return address(_mechanicsRegistry);\n }\n\n function isMechanic(address _mechanic) public view override returns (bool _isMechanic) {\n return _mechanicsRegistry.isMechanic(_mechanic);\n }\n}\n"
},
"contracts/swappers/async/AsyncSwapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '../Swapper.sol';\n\ninterface IAsyncSwapper is ISwapper {\n event Swapped(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _minAmountOut,\n uint256 _receivedAmount,\n bytes _data\n );\n\n error InvalidAmountOut();\n\n function swap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _minAmountOut,\n bytes calldata _data\n ) external returns (uint256 _receivedAmount);\n}\n\nabstract contract AsyncSwapper is IAsyncSwapper, Swapper {\n // solhint-disable-next-line var-name-mixedcase\n SwapperType public constant override SWAPPER_TYPE = SwapperType.ASYNC;\n\n constructor(address _governor, address _tradeFactory) Governable(_governor) Swapper(_tradeFactory) {}\n\n function _assertPreSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _minAmountOut\n ) internal pure {\n if (_receiver == address(0) || _tokenIn == address(0) || _tokenOut == address(0)) revert CommonErrors.ZeroAddress();\n if (_amountIn == 0) revert CommonErrors.ZeroAmount();\n if (_minAmountOut == 0) revert CommonErrors.ZeroAmount();\n }\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n bytes calldata _data\n ) internal virtual returns (uint256 _receivedAmount);\n\n function swap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _minAmountOut,\n bytes calldata _data\n ) external virtual override onlyTradeFactory returns (uint256 _receivedAmount) {\n _assertPreSwap(_receiver, _tokenIn, _tokenOut, _amountIn, _minAmountOut);\n uint256 _preExecutionBalance = IERC20(_tokenOut).balanceOf(_receiver);\n _receivedAmount = _executeSwap(_receiver, _tokenIn, _tokenOut, _amountIn, _data);\n if (IERC20(_tokenOut).balanceOf(_receiver) - _preExecutionBalance < _minAmountOut) revert InvalidAmountOut();\n emit Swapped(_receiver, _tokenIn, _tokenOut, _amountIn, _minAmountOut, _receivedAmount, _data);\n }\n}\n"
},
"contracts/swappers/sync/SyncSwapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '../Swapper.sol';\n\ninterface ISyncSwapper is ISwapper {\n event Swapped(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n uint256 _receivedAmount,\n bytes _data\n );\n\n // solhint-disable-next-line func-name-mixedcase\n function SLIPPAGE_PRECISION() external view returns (uint256);\n\n function swap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n bytes calldata _data\n ) external returns (uint256 _receivedAmount);\n}\n\nabstract contract SyncSwapper is ISyncSwapper, Swapper {\n // solhint-disable-next-line var-name-mixedcase\n uint256 public immutable override SLIPPAGE_PRECISION = 10000; // 1 is 0.0001%, 1_000 is 0.1%\n\n // solhint-disable-next-line var-name-mixedcase\n SwapperType public constant override SWAPPER_TYPE = SwapperType.SYNC;\n\n constructor(address _governor, address _tradeFactory) Governable(_governor) Swapper(_tradeFactory) {}\n\n function _assertPreSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage\n ) internal pure {\n if (_receiver == address(0) || _tokenIn == address(0) || _tokenOut == address(0)) revert CommonErrors.ZeroAddress();\n if (_amountIn == 0) revert CommonErrors.ZeroAmount();\n if (_maxSlippage == 0) revert CommonErrors.ZeroSlippage();\n }\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n bytes calldata _data\n ) internal virtual returns (uint256 _receivedAmount);\n\n function swap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n bytes calldata _data\n ) external virtual override onlyTradeFactory returns (uint256 _receivedAmount) {\n _assertPreSwap(_receiver, _tokenIn, _tokenOut, _amountIn, _maxSlippage);\n _receivedAmount = _executeSwap(_receiver, _tokenIn, _tokenOut, _amountIn, _maxSlippage, _data);\n emit Swapped(_receiver, _tokenIn, _tokenOut, _amountIn, _maxSlippage, _receivedAmount, _data);\n }\n}\n"
},
"contracts/OTCPool.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/token/ERC20/IERC20.sol';\nimport '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';\n\nimport '@yearn-contracts/contract-utils/solidity/contracts/utils/Governable.sol';\nimport '@yearn-contracts/contract-utils/solidity/contracts/utils/CollectableDust.sol';\n\nimport './CommonErrors.sol';\n\ninterface IOTCPool is IGovernable {\n event TradeFactorySet(address indexed _tradeFactory);\n\n event OfferCreate(address indexed _offeredToken, uint256 _amount);\n\n event OfferTaken(address indexed _wantedToken, uint256 _amount, address _receiver);\n\n function tradeFactory() external view returns (address);\n\n function offers(address) external view returns (uint256);\n\n function setTradeFactory(address _tradeFactory) external;\n\n function create(address _offeredToken, uint256 _amount) external;\n\n function take(\n address _wantedToken,\n uint256 _amount,\n address _receiver\n ) external;\n}\n\ncontract OTCPool is IOTCPool, CollectableDust, Governable {\n using SafeERC20 for IERC20;\n\n address public override tradeFactory;\n\n mapping(address => uint256) public override offers;\n\n constructor(address _governor, address _tradeFactory) Governable(_governor) {\n if (_tradeFactory == address(0)) revert CommonErrors.ZeroAddress();\n tradeFactory = _tradeFactory;\n }\n\n modifier onlyTradeFactory() {\n if (msg.sender != tradeFactory) revert CommonErrors.NotAuthorized();\n _;\n }\n\n function setTradeFactory(address _tradeFactory) external override onlyGovernor {\n if (_tradeFactory == address(0)) revert CommonErrors.ZeroAddress();\n tradeFactory = _tradeFactory;\n emit TradeFactorySet(_tradeFactory);\n }\n\n function create(address _offeredToken, uint256 _amount) external override onlyGovernor {\n if (_offeredToken == address(0)) revert CommonErrors.ZeroAddress();\n if (_amount == 0) revert CommonErrors.ZeroAmount();\n if (IERC20(_offeredToken).allowance(governor, address(this)) < offers[_offeredToken]) revert CommonErrors.NotAuthorized();\n offers[_offeredToken] += _amount;\n emit OfferCreate(_offeredToken, _amount);\n }\n\n function take(\n address _wantedToken,\n uint256 _amount,\n address _receiver\n ) external override onlyTradeFactory {\n // No checks more than permission are made, since every argument should be already valid\n IERC20(_wantedToken).safeTransferFrom(governor, _receiver, _amount);\n offers[_wantedToken] -= _amount;\n emit OfferTaken(_wantedToken, _amount, _receiver);\n }\n\n // CollectableDust\n function sendDust(\n address _to,\n address _token,\n uint256 _amount\n ) external override onlyGovernor {\n _sendDust(_to, _token, _amount);\n }\n}\n"
},
"contracts/TradeFactory/TradeFactoryPositionsHandler.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';\nimport './TradeFactorySwapperHandler.sol';\n\ninterface ITradeFactoryPositionsHandler {\n struct Trade {\n uint256 _id;\n address _strategy;\n address _tokenIn;\n address _tokenOut;\n uint256 _amountIn;\n uint256 _deadline;\n }\n\n event TradeCreated(uint256 indexed _id, address _strategy, address _tokenIn, address _tokenOut, uint256 _amountIn, uint256 _deadline);\n\n event TradesCanceled(address indexed _strategy, uint256[] _ids);\n\n event TradesSwapperChanged(uint256[] _ids, address _newSwapper);\n\n event TradesMerged(uint256 indexed _anchorTrade, uint256[] _ids);\n\n error InvalidTrade();\n\n error InvalidDeadline();\n\n function pendingTradesById(uint256)\n external\n view\n returns (\n uint256 _id,\n address _strategy,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _deadline\n );\n\n function pendingTradesIds() external view returns (uint256[] memory _pendingIds);\n\n function pendingTradesIds(address _strategy) external view returns (uint256[] memory _pendingIds);\n\n function create(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _deadline\n ) external returns (uint256 _id);\n\n function cancelPendingTrades(uint256[] calldata _ids) external;\n\n function mergePendingTrades(uint256 _anchorTradeId, uint256[] calldata _toMergeIds) external;\n}\n\nabstract contract TradeFactoryPositionsHandler is ITradeFactoryPositionsHandler, TradeFactorySwapperHandler {\n using EnumerableSet for EnumerableSet.UintSet;\n using EnumerableSet for EnumerableSet.AddressSet;\n\n bytes32 public constant STRATEGY = keccak256('STRATEGY');\n bytes32 public constant STRATEGY_ADDER = keccak256('STRATEGY_ADDER');\n bytes32 public constant TRADES_MODIFIER = keccak256('TRADES_MODIFIER');\n\n uint256 private _tradeCounter = 1;\n\n mapping(uint256 => Trade) public override pendingTradesById;\n\n EnumerableSet.UintSet internal _pendingTradesIds;\n\n mapping(address => EnumerableSet.UintSet) internal _pendingTradesByOwner;\n\n constructor(address _strategyAdder, address _tradesModifier) {\n if (_strategyAdder == address(0) || _tradesModifier == address(0)) revert CommonErrors.ZeroAddress();\n _setRoleAdmin(STRATEGY, STRATEGY_ADDER);\n _setRoleAdmin(STRATEGY_ADDER, MASTER_ADMIN);\n _setupRole(STRATEGY_ADDER, _strategyAdder);\n _setRoleAdmin(TRADES_MODIFIER, MASTER_ADMIN);\n _setupRole(TRADES_MODIFIER, _tradesModifier);\n }\n\n function pendingTradesIds() external view override returns (uint256[] memory _pendingIds) {\n _pendingIds = _pendingTradesIds.values();\n }\n\n function pendingTradesIds(address _strategy) external view override returns (uint256[] memory _pendingIds) {\n _pendingIds = _pendingTradesByOwner[_strategy].values();\n }\n\n function create(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _deadline\n ) external override onlyRole(STRATEGY) returns (uint256 _id) {\n if (_tokenIn == address(0) || _tokenOut == address(0)) revert CommonErrors.ZeroAddress();\n if (_amountIn == 0) revert CommonErrors.ZeroAmount();\n if (_deadline <= block.timestamp) revert InvalidDeadline();\n _id = _tradeCounter;\n Trade memory _trade = Trade(_tradeCounter, msg.sender, _tokenIn, _tokenOut, _amountIn, _deadline);\n pendingTradesById[_trade._id] = _trade;\n _pendingTradesByOwner[msg.sender].add(_trade._id);\n _pendingTradesIds.add(_trade._id);\n _tradeCounter += 1;\n emit TradeCreated(_trade._id, _trade._strategy, _trade._tokenIn, _trade._tokenOut, _trade._amountIn, _trade._deadline);\n }\n\n function cancelPendingTrades(uint256[] calldata _ids) external override onlyRole(STRATEGY) {\n for (uint256 i; i < _ids.length; i++) {\n if (!_pendingTradesIds.contains(_ids[i])) revert InvalidTrade();\n if (pendingTradesById[_ids[i]]._strategy != msg.sender) revert CommonErrors.NotAuthorized();\n _removePendingTrade(msg.sender, _ids[i]);\n }\n emit TradesCanceled(msg.sender, _ids);\n }\n\n function mergePendingTrades(uint256 _anchorTradeId, uint256[] calldata _toMergeIds) external override onlyRole(TRADES_MODIFIER) {\n Trade storage _anchorTrade = pendingTradesById[_anchorTradeId];\n for (uint256 i; i < _toMergeIds.length; i++) {\n Trade storage _trade = pendingTradesById[_toMergeIds[i]];\n if (\n _anchorTrade._id == _trade._id ||\n _anchorTrade._strategy != _trade._strategy ||\n _anchorTrade._tokenIn != _trade._tokenIn ||\n _anchorTrade._tokenOut != _trade._tokenOut\n ) revert InvalidTrade();\n _anchorTrade._amountIn += _trade._amountIn;\n _removePendingTrade(_trade._strategy, _trade._id);\n }\n emit TradesMerged(_anchorTradeId, _toMergeIds);\n }\n\n function _removePendingTrade(address _strategy, uint256 _id) internal {\n _pendingTradesByOwner[_strategy].remove(_id);\n _pendingTradesIds.remove(_id);\n delete pendingTradesById[_id];\n }\n}\n"
},
"@openzeppelin/contracts/utils/Address.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity ^0.8.0;\n\n/**\n * @dev Collection of functions related to the address type\n */\nlibrary Address {\n /**\n * @dev Returns true if `account` is a contract.\n *\n * [IMPORTANT]\n * ====\n * It is unsafe to assume that an address for which this function returns\n * false is an externally-owned account (EOA) and not a contract.\n *\n * Among others, `isContract` will return false for the following\n * types of addresses:\n *\n * - an externally-owned account\n * - a contract in construction\n * - an address where a contract will be created\n * - an address where a contract lived, but was destroyed\n * ====\n */\n function isContract(address account) internal view returns (bool) {\n // This method relies on extcodesize, which returns 0 for contracts in\n // construction, since the code is only stored at the end of the\n // constructor execution.\n\n uint256 size;\n assembly {\n size := extcodesize(account)\n }\n return size > 0;\n }\n\n /**\n * @dev Replacement for Solidity's `transfer`: sends `amount` wei to\n * `recipient`, forwarding all available gas and reverting on errors.\n *\n * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost\n * of certain opcodes, possibly making contracts go over the 2300 gas limit\n * imposed by `transfer`, making them unable to receive funds via\n * `transfer`. {sendValue} removes this limitation.\n *\n * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].\n *\n * IMPORTANT: because control is transferred to `recipient`, care must be\n * taken to not create reentrancy vulnerabilities. Consider using\n * {ReentrancyGuard} or the\n * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].\n */\n function sendValue(address payable recipient, uint256 amount) internal {\n require(address(this).balance >= amount, \"Address: insufficient balance\");\n\n (bool success, ) = recipient.call{value: amount}(\"\");\n require(success, \"Address: unable to send value, recipient may have reverted\");\n }\n\n /**\n * @dev Performs a Solidity function call using a low level `call`. A\n * plain `call` is an unsafe replacement for a function call: use this\n * function instead.\n *\n * If `target` reverts with a revert reason, it is bubbled up by this\n * function (like regular Solidity function calls).\n *\n * Returns the raw returned data. To convert to the expected return value,\n * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].\n *\n * Requirements:\n *\n * - `target` must be a contract.\n * - calling `target` with `data` must not revert.\n *\n * _Available since v3.1._\n */\n function functionCall(address target, bytes memory data) internal returns (bytes memory) {\n return functionCall(target, data, \"Address: low-level call failed\");\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with\n * `errorMessage` as a fallback revert reason when `target` reverts.\n *\n * _Available since v3.1._\n */\n function functionCall(\n address target,\n bytes memory data,\n string memory errorMessage\n ) internal returns (bytes memory) {\n return functionCallWithValue(target, data, 0, errorMessage);\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\n * but also transferring `value` wei to `target`.\n *\n * Requirements:\n *\n * - the calling contract must have an ETH balance of at least `value`.\n * - the called Solidity function must be `payable`.\n *\n * _Available since v3.1._\n */\n function functionCallWithValue(\n address target,\n bytes memory data,\n uint256 value\n ) internal returns (bytes memory) {\n return functionCallWithValue(target, data, value, \"Address: low-level call with value failed\");\n }\n\n /**\n * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but\n * with `errorMessage` as a fallback revert reason when `target` reverts.\n *\n * _Available since v3.1._\n */\n function functionCallWithValue(\n address target,\n bytes memory data,\n uint256 value,\n string memory errorMessage\n ) internal returns (bytes memory) {\n require(address(this).balance >= value, \"Address: insufficient balance for call\");\n require(isContract(target), \"Address: call to non-contract\");\n\n (bool success, bytes memory returndata) = target.call{value: value}(data);\n return verifyCallResult(success, returndata, errorMessage);\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\n * but performing a static call.\n *\n * _Available since v3.3._\n */\n function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {\n return functionStaticCall(target, data, \"Address: low-level static call failed\");\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],\n * but performing a static call.\n *\n * _Available since v3.3._\n */\n function functionStaticCall(\n address target,\n bytes memory data,\n string memory errorMessage\n ) internal view returns (bytes memory) {\n require(isContract(target), \"Address: static call to non-contract\");\n\n (bool success, bytes memory returndata) = target.staticcall(data);\n return verifyCallResult(success, returndata, errorMessage);\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],\n * but performing a delegate call.\n *\n * _Available since v3.4._\n */\n function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {\n return functionDelegateCall(target, data, \"Address: low-level delegate call failed\");\n }\n\n /**\n * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],\n * but performing a delegate call.\n *\n * _Available since v3.4._\n */\n function functionDelegateCall(\n address target,\n bytes memory data,\n string memory errorMessage\n ) internal returns (bytes memory) {\n require(isContract(target), \"Address: delegate call to non-contract\");\n\n (bool success, bytes memory returndata) = target.delegatecall(data);\n return verifyCallResult(success, returndata, errorMessage);\n }\n\n /**\n * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the\n * revert reason using the provided one.\n *\n * _Available since v4.3._\n */\n function verifyCallResult(\n bool success,\n bytes memory returndata,\n string memory errorMessage\n ) internal pure returns (bytes memory) {\n if (success) {\n return returndata;\n } else {\n // Look for revert reason and bubble it up if present\n if (returndata.length > 0) {\n // The easiest way to bubble the revert reason is using memory via assembly\n\n assembly {\n let returndata_size := mload(returndata)\n revert(add(32, returndata), returndata_size)\n }\n } else {\n revert(errorMessage);\n }\n }\n }\n}\n"
},
"@yearn-contracts/contract-utils/solidity/contracts/interfaces/utils/IMachinery.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\ninterface IMachinery {\n // View helpers\n function mechanicsRegistry() external view returns (address _mechanicsRegistry);\n\n function isMechanic(address mechanic) external view returns (bool _isMechanic);\n\n // Setters\n function setMechanicsRegistry(address _mechanicsRegistry) external;\n}\n"
},
"@yearn-contracts/contract-utils/solidity/contracts/interfaces/mechanics/IMechanicsRegistry.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\ninterface IMechanicsRegistry {\n event MechanicAdded(address _mechanic);\n event MechanicRemoved(address _mechanic);\n\n function addMechanic(address _mechanic) external;\n\n function removeMechanic(address _mechanic) external;\n\n function mechanics() external view returns (address[] memory _mechanicsList);\n\n function isMechanic(address mechanic) external view returns (bool _isMechanic);\n}\n"
},
"contracts/swappers/Swapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/token/ERC20/IERC20.sol';\nimport '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';\n\nimport '@yearn-contracts/contract-utils/solidity/contracts/utils/Governable.sol';\nimport '@yearn-contracts/contract-utils/solidity/contracts/utils/CollectableDust.sol';\n\nimport '../CommonErrors.sol';\n\ninterface ISwapper {\n enum SwapperType {\n ASYNC,\n SYNC\n }\n\n // solhint-disable-next-line func-name-mixedcase\n function TRADE_FACTORY() external view returns (address);\n\n // solhint-disable-next-line func-name-mixedcase\n function SWAPPER_TYPE() external view returns (SwapperType);\n}\n\nabstract contract Swapper is ISwapper, Governable, CollectableDust {\n using SafeERC20 for IERC20;\n\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override TRADE_FACTORY;\n\n constructor(address _tradeFactory) {\n if (_tradeFactory == address(0)) revert CommonErrors.ZeroAddress();\n TRADE_FACTORY = _tradeFactory;\n }\n\n modifier onlyTradeFactory() {\n if (msg.sender != TRADE_FACTORY) revert CommonErrors.NotAuthorized();\n _;\n }\n\n function sendDust(\n address _to,\n address _token,\n uint256 _amount\n ) external virtual override onlyGovernor {\n _sendDust(_to, _token, _amount);\n }\n}\n"
},
"@yearn-contracts/contract-utils/solidity/contracts/utils/Governable.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '../interfaces/utils/IGovernable.sol';\n\ncontract Governable is IGovernable {\n address public override governor;\n address public override pendingGovernor;\n\n constructor(address _governor) {\n require(_governor != address(0), 'governable/governor-should-not-be-zero-address');\n governor = _governor;\n }\n\n function setPendingGovernor(address _pendingGovernor) external virtual override onlyGovernor {\n _setPendingGovernor(_pendingGovernor);\n }\n\n function acceptGovernor() external virtual override onlyPendingGovernor {\n _acceptGovernor();\n }\n\n function _setPendingGovernor(address _pendingGovernor) internal {\n require(_pendingGovernor != address(0), 'governable/pending-governor-should-not-be-zero-addres');\n pendingGovernor = _pendingGovernor;\n emit PendingGovernorSet(_pendingGovernor);\n }\n\n function _acceptGovernor() internal {\n governor = pendingGovernor;\n pendingGovernor = address(0);\n emit GovernorAccepted();\n }\n\n function isGovernor(address _account) public view override returns (bool _isGovernor) {\n return _account == governor;\n }\n\n modifier onlyGovernor() {\n require(isGovernor(msg.sender), 'governable/only-governor');\n _;\n }\n\n modifier onlyPendingGovernor() {\n require(msg.sender == pendingGovernor, 'governable/only-pending-governor');\n _;\n }\n}\n"
},
"@yearn-contracts/contract-utils/solidity/contracts/utils/CollectableDust.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';\nimport '@openzeppelin/contracts/token/ERC20/IERC20.sol';\nimport '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';\n\nimport '../interfaces/utils/ICollectableDust.sol';\n\nabstract contract CollectableDust is ICollectableDust {\n using SafeERC20 for IERC20;\n using EnumerableSet for EnumerableSet.AddressSet;\n\n address public constant ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;\n EnumerableSet.AddressSet internal protocolTokens;\n\n constructor() {}\n\n function _addProtocolToken(address _token) internal {\n require(!protocolTokens.contains(_token), 'collectable-dust/token-is-part-of-the-protocol');\n protocolTokens.add(_token);\n }\n\n function _removeProtocolToken(address _token) internal {\n require(protocolTokens.contains(_token), 'collectable-dust/token-not-part-of-the-protocol');\n protocolTokens.remove(_token);\n }\n\n function _sendDust(\n address _to,\n address _token,\n uint256 _amount\n ) internal {\n require(_to != address(0), 'collectable-dust/cant-send-dust-to-zero-address');\n require(!protocolTokens.contains(_token), 'collectable-dust/token-is-part-of-the-protocol');\n if (_token == ETH_ADDRESS) {\n payable(_to).transfer(_amount);\n } else {\n IERC20(_token).safeTransfer(_to, _amount);\n }\n emit DustSent(_to, _token, _amount);\n }\n}\n"
},
"@yearn-contracts/contract-utils/solidity/contracts/interfaces/utils/IGovernable.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\ninterface IGovernable {\n event PendingGovernorSet(address pendingGovernor);\n event GovernorAccepted();\n\n function setPendingGovernor(address _pendingGovernor) external;\n\n function acceptGovernor() external;\n\n function governor() external view returns (address _governor);\n\n function pendingGovernor() external view returns (address _pendingGovernor);\n\n function isGovernor(address _account) external view returns (bool _isGovernor);\n}\n"
},
"@yearn-contracts/contract-utils/solidity/contracts/interfaces/utils/ICollectableDust.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\ninterface ICollectableDust {\n event DustSent(address _to, address token, uint256 amount);\n\n function sendDust(\n address _to,\n address _token,\n uint256 _amount\n ) external;\n}\n"
},
"contracts/TradeFactory/TradeFactorySwapperHandler.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/access/AccessControl.sol';\nimport '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';\n\nimport '../swappers/Swapper.sol';\n\nimport './TradeFactoryAccessManager.sol';\n\ninterface ITradeFactorySwapperHandler {\n event SyncStrategySwapperSet(address indexed _strategy, address _swapper);\n event AsyncStrategySwapperSet(address indexed _strategy, address _swapper);\n event StrategyPermissionsSet(address indexed _strategy, bytes1 _permissions);\n event OTCPoolSet(address _otcPool);\n event SwappersAdded(address[] _swappers);\n event SwappersRemoved(address[] _swapper);\n\n error NotAsyncSwapper();\n error NotSyncSwapper();\n error InvalidSwapper();\n error InvalidPermissions();\n error SwapperInUse();\n\n function strategySyncSwapper(address _strategy) external view returns (address _swapper);\n\n function strategyPermissions(address _strategy) external view returns (bytes1 _permissions);\n\n function swappers() external view returns (address[] memory _swappersList);\n\n function isSwapper(address _swapper) external view returns (bool _isSwapper);\n\n function swapperStrategies(address _swapper) external view returns (address[] memory _strategies);\n\n function setStrategyPermissions(address _strategy, bytes1 _permissions) external;\n\n function setOTCPool(address _otcPool) external;\n\n function setStrategySyncSwapper(address _strategy, address _swapper) external;\n\n function addSwappers(address[] memory __swappers) external;\n\n function removeSwappers(address[] memory __swappers) external;\n}\n\nabstract contract TradeFactorySwapperHandler is ITradeFactorySwapperHandler, TradeFactoryAccessManager {\n using EnumerableSet for EnumerableSet.AddressSet;\n\n bytes32 public constant SWAPPER_ADDER = keccak256('SWAPPER_ADDER');\n bytes32 public constant SWAPPER_SETTER = keccak256('SWAPPER_SETTER');\n\n bytes1 internal constant _OTC_MASK = 0x01;\n bytes1 internal constant _COW_MASK = 0x02;\n\n // OTC Handler\n address public otcPool;\n // swappers list\n EnumerableSet.AddressSet internal _swappers;\n // swapper -> strategy list (useful to know if we can safely deprecate a swapper)\n mapping(address => EnumerableSet.AddressSet) internal _swapperStrategies;\n // strategy -> sync swapper\n mapping(address => address) public override strategySyncSwapper;\n // strategy -> permissions\n // permissions[_OTC_PERMISSION_INDEX] => OTC\n // permissions[_COW_PERMISSION_INDEX] => COW\n mapping(address => bytes1) public override strategyPermissions;\n\n constructor(address _swapperAdder, address _swapperSetter) {\n if (_swapperAdder == address(0) || _swapperSetter == address(0)) revert CommonErrors.ZeroAddress();\n _setRoleAdmin(SWAPPER_ADDER, MASTER_ADMIN);\n _setRoleAdmin(SWAPPER_SETTER, MASTER_ADMIN);\n _setupRole(SWAPPER_ADDER, _swapperAdder);\n _setupRole(SWAPPER_SETTER, _swapperSetter);\n }\n\n function isSwapper(address _swapper) external view override returns (bool _isSwapper) {\n _isSwapper = _swappers.contains(_swapper);\n }\n\n function swappers() external view override returns (address[] memory _swappersList) {\n _swappersList = _swappers.values();\n }\n\n function swapperStrategies(address _swapper) external view override returns (address[] memory _strategies) {\n _strategies = _swapperStrategies[_swapper].values();\n }\n\n function setStrategyPermissions(address _strategy, bytes1 _permissions) external override onlyRole(SWAPPER_SETTER) {\n if (_strategy == address(0)) revert CommonErrors.ZeroAddress();\n strategyPermissions[_strategy] = _permissions;\n emit StrategyPermissionsSet(_strategy, _permissions);\n }\n\n function setOTCPool(address _otcPool) external override onlyRole(MASTER_ADMIN) {\n if (_otcPool == address(0)) revert CommonErrors.ZeroAddress();\n otcPool = _otcPool;\n emit OTCPoolSet(_otcPool);\n }\n\n function setStrategySyncSwapper(address _strategy, address _swapper) external override onlyRole(SWAPPER_SETTER) {\n if (_strategy == address(0) || _swapper == address(0)) revert CommonErrors.ZeroAddress();\n // we check that swapper being added is async\n if (ISwapper(_swapper).SWAPPER_TYPE() != ISwapper.SwapperType.SYNC) revert NotSyncSwapper();\n // we check that swapper is not already added\n if (!_swappers.contains(_swapper)) revert InvalidSwapper();\n // remove strategy from previous swapper if any\n if (strategySyncSwapper[_strategy] != address(0)) _swapperStrategies[strategySyncSwapper[_strategy]].remove(_strategy);\n // set new strategy's sync swapper\n strategySyncSwapper[_strategy] = _swapper;\n // add strategy into new swapper\n _swapperStrategies[_swapper].add(_strategy);\n // emit event\n emit SyncStrategySwapperSet(_strategy, _swapper);\n }\n\n function addSwappers(address[] memory __swappers) external override onlyRole(SWAPPER_ADDER) {\n for (uint256 i; i < __swappers.length; i++) {\n if (__swappers[i] == address(0)) revert CommonErrors.ZeroAddress();\n _swappers.add(__swappers[i]);\n }\n emit SwappersAdded(__swappers);\n }\n\n function removeSwappers(address[] memory __swappers) external override onlyRole(SWAPPER_ADDER) {\n for (uint256 i; i < __swappers.length; i++) {\n if (_swapperStrategies[__swappers[i]].length() > 0) revert SwapperInUse();\n _swappers.remove(__swappers[i]);\n }\n emit SwappersRemoved(__swappers);\n }\n}\n"
},
"contracts/TradeFactory/TradeFactory.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@yearn-contracts/contract-utils/solidity/contracts/utils/CollectableDust.sol';\n\nimport './TradeFactoryPositionsHandler.sol';\nimport './TradeFactoryExecutor.sol';\n\ninterface ITradeFactory is ITradeFactoryExecutor, ITradeFactoryPositionsHandler {}\n\ncontract TradeFactory is TradeFactoryExecutor, CollectableDust, ITradeFactory {\n constructor(\n address _masterAdmin,\n address _swapperAdder,\n address _swapperSetter,\n address _strategyAdder,\n address _tradesModifier,\n address _tradesSettler,\n address _mechanicsRegistry\n )\n TradeFactoryAccessManager(_masterAdmin)\n TradeFactoryPositionsHandler(_strategyAdder, _tradesModifier)\n TradeFactorySwapperHandler(_swapperAdder, _swapperSetter)\n TradeFactoryExecutor(_tradesSettler, _mechanicsRegistry)\n {}\n\n // Collectable Dust\n function sendDust(\n address _to,\n address _token,\n uint256 _amount\n ) external virtual override onlyRole(MASTER_ADMIN) {\n _sendDust(_to, _token, _amount);\n }\n}\n"
},
"contracts/utils/SwapperEnabled.sol": {
"content": "// SPDX-License-Identifier: MIT\n\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';\n\nimport '../TradeFactory/TradeFactory.sol';\n\ninterface ISwapperEnabled {\n event TradeFactorySet(address indexed _tradeFactory);\n event SwapperSet(string indexed _swapper);\n\n function tradeFactory() external returns (address _tradeFactory);\n\n function swapper() external returns (string memory _swapper);\n\n function setSwapper(string calldata _swapper, bool _migrateSwaps) external;\n\n function setTradeFactory(address _tradeFactory) external;\n\n function createTrade(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _deadline\n ) external returns (uint256 _id);\n\n function executeTrade(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn\n ) external returns (uint256 _receivedAmount);\n\n function executeTrade(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n bytes calldata _data\n ) external returns (uint256 _receivedAmount);\n\n function cancelPendingTrades(uint256[] calldata _pendingTrades) external;\n}\n\n/*\n * SwapperEnabled Abstract\n */\nabstract contract SwapperEnabled is ISwapperEnabled {\n using SafeERC20 for IERC20;\n\n address public override tradeFactory;\n\n constructor(address _tradeFactory) {\n _setTradeFactory(_tradeFactory);\n }\n\n // onlyMultisig:\n function _setTradeFactory(address _tradeFactory) internal {\n tradeFactory = _tradeFactory;\n emit TradeFactorySet(_tradeFactory);\n }\n\n // onlyMultisig or internal use:\n function _createTrade(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _deadline\n ) internal returns (uint256 _id) {\n IERC20(_tokenIn).safeIncreaseAllowance(tradeFactory, _amountIn);\n return ITradeFactoryPositionsHandler(tradeFactory).create(_tokenIn, _tokenOut, _amountIn, _deadline);\n }\n\n function _executeTrade(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage\n ) internal returns (uint256 _receivedAmount) {\n IERC20(_tokenIn).safeIncreaseAllowance(tradeFactory, _amountIn);\n return ITradeFactoryExecutor(tradeFactory).execute(_tokenIn, _tokenOut, _amountIn, _maxSlippage, '');\n }\n\n function _executeTrade(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n bytes calldata _data\n ) internal returns (uint256 _receivedAmount) {\n IERC20(_tokenIn).safeIncreaseAllowance(tradeFactory, _amountIn);\n return ITradeFactoryExecutor(tradeFactory).execute(_tokenIn, _tokenOut, _amountIn, _maxSlippage, _data);\n }\n\n // onlyStrategist or multisig:\n function _cancelPendingTrades(uint256[] calldata _tradesIds) internal {\n for (uint256 i; i < _tradesIds.length; i++) {\n (, , address _tokenIn, , uint256 _amountIn, ) = ITradeFactoryPositionsHandler(tradeFactory).pendingTradesById(_tradesIds[i]);\n IERC20(_tokenIn).safeDecreaseAllowance(tradeFactory, _amountIn);\n }\n ITradeFactoryPositionsHandler(tradeFactory).cancelPendingTrades(_tradesIds);\n }\n\n function _tradeFactoryAllowance(address _token) internal view returns (uint256 _allowance) {\n return IERC20(_token).allowance(address(this), tradeFactory);\n }\n}\n"
},
"contracts/swappers/sync/UniswapV2Swapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol';\nimport '@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol';\nimport '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';\n\nimport './SyncSwapper.sol';\n\ninterface IUniswapV2Swapper is ISyncSwapper {\n // solhint-disable-next-line func-name-mixedcase\n function WETH() external view returns (address);\n\n // solhint-disable-next-line func-name-mixedcase\n function FACTORY() external view returns (address);\n\n // solhint-disable-next-line func-name-mixedcase\n function ROUTER() external view returns (address);\n}\n\ncontract UniswapV2Swapper is IUniswapV2Swapper, SyncSwapper {\n using SafeERC20 for IERC20;\n\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override WETH;\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override FACTORY;\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override ROUTER;\n\n constructor(\n address _governor,\n address _tradeFactory,\n address _weth,\n address _factory,\n address _router\n ) SyncSwapper(_governor, _tradeFactory) {\n WETH = _weth;\n FACTORY = _factory;\n ROUTER = _router;\n }\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n bytes calldata _data\n ) internal override returns (uint256 _receivedAmount) {\n address[] memory _path;\n uint256 _amountOut;\n if (_data.length > 0) {\n _path = abi.decode(_data, (address[]));\n _amountOut = IUniswapV2Router02(ROUTER).getAmountsOut(_amountIn, _path)[_path.length - 1];\n } else {\n (_path, _amountOut) = _getPathAndAmountOut(_tokenIn, _tokenOut, _amountIn);\n }\n IERC20(_path[0]).approve(ROUTER, 0);\n IERC20(_path[0]).approve(ROUTER, _amountIn);\n _receivedAmount = IUniswapV2Router02(ROUTER).swapExactTokensForTokens(\n _amountIn,\n _amountOut - ((_amountOut * _maxSlippage) / SLIPPAGE_PRECISION / 100), // slippage calcs\n _path,\n _receiver,\n block.timestamp\n )[_path.length - 1];\n }\n\n function _getPathAndAmountOut(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn\n ) internal view returns (address[] memory _path, uint256 _amountOut) {\n uint256 _amountOutByDirectPath;\n address[] memory _directPath;\n\n if (IUniswapV2Factory(FACTORY).getPair(_tokenIn, _tokenOut) != address(0)) {\n _directPath = new address[](2);\n _directPath[0] = _tokenIn;\n _directPath[1] = _tokenOut;\n _amountOutByDirectPath = IUniswapV2Router02(ROUTER).getAmountsOut(_amountIn, _directPath)[1];\n }\n\n uint256 _amountOutByWETHHopPath;\n // solhint-disable-next-line var-name-mixedcase\n address[] memory _WETHHopPath;\n if (IUniswapV2Factory(FACTORY).getPair(_tokenIn, WETH) != address(0) && IUniswapV2Factory(FACTORY).getPair(WETH, _tokenOut) != address(0)) {\n _WETHHopPath = new address[](3);\n _WETHHopPath[0] = _tokenIn;\n _WETHHopPath[1] = WETH;\n _WETHHopPath[2] = _tokenOut;\n _amountOutByWETHHopPath = IUniswapV2Router02(ROUTER).getAmountsOut(_amountIn, _WETHHopPath)[2];\n }\n\n if (_amountOutByDirectPath >= _amountOutByWETHHopPath) return (_directPath, _amountOutByDirectPath);\n\n return (_WETHHopPath, _amountOutByWETHHopPath);\n }\n}\n"
},
"@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol": {
"content": "pragma solidity >=0.6.2;\n\nimport './IUniswapV2Router01.sol';\n\ninterface IUniswapV2Router02 is IUniswapV2Router01 {\n function removeLiquidityETHSupportingFeeOnTransferTokens(\n address token,\n uint liquidity,\n uint amountTokenMin,\n uint amountETHMin,\n address to,\n uint deadline\n ) external returns (uint amountETH);\n function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(\n address token,\n uint liquidity,\n uint amountTokenMin,\n uint amountETHMin,\n address to,\n uint deadline,\n bool approveMax, uint8 v, bytes32 r, bytes32 s\n ) external returns (uint amountETH);\n\n function swapExactTokensForTokensSupportingFeeOnTransferTokens(\n uint amountIn,\n uint amountOutMin,\n address[] calldata path,\n address to,\n uint deadline\n ) external;\n function swapExactETHForTokensSupportingFeeOnTransferTokens(\n uint amountOutMin,\n address[] calldata path,\n address to,\n uint deadline\n ) external payable;\n function swapExactTokensForETHSupportingFeeOnTransferTokens(\n uint amountIn,\n uint amountOutMin,\n address[] calldata path,\n address to,\n uint deadline\n ) external;\n}\n"
},
"@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol": {
"content": "pragma solidity >=0.5.0;\n\ninterface IUniswapV2Factory {\n event PairCreated(address indexed token0, address indexed token1, address pair, uint);\n\n function feeTo() external view returns (address);\n function feeToSetter() external view returns (address);\n\n function getPair(address tokenA, address tokenB) external view returns (address pair);\n function allPairs(uint) external view returns (address pair);\n function allPairsLength() external view returns (uint);\n\n function createPair(address tokenA, address tokenB) external returns (address pair);\n\n function setFeeTo(address) external;\n function setFeeToSetter(address) external;\n}\n"
},
"@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router01.sol": {
"content": "pragma solidity >=0.6.2;\n\ninterface IUniswapV2Router01 {\n function factory() external pure returns (address);\n function WETH() external pure returns (address);\n\n function addLiquidity(\n address tokenA,\n address tokenB,\n uint amountADesired,\n uint amountBDesired,\n uint amountAMin,\n uint amountBMin,\n address to,\n uint deadline\n ) external returns (uint amountA, uint amountB, uint liquidity);\n function addLiquidityETH(\n address token,\n uint amountTokenDesired,\n uint amountTokenMin,\n uint amountETHMin,\n address to,\n uint deadline\n ) external payable returns (uint amountToken, uint amountETH, uint liquidity);\n function removeLiquidity(\n address tokenA,\n address tokenB,\n uint liquidity,\n uint amountAMin,\n uint amountBMin,\n address to,\n uint deadline\n ) external returns (uint amountA, uint amountB);\n function removeLiquidityETH(\n address token,\n uint liquidity,\n uint amountTokenMin,\n uint amountETHMin,\n address to,\n uint deadline\n ) external returns (uint amountToken, uint amountETH);\n function removeLiquidityWithPermit(\n address tokenA,\n address tokenB,\n uint liquidity,\n uint amountAMin,\n uint amountBMin,\n address to,\n uint deadline,\n bool approveMax, uint8 v, bytes32 r, bytes32 s\n ) external returns (uint amountA, uint amountB);\n function removeLiquidityETHWithPermit(\n address token,\n uint liquidity,\n uint amountTokenMin,\n uint amountETHMin,\n address to,\n uint deadline,\n bool approveMax, uint8 v, bytes32 r, bytes32 s\n ) external returns (uint amountToken, uint amountETH);\n function swapExactTokensForTokens(\n uint amountIn,\n uint amountOutMin,\n address[] calldata path,\n address to,\n uint deadline\n ) external returns (uint[] memory amounts);\n function swapTokensForExactTokens(\n uint amountOut,\n uint amountInMax,\n address[] calldata path,\n address to,\n uint deadline\n ) external returns (uint[] memory amounts);\n function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)\n external\n payable\n returns (uint[] memory amounts);\n function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)\n external\n returns (uint[] memory amounts);\n function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)\n external\n returns (uint[] memory amounts);\n function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)\n external\n payable\n returns (uint[] memory amounts);\n\n function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);\n function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);\n function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);\n function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);\n function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);\n}\n"
},
"contracts/swappers/sync/UniswapV2AnchorSwapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol';\nimport '@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol';\nimport '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';\nimport '@openzeppelin/contracts/utils/math/Math.sol';\nimport './SyncSwapper.sol';\n\ninterface IUniswapV2AnchorSwapper is ISyncSwapper {\n // solhint-disable-next-line func-name-mixedcase\n function WANCHOR() external view returns (address);\n\n // solhint-disable-next-line func-name-mixedcase\n function WETH() external view returns (address);\n\n // solhint-disable-next-line func-name-mixedcase\n function FACTORY() external view returns (address);\n\n // solhint-disable-next-line func-name-mixedcase\n function ROUTER() external view returns (address);\n}\n\ncontract UniswapV2AnchorSwapper is IUniswapV2AnchorSwapper, SyncSwapper {\n using SafeERC20 for IERC20;\n\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override WETH;\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override WANCHOR;\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override FACTORY;\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override ROUTER;\n\n constructor(\n address _governor,\n address _tradeFactory,\n address _weth,\n address _wanchor,\n address _factory,\n address _router\n ) SyncSwapper(_governor, _tradeFactory) {\n WETH = _weth;\n WANCHOR = _wanchor;\n FACTORY = _factory;\n ROUTER = _router;\n }\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n bytes calldata _data\n ) internal override returns (uint256 _receivedAmount) {\n address[] memory _path;\n uint256 _amountOut;\n if (_data.length > 0) {\n _path = abi.decode(_data, (address[]));\n _amountOut = IUniswapV2Router02(ROUTER).getAmountsOut(_amountIn, _path)[_path.length - 1];\n } else {\n (_path, _amountOut) = _getPathAndAmountOut(_tokenIn, _tokenOut, _amountIn);\n }\n IERC20(_path[0]).approve(ROUTER, 0);\n IERC20(_path[0]).approve(ROUTER, _amountIn);\n _receivedAmount = IUniswapV2Router02(ROUTER).swapExactTokensForTokens(\n _amountIn,\n _amountOut - ((_amountOut * _maxSlippage) / SLIPPAGE_PRECISION / 100), // slippage calcs\n _path,\n _receiver,\n block.timestamp + 1800\n )[_path.length - 1];\n }\n\n function _getPathAndAmountOut(\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn\n ) internal view returns (address[] memory _path, uint256 _amountOut) {\n uint256 _amountOutByDirectPath;\n address[] memory _directPath;\n if (IUniswapV2Factory(FACTORY).getPair(_tokenIn, _tokenOut) != address(0)) {\n _directPath = new address[](2);\n _directPath[0] = _tokenIn;\n _directPath[1] = _tokenOut;\n _amountOutByDirectPath = IUniswapV2Router02(ROUTER).getAmountsOut(_amountIn, _directPath)[1];\n }\n\n uint256 _amountOutByWETHHopPath;\n // solhint-disable-next-line var-name-mixedcase\n address[] memory _WETHHopPath;\n if (IUniswapV2Factory(FACTORY).getPair(_tokenIn, WETH) != address(0) && IUniswapV2Factory(FACTORY).getPair(WETH, _tokenOut) != address(0)) {\n _WETHHopPath = new address[](3);\n _WETHHopPath[0] = _tokenIn;\n _WETHHopPath[1] = WETH;\n _WETHHopPath[2] = _tokenOut;\n _amountOutByWETHHopPath = IUniswapV2Router02(ROUTER).getAmountsOut(_amountIn, _WETHHopPath)[2];\n }\n\n uint256 _amountOutByWANCHORHopPath;\n // solhint-disable-next-line var-name-mixedcase\n address[] memory _WANCHORHopPath;\n if (\n IUniswapV2Factory(FACTORY).getPair(_tokenIn, WANCHOR) != address(0) && IUniswapV2Factory(FACTORY).getPair(WANCHOR, _tokenOut) != address(0)\n ) {\n _WANCHORHopPath = new address[](3);\n _WANCHORHopPath[0] = _tokenIn;\n _WANCHORHopPath[1] = WANCHOR;\n _WANCHORHopPath[2] = _tokenOut;\n _amountOutByWANCHORHopPath = IUniswapV2Router02(ROUTER).getAmountsOut(_amountIn, _WANCHORHopPath)[2];\n }\n\n if (\n Math.max(Math.max(_amountOutByDirectPath, _amountOutByWETHHopPath), Math.max(_amountOutByDirectPath, _amountOutByWANCHORHopPath)) ==\n _amountOutByDirectPath\n ) {\n return (_directPath, _amountOutByDirectPath);\n }\n\n if (Math.max(_amountOutByWETHHopPath, _amountOutByWANCHORHopPath) == _amountOutByWETHHopPath) {\n return (_WETHHopPath, _amountOutByWETHHopPath);\n }\n\n return (_WANCHORHopPath, _amountOutByWANCHORHopPath);\n }\n}\n"
},
"contracts/swappers/async/UniswapV2Swapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol';\nimport '@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol';\nimport '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';\nimport './AsyncSwapper.sol';\n\ninterface IUniswapV2Swapper is IAsyncSwapper {\n // solhint-disable-next-line func-name-mixedcase\n function FACTORY() external view returns (address);\n\n // solhint-disable-next-line func-name-mixedcase\n function ROUTER() external view returns (address);\n}\n\ncontract UniswapV2Swapper is IUniswapV2Swapper, AsyncSwapper {\n using SafeERC20 for IERC20;\n\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override FACTORY;\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override ROUTER;\n\n constructor(\n address _governor,\n address _tradeFactory,\n address _uniswapFactory,\n address _uniswapRouter\n ) AsyncSwapper(_governor, _tradeFactory) {\n FACTORY = _uniswapFactory;\n ROUTER = _uniswapRouter;\n }\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n bytes calldata _data\n ) internal override returns (uint256 _receivedAmount) {\n address[] memory _path = abi.decode(_data, (address[]));\n if (_tokenIn != _path[0] || _tokenOut != _path[_path.length - 1]) revert CommonErrors.IncorrectSwapInformation();\n IERC20(_path[0]).approve(ROUTER, 0);\n IERC20(_path[0]).approve(ROUTER, _amountIn);\n _receivedAmount = IUniswapV2Router02(ROUTER).swapExactTokensForTokens(\n _amountIn,\n 0, // Slippage protection is done in AsyncSwapper abstract\n _path,\n _receiver,\n block.timestamp\n )[_path.length - 1];\n }\n}\n"
},
"contracts/swappers/async/ZRXSwapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport './AsyncSwapper.sol';\n\ninterface IZRXSwapper is IAsyncSwapper {\n error TradeReverted();\n\n // solhint-disable-next-line func-name-mixedcase\n function ZRX() external view returns (address);\n}\n\ncontract ZRXSwapper is IZRXSwapper, AsyncSwapper {\n using SafeERC20 for IERC20;\n\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override ZRX;\n\n constructor(\n address _governor,\n address _tradeFactory,\n // solhint-disable-next-line var-name-mixedcase\n address _ZRX\n ) AsyncSwapper(_governor, _tradeFactory) {\n ZRX = _ZRX;\n }\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n bytes calldata _data\n ) internal override returns (uint256 _receivedAmount) {\n uint256 _initialBalanceTokenIn = IERC20(_tokenIn).balanceOf(address(this));\n uint256 _initialBalanceOfTokenOut = IERC20(_tokenOut).balanceOf(address(this));\n IERC20(_tokenIn).approve(ZRX, 0);\n IERC20(_tokenIn).approve(ZRX, _amountIn);\n // solhint-disable-next-line avoid-low-level-calls\n (bool success, ) = ZRX.call{value: 0}(_data);\n if (!success) revert TradeReverted();\n // Check that token in & amount in was correct\n if (_initialBalanceTokenIn - IERC20(_tokenIn).balanceOf(address(this)) < _amountIn) revert CommonErrors.IncorrectSwapInformation();\n // Check that token out was correct\n uint256 _finalBalanceOfTokenOut = IERC20(_tokenOut).balanceOf(address(this));\n _receivedAmount = _finalBalanceOfTokenOut - _initialBalanceOfTokenOut;\n if (_receivedAmount == 0) revert CommonErrors.IncorrectSwapInformation();\n IERC20(_tokenOut).safeTransfer(_receiver, _receivedAmount);\n }\n}\n"
},
"contracts/swappers/async/OneInchAggregatorSwapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport './AsyncSwapper.sol';\n\ninterface IAggregationExecutor {\n function callBytes(bytes calldata data) external payable;\n}\n\ninterface IOneInchExchange {\n struct SwapDescription {\n IERC20 srcToken;\n IERC20 dstToken;\n address srcReceiver;\n address dstReceiver;\n uint256 amount;\n uint256 minReturnAmount;\n uint256 flags;\n bytes permit;\n }\n\n event Swapped(address sender, IERC20 srcToken, IERC20 dstToken, address dstReceiver, uint256 spentAmount, uint256 returnAmount);\n\n function unoswapWithPermit(\n IERC20 srcToken,\n uint256 amount,\n uint256 minReturn,\n bytes32[] calldata pools,\n bytes calldata permit\n ) external payable returns (uint256 returnAmount);\n\n function unoswap(\n IERC20 srcToken,\n uint256 amount,\n uint256 minReturn,\n bytes32[] calldata\n ) external payable returns (uint256 returnAmount);\n\n function discountedSwap(\n IAggregationExecutor caller,\n SwapDescription calldata desc,\n bytes calldata data\n )\n external\n payable\n returns (\n uint256 returnAmount,\n uint256 gasLeft,\n uint256 chiSpent\n );\n\n function swap(\n IAggregationExecutor caller,\n SwapDescription calldata desc,\n bytes calldata data\n ) external payable returns (uint256 returnAmount, uint256 gasLeft);\n}\n\ninterface IOneInchAggregatorSwapper is IAsyncSwapper {\n // solhint-disable-next-line func-name-mixedcase\n function AGGREGATION_ROUTER_V3() external view returns (address);\n}\n\ncontract OneInchAggregatorSwapper is IOneInchAggregatorSwapper, AsyncSwapper {\n using SafeERC20 for IERC20;\n\n uint256 private constant _SHOULD_CLAIM_FLAG = 0x04;\n\n // solhint-disable-next-line var-name-mixedcase\n address public immutable override AGGREGATION_ROUTER_V3;\n\n constructor(\n address _governor,\n address _tradeFactory,\n address _aggregationRouter\n ) AsyncSwapper(_governor, _tradeFactory) {\n AGGREGATION_ROUTER_V3 = _aggregationRouter;\n }\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n bytes calldata _data\n ) internal override returns (uint256 _receivedAmount) {\n (IAggregationExecutor _caller, IOneInchExchange.SwapDescription memory _swapDescription, bytes memory _tradeData) = abi.decode(\n _data[4:],\n (IAggregationExecutor, IOneInchExchange.SwapDescription, bytes)\n );\n if (\n _swapDescription.dstReceiver != _receiver ||\n address(_swapDescription.srcToken) != _tokenIn ||\n address(_swapDescription.dstToken) != _tokenOut ||\n _swapDescription.amount != _amountIn ||\n _swapDescription.flags != _SHOULD_CLAIM_FLAG\n ) revert CommonErrors.IncorrectSwapInformation();\n IERC20(_tokenIn).approve(AGGREGATION_ROUTER_V3, 0);\n IERC20(_tokenIn).approve(AGGREGATION_ROUTER_V3, _amountIn);\n (_receivedAmount, ) = IOneInchExchange(AGGREGATION_ROUTER_V3).swap(_caller, _swapDescription, _tradeData);\n }\n}\n"
},
"contracts/swappers/async/MultiCallOptimizedSwapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';\nimport './AsyncSwapper.sol';\n\ninterface IMultiCallOptimizedSwapper is IAsyncSwapper {\n error MultiCallRevert();\n error CallOnlyOptimizationRequired();\n enum MulticallOptimization {\n None, // 0\n CallOnly, // 1\n SameTo, // 2\n CallOnlySameTo, // 3\n NoValue, // 4\n CallOnlyNoValue, // 5\n _6, // filler\n CallOnlySameToNoValue // 7\n }\n}\n\ncontract MultiCallOptimizedSwapper is IMultiCallOptimizedSwapper, AsyncSwapper {\n using SafeERC20 for IERC20;\n\n constructor(address _governor, address _tradeFactory) AsyncSwapper(_governor, _tradeFactory) {}\n\n function swap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _minAmountOut,\n bytes calldata _data\n ) external override(AsyncSwapper, IAsyncSwapper) onlyTradeFactory returns (uint256 _receivedAmount) {\n _assertPreSwap(_receiver, _tokenIn, _tokenOut, _amountIn, _minAmountOut);\n uint256 _preExecutionBalance = IERC20(_tokenOut).balanceOf(_receiver);\n\n uint8 multicallOptimization = _getMultiCallOptimization(_data);\n\n bool _success;\n if (multicallOptimization == uint8(MulticallOptimization.CallOnly)) {\n _success = _multiSendCallOnly(_data); // OptimizedCall;\n } else if (multicallOptimization == uint8(MulticallOptimization.CallOnlySameTo)) {\n _success = _multiSendCallOnlySameTo(_data); // OptimizedCallSameTo;\n } else if (multicallOptimization == uint8(MulticallOptimization.CallOnlyNoValue)) {\n _success = _multiSendCallOnlyNoValue(_data); // OptimizedCallNoValue;\n } else if (multicallOptimization == uint8(MulticallOptimization.CallOnlySameToNoValue)) {\n _success = _multiSendCallOnlySameToNoValue(_data); // OptimizedCallSameToNoValue;\n } else {\n revert CallOnlyOptimizationRequired();\n }\n\n if (!_success) revert MultiCallRevert();\n\n _receivedAmount = IERC20(_tokenOut).balanceOf(_receiver) - _preExecutionBalance;\n\n if (_receivedAmount < _minAmountOut) revert InvalidAmountOut();\n emit Swapped(_receiver, _tokenIn, _tokenOut, _amountIn, _minAmountOut, _receivedAmount, _data);\n }\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n bytes calldata _data\n ) internal override returns (uint256 _receivedAmount) {\n // unused\n }\n\n function _getMultiCallOptimization(bytes memory _data) internal pure returns (uint8 multicallOptimization) {\n assembly {\n multicallOptimization := shr(0xf8, shl(0x0, mload(add(_data, 0x20))))\n }\n }\n\n function _multiSendCallOnly(bytes memory transactions) internal returns (bool _success) {\n // solhint-disable-next-line no-inline-assembly\n assembly {\n let length := mload(transactions)\n // starts at + 32 bytes + 1 multicallOptimization byte\n let i := 0x21\n for {\n // Pre block is not used in \"while mode\"\n } lt(i, length) {\n // Post block is not used in \"while mode\"\n } {\n // We shift it right by 96 bits (256 - 160 [20 address bytes]) to right-align the data and zero out unused data.\n let to := shr(0x60, mload(add(transactions, i)))\n // We offset the load address by 20 byte (20 address bytes)\n let value := mload(add(transactions, add(i, 0x14)))\n // We offset the load address by 52 byte (20 address bytes + 32 value bytes)\n let dataLength := mload(add(transactions, add(i, 0x34)))\n // We offset the load address by 84 byte (20 address bytes + 32 value bytes + 32 data length bytes)\n let data := add(transactions, add(i, 0x54))\n _success := call(gas(), to, value, data, dataLength, 0, 0)\n if eq(_success, 0) {\n break\n }\n\n // Next entry starts at 84 byte + data length\n i := add(i, add(0x54, dataLength))\n }\n }\n }\n\n function _multiSendCallOnlySameTo(bytes memory transactions) internal returns (bool _success) {\n // solhint-disable-next-line no-inline-assembly\n assembly {\n let length := mload(transactions)\n // starts at + 32 bytes + 1 operation byte\n let i := 0x21\n // We shift it right by 96 bits (256 - 160 [20 address bytes]) to right-align the data and zero out unused data.\n let to := shr(0x60, mload(add(transactions, i)))\n // We offset the load address by 20 byte (20 address bytes)\n i := add(i, 0x14)\n for {\n // Pre block is not used in \"while mode\"\n } lt(i, length) {\n // Post block is not used in \"while mode\"\n } {\n let value := mload(add(transactions, add(i, 0x14)))\n // We offset the load address by 52 byte (20 address bytes + 32 value bytes)\n let dataLength := mload(add(transactions, add(i, 0x34)))\n // We offset the load address by 84 byte (20 address bytes + 32 value bytes + 32 data length bytes)\n let data := add(transactions, add(i, 0x54))\n _success := call(gas(), to, value, data, dataLength, 0, 0)\n if eq(_success, 0) {\n break\n }\n\n // Next entry starts at 84 byte + data length\n i := add(i, add(0x54, dataLength))\n }\n }\n }\n\n function _multiSendCallOnlyNoValue(bytes memory transactions) internal returns (bool _success) {\n // solhint-disable-next-line no-inline-assembly\n assembly {\n let length := mload(transactions)\n // starts at + 32 bytes + 1 operation byte\n let i := 0x21\n for {\n // Pre block is not used in \"while mode\"\n } lt(i, length) {\n // Post block is not used in \"while mode\"\n } {\n // We shift it right by 96 bits (256 - 160 [20 address bytes]) to right-align the data and zero out unused data.\n let to := shr(0x60, mload(add(transactions, i)))\n // We offset the load address by 20 byte (20 address bytes)\n let dataLength := mload(add(transactions, add(i, 0x14)))\n // We offset the load address by 52 byte (20 address bytes + 32 data length bytes)\n let data := add(transactions, add(i, 0x34))\n _success := call(gas(), to, 0, data, dataLength, 0, 0)\n if eq(_success, 0) {\n break\n }\n\n // Next entry starts at 52 byte + data length\n i := add(i, add(0x34, dataLength))\n }\n }\n }\n\n function _multiSendCallOnlySameToNoValue(bytes memory transactions) internal returns (bool _success) {\n // solhint-disable-next-line no-inline-assembly\n assembly {\n let length := mload(transactions)\n // starts at + 32 bytes + 1 operation byte\n let i := 0x21\n // We shift it right by 96 bits (256 - 160 [20 address bytes]) to right-align the data and zero out unused data.\n let to := shr(0x60, mload(add(transactions, i)))\n // We offset the load address by 20 byte (20 address bytes)\n i := add(i, 0x14)\n for {\n // Pre block is not used in \"while mode\"\n } lt(i, length) {\n // Post block is not used in \"while mode\"\n } {\n let dataLength := mload(add(transactions, i))\n // We offset the load address by 32 byte (32 data length bytes)\n let data := add(transactions, add(i, 0x20))\n _success := call(gas(), to, 0, data, dataLength, 0, 0)\n if eq(_success, 0) {\n break\n }\n // Next entry starts at 32 bytes + data length\n i := add(i, add(0x20, dataLength))\n }\n }\n }\n\n // default msg.data handler\n fallback() external payable {}\n\n // direct payment no msg.data handler\n receive() external payable {}\n}\n"
},
"contracts/swappers/async/BancorSwapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/token/ERC20/IERC20.sol';\nimport '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';\nimport './AsyncSwapper.sol';\n\n\n\ninterface IContractRegistry {\n function addressOf(bytes32 contractName) external returns (address);\n}\n\ninterface IBancorNetwork {\n function convertByPath(\n address[] memory _path,\n uint256 _amount,\n uint256 _minReturn,\n address _beneficiary,\n address _affiliateAccount,\n uint256 _affiliateFee\n ) external payable returns (uint256);\n\n function rateByPath(address[] memory _path, uint256 _amount) external view returns (uint256);\n\n function conversionPath(address _sourceToken, address _targetToken) external view returns (address[] memory);\n\n function convert(\n address[] memory path,\n uint256 amount,\n uint256 minReturn\n ) external payable returns (uint256 returnAmount);\n}\n\ninterface IBancorSwapper is IAsyncSwapper {}\n\ncontract BancorSwapper is IBancorSwapper, AsyncSwapper {\n using SafeERC20 for IERC20;\n\n IContractRegistry public contractRegistry;\n bytes32 public bancorNetworkName;\n\n constructor(\n address _governor,\n address _tradeFactory,\n IContractRegistry _contractRegistry,\n bytes32 _bancorNetworkName\n ) AsyncSwapper(_governor, _tradeFactory) {\n contractRegistry = _contractRegistry;\n bancorNetworkName = _bancorNetworkName;\n }\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n bytes calldata _data\n ) internal override returns (uint256 _receivedAmount) {\n address[] memory _path = abi.decode(_data, (address[]));\n if (_tokenIn != _path[0] || _tokenOut != _path[_path.length - 1]) revert CommonErrors.IncorrectSwapInformation();\n IBancorNetwork _bancorNetwork = IBancorNetwork(contractRegistry.addressOf(bancorNetworkName));\n IERC20(_tokenIn).approve(address(_bancorNetwork), 0);\n IERC20(_tokenIn).approve(address(_bancorNetwork), _amountIn);\n _receivedAmount = _bancorNetwork.convert(_path, _amountIn, 1);\n IERC20(_tokenOut).safeTransfer(_receiver, _receivedAmount);\n }\n}\n"
},
"contracts/swappers/sync/BancorSwapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/token/ERC20/IERC20.sol';\nimport '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';\nimport './SyncSwapper.sol';\n\ninterface IContractRegistry {\n function addressOf(bytes32 contractName) external returns (address);\n}\n\ninterface IBancorNetwork {\n function convertByPath(\n address[] memory _path,\n uint256 _amount,\n uint256 _minReturn,\n address _beneficiary,\n address _affiliateAccount,\n uint256 _affiliateFee\n ) external payable returns (uint256);\n\n function rateByPath(address[] memory _path, uint256 _amount) external view returns (uint256);\n\n function conversionPath(address _sourceToken, address _targetToken) external view returns (address[] memory);\n\n function convert(\n address[] memory path,\n uint256 amount,\n uint256 minReturn\n ) external payable returns (uint256 returnAmount);\n}\n\ninterface IBancorSwapper is ISyncSwapper {}\n\ncontract BancorSwapper is IBancorSwapper, SyncSwapper {\n using SafeERC20 for IERC20;\n\n IContractRegistry public contractRegistry;\n bytes32 public bancorNetworkName;\n\n constructor(\n address _governor,\n address _tradeFactory,\n IContractRegistry _contractRegistry,\n bytes32 _bancorNetworkName\n ) SyncSwapper(_governor, _tradeFactory) {\n contractRegistry = _contractRegistry;\n bancorNetworkName = _bancorNetworkName;\n }\n\n // path and minReturn generated on chain\n function trade(\n address _sourceToken,\n address _targetToken,\n uint256 _amount\n ) external payable returns (uint256 returnAmount) {\n IBancorNetwork bancorNetwork = IBancorNetwork(contractRegistry.addressOf(bancorNetworkName));\n address[] memory path = bancorNetwork.conversionPath(_sourceToken, _targetToken);\n uint256 minReturn = bancorNetwork.rateByPath(path, _amount);\n returnAmount = bancorNetwork.convertByPath{value: msg.value}(path, _amount, minReturn, address(0x0), address(0x0), 0);\n }\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n bytes calldata\n ) internal override returns (uint256 _receivedAmount) {\n IBancorNetwork _bancorNetwork = IBancorNetwork(contractRegistry.addressOf(bancorNetworkName));\n address[] memory _path = _bancorNetwork.conversionPath(_tokenIn, _tokenOut);\n uint256 _minReturn = _bancorNetwork.rateByPath(_path, _amountIn);\n _minReturn = _minReturn - ((_minReturn * _maxSlippage) / SLIPPAGE_PRECISION / 100); // slippage calcs\n IERC20(_tokenIn).approve(address(_bancorNetwork), 0);\n IERC20(_tokenIn).approve(address(_bancorNetwork), _amountIn);\n _receivedAmount = _bancorNetwork.convert(_path, _amountIn, _minReturn);\n IERC20(_tokenOut).safeTransfer(_receiver, _receivedAmount);\n }\n}\n"
},
"contracts/mock/TradeFactory/TradeFactorySwapperHandler.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';\n\nimport '../../TradeFactory/TradeFactorySwapperHandler.sol';\n\ncontract TradeFactorySwapperHandlerMock is TradeFactorySwapperHandler {\n using EnumerableSet for EnumerableSet.AddressSet;\n \n constructor(\n address _masterAdmin, \n address _swapperAdder, \n address _swapperSetter\n ) TradeFactorySwapperHandler(_swapperAdder, _swapperSetter) TradeFactoryAccessManager(_masterAdmin) {}\n\n function addSwapperInternal(address _swapper) external {\n _swappers.add(_swapper);\n }\n\n function removeSwapperInternal(address _swapper) external {\n _swappers.remove(_swapper);\n }\n\n function addSwapperToStrategyInternal(address _swapper, address _strategy) external {\n _swapperStrategies[_swapper].add(_strategy);\n }\n\n function removeSwapperFromStrategyInternal(address _swapper, address _strategy) external {\n _swapperStrategies[_swapper].remove(_strategy);\n }\n\n}\n"
},
"contracts/mock/swappers/Swapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '../../swappers/Swapper.sol';\n\ncontract SwapperMock is Swapper {\n\n constructor(address _governor, address _tradeFactory) Swapper(_tradeFactory) Governable(_governor) {}\n\n // solhint-disable-next-line func-name-mixedcase\n function SWAPPER_TYPE() external view override returns (SwapperType) {\n return SwapperType.ASYNC;\n }\n\n function modifierOnlyTradeFactory() external onlyTradeFactory { }\n}\n"
},
"contracts/mock/TradeFactory/TradeFactoryPositionsHandler.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '../../TradeFactory/TradeFactoryPositionsHandler.sol';\nimport './TradeFactorySwapperHandler.sol';\n\ncontract TradeFactoryPositionsHandlerMock is TradeFactorySwapperHandlerMock, TradeFactoryPositionsHandler {\n constructor(\n address _masterAdmin, \n address _swapperAdder, \n address _swapperSetter,\n address _strategyAdder,\n address _tradesModifier\n ) \n TradeFactoryPositionsHandler(\n _strategyAdder,\n _tradesModifier\n ) \n TradeFactorySwapperHandlerMock(\n _masterAdmin, \n _swapperAdder, \n _swapperSetter\n ) {}\n\n function removePendingTrade(address _strategy, uint256 _id) external {\n _removePendingTrade(_strategy, _id);\n }\n}\n"
},
"contracts/mock/TradeFactory/TradeFactoryExecutor.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '../../TradeFactory/TradeFactoryExecutor.sol';\nimport './TradeFactoryPositionsHandler.sol';\n\ncontract TradeFactoryExecutorMock is TradeFactoryPositionsHandlerMock, TradeFactoryExecutor {\n constructor(\n address _masterAdmin, \n address _swapperAdder, \n address _swapperSetter, \n address _strategyAdder, \n address _tradesModifier,\n address _tradesSettler,\n address _mechanicsRegistry\n ) \n TradeFactoryPositionsHandlerMock(_masterAdmin, _swapperAdder, _swapperSetter, _strategyAdder, _tradesModifier)\n TradeFactoryExecutor(_tradesSettler, _mechanicsRegistry) {}\n}\n"
},
"contracts/mock/swappers/AsyncSwapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '../../swappers/async/AsyncSwapper.sol';\n\ncontract AsyncSwapperMock is AsyncSwapper {\n uint256 internal _receivedAmount;\n\n event MyInternalExecuteSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n bytes _data\n );\n\n constructor(address _governor, address _tradeFactory) AsyncSwapper(_governor, _tradeFactory) {}\n\n function setReceivedAmount(uint256 __receivedAmount) external {\n _receivedAmount = __receivedAmount;\n }\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n bytes calldata _data\n ) internal override virtual returns (uint256 __receivedAmount) {\n __receivedAmount = _receivedAmount;\n emit MyInternalExecuteSwap(\n _receiver,\n _tokenIn,\n _tokenOut,\n _amountIn,\n _data\n );\n }\n}\n"
},
"contracts/mock/swappers/SyncSwapper.sol": {
"content": "// SPDX-License-Identifier: MIT\npragma solidity >=0.8.4 <0.9.0;\n\nimport '../../swappers/sync/SyncSwapper.sol';\n\ncontract SyncSwapperMock is SyncSwapper {\n\n constructor(address _governor, address _tradeFactory) SyncSwapper(_governor, _tradeFactory) {}\n\n function _executeSwap(\n address _receiver,\n address _tokenIn,\n address _tokenOut,\n uint256 _amountIn,\n uint256 _maxSlippage,\n bytes calldata _data\n ) internal override virtual returns (uint256 _receivedAmount) { }\n}\n"
},
"contracts/mock/ERC20Mock.sol": {
"content": "// SPDX-License-Identifier: UNLICENSED\n\npragma solidity ^0.8.6;\n\nimport '@openzeppelin/contracts/token/ERC20/ERC20.sol';\n\ncontract ERC20Mock is ERC20 {\n uint8 private _decimals;\n\n constructor(\n string memory _name,\n string memory _symbol,\n uint8 __decimals,\n address _initialAccount,\n uint256 _initialBalance\n ) payable ERC20(_name, _symbol) {\n _decimals = __decimals;\n if (_initialBalance > 0) {\n _mint(_initialAccount, _initialBalance);\n }\n }\n\n function mint(address _account, uint256 _amount) external {\n _mint(_account, _amount);\n }\n\n function burn(address _account, uint256 _amount) external {\n _burn(_account, _amount);\n }\n\n function transferInternal(\n address _from,\n address _to,\n uint256 _value\n ) external {\n _transfer(_from, _to, _value);\n }\n\n function approveInternal(\n address _owner,\n address _spender,\n uint256 _value\n ) external {\n _approve(_owner, _spender, _value);\n }\n\n function decimals() public view virtual override returns (uint8) {\n return _decimals;\n }\n}"
}
},
"settings": {
"optimizer": {
"enabled": true,
"runs": 200
},
"outputSelection": {
"*": {
"*": [
"abi",
"evm.bytecode",
"evm.deployedBytecode",
"evm.methodIdentifiers",
"metadata",
"devdoc",
"userdoc",
"storageLayout",
"evm.gasEstimates"
],
"": [
"ast"
]
}
},
"metadata": {
"useLiteralContent": true
},
"libraries": {
"": {
"__CACHE_BREAKER__": "0x00000000d41867734bbee4c6863d9255b2b06ac1"
}
}
}
}