How to use the ripple-lib.Amount.from_json function in ripple-lib

To help you get started, we’ve selected a few ripple-lib examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ripple / rippled / test / testutils.js View on Github external
function verify_limit(remote, src, amount, callback) {
  assert.strictEqual(arguments.length, 4);

  var _m = amount.match(/^(\d+\/...\/[^\:]+)(?::(\d+)(?:,(\d+))?)?$/);

  if (!_m) {
    // console.log('credit_limit: parse error: %s', amount);
    return callback(new Error('parse_error'));
  }

  // console.log('_m', _m.length, _m);
  // console.log('verify_limit: parsed: %s', JSON.stringify(_m, undefined, 2));
  var account_limit = _m[1];
  var quality_in    = Number(_m[2]);
  var quality_out   = Number(_m[3]);
  var limit         = Amount.from_json(account_limit);

  var options = {
    account:   src,
    issuer:    limit.issuer().to_json(),
    currency:  limit.currency().to_json(),
    ledger:    'current'
  };

  remote.request_ripple_balance(options, function(err, m) {
    if (err) {
      callback(err);
    } else {
      assert(m.account_limit.equals(limit));
      assert(isNaN(quality_in) || m.account_quality_in === quality_in);
      assert(isNaN(quality_out) || m.account_quality_out === quality_out);
      callback(null);
github ripple-unmaintained / ripple-chart / rchart.js View on Github external
if (n.ModifiedNode)
                base  = n.ModifiedNode;
              else if (n.DeletedNode)
                base  = n.DeletedNode;

              if (base
                && base.LedgerEntryType === 'Offer'
                && base.PreviousFields
                && 'TakerPays' in base.PreviousFields
                && 'TakerGets' in base.PreviousFields) {
                var pf          = base.PreviousFields;
                var ff          = base.FinalFields;
// console.log("- l=%s node=%s", ledger.ledger_index, JSON.stringify(base, undefined, 2));
//
                // The price of taker_get/taker_pays
                var taker_pays    = Amount.from_json(pf.TakerPays).subtract(Amount.from_json(ff.TakerPays));
                var taker_gets    = Amount.from_json(pf.TakerGets).subtract(Amount.from_json(ff.TakerGets));

                var pays_currency = taker_pays.currency().to_human();
                var pays_issuer   = taker_pays.issuer().to_json();
                var gets_currency = taker_gets.currency().to_human();
                var gets_issuer   = taker_gets.issuer().to_json();

                if (taker_gets.is_positive()
                  && taker_pays.is_positive()
                  && markets[pays_currency]
                  && (markets[pays_currency][pays_issuer] || pays_currency == 'XRP')
                  && markets[gets_currency]
                  && (markets[gets_currency][gets_issuer] || gets_currency == 'XRP'))
                {
// console.log("* l=%s node=%s", ledger.ledger_index, JSON.stringify(base));
//  console.log("l=%s t=%s", ledger.ledger_index, JSON.stringify(t));
github ripple-unmaintained / ripple-chart / rchart.js View on Github external
//  console.log("l=%s t=%s", ledger.ledger_index, JSON.stringify(t));
                  var book_price    = Amount.from_quality(ff.BookDirectory, "1", "1");
//console.log("book_price> ", book_price.to_human_full());

                  if (taker_pays.is_native())
                  {
                    // Adjust for drops: The result would be a million times too large.

                    book_price  = book_price.divide(Amount.from_json("1000000"));
                  }

                  if (taker_gets.is_native())
                  {
                    // Adjust for drops: The result would be a million times too small.

                    book_price  = book_price.multiply(Amount.from_json("1000000"));
                  }
//console.log("book_price< ", book_price.to_human_full());
//console.log(" old_price> ", taker_gets.ratio_human(taker_pays).to_human_full());
//console.log(" old_price< ", taker_pays.ratio_human(taker_gets).to_human_full());
//console.log("taker_pays> ", taker_pays.to_human_full());
//console.log("taker_gets> ", taker_gets.to_human_full());
//console.log("book_price> ", book_price.to_human_full());
                  var gets_price  = book_price;
                  var pays_price  = Amount.from_json("1.0/1/1").divide(gets_price);
                  var record      = {
                    TransactionIndex: t.TransactionIndex,
                    Hash:         t.hash,
                    Market:       pays_currency < gets_currency
                                    ? pays_currency+gets_currency
                                    : gets_currency+pays_currency,
                    Pays:         pays_currency,
github ripple-unmaintained / ripple-watch / bot.js View on Github external
+ UInt160.json_rewrite(m.transaction.Destination, opts_gateways) + dt
                    + (created ? "\u000f" : "");

    process_offers(m);
  }
  else if (m.transaction.TransactionType === 'AccountSet')
  {
    console.log("transaction: ", JSON.stringify(m, undefined, 2));

    say_type  = 'ACT';
    say_watch = UInt160.json_rewrite(m.transaction.Account, opts_gateways);
  }
  else if (m.transaction.TransactionType === 'TrustSet')
  {
    var limit = 'LimitAmount' in m.transaction
                  ? Amount.from_json(m.transaction.LimitAmount).to_human_full(opts_gateways) + " "
                  : "";

    say_type  = 'TRS';
    say_watch = limit
                  + UInt160.json_rewrite(m.transaction.Account, opts_gateways);
  }
  else if (m.transaction.TransactionType === 'OfferCreate')
  {
    // console.log("OfferCreate: ", JSON.stringify(m, undefined, 2));

    var owner       = UInt160.json_rewrite(m.transaction.Account, opts_gateways);
    var taker_gets  = Amount.from_json(m.transaction.TakerGets);
    var taker_pays  = Amount.from_json(m.transaction.TakerPays);
    var b_fok       = !!(m.transaction.Flags & Transaction.flags.OfferCreate.FillOrKill);
    var b_ioc       = !!(m.transaction.Flags & Transaction.flags.OfferCreate.ImmediateOrCancel);
github ripple / ripple-data-api / interpreter.js View on Github external
var cur, account, balance_new, balance_old, negate = false;
      if ((cur = currenciesById[an.fields.LowLimit.currency + ":" + an.fields.LowLimit.issuer])) {
        account = an.fields.HighLimit.issuer;
        negate = true;
      } else if ((cur = currenciesById[an.fields.HighLimit.currency + ":" + an.fields.HighLimit.issuer])) {
        account = an.fields.LowLimit.issuer;
      } else return;

      var gateway = index.issuers[cur.gat];

      balance_new = (an.diffType === "DeletedNode")
        ? Amount.from_json("0/"+cur.cur+"/"+cur.iss)
        : Amount.from_json(an.fieldsFinal.Balance);
      balance_old = (an.diffType === "CreatedNode")
        ? Amount.from_json("0/"+cur.cur+"/"+cur.iss)
        : Amount.from_json(an.fieldsPrev.Balance);

      if (negate) {
        balance_new = balance_new.negate();
        balance_old = balance_old.negate();
      }

      var balance_diff = balance_new.subtract(balance_old);

      // if (!balance_diff.is_zero())

      if (gateway.hotwallets && gateway.hotwallets[account]) {
        cur.hot = Amount.from_json(cur.hot).add(balance_diff).to_json();

        console.log("HOT", cur.gat, cur.cur, balance_diff.to_text(), Amount.from_json(cur.hot).to_text());
      } else {
        cur.cap = Amount.from_json(cur.cap).add(balance_diff).to_json();
github ripple / ripple-data-api / interpreter.js View on Github external
_.each(index.xrp, function (data) {
    // Initialize field with basic properties
    tickers[data.first] = {
      sym: data.sym,
      first: data.first,
      second: "XRP",
      bid: Amount.from_json("0"),
      ask: Amount.from_json("0"),
      last: Amount.from_json("0"),
      vol: Amount.from_json("0/"+data.first)
    };
  });
github ripple / ripple-data-api / classifier.js View on Github external
an.i1 = an.reverse ? ip.id : ig.id;
      an.i2 = an.reverse ? ig.id : ip.id;

      var price = Amount.from_quality(an.fieldsFinal.BookDirectory, "1", "1");

      var takerGets = Amount.from_json(an.fieldsPrev.TakerGets),
          takerPays = Amount.from_json(an.fieldsPrev.TakerPays);

      if (takerPays.is_native()) {
        // Adjust for drops: The result would be a million times too large.
        price = price.divide(Amount.from_json("1000000"));
      }

      if (takerGets.is_native()) {
        // Adjust for drops: The result would be a million times too small.
        price = price.multiply(Amount.from_json("1000000"));
      }

      an.price = price;
      an.sort = price.to_number();

      return true;
    });
github ripple / ripple-data-api / classifier.js View on Github external
// Transaction aggregates
    if (tx.TransactionType === "Payment" &&
        tx.meta.TransactionResult === "tesSUCCESS" &&
        !tx.Paths &&
        !tx.SendMax) {
      var xrpTransferred = Amount.from_json(tx.Amount).to_number() >>> 0;
      processed.ledger.txs_xrp_total += xrpTransferred;
		} else if (tx.TransactionType === "Payment" &&
              tx.meta.TransactionResult === "tesSUCCESS" &&
              tx.Paths && tx.Paths.length) {
      //txs_cross_total += Amount.from_json(tx.Amount).to_number() * 1;
      processed.ledger.txs_cross_total++;
    }

    // Fee aggregate
    processed.ledger.fees += Amount.from_json(tx.Fee).to_number();

    var isTradingTx = false,
        isTradingPay = false;

    // XXX: Temporary. Won't be needed once the S3 JSONs are fixed.
    var TRANSACTION_TYPES = {
      0:    "Payment",
      3:    "AccountSet",
      5:    "SetRegularKey",
      7:    "OfferCreate",
      8:    "OfferCancel",
      9:    "Contract",
      10:   "RemoveContract",
      20:   "TrustSet",
      100:  "EnableFeature",
      101:  "SetFee"
github ripple / ripple-data-api / classifier.js View on Github external
var cur, cur_issuer, account, balance_new, balance_old, negate = false;
        if ((cur_issuer = index.issuerByCurrencyAddress[an.fields.LowLimit.currency + ":" + an.fields.LowLimit.issuer])) {
          account = an.fields.LowLimit.issuer;
          cur = an.fields.LowLimit.currency;
          negate = true;
        } else if ((cur_issuer = index.issuerByCurrencyAddress[an.fields.HighLimit.currency + ":" + an.fields.HighLimit.issuer])) {
          account = an.fields.HighLimit.issuer;
          cur = an.fields.HighLimit.currency;
        } else return;

        var gateway = cur_issuer;

        balance_new = (an.diffType === "DeletedNode")
          ? Amount.from_json("0/"+cur+"/"+account)
          : Amount.from_json(an.fieldsFinal.Balance);
        balance_old = (an.diffType === "CreatedNode")
          ? Amount.from_json("0/"+cur+"/"+account)
          : Amount.from_json(an.fieldsPrev.Balance);

        if (negate) {
          balance_new = balance_new.negate();
          balance_old = balance_old.negate();
        }

        var balance_diff = balance_new.subtract(balance_old);
        var currency_id = index.currenciesByCode[cur].id;
        var issuer_id = cur_issuer.id;

        var type = 0;
        var hot_val = 0;
        var cap_val = 0;
github ripple / ripple-data-api / classifier.js View on Github external
if ((cur_issuer = index.issuerByCurrencyAddress[an.fields.LowLimit.currency + ":" + an.fields.LowLimit.issuer])) {
          account = an.fields.LowLimit.issuer;
          cur = an.fields.LowLimit.currency;
          negate = true;
        } else if ((cur_issuer = index.issuerByCurrencyAddress[an.fields.HighLimit.currency + ":" + an.fields.HighLimit.issuer])) {
          account = an.fields.HighLimit.issuer;
          cur = an.fields.HighLimit.currency;
        } else return;

        var gateway = cur_issuer;

        balance_new = (an.diffType === "DeletedNode")
          ? Amount.from_json("0/"+cur+"/"+account)
          : Amount.from_json(an.fieldsFinal.Balance);
        balance_old = (an.diffType === "CreatedNode")
          ? Amount.from_json("0/"+cur+"/"+account)
          : Amount.from_json(an.fieldsPrev.Balance);

        if (negate) {
          balance_new = balance_new.negate();
          balance_old = balance_old.negate();
        }

        var balance_diff = balance_new.subtract(balance_old);
        var currency_id = index.currenciesByCode[cur].id;
        var issuer_id = cur_issuer.id;

        var type = 0;
        var hot_val = 0;
        var cap_val = 0;

        if (gateway.hotwallets && gateway.hotwallets[account]) {