// File: contracts/5/ConfigurableInterestStationConfig.sol



pragma solidity 0.5.16;
import "openzeppelin-solidity-2.3.0/contracts/ownership/Ownable.sol";
import "openzeppelin-solidity-2.3.0/contracts/math/SafeMath.sol";
import "./StationConfig.sol";
import "./OrbitConfig.sol";


interface InterestModel {
    /// @dev Return the interest rate per second, using 1e18 as denom.
    function getInterestRate(uint256 debt, uint256 floating) external view returns (uint256);
}

contract ThreeDegreeTrajectory {
    using SafeMath for uint256;

    /// @dev Return the interest rate per second, using 1e18 as denom.
    function getInterestRate(uint256 debt, uint256 floating) external pure returns (uint256) {
        uint256 total = debt.add(floating);
        uint256 utilization = debt.mul(100e18).div(total);
        if (utilization < 80e18) {
            // Less than 80% utilization - 0%-10% APY
            return utilization.mul(10e16).div(80e18) / 365 days;
        } else if (utilization < 90e18) {
            // Between 80% and 90% - 10% APY
            return uint256(10e16) / 365 days;
        } else if (utilization < 100e18) {
            // Between 90% and 100% - 10%-50% APY
            return (10e16 + utilization.sub(90e18).mul(40e16).div(10e18)) / 365 days;
        } else {
            // Not possible, but just in case - 50% APY
            return uint256(50e16) / 365 days;
        }
    }
}

contract ConfigurableInterestStationConfig is StationConfig, Ownable {
    /// The minimum ETH debt size per position.
    uint256 public minDebtSize;
    /// The portion of interests allocated to the reserve pool.
    uint256 public getStarGateBps;
    /// The reward for successfully killing a position.
    uint256 public getTerminateBps;
    /// Mapping for orbit address to its configuration.
    mapping (address => OrbitConfig) public orbits;
    /// Interest rate model
    InterestModel public interestModel;

    constructor(
        uint256 _minDebtSize,
        uint256 _reserveGateBps,
        uint256 _terminateBps,
        InterestModel _interestModel
    ) public {
        setParams(_minDebtSize, _reserveGateBps, _terminateBps, _interestModel);
    }

    /// @dev Set all the basic parameters. Must only be called by the owner.
    /// @param _minDebtSize The new minimum debt size value.
    /// @param _reserveGateBps The new interests allocated to the reserve pool value.
    /// @param _terminateBps The new reward for killing a position value.
    /// @param _interestModel The new interest rate model contract.
    function setParams(
        uint256 _minDebtSize,
        uint256 _reserveGateBps,
        uint256 _terminateBps,
        InterestModel _interestModel
    ) public onlyOwner {
        minDebtSize = _minDebtSize;
        getStarGateBps = _reserveGateBps;
        getTerminateBps = _terminateBps;
        interestModel = _interestModel;
    }

    /// @dev Set the configuration for the given orbits. Must only be called by the owner.
    function setOrbits(address[] calldata addrs, OrbitConfig[] calldata configs) external onlyOwner {
        require(addrs.length == configs.length, "bad length");
        for (uint256 idx = 0; idx < addrs.length; idx++) {
            orbits[addrs[idx]] = configs[idx];
        }
    }

    /// @dev Return the interest rate per second, using 1e18 as denom.
    function getInterestRate(uint256 debt, uint256 floating) external view returns (uint256) {
        return interestModel.getInterestRate(debt, floating);
    }

    /// @dev Return whether the given address is a orbit.
    function isOrbit(address orbit) external view returns (bool) {
        return address(orbits[orbit]) != address(0);
    }

    /// @dev Return whether the given orbit accepts more debt. Revert on non-orbit.
    function acceptDebt(address orbit) external view returns (bool) {
        return orbits[orbit].acceptDebt(orbit);
    }

    /// @dev Return the work factor for the orbit + ETH debt, using 1e4 as denom. Revert on non-orbit.
    function launcher(address orbit, uint256 debt) external view returns (uint256) {
        return orbits[orbit].launcher(orbit, debt);
    }

    /// @dev Return the kill factor for the orbit + ETH debt, using 1e4 as denom. Revert on non-orbit.
    function terminator(address orbit, uint256 debt) external view returns (uint256) {
        return orbits[orbit].terminator(orbit, debt);
    }
}

// File: openzeppelin-solidity-2.3.0/contracts/ownership/Ownable.sol



pragma solidity ^0.5.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 () internal {
        _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: openzeppelin-solidity-2.3.0/contracts/math/SafeMath.sol



pragma solidity ^0.5.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: contracts/5/StationConfig.sol



pragma solidity 0.5.16;

interface StationConfig {
    /// @dev Return minimum ETH debt size per position.
    function minDebtSize() external view returns (uint256);

    /// @dev Return the interest rate per second, using 1e18 as denom.
    function getInterestRate(uint256 debt, uint256 floating) external view returns (uint256);

    /// @dev Return the bps rate for reserve pool.
    function getStarGateBps() external view returns (uint256);

    /// @dev Return the bps rate for Avada Kill caster.
    function getTerminateBps() external view returns (uint256);

    /// @dev Return whether the given address is a orbit.
    function isOrbit(address orbit) external view returns (bool);

    /// @dev Return whether the given orbit accepts more debt. Revert on non-orbit.
    function acceptDebt(address orbit) external view returns (bool);

    /// @dev Return the work factor for the orbit + ETH debt, using 1e4 as denom. Revert on non-orbit.
    function launcher(address orbit, uint256 debt) external view returns (uint256);

    /// @dev Return the kill factor for the orbit + ETH debt, using 1e4 as denom. Revert on non-orbit.
    function terminator(address orbit, uint256 debt) external view returns (uint256);
}

// File: contracts/5/OrbitConfig.sol



pragma solidity 0.5.16;

interface OrbitConfig {
    /// @dev Return whether the given orbit accepts more debt.
    function acceptDebt(address orbit) external view returns (bool);
    /// @dev Return the work factor for the orbit + ETH debt, using 1e4 as denom.
    function launcher(address orbit, uint256 debt) external view returns (uint256);
    /// @dev Return the kill factor for the orbit + ETH debt, using 1e4 as denom.
    function terminator(address orbit, uint256 debt) external view returns (uint256);
}