/******************************************************************************
* 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.Account;
import nxt.Attachment;
import nxt.Currency;
import nxt.NxtException;
import org.json.simple.JSONStreamAware;
import javax.servlet.http.HttpServletRequest;
/**
* Sell currency for NXT
* <p>
* Parameters
* <ul>
* <li>currency - currency id
* <li>rateNQT - exchange rate between NXT amount and currency units
* <li>units - number of units to sell
* </ul>
*
* <p>
* currency sell transaction attempts to match existing exchange offers. When a match is found, the minimum number of units
* between the number of units offered and the units requested are exchanged at a rate matching the lowest buy offer<br>
* A single transaction can match multiple buy offers or none.
* Unlike asset ask order, currency sell is not saved. It's either executed immediately (fully or partially) or not executed
* at all.
* For every match between buyer and seller an exchange record is saved, exchange records can be retrieved using the {@link GetExchanges} API
*/
public final class CurrencySell extends CreateTransaction {
static final CurrencySell instance = new CurrencySell();
private CurrencySell() {
super(new APITag[] {APITag.MS, APITag.CREATE_TRANSACTION}, "currency", "rateNQT", "units");
}
@Override
JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
Currency currency = ParameterParser.getCurrency(req);
long rateNQT = ParameterParser.getLong(req, "rateNQT", 0, Long.MAX_VALUE, true);
long units = ParameterParser.getLong(req, "units", 0, Long.MAX_VALUE, true);
Account account = ParameterParser.getSenderAccount(req);
Attachment attachment = new Attachment.MonetarySystemExchangeSell(currency.getId(), rateNQT, units);
try {
return createTransaction(req, account, attachment);
} catch (NxtException.InsufficientBalanceException e) {
return JSONResponses.NOT_ENOUGH_CURRENCY;
}
}
}