/******************************************************************************
* 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.Alias;
import nxt.Attachment;
import nxt.Constants;
import nxt.NxtException;
import nxt.util.Convert;
import org.json.simple.JSONStreamAware;
import javax.servlet.http.HttpServletRequest;
import static nxt.http.JSONResponses.INCORRECT_ALIAS_OWNER;
import static nxt.http.JSONResponses.INCORRECT_RECIPIENT;
public final class SellAlias extends CreateTransaction {
static final SellAlias instance = new SellAlias();
private SellAlias() {
super(new APITag[] {APITag.ALIASES, APITag.CREATE_TRANSACTION}, "alias", "aliasName", "recipient", "priceNQT");
}
@Override
JSONStreamAware processRequest(HttpServletRequest req) throws NxtException {
Alias alias = ParameterParser.getAlias(req);
Account owner = ParameterParser.getSenderAccount(req);
long priceNQT = ParameterParser.getLong(req, "priceNQT", 0L, Constants.MAX_BALANCE_NQT, true);
String recipientValue = Convert.emptyToNull(req.getParameter("recipient"));
long recipientId = 0;
if (recipientValue != null) {
try {
recipientId = Convert.parseAccountId(recipientValue);
} catch (RuntimeException e) {
return INCORRECT_RECIPIENT;
}
if (recipientId == 0) {
return INCORRECT_RECIPIENT;
}
}
if (alias.getAccountId() != owner.getId()) {
return INCORRECT_ALIAS_OWNER;
}
Attachment attachment = new Attachment.MessagingAliasSell(alias.getAliasName(), priceNQT);
return createTransaction(req, owner, recipientId, 0, attachment);
}
}