/******************************************************************************
* Copyright © 2013-2016 The Nxt Core Developers. *
* *
* See the AUTHORS.txt, DEVELOPER-AGREEMENT.txt and LICENSE.txt files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Nxt software, including this file, may be copied, modified, propagated, *
* or distributed except according to the terms contained in the LICENSE.txt *
* file. *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/
package nxt.http;
import nxt.Attachment;
import nxt.MonetarySystem;
import nxt.Nxt;
import nxt.NxtException;
import nxt.Transaction;
import nxt.util.Filter;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONStreamAware;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
public final class GetExpectedExchangeRequests extends APIServlet.APIRequestHandler {
static final GetExpectedExchangeRequests instance = new GetExpectedExchangeRequests();
private GetExpectedExchangeRequests() {
super(new APITag[] {APITag.ACCOUNTS, APITag.MS}, "account", "currency", "includeCurrencyInfo");
}
@Override
JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
long accountId = ParameterParser.getAccountId(req, "account", false);
long currencyId = ParameterParser.getUnsignedLong(req, "currency", false);
boolean includeCurrencyInfo = "true".equalsIgnoreCase(req.getParameter("includeCurrencyInfo"));
Filter<Transaction> filter = transaction -> {
if (transaction.getType() != MonetarySystem.EXCHANGE_BUY && transaction.getType() != MonetarySystem.EXCHANGE_SELL) {
return false;
}
if (accountId != 0 && transaction.getSenderId() != accountId) {
return false;
}
Attachment.MonetarySystemExchange attachment = (Attachment.MonetarySystemExchange)transaction.getAttachment();
return currencyId == 0 || attachment.getCurrencyId() == currencyId;
};
List<? extends Transaction> transactions = Nxt.getBlockchain().getExpectedTransactions(filter);
JSONArray exchangeRequests = new JSONArray();
transactions.forEach(transaction -> exchangeRequests.add(JSONData.expectedExchangeRequest(transaction, includeCurrencyInfo)));
JSONObject response = new JSONObject();
response.put("exchangeRequests", exchangeRequests);
return response;
}
}