static public LedgerEntryType ledgerEntryType(STObject obj) { return (LedgerEntryType) obj.get(Field.LedgerEntryType); }
public static STObject fromJSON(String offerJson) { return fromJSONObject(new JSONObject(offerJson)); } public static STObject fromJSONObject(JSONObject json) {
private STObject rebuildFromMeta(boolean layerPrevious) { STObject mixed = new STObject(); boolean created = isCreatedNode(); if (!wrapped.has(finalFields)) { STObject source = new STObject(wrapped.getFields()); source.put(Hash256.index, wrapped.get(Hash256.LedgerIndex)); return STObject.formatted(source); STObject finals = (STObject) wrapped.get(finalFields); for (Field field : finals) { mixed.put(field, finals.get(field)); if (layerPrevious && wrapped.has(Field.PreviousFields)) { STObject previous = wrapped.get(STObject.PreviousFields); STObject changed = new STObject(); mixed.put(Field.FinalFields, changed); mixed.put(field, previous.get(field)); changed.put(field, finals.get(field)); continue; default: SerializedType value = wrapped.get(field); mixed.put(field, value); return STObject.formatted(mixed);
public void setFromSTObject(STObject so) { if (so.has(AccountID.Account)) Account = so.get(AccountID.Account); if (so.has(Amount.Balance)) Balance = so.get(Amount.Balance); if (so.has(UInt32.Sequence)) Sequence = so.get(UInt32.Sequence); if (so.has(UInt32.OwnerCount)) OwnerCount = so.get(UInt32.OwnerCount); if (so.has(UInt32.Flags)) Flags = so.get(UInt32.Flags); if (so.has(Hash256.PreviousTxnID)) PreviousTxnID = so.get(Hash256.PreviousTxnID); if (so.has(UInt32.PreviousTxnLgrSeq)) PreviousTxnLgrSeq = so.get(UInt32.PreviousTxnLgrSeq); notifyUpdate(); }
public Map<AccountID, STObject> modifiedRoots() { HashMap<AccountID, STObject> accounts = null; if (meta.has(Field.AffectedNodes)) { accounts = new HashMap<AccountID, STObject>(); STArray affected = meta.get(STArray.AffectedNodes); for (STObject node : affected) { if (node.has(Field.ModifiedNode)) { node = node.get(STObject.ModifiedNode); if (STObject.ledgerEntryType(node) == LedgerEntryType.AccountRoot) { STObject finalFields = node.get(STObject.FinalFields); AccountID key; if (finalFields != null) { key = finalFields.get(AccountID.Account); accounts.put(key, node); } } } } } return accounts; }
public STObject executed(STObject finalFields) { // where `this` is an AffectedNode nodeAsPrevious STObject executed = new STObject(); executed.put(Amount.TakerPays, finalFields.get(Amount.TakerPays).subtract(takerPays())); executed.put(Amount.TakerGets, finalFields.get(Amount.TakerGets).subtract(takerGets())); return executed; }
public void checkFormat() { Format fmt = getFormat(); EnumMap<Field, Format.Requirement> requirements = fmt.requirements(); for (Field field : this) { if (!requirements.containsKey(field)) { throw new FormatException(fmt.name() + " doesn't have field: " + field); } } for (Field field : requirements.keySet()) { Format.Requirement req = requirements.get(field); if (!has(field)) { if (req == Format.Requirement.REQUIRED) { throw new FormatException(fmt.name() + " requires " + field + " of type " + field.getType()); } } else { SerializedType type = get(field); if (type.type() != field.getType()) { if (!(field.getType() == Type.Hash160 && type.type() == Type.AccountID)) { throw new FormatException(type.toString() + " is not " + field.getType()); } } } } }
public static void testOfferQuality() throws Exception { JSONObject transaction = new JSONObject(getFileText("offer-create-txn.json")); JSONObject metaJSON = (JSONObject) transaction.remove("meta"); TransactionMeta meta = (TransactionMeta) STObject.fromJSONObject(metaJSON); Transaction txn = (Transaction) STObject.fromJSONObject(transaction); System.out.println("Get/Pay: " + offer.getPayCurrencyPair()); System.out.println("Ask: " + offer.directoryAskQuality().stripTrailingZeros().toPlainString()); System.out.println("Paid: " + executed.get(Amount.TakerPays)); System.out.println("Got: " + executed.get(Amount.TakerGets)); System.out.println("---------------------------------------------------------------"); System.out.println(offer.prettyJSON());
@Override public STArray fromParser(BinaryParser parser, Integer hint) { STArray stArray = new STArray(); while (!parser.end()) { Field field = parser.readField(); if (field == Field.ArrayEndMarker) { break; } STObject outer = new STObject(); // assert field.getType() == Type.STObject; outer.put(field, STObject.translate.fromParser(parser)); stArray.add(STObject.formatted(outer)); } return stArray; }
@Override public STObject fromJSONObject(JSONObject jsonObject) { STObject so = new STObject(); Iterator keys = jsonObject.keys(); while (keys.hasNext()) { String key = (String) keys.next(); Object value = jsonObject.get(key); Field fieldKey = Field.fromString(key); if (fieldKey == null) { continue; } so.putTranslated(fieldKey, value); } return STObject.formatted(so); } }
public Format getFormat() { if (format == null) computeFormat(); return format; }
public CreateOffer (Client client, String seed) { Account account = client.accountFromSeed(seed); TransactionManager tm = account.transactionManager(); OfferCreate offer = new OfferCreate(); offer.as(Amount.TakerPays, "1000000") .as(Amount.TakerGets, "1/USD/" + account.id()); tm.queue(tm.manage(offer) .onValidated(this::onValidated) .onError(this::onError)); }
public AccountID createdAccount() { AccountID destination = null; Hash256 destinationIndex = null; if (transactionType() == TransactionType.Payment && meta.has(Field.AffectedNodes)) { STArray affected = meta.get(STArray.AffectedNodes); for (STObject node : affected) { if (node.has(STObject.CreatedNode)) { STObject created = node.get(STObject.CreatedNode); if (STObject.ledgerEntryType(created) == LedgerEntryType.AccountRoot) { if (destination == null) { destination = txn.get(AccountID.Destination); destinationIndex = Index.accountRoot(destination); } if (destinationIndex.equals(created.get(Hash256.LedgerIndex))) { return destination; } } } } } return null; }
@Override public STObject fromParser(BinaryParser parser, Integer hint) { STObject so = new STObject(); TypeTranslator<SerializedType> tr; SerializedType st; Field field; Integer sizeHint; // hint, is how many bytes to parse if (hint != null) { // end hint hint = parser.pos() + hint; } while (!parser.end(hint)) { field = parser.readField(); if (field == Field.ObjectEndMarker) { break; } tr = Translators.forField(field); sizeHint = field.isVLEncoded() ? parser.readVLLength() : null; st = tr.fromParser(parser, sizeHint); if (st == null) { throw new IllegalStateException("Parsed " + field + " as null"); } so.put(field, st); } return STObject.formatted(so); }
public <T extends HasField> boolean has(T hf) { return has(hf.getField()); }
public static EngineResult engineResult(STObject obj) { return (EngineResult) obj.get(Field.TransactionResult); }
@Override public STArray fromJSONArray(JSONArray jsonArray) { STArray arr = new STArray(); for (int i = 0; i < jsonArray.length(); i++) { Object o = jsonArray.get(i); arr.add(STObject.fromJSONObject((JSONObject) o)); } return arr; } }