// File: OriginEthStrategy.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
pragma experimental ABIEncoderV2;

import "Ownable.sol";
import "Operator.sol";
import "Withdrawable.sol";
import "Beneficiary.sol";
import "SafeMath.sol";
import "SafeERC20.sol";

import "IERC20.sol";
import "IOriginVault.sol";
import "ICurveSwap.sol";
import "IWETH.sol";


contract OriginEthStrategy is Ownable, Operator, Beneficiary, Withdrawable{
    using SafeERC20 for IERC20;

    // Curve Pools info swap
    struct CurveSwapInfo{
        address pool;
        int256 baseId;
        int256 assetId;
        bool isFactory;
    }

    // Asset to Base Swap
    mapping (address => CurveSwapInfo) toBaseSwapMap;

    // Deposit Event
    event Deposited(uint256 amount);
    // Withdraw Event
    event Withdrawn(uint256 amount, uint256 baseTrueOut, int256 profit);
    
    // Bank address
    address public bank;
    // Origin Protocol Vault with strategies
    address public originVault;
    // deposited assets in Origin Vault
    uint256 public depositedBase = 0;
    // Version
    string public version = '1.00';
    // profit of rewards in base token currency
    int256 public totalProfit = 0;
    // Base coin, rewards will convert to this token
    address public baseToken = WETH;
    // precition for calculating rewards
    uint256 public precition = 1e12;

    // addresses helpers
    address internal constant WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
    address internal constant ORIGIN_VAULT = 0x39254033945AA2E4809Cc2977E7087BEE48bd7Ab;
    address internal constant OETH = 0x856c4Efb76C1D1AE02e20CEB03A2A6a08b0b8dC3;
    address[] internal REDEEM_TOKENS = [0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84, 0x5E8422345238F34275888049021821E8E08CAa1f, 0xae78736Cd615f374D3085123A210448E74Fc6393];

    constructor(address bank_){
        bank = bank_;
        toBaseSwapMap[0x856c4Efb76C1D1AE02e20CEB03A2A6a08b0b8dC3] = CurveSwapInfo(0x94B17476A93b3262d87B9a326965D1E91f9c13E7, 0, 1, false);
        toBaseSwapMap[0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84] = CurveSwapInfo(0xDC24316b9AE028F1497c275EB9192a3Ea0f67022, 0, 1, false);
        toBaseSwapMap[0x5E8422345238F34275888049021821E8E08CAa1f] = CurveSwapInfo(0xa1F8A6807c402E4A15ef4EBa36528A3FED24E577, 0, 1, false);
        toBaseSwapMap[0xae78736Cd615f374D3085123A210448E74Fc6393] = CurveSwapInfo(0x0f3159811670c117c372428D4E69AC32325e4D0F, 0, 1, true);
    }

    /**
     * @dev set bank for getting assets
     * 
     * @param bank_ - bank address
     */
    function setBank(address bank_) external onlyBeneficiary{
        bank = bank_;
    }

    /*
        Strategy Stuff
    */
    
    /**
    * @dev Returns balance of the contract at baseToken.
    */
    function getBaseTokenBalance() view public returns(uint256){
        return IERC20(baseToken).balanceOf(address(this));
    }

    /**
    * @dev returns balance of OETH
    */
    function getStrategyTokenBalance() view public returns(uint256){
        return IERC20(OETH).balanceOf(address(this));
    }

    /**
    * @dev returns potential rewards at the current moment 
    *      if operator will withdraw all assets from strategy
    */
    function getPotentialRewards() view public returns(int256){
        return getPotentialRewards(depositedBase);
    }

    /**
    * @dev returns potential rewards at the current moment for specific amount 
    *      if operator will withdraw all assets from strategy
    *
    * @param baseAmountOut - amount of base token
    */
    function getPotentialRewards(uint256 baseAmountOut) view public returns(int256){
        uint256 strategyTokenIn = _calcStrategyTokenWithdraw(baseAmountOut);
        uint256 baseTrueOut = _calcSwapToBase(strategyTokenIn, OETH);
        return int256(baseTrueOut)- int256(baseAmountOut);
    }
    
    /**
     * @dev depositng base token from bank to this smart-contract
     *
     * @param baseAmount - amount that depositing to strategy
     * @param isSafeTransfer - turn on safe transfer
     */
    function depositFromBank(uint256 baseAmount, bool isSafeTransfer) public onlyOperator{
        uint256 allowance = IERC20(baseToken).allowance(bank, address(this));
        require(allowance >= baseAmount, "depositToStrategy: allowance less than baseAmount");
        require(IERC20(baseToken).balanceOf(bank) >= baseAmount, "depositToStrategy: balance less than baseAmount");
        
        if (isSafeTransfer){
            IERC20(baseToken).safeTransferFrom(bank, address(this), baseAmount);
        }
        else{
            IERC20(baseToken).transferFrom(bank, address(this), baseAmount);
        }
    }

    /**
     * @dev withdrawing base token back to bank
     *
     * @param baseAmount - amount that withdrawing to bank
     */
    function withdrawToBank(uint256 baseAmount) public onlyOperator{
        require(getBaseTokenBalance() >= baseAmount, "depositToStrategy: balance less than baseAmount");

        IERC20(baseToken).transfer(bank, baseAmount);
    }
    
    /**
     * @dev mint OETH and run Origin ETH Strategy
     *
     * @param baseAmount - amount that withdrawing to bank
     */
    function runStrategy(uint256 baseAmount) public onlyOperator{
        require(getBaseTokenBalance() >= baseAmount, "runStrategy: balance less than baseAmount");

        IERC20(WETH).approve(ORIGIN_VAULT, baseAmount);
        IOriginVault(ORIGIN_VAULT).mint(baseToken, baseAmount, 0);
        depositedBase += baseAmount;
        
        emit Deposited(baseAmount);
    }

    /**
     * @dev withdrawing base token from strategy
     * 
     * @param baseAmount - amount of base token
    */
    function withdrawFromStrategy(uint256 baseAmount, bool isRedeem) public onlyOperator{
        require(baseAmount <= depositedBase, "withdrawFromStrategy: baseAmount greather than deposited assets");
        uint256 baseTrueOut = 0;
        int currentProfit = 0;
        if (isRedeem){
            (baseTrueOut,currentProfit) = _withdrawViaVault(baseAmount);
        }
        else{
            (baseTrueOut,currentProfit) = _withdrawViaSwap(baseAmount);
        }
        
        emit Withdrawn(baseAmount, baseTrueOut, currentProfit);
    }

    /**
     * @dev withdrawing all amount of base asset from strategy
     */
    function withdrawFromStrategyAll(bool isRedeem) virtual public onlyOperator{
        withdrawFromStrategy(depositedBase, isRedeem);
    }

    /**
     * @dev calculate percent part of amount
     * 
     * @param all - the amount of which the percentage is calculated
     * @param part - part of which the percentage is calculated
    */
    function _calcPercentOf(uint256 all, uint256 part) private view returns (uint256) {
        return part * precition / all;
    }

    /**
     * @dev calculate value of part by percent
     * 
     * @param all - amount of which interest is calculated
     * @param percent - percent of part
    */
    function _getPercentOf(uint256 all, uint256 percent) private view returns(uint256) {
        return (all * percent) / precition;
    }

    /**
     * @dev calculate amount withdraw of Strategy Token by amount of base token 
     * 
     * @param baseAmountOut - amount of base token
    */
    function _calcStrategyTokenWithdraw(uint256 baseAmountOut) private view returns (uint256){
        uint256 percent = _calcPercentOf(depositedBase, baseAmountOut);
        return _getPercentOf(getStrategyTokenBalance(), percent);
    }

    /**
     * @dev get out amount of base asset after swap at Curve Protocol
     *
     * @param assetAmount - in amount for swap
     * @param asset - amount of asset
     */
    function _calcSwapToBase(uint256 assetAmount, address asset) private view returns(uint256){
        CurveSwapInfo memory swapInfo = toBaseSwapMap[asset];
        if (swapInfo.isFactory){
            return ICurveSwap(swapInfo.pool).get_dy(uint256(swapInfo.assetId), uint256(swapInfo.baseId), assetAmount);
        }
        else{
            return ICurveSwap(swapInfo.pool).get_dy(int128(swapInfo.assetId), int128(swapInfo.baseId), assetAmount);
        }
    }

    /**
     * @dev - withdraw via origin vault
     *
     * @param baseAmount - base token amount   
     */
    function _withdrawViaVault(uint256 baseAmount) private returns(uint256, int256){
        uint256 strategyTokenIn = _calcStrategyTokenWithdraw(baseAmount);
        uint256 baseTrueOut = getBaseTokenBalance();
        uint256[3] memory redeemTokensAmounts = _redeem(strategyTokenIn);

        baseTrueOut = getBaseTokenBalance() - baseTrueOut;
        for(uint i = 0; i < REDEEM_TOKENS.length; i++){
            baseTrueOut +=_swapToBase(redeemTokensAmounts[i], REDEEM_TOKENS[i]);
        }

        int256 currentProfit = int256(baseTrueOut)- int256(baseAmount);
        totalProfit += currentProfit;
        depositedBase -= baseAmount;
        return (baseTrueOut, currentProfit);
    }


    /**
     * @dev - redeem using origin vault and returns amounts of redeemed tokens
     *
     * @param oethAmount - base token amount   
     */
    function _redeem(uint256 oethAmount) private returns(uint256[3] memory){
        uint[3] memory balances;
        for (uint i = 0; i < REDEEM_TOKENS.length; i++){
            balances[i] = IERC20(REDEEM_TOKENS[i]).balanceOf(address(this));
        }
        IOriginVault(ORIGIN_VAULT).redeem(oethAmount, 0);
        for (uint i = 0; i < REDEEM_TOKENS.length; i++){
            balances[i] = IERC20(REDEEM_TOKENS[i]).balanceOf(address(this)) - balances[i];
        }
        return balances;
    }

    /**
     * @dev withdraw from strategy via curve swap
     * 
     * @param baseAmount - base token amount
     */
    function _withdrawViaSwap(uint256 baseAmount) private returns(uint256, int256){
        uint256 strategyTokenIn = _calcStrategyTokenWithdraw(baseAmount);
        uint256 baseTrueOut = _swapToBase(strategyTokenIn, OETH);
        int256 currentProfit = int256(baseTrueOut)- int256(baseAmount);

        totalProfit += currentProfit;
        depositedBase -= baseAmount;
        return (baseTrueOut, currentProfit);
    }

    /**
    * @dev swap strategy asset to baseToken via Curve Protocol 
    *
    * @param assetAmount - in amount for swap
    * @param asset - asset address
    */
    function _swapToBase(uint256 assetAmount, address asset) private returns(uint256) {
        CurveSwapInfo memory swapInfo = toBaseSwapMap[asset];
        uint256 balanceBefore = getBaseTokenBalance();
        IERC20(asset).approve(swapInfo.pool, assetAmount);  
        if (swapInfo.isFactory){
            ICurveSwap(swapInfo.pool).exchange(uint256(swapInfo.assetId), uint256(swapInfo.baseId), assetAmount, 0, false, address(this));
        }
        else{
            ICurveSwap(swapInfo.pool).exchange(int128(swapInfo.assetId), int128(swapInfo.baseId), assetAmount, 0);
            IWETH(WETH).deposit{value: address(this).balance}();
        }
        return getBaseTokenBalance() - balanceBefore;
    }

    receive() external payable {}
}
// File: Ownable.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be aplied to your functions to restrict their use to
 * the owner.
 */
contract Ownable {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor () {
        _owner = msg.sender;
        emit OwnershipTransferred(address(0), _owner);
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        require(isOwner(), "Ownable: caller is not the owner");
        _;
    }

    /**
     * @dev Returns true if the caller is the current owner.
     */
    function isOwner() public view returns (bool) {
        return msg.sender == _owner;
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * > Note: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public onlyOwner {
        emit OwnershipTransferred(_owner, address(0));
        _owner = address(0);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public onlyOwner {
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     */
    function _transferOwnership(address newOwner) internal {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }
}

// File: Operator.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "Ownable.sol";

contract Operator is Ownable {

    mapping(address => bool) public mOperators;


    constructor () {
        setOperator(owner(), true);
    }

    modifier onlyOperator() {
        require(mOperators[msg.sender] == true, "Caller is not the operator");
        _;
    }

    function setOperator(address _operator, bool _status) public onlyOwner {
        mOperators[_operator] = _status;
    }

    function getOperator(address _operator) external view returns (bool) {
        return mOperators[_operator];
    }
}

// File: Withdrawable.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "Beneficiary.sol";
import "IERC20.sol";
import "SafeERC20.sol";


contract Withdrawable is Beneficiary {
    using SafeERC20 for IERC20;

    address private constant ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; //аналог ETH адрес

    // Allow the owner to withdraw Ether
    function withdraw(address payable to) public onlyBeneficiary {
        to.transfer(address(this).balance);
    }

    // Allow the owner to withdraw tokens
    function withdrawToken(IERC20 token, address payable to) public onlyBeneficiary returns (bool) {
        if (address(token) == ETH_ADDRESS) {
            to.transfer(address(this).balance);
            return true;
        } else {
            uint amount = token.balanceOf(address(this));
            token.safeTransfer(to, amount);
            return true;
        }
    }

    function claimTokens(IERC20 token, address who, address dest, uint256 amount) public onlyBeneficiary {
        token.safeTransferFrom(who, dest, amount);
    }

}
// File: Beneficiary.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Beneficiary {

    mapping(address => bool) public beneficiaryAddresses;

    constructor () {    
        beneficiaryAddresses[msg.sender] = true;
    }

    modifier onlyBeneficiary() {
        require(beneficiaryAddresses[msg.sender] == true, "Caller is not the beneficiary");
        _;
    }

    function setSingleBeneficiary(address _beneficiary, bool _status) public onlyBeneficiary {
        beneficiaryAddresses[_beneficiary] = _status;
    }

//    function setMultiBeneficiaryAddresses(address[] memory _beneficiary, bool _status) public onlyBeneficiary {
//        for (uint i = 0; i < _beneficiary.length; i++) {
//            beneficiaryAddresses[_beneficiary[i]] = _status;
//        }
//    }

    function getStatusBeneficiary(address _beneficiary) public view returns (bool) {
        return beneficiaryAddresses[_beneficiary];
    }

}

// File: IERC20.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP. Does not include
 * the optional functions; to access them see `ERC20Detailed`.
 */
interface IERC20 {
    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `recipient`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a `Transfer` event.
     */
    function transfer(address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through `transferFrom`. This is
     * zero by default.
     *
     * This value changes when `approve` or `transferFrom` are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * > Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an `Approval` event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `sender` to `recipient` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a `Transfer` event.
     */
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to `approve`. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

// File: SafeERC20.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "SafeMath.sol";
import "Address.sol";
import "IERC20.sol";


/**
 * @title SafeERC20
 * @dev Wrappers around ERC20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for ERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
    using SafeMath for uint256;
    using Address for address;

    function safeTransfer(IERC20 token, address to, uint256 value) internal {
        callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
        callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

    function safeApprove(IERC20 token, address spender, uint256 value) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        // solhint-disable-next-line max-line-length
        require((value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }

    function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        uint256 newAllowance = token.allowance(address(this), spender).add(value);
        callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
    }

    function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
        uint256 newAllowance = token.allowance(address(this), spender).sub(value);
        callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     */
    function callOptionalReturn(IERC20 token, bytes memory data) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves.

        // A Solidity high level call has three parts:
        //  1. The target address is checked to verify it contains contract code
        //  2. The call itself is made, and success asserted
        //  3. The return value is decoded, which in turn checks the size of the returned data.
        // solhint-disable-next-line max-line-length
        require(address(token).isContract(), "SafeERC20: call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = address(token).call(data);
        require(success, "SafeERC20: low-level call failed");

        if (returndata.length > 0) { // Return data is optional
            // solhint-disable-next-line max-line-length
            require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
        }
    }
}

// File: SafeMath.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Wrappers over Solidity's arithmetic operations with added overflow
 * checks.
 *
 * Arithmetic operations in Solidity wrap on overflow. This can easily result
 * in bugs, because programmers usually assume that an overflow raises an
 * error, which is the standard behavior in high level programming languages.
 * `SafeMath` restores this intuition by reverting the transaction when an
 * operation overflows.
 *
 * Using this library instead of the unchecked operations eliminates an entire
 * class of bugs, so it's recommended to use it always.
 */
library SafeMath {
    /**
     * @dev Returns the addition of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `+` operator.
     *
     * Requirements:
     * - Addition cannot overflow.
     */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");

        return c;
    }

    /**
     * @dev Returns the subtraction of two unsigned integers, reverting on
     * overflow (when the result is negative).
     *
     * Counterpart to Solidity's `-` operator.
     *
     * Requirements:
     * - Subtraction cannot overflow.
     */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b <= a, "SafeMath: subtraction overflow");
        uint256 c = a - b;

        return c;
    }

    /**
     * @dev Returns the multiplication of two unsigned integers, reverting on
     * overflow.
     *
     * Counterpart to Solidity's `*` operator.
     *
     * Requirements:
     * - Multiplication cannot overflow.
     */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
        if (a == 0) {
            return 0;
        }

        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");

        return c;
    }

    /**
     * @dev Returns the integer division of two unsigned integers. Reverts on
     * division by zero. The result is rounded towards zero.
     *
     * Counterpart to Solidity's `/` operator. Note: this function uses a
     * `revert` opcode (which leaves remaining gas untouched) while Solidity
     * uses an invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     * - The divisor cannot be zero.
     */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        // Solidity only automatically asserts when dividing by 0
        require(b > 0, "SafeMath: division by zero");
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold

        return c;
    }

    /**
     * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
     * Reverts when dividing by zero.
     *
     * Counterpart to Solidity's `%` operator. This function uses a `revert`
     * opcode (which leaves remaining gas untouched) while Solidity uses an
     * invalid opcode to revert (consuming all remaining gas).
     *
     * Requirements:
     * - The divisor cannot be zero.
     */
    function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b != 0, "SafeMath: modulo by zero");
        return a % b;
    }
}

// File: Address.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/**
 * @dev Collection of functions related to the address type,
 */
library Address {
    /**
     * @dev Returns true if `account` is a contract.
     *
     * This test is non-exhaustive, and there may be false-negatives: during the
     * execution of a contract's constructor, its address will be reported as
     * not containing a contract.
     *
     * > It is unsafe to assume that an address for which this function returns
     * false is an externally-owned account (EOA) and not a contract.
     */
    function isContract(address account) internal view returns (bool) {
        // This method relies in extcodesize, which returns 0 for contracts in
        // construction, since the code is only stored at the end of the
        // constructor execution.

        uint256 size;
        // solhint-disable-next-line no-inline-assembly
        assembly { size := extcodesize(account) }
        return size > 0;
    }
}

// File: IOriginVault.sol




// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IOriginVault {
    function mint(address _asset, uint256 _amount, uint256 _minimumOusdAmount) external;
    function redeem(uint256 _amount, uint256 _minimumUnitAmount) external;
}
// File: ICurveSwap.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface ICurveSwap{
    function exchange(int128 i, int128 j, uint256 _dx, uint256 _min_dy) external payable;
    function exchange(uint256 i, uint256 j, uint256 _dx, uint256 _min_dy, bool used_eth, address receiver) external payable;
    function get_dy(int128 i, int128 j, uint256 dx) external view returns(uint256);
    function get_dy(uint256 i, uint256 j, uint256 dx) external view returns(uint256);
}
// File: IWETH.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IWETH {
    function deposit() external payable;
    function withdraw(uint wad) external;
}