Add editoffer to CLI

This commit is contained in:
ghubstan 2021-06-13 12:56:29 -03:00
parent 2344285ed3
commit be249c5e79
No known key found for this signature in database
GPG Key ID: E35592D6800A861E
3 changed files with 76 additions and 30 deletions

View File

@ -39,10 +39,7 @@ import java.util.List;
import lombok.extern.slf4j.Slf4j;
import static bisq.cli.CurrencyFormat.formatMarketPrice;
import static bisq.cli.CurrencyFormat.formatTxFeeRateInfo;
import static bisq.cli.CurrencyFormat.toSatoshis;
import static bisq.cli.CurrencyFormat.toSecurityDepositAsPct;
import static bisq.cli.CurrencyFormat.*;
import static bisq.cli.Method.*;
import static bisq.cli.TableFormat.*;
import static bisq.cli.opts.OptLabel.*;
@ -59,6 +56,7 @@ import bisq.cli.opts.CancelOfferOptionParser;
import bisq.cli.opts.CreateCryptoCurrencyPaymentAcctOptionParser;
import bisq.cli.opts.CreateOfferOptionParser;
import bisq.cli.opts.CreatePaymentAcctOptionParser;
import bisq.cli.opts.EditOfferOptionParser;
import bisq.cli.opts.GetAddressBalanceOptionParser;
import bisq.cli.opts.GetBTCMarketPriceOptionParser;
import bisq.cli.opts.GetBalanceOptionParser;
@ -200,7 +198,7 @@ public class CliMain {
}
var currencyCode = opts.getCurrencyCode();
var price = client.getBtcPrice(currencyCode);
out.println(formatMarketPrice(price));
out.println(formatInternalFiatPrice(price));
return;
}
case getfundingaddresses: {
@ -337,6 +335,7 @@ public class CliMain {
var marketPriceMargin = opts.getMktPriceMarginAsBigDecimal();
var securityDeposit = toSecurityDepositAsPct(opts.getSecurityDeposit());
var makerFeeCurrencyCode = opts.getMakerFeeCurrencyCode();
var triggerPrice = 0; // Cannot be defined until offer is in book.
var offer = client.createOffer(direction,
currencyCode,
amount,
@ -346,10 +345,34 @@ public class CliMain {
marketPriceMargin.doubleValue(),
securityDeposit,
paymentAcctId,
makerFeeCurrencyCode);
makerFeeCurrencyCode,
triggerPrice);
out.println(formatOfferTable(singletonList(offer), currencyCode));
return;
}
case editoffer: {
var opts = new EditOfferOptionParser(args).parse();
if (opts.isForHelp()) {
out.println(client.getMethodHelp(method));
return;
}
var offerId = opts.getOfferId();
var fixedPrice = opts.getFixedPrice();
var isUsingMktPriceMargin = opts.isUsingMktPriceMargin();
var marketPriceMargin = opts.getMktPriceMarginAsBigDecimal();
var triggerPrice = toInternalFiatPrice(opts.getTriggerPriceAsBigDecimal());
var enable = opts.getEnableAsSignedInt();
var editOfferType = opts.getOfferEditType();
client.editOffer(offerId,
fixedPrice,
isUsingMktPriceMargin,
marketPriceMargin.doubleValue(),
triggerPrice,
enable,
editOfferType);
out.println("edited offer being re-added to offer book");
return;
}
case canceloffer: {
var opts = new CancelOfferOptionParser(args).parse();
if (opts.isForHelp()) {
@ -754,6 +777,13 @@ public class CliMain {
stream.format(rowFormat, "", "--fixed-price=<price> | --market-price=margin=<percent> \\", "");
stream.format(rowFormat, "", "--security-deposit=<percent> \\", "");
stream.format(rowFormat, "", "[--fee-currency=<bsq|btc>]", "");
stream.format(rowFormat, "", "[--trigger-price=<price>]", "");
stream.println();
stream.format(rowFormat, editoffer.name(), "--offer-id=<offer-id> \\", "Edit offer with id");
stream.format(rowFormat, "", "[--fixed-price=<price>] \\", "");
stream.format(rowFormat, "", "[--market-price=margin=<percent>] \\", "");
stream.format(rowFormat, "", "[--trigger-price=<price>] \\", "");
stream.format(rowFormat, "", "[--enabled=<true|false>]", "");
stream.println();
stream.format(rowFormat, canceloffer.name(), "--offer-id=<offer-id>", "Cancel offer with id");
stream.println();

View File

@ -35,7 +35,12 @@ import static java.math.RoundingMode.UNNECESSARY;
@VisibleForTesting
public class CurrencyFormat {
private static final NumberFormat NUMBER_FORMAT = NumberFormat.getInstance(Locale.US);
// Formats numbers in US locale, human friendly style.
private static final NumberFormat FRIENDLY_NUMBER_FORMAT = NumberFormat.getInstance(Locale.US);
// Formats numbers for internal use, i.e., grpc request parameters.
private static final DecimalFormat INTERNAL_FIAT_DECIMAL_FORMAT = new DecimalFormat("##############0.0000");
private static final DecimalFormat INTERNAL_ALTCOIN_DECIMAL_FORMAT = new DecimalFormat("##############0.00000000");
static final BigDecimal SATOSHI_DIVISOR = new BigDecimal(100_000_000);
static final DecimalFormat BTC_FORMAT = new DecimalFormat("###,##0.00000000");
@ -59,9 +64,9 @@ public class CurrencyFormat {
public static String formatBsqAmount(long bsqSats) {
// BSQ sats = trade.getOffer().getVolume()
NUMBER_FORMAT.setMinimumFractionDigits(2);
NUMBER_FORMAT.setMaximumFractionDigits(2);
NUMBER_FORMAT.setRoundingMode(HALF_UP);
FRIENDLY_NUMBER_FORMAT.setMinimumFractionDigits(2);
FRIENDLY_NUMBER_FORMAT.setMaximumFractionDigits(2);
FRIENDLY_NUMBER_FORMAT.setRoundingMode(HALF_UP);
return SEND_BSQ_FORMAT.format((double) bsqSats / SATOSHI_DIVISOR.doubleValue());
}
@ -95,38 +100,48 @@ public class CurrencyFormat {
: formatCryptoCurrencyOfferVolume(volume);
}
public static String formatMarketPrice(double price) {
NUMBER_FORMAT.setMinimumFractionDigits(4);
NUMBER_FORMAT.setMaximumFractionDigits(4);
return NUMBER_FORMAT.format(price);
public static String formatInternalFiatPrice(BigDecimal price) {
INTERNAL_FIAT_DECIMAL_FORMAT.setMinimumFractionDigits(4);
INTERNAL_FIAT_DECIMAL_FORMAT.setMaximumFractionDigits(4);
return INTERNAL_FIAT_DECIMAL_FORMAT.format(price);
}
public static String formatInternalFiatPrice(double price) {
FRIENDLY_NUMBER_FORMAT.setMinimumFractionDigits(4);
FRIENDLY_NUMBER_FORMAT.setMaximumFractionDigits(4);
return FRIENDLY_NUMBER_FORMAT.format(price);
}
public static String formatPrice(long price) {
NUMBER_FORMAT.setMinimumFractionDigits(4);
NUMBER_FORMAT.setMaximumFractionDigits(4);
NUMBER_FORMAT.setRoundingMode(UNNECESSARY);
return NUMBER_FORMAT.format((double) price / 10_000);
FRIENDLY_NUMBER_FORMAT.setMinimumFractionDigits(4);
FRIENDLY_NUMBER_FORMAT.setMaximumFractionDigits(4);
FRIENDLY_NUMBER_FORMAT.setRoundingMode(UNNECESSARY);
return FRIENDLY_NUMBER_FORMAT.format((double) price / 10_000);
}
public static String formatCryptoCurrencyPrice(long price) {
NUMBER_FORMAT.setMinimumFractionDigits(8);
NUMBER_FORMAT.setMaximumFractionDigits(8);
NUMBER_FORMAT.setRoundingMode(UNNECESSARY);
return NUMBER_FORMAT.format((double) price / SATOSHI_DIVISOR.doubleValue());
FRIENDLY_NUMBER_FORMAT.setMinimumFractionDigits(8);
FRIENDLY_NUMBER_FORMAT.setMaximumFractionDigits(8);
FRIENDLY_NUMBER_FORMAT.setRoundingMode(UNNECESSARY);
return FRIENDLY_NUMBER_FORMAT.format((double) price / SATOSHI_DIVISOR.doubleValue());
}
public static String formatOfferVolume(long volume) {
NUMBER_FORMAT.setMinimumFractionDigits(0);
NUMBER_FORMAT.setMaximumFractionDigits(0);
NUMBER_FORMAT.setRoundingMode(HALF_UP);
return NUMBER_FORMAT.format((double) volume / 10_000);
FRIENDLY_NUMBER_FORMAT.setMinimumFractionDigits(0);
FRIENDLY_NUMBER_FORMAT.setMaximumFractionDigits(0);
FRIENDLY_NUMBER_FORMAT.setRoundingMode(HALF_UP);
return FRIENDLY_NUMBER_FORMAT.format((double) volume / 10_000);
}
public static String formatCryptoCurrencyOfferVolume(long volume) {
NUMBER_FORMAT.setMinimumFractionDigits(2);
NUMBER_FORMAT.setMaximumFractionDigits(2);
NUMBER_FORMAT.setRoundingMode(HALF_UP);
return NUMBER_FORMAT.format((double) volume / SATOSHI_DIVISOR.doubleValue());
FRIENDLY_NUMBER_FORMAT.setMinimumFractionDigits(2);
FRIENDLY_NUMBER_FORMAT.setMaximumFractionDigits(2);
FRIENDLY_NUMBER_FORMAT.setRoundingMode(HALF_UP);
return FRIENDLY_NUMBER_FORMAT.format((double) volume / SATOSHI_DIVISOR.doubleValue());
}
public static long toInternalFiatPrice(BigDecimal humanFriendlyFiatPrice) {
return humanFriendlyFiatPrice.multiply(new BigDecimal(10_000)).longValue();
}
public static long toSatoshis(String btc) {

View File

@ -25,6 +25,7 @@ public enum Method {
confirmpaymentreceived,
confirmpaymentstarted,
createoffer,
editoffer,
createpaymentacct,
createcryptopaymentacct,
getaddressbalance,