// File: ./contracts/global/SavingsRegistry.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.5.16;
pragma experimental ABIEncoderV2;

import {Ownable} from "../lib/Ownable.sol";

contract SavingsRegistry is Ownable {

    // Available synths which have a savings contract
    address[] public availableSavings;

    // Synth synthetic (proxy) to savings contract
    mapping(address => address) public savings;

    /* ========== Events ========== */

    event SavingsAdded(address synthetic, address savings);
    event SavingsRemoved(address synthetic, address savings);

    /* ========== View Functions ========== */

    function getAllSavings()
        public
        view
        returns (address[] memory)
    {
        return availableSavings;
    }

    /* ========== Mutative Functions ========== */

    /**
     * @dev Add a new savings contract to the registry.
     *
     * @param synthetic The address of the synthetic token proxy contract
     * @param saving The address of the savings contract proxy address
     */
    function addSavings(
        address synthetic,
        address saving
    )
        external
        onlyOwner
    {
        require(
            savings[synthetic] == address(0),
            "Savings already exists"
        );

        availableSavings.push(synthetic);
        savings[synthetic] = saving;

        emit SavingsAdded(synthetic, saving);
    }


    /**
     * @dev Remove a savings contract from registry.
     *
     * @param synthetic The address of the synthetic token proxy contract
     */
    function removeSavings(
        address synthetic
    )
        external
        onlyOwner
    {
        require(
            address(savings[synthetic]) != address(0),
            "Synth does not exist"
        );

        // Save the address we're removing for emitting the event at the end.
        address savingsToRemove = savings[synthetic];

        // Remove the synth from the availableSynths array.
        for (uint i = 0; i < availableSavings.length; i++) {
            if (address(availableSavings[i]) == savingsToRemove) {
                delete availableSavings[i];
                availableSavings[i] = availableSavings[availableSavings.length - 1];
                availableSavings.length--;

                break;
            }
        }

        // And remove it from the synths mapping
        delete savings[synthetic];

        emit SavingsRemoved(synthetic, savingsToRemove);
    }
}

// File: ./contracts/lib/Ownable.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.5.16;

/**
 * @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.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied 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), msg.sender);
    }

    /**
     * @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(_owner == msg.sender, "Ownable: caller is not the 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 {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        emit OwnershipTransferred(_owner, newOwner);
        _owner = newOwner;
    }
}