public JSONObject toJSONObject() { if (isNative()) { throw new RuntimeException("Native amounts must be serialized as a string"); } JSONObject out = new JSONObject(); out.put("currency", currencyString()); out.put("value", valueText()); out.put("issuer", issuerString()); return out; }
public Amount multiply(Amount multiplicand) { return multiply(multiplicand.value); }
public Amount copy() { return new Amount(value, currency, issuer, isNative, unbounded); }
private Amount newValue(BigDecimal newValue, boolean round, boolean unbounded) { if (round) { newValue = roundValue(newValue, isNative); } return new Amount(newValue, currency, issuer, isNative, unbounded); }
/** * @return A String representation as used by ripple json format */ public String stringRepr() { if (isNative()) { return toDropsString(); } else { return iouTextFull(); } }
public String toTextFull() { if (isNative()) { return nativeText(); } else { return iouTextFull(); } }
@Override public void toBytesSink(BytesSink to) { UInt64 man = mantissa(); if (isNative()) { if (!isNegative()) { man = man.or(BINARY_FLAG_IS_NON_NEGATIVE_NATIVE); } to.add(man.toByteArray()); } else { int exponent = exponent(); UInt64 packed; if (isZero()) { packed = BINARY_FLAG_IS_IOU; } else if (isNegative()) { packed = man.or(new UInt64(512 + 0 + 97 + exponent).shiftLeft(64 - 10)); } else { packed = man.or(new UInt64(512 + 256 + 97 + exponent).shiftLeft(64 - 10)); } to.add(packed.toByteArray()); to.add(currency.bytes()); to.add(issuer.bytes()); } }
private static void showOfferInfo(Offer offer) { BigDecimal payForOne = offer.askQuality(); Amount getsOne = offer.getsOne(); Amount paysOne = offer.paysOne(); printSeparatorBanner(); System.out.println("Directory quality: "+ offer.directoryAskQuality().toPlainString()); // Multiply and divide will round/scale to the required bounds print("%40s == %s\n", paysOne.multiply(payForOne).toText(), getsOne.toText()); print("%40s == %s\n", getsOne.divide(payForOne).toText(), paysOne.toText()); }
private void calculateStats() { Offer firstAsk = (Offer) asks.get(0); Offer firstBid = (Offer) bids.get(0); BigDecimal askQuality = firstAsk.askQuality(); BigDecimal bidQuality = firstBid.bidQuality(); Amount secondOne = firstAsk.paysOne(); ask = secondOne.multiply(askQuality); bid = secondOne.multiply(bidQuality); spread = ask.subtract(bid).abs(); }
@Override public Object toJSON() { if (isNative()) { return toDropsString(); } else { return toJSONObject(); } }
public String toText() { if (isNative()) { return nativeText(); } else { return iouText(); } }
public boolean hasNativeSource() { for (Alternative alt : this) { if (alt.sourceAmount.isNative()) { return true; } } return false; }
/** * @return a positive value for the mantissa */ private UInt64 calculateMantissa() { if (isNative()) { return new UInt64(bigIntegerDrops().abs()); } else { return new UInt64(bigIntegerIOUMantissa()); } }
private void initialize() { if (isNative()) { issuer = AccountID.XRP_ISSUER; if (!unbounded) { checkXRPBounds(); } // Offset is unused for native amounts exponent = -6; // compared to drops. } else { issuer = AccountID.NEUTRAL; exponent = calculateExponent(); if (value.precision() > MAXIMUM_IOU_PRECISION && !unbounded) { String err = "value precision of " + value.precision() + " is greater than maximum " + "iou precision of " + MAXIMUM_IOU_PRECISION; throw new PrecisionError(err, this); } } }
public String iouTextFull() { return String.format("%s/%s/%s", valueText(), currencyString(), issuerString()); }
public String toDropsString() { if (!isNative()) { throw new RuntimeException("Amount is not native"); } return bigIntegerDrops().toString(); }
/** * Thread: Client thread */ private boolean accountIsUnfunded() { return account.getAccountRoot().getBalance().isZero(); }