Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
dir_nodes.forEach(function (node) {
var book = {
taker_gets: {
currency: UInt160.from_generic(node.TakerGetsCurrency).to_json(),
issuer: UInt160.from_generic(node.TakerGetsIssuer).to_json()
},
taker_pays: {
currency: UInt160.from_generic(node.TakerPaysCurrency).to_json(),
issuer: UInt160.from_generic(node.TakerPaysIssuer).to_json()
},
quality: Amount.from_quality(node.RootIndex),
index: node.RootIndex
};
books[book_key(book)] = book;
// console.log(JSON.stringify(node, undefined, 2));
});
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));
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());
txData.meta.AffectedNodes.forEach( function( affNode ) {
var node = affNode.CreatedNode || affNode.ModifiedNode || affNode.DeletedNode;
if ( node.LedgerEntryType !== 'Offer' || !node.PreviousFields || !node.PreviousFields.TakerPays || !node.PreviousFields.TakerGets ) {
return;
}
var exchange_rate = Amount.from_quality( node.FinalFields.BookDirectory ).to_json( ).value,
pay_curr,
pay_amnt,
get_curr,
get_amnt,
currPair,
revCurrPair;
// parse TakerPays and TakerGets and handle XRP case
if ( typeof node.PreviousFields.TakerPays === "object" ) {
pay_curr = [ node.PreviousFields.TakerPays.currency, node.PreviousFields.TakerPays.issuer ];
pay_amnt = node.PreviousFields.TakerPays.value - node.FinalFields.TakerPays.value;
} else {
pay_curr = [ "XRP" ];
pay_amnt = ( node.PreviousFields.TakerPays - node.FinalFields.TakerPays ) / 1000000.0; // convert from drops
exchange_rate = exchange_rate / 1000000.0;
}
return false;
}
// Pair ordering determined by IDs
if (cg.id < cp.id || (cg.id === cp.id && ig.id < ip.id)) {
an.reverse = false;
} else {
an.reverse = true;
}
an.c1 = an.reverse ? cp.id : cg.id;
an.c2 = an.reverse ? cg.id : cp.id;
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();
type = 'DeletedNode';
var base = type ? n[type] : undefined;
if (base // A relevant type.
&& base.LedgerEntryType === 'Offer'
&& base.PreviousFields // Not an unfunded delete.
&& 'TakerGets' in base.PreviousFields // Not a microscopic offer
&& 'TakerPays' in base.PreviousFields) { // Not a microscopic offer
var pf = base.PreviousFields;
var ff = base.FinalFields;
var offer_owner = ff.Account;
var offer_sequence = ff.Sequence;
var taker_got = Amount.from_json(pf.TakerGets).subtract(Amount.from_json(ff.TakerGets));
var taker_paid = Amount.from_json(pf.TakerPays).subtract(Amount.from_json(ff.TakerPays));
var book_price = Amount.from_quality(ff.BookDirectory, "1", "1");
if (taker_got.is_native())
{
buying = true;
book_price = book_price.multiply(Amount.from_json("1000000")); // Adjust for drops: The result would be a million times too small.
book_price = Amount.from_json("1.0/1/1").divide(book_price);
var tg = taker_got;
var tp = taker_paid;
taker_got = tp;
taker_paid = tg;
}
else
{
book_price = book_price.divide(Amount.from_json("1000000")); // Adjust for drops: The result would be a million times too large.
function parseOfferExercised (node, tx) {
var exchangeRate = Amount.from_quality(node.FinalFields.BookDirectory).to_json().value;
var counterparty = node.FinalFields.Account;
var base;
var counter;
if ( typeof node.PreviousFields.TakerPays === "object" ) {
base = {
currency : node.PreviousFields.TakerPays.currency,
issuer : node.PreviousFields.TakerPays.issuer,
amount : node.PreviousFields.TakerPays.value - node.FinalFields.TakerPays.value
}
} else {
base = {
currency : 'XRP',
amount : (node.PreviousFields.TakerPays - node.FinalFields.TakerPays) / 1000000.0
}
var offers = book_entries.map(function(book_entry){
var curr1_volume = (typeof book_entry.TakerPays === "object" ? book_entry.TakerPays.value : parseInt(book_entry.TakerPays, 10) / 1000000.0);
var curr2_volume = (typeof book_entry.TakerGets === "object" ? book_entry.TakerGets.value : parseInt(book_entry.TakerGets, 10) / 1000000.0);
var exchange_rate = Amount.from_quality(book_entry.BookDirectory).to_json().value;
if (typeof book_entry.TakerPays !== "object")
exchange_rate = exchange_rate / 1000000.0;
if (typeof book_entry.TakerGets !== "object")
exchange_rate = exchange_rate * 1000000.0;
return {curr1_volume: curr1_volume, curr2_volume: curr2_volume, price: exchange_rate};
});