contract MarketplaceElectron is MarketplaceCommon, DigixConstantsElectron {

    function MarketplaceElectron(address _resolver) public
    {
        require(init(CONTRACT_INTERACTIVE_MARKETPLACE_ELECTRON, _resolver));
    }

    function marketplace_controller_electron()
        internal
        constant
        returns (MarketplaceControllerElectron _contract)
    {
        _contract = MarketplaceControllerElectron(get_contract(CONTRACT_CONTROLLER_MARKETPLACE));
    }

    function marketplace_storage_electron()
        internal
        constant
        returns (MarketplaceStorageElectron _contract)
    {
        _contract = MarketplaceStorageElectron(get_contract(CONTRACT_STORAGE_MARKETPLACE_ELECTRON));
    }

    function token_contract()
        internal
        constant
        returns (ERCTwenty _contract)
    {
        _contract = ERCTwenty(get_contract(CONTRACT_INTERACTIVE_TOKEN));
    }

    /// @dev purchase DGX gold using ETH
    /// @param _kyc_tier KYC tier of the user
    /// @param _kyc_expiry KYC expiry of the user
    /// @param _block_number Block number from DTPO (Digix Trusted Price Oracle)
    /// @param _wei_per_dgx_mg Price in wei for one milligram of DGX
    /// @param _signer Address of the DTPO signer
    /// @param _signature Signature of the payload
    /// @return {
    ///   "_success": "returns true if operation is successful",
    ///   "_purchased_amount": "DGX nanograms received"
    /// }
    function purchaseWithEth(
        uint256 _kyc_tier,
        uint256 _kyc_expiry,
        uint256 _block_number,
        uint256 _wei_per_dgx_mg,
        address _signer,
        bytes _signature
    )
        payable
        public
        returns (bool _success, uint256 _purchased_amount)
    {
        address _sender = msg.sender;

        (_success, _purchased_amount) = marketplace_controller_electron()
            .purchase_with_eth
            .value(msg.value)(
                msg.value,
                _sender,
                _kyc_tier,
                _kyc_expiry,
                _block_number,
                _wei_per_dgx_mg,
                _signer,
                _signature
            );

        require(_success);
    }

    /// @dev purchase DGX gold using erc20 tokens
    /// @param _tokens_sent amount of erc20 tokens sent
    /// @param _kyc_tier KYC tier of the buyer
    /// @param _kyc_expiry KYC expiry (UTC timestamp in seconds) of the buyer
    /// @param _block_number Block number from DTPO (Digix Trusted Price Oracle)
    /// @param _token_per_1000ton Price in dai for one ton of DGX
    /// @param _signer Address of the DTPO signer
    /// @param _token_index Index of the ERC-20 paid with
    /// @param _signature Signature of the payload
    /// @return {
    ///   "_success": "returns true if operation is successful",
    ///   "_purchased_amount": "DGX nanograms received"
    /// }
    function purchaseWithToken(
        uint256 _tokens_sent,
        uint256 _kyc_tier,
        uint256 _kyc_expiry,
        uint256 _block_number,
        uint256 _token_per_1000ton,
        address _signer,
        uint256 _token_index,
        bytes _signature
    )
        public
        returns (bool _success, uint256 _purchased_amount)
    {
        address _sender = msg.sender;

        (_success, _purchased_amount) =
            marketplace_controller_electron().purchase_with_token(
                _tokens_sent,
                _sender,
                _kyc_tier,
                _kyc_expiry,
                _block_number,
                _token_per_1000ton,
                _signer,
                _token_index,
                _signature
            );

        require(_success);
    }

    /// @dev purchase DGX gold using fiat methods
    ///      verification is done in one of the servers
    ///      this method is supposed to be called by a key with designated role
    /// @param _buyer Ethereum address of the user who has made the fiat purchase
    /// @param _kyc_tier KYC tier of the user
    /// @param _kyc_expiry KYC expiry of the user
    /// @param _amount_dgx_ng nanograms of DGX purchased by the user
    function addFiatPurchase(
        address _buyer,
        uint256 _kyc_tier,
        uint256 _kyc_expiry,
        uint256 _amount_dgx_ng
    )
        public
    {
        marketplace_controller_electron().add_purchase_with_fiat(msg.sender, _buyer, _kyc_tier, _kyc_expiry, _amount_dgx_ng);
    }

    /// @dev get purchase limits for a buyer
    /// @param _buyer ethereum address of user who wants to buy DGX
    /// @param _kyc_tier kyc tier of the buyer
    /// @param _kyc_expiry kyc expiry of the buyer
    /// @return {
    ///   "_daily_limit": ""
    ///   "_lifetime_limit": ""
    ///   "_purchased_today": ""
    ///   "_purchased_lifetime": ""
    ///   "_marketplace_balance": ""
    ///   "_marketplace_daily_limit": ""
    ///   "_minimum_purchase_amount": ""
    /// }
    function getPurchaseLimits(
        address _buyer,
        uint256 _kyc_tier,
        uint256 _kyc_expiry
    )
        public
        returns (
            uint256 _daily_limit,
            uint256 _lifetime_limit,
            uint256 _purchased_today,
            uint256 _purchased_lifetime,
            uint256 _marketplace_balance,
            uint256 _marketplace_daily_limit,
            uint256 _minimum_purchase_amount,
            uint256 _maximum_block_drift
        )
    {
        (_daily_limit, _lifetime_limit) = marketplace_controller_electron().get_user_limits(_buyer, _kyc_tier, _kyc_expiry);
        (, _purchased_today) = marketplace_storage().read_user(_buyer);
        (,, _purchased_lifetime,,) = marketplace_storage_electron().read_user(_buyer);
        _marketplace_balance = token_contract().balanceOf(get_contract(CONTRACT_STORAGE_MARKETPLACE));
        (_marketplace_daily_limit, _minimum_purchase_amount, _maximum_block_drift,) = marketplace_storage().read_config();
    }
}
/// @title Digix's Marketplace
/// @author Digix Holdings Pte Ltd
/// @notice This contract is for KYC-approved users to purchase DGX using ETH
function addFiatPurchase(
    address _buyer,
    uint256 _kyc_tier,
    uint256 _kyc_expiry,
    uint256 _amount_dgx_ng
)
    public
{
    marketplace_controller_electron().add_purchase_with_fiat(msg.sender, _buyer, _kyc_tier, _kyc_expiry, _amount_dgx_ng);
}

/// @dev purchase DGX gold using fiat methods
///      verification is done in one of the servers
///      this method is supposed to be called by a key with designated role
/// @param _buyer Ethereum address of the user who has made the fiat purchase
/// @param _kyc_tier KYC tier of the user
/// @param _kyc_expiry KYC expiry of the user
/// @param _amount_dgx_ng nanograms of DGX purchased by the user