-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpretTransaction.js
More file actions
512 lines (432 loc) · 17 KB
/
interpretTransaction.js
File metadata and controls
512 lines (432 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
/**
* Calculate executed order price
*
* @param effect
* @returns {*}
*/
var getPrice = function(effect, referenceDate){
var g = effect.got ? effect.got : effect.gets;
var p = effect.paid ? effect.paid : effect.pays;
var price;
_.find(pairs, function(pair){
if (pair.name == g.currency().to_human() + '/' + p.currency().to_human()) {
price = p.ratio_human(g, {reference_date: referenceDate});
}
});
if (!price) {
price = g.ratio_human(p, {reference_date: referenceDate});
}
return price;
};
/**
* Determine if the transaction is a "rippling" transaction based on effects
*
* @param effects
*/
var isRippling = function(effects){
if (
effects
&& effects.length
&& 2 === effects.length
&& 'trust_change_balance' == effects[0].type
&& 'trust_change_balance' == effects[1].type
&& effects[0].currency == effects[1].currency
&& !effects[0].amount.compareTo(effects[1].amount.negate())
) {
return true;
}
};
/**
* Simple static class for processing server-side JSON.
*/
var JsonRewriter = module.exports = {
/**
* Filter affected nodes by type.
*
* If affectedNodes is not a valid set of nodes, returns an empty array.
*/
filterAnodes: function (affectedNodes, type) {
if (!affectedNodes) return [];
return affectedNodes.filter(function (an) {
an = an.CreatedNode ? an.CreatedNode :
an.ModifiedNode ? an.ModifiedNode :
{};
return an.LedgerEntryType === type;
});
},
/**
* Returns resulting (new or modified) fields from an affected node.
*/
getAnodeResult: function (an) {
an = an.CreatedNode ? an.CreatedNode :
an.ModifiedNode ? an.ModifiedNode :
{};
var fields = $.extend({}, an.NewFields, an.FinalFields);
return fields;
},
/**
* Takes a metadata affected node and returns a simpler JSON object.
*
* The resulting object looks like this:
*
* {
* // Type of diff, e.g. CreatedNode, ModifiedNode
* diffType: 'CreatedNode'
*
* // Type of node affected, e.g. RippleState, AccountRoot
* entryType: 'RippleState',
*
* // Index of the ledger this change occurred in
* ledgerIndex: '01AB01AB...',
*
* // Contains all fields with later versions taking precedence
* //
* // This is a shorthand for doing things like checking which account
* // this affected without having to check the diffType.
* fields: {...},
*
* // Old fields (before the change)
* fieldsPrev: {...},
*
* // New fields (that have been added)
* fieldsNew: {...},
*
* // Changed fields
* fieldsFinal: {...}
* }
*/
processAnode: function (an) {
var result = {};
["CreatedNode", "ModifiedNode", "DeletedNode"].forEach(function (x) {
if (an[x]) result.diffType = x;
});
if (!result.diffType) return null;
an = an[result.diffType];
result.entryType = an.LedgerEntryType;
result.ledgerIndex = an.LedgerIndex;
result.fields = $.extend({}, an.PreviousFields, an.NewFields, an.FinalFields);
result.fieldsPrev = an.PreviousFields || {};
result.fieldsNew = an.NewFields || {};
result.fieldsFinal = an.FinalFields || {};
return result;
},
/**
* Convert transactions into a more useful (for our purposes) format.
*
* The main operation this function performs is to change the view on the
* transaction from a neutral view to a subjective view specific to our
* account.
*
* For example, rather than having a sender and receiver, the transaction has
* a counterparty and a flag whether it is incoming or outgoing.
*
* processTxn returns main purpose of transaction and side effects.
*
* Main purpose
* Real transaction names
* - Payment (sent/received/exchange)
* - TrustSet (trusting/trusted)
* - OfferCreate (offernew)
* - OfferCancel (offercancel)
*
* Virtual transaction names
* - Failed
* - Rippling
*
* Side effects
* - balance_change
* - Trust (trust_create_local, trust_create_remote, trust_change_local,
* trust_change_remote, trust_change_balance, trust_change_no_ripple)
* - Offer (offer_created, offer_funded, offer_partially_funded,
* offer_cancelled, offer_bought)
*/
processTxn: function (tx, meta, account) {
var obj = {};
// Currency balances that have been affected by the transaction
var affected_currencies = [];
// Main transaction
if (tx.Account === account
|| (tx.Destination && tx.Destination === account)
|| (tx.LimitAmount && tx.LimitAmount.issuer === account)) {
var transaction = {};
if ('tesSUCCESS' === meta.TransactionResult) {
switch (tx.TransactionType) {
case 'Payment':
var amount = ripple.Amount.from_json(tx.Amount);
if (tx.Account === account) {
if (tx.Destination === account) {
transaction.type = 'exchange';
transaction.spent = ripple.Amount.from_json(tx.SendMax);
}
else {
transaction.type = 'sent';
transaction.counterparty = tx.Destination;
}
}
else {
transaction.type = 'received';
transaction.counterparty = tx.Account;
}
transaction.amount = amount;
transaction.currency = amount.currency().to_human();
break;
case 'TrustSet':
transaction.type = tx.Account === account ? 'trusting' : 'trusted';
transaction.counterparty = tx.Account === account ? tx.LimitAmount.issuer : tx.Account;
transaction.amount = ripple.Amount.from_json(tx.LimitAmount);
transaction.currency = tx.LimitAmount.currency;
break;
case 'OfferCreate':
transaction.type = 'offernew';
transaction.pays = ripple.Amount.from_json(tx.TakerPays);
transaction.gets = ripple.Amount.from_json(tx.TakerGets);
transaction.sell = tx.Flags & ripple.Transaction.flags.OfferCreate.Sell;
break;
case 'OfferCancel':
transaction.type = 'offercancel';
break;
case 'AccountSet':
// Ignore empty accountset transactions. (Used to sync sequence numbers)
if (meta.AffectedNodes.length === 1 && _.size(meta.AffectedNodes[0].ModifiedNode.PreviousFields) === 2)
break;
transaction.type = 'accountset';
break;
default:
console.log('Unknown transaction type: "'+tx.TransactionType+'"', tx);
}
if (tx.Flags) {
transaction.flags = tx.Flags;
}
} else {
transaction.type = 'failed';
}
if (!$.isEmptyObject(transaction)) {
obj.transaction = transaction;
}
}
// Side effects
if ('tesSUCCESS' === meta.TransactionResult) {
meta.AffectedNodes.forEach(function (n) {
var node = JsonRewriter.processAnode(n);
var feeEff;
var effect = {};
// AccountRoot - Current account node
if (node.entryType === "AccountRoot" && node.fields.Account === account) {
obj.accountRoot = node.fields;
if (node.fieldsPrev.Balance) {
var balance = ripple.Amount.from_json(node.fields.Balance);
// Fee
if(tx.Account === account && tx.Fee) {
feeEff = {
type: "fee",
amount: ripple.Amount.from_json(tx.Fee).negate(),
balance: balance
};
}
// Updated XRP Balance
if (tx.Fee != node.fieldsPrev.Balance - node.fields.Balance) {
if (feeEff)
balance = balance.subtract(feeEff.amount);
effect.type = "balance_change";
effect.amount = balance.subtract(node.fieldsPrev.Balance);
effect.balance = balance;
// balance_changer is set to true if the transaction / effect has changed one of the account balances
obj.balance_changer = effect.balance_changer = true;
affected_currencies.push('XRP');
}
}
}
// RippleState - Ripple Lines
if (node.entryType === "RippleState"
&& (node.fields.HighLimit.issuer === account || node.fields.LowLimit.issuer === account)) {
var high = node.fields.HighLimit;
var low = node.fields.LowLimit;
var which = high.issuer === account ? 'HighNoRipple' : 'LowNoRipple';
// New trust line
if (node.diffType === "CreatedNode") {
effect.limit = ripple.Amount.from_json(high.value > 0 ? high : low);
effect.limit_peer = ripple.Amount.from_json(high.value > 0 ? low : high);
if ((high.value > 0 && high.issuer === account)
|| (low.value > 0 && low.issuer === account)) {
effect.type = "trust_create_local";
} else {
effect.type = "trust_create_remote";
}
}
// Modified trust line
else if (node.diffType === "ModifiedNode" || node.diffType === "DeletedNode") {
var highPrev = node.fieldsPrev.HighLimit;
var lowPrev = node.fieldsPrev.LowLimit;
// Trust Balance change
if (node.fieldsPrev.Balance) {
effect.type = "trust_change_balance";
var issuer = node.fields.Balance.value > 0 || node.fieldsPrev.Balance.value > 0
? high.issuer : low.issuer;
effect.amount = high.issuer === account
? effect.amount = ripple.Amount.from_json(
node.fieldsPrev.Balance.value
+ "/" + node.fieldsPrev.Balance.currency
+ "/" + issuer).subtract(node.fields.Balance)
: effect.amount = ripple.Amount.from_json(
node.fields.Balance.value
+ "/" + node.fields.Balance.currency
+ "/" + issuer).subtract(node.fieldsPrev.Balance);
obj.balance_changer = effect.balance_changer = true;
affected_currencies.push(high.currency.toUpperCase());
}
// Trust Limit change
else if (highPrev || lowPrev) {
if (high.issuer === account) {
effect.limit = ripple.Amount.from_json(high);
effect.limit_peer = ripple.Amount.from_json(low);
} else {
effect.limit = ripple.Amount.from_json(low);
effect.limit_peer = ripple.Amount.from_json(high);
}
if (highPrev) {
effect.prevLimit = ripple.Amount.from_json(highPrev);
effect.type = high.issuer === account ? "trust_change_local" : "trust_change_remote";
}
else if (lowPrev) {
effect.prevLimit = ripple.Amount.from_json(lowPrev);
effect.type = high.issuer === account ? "trust_change_remote" : "trust_change_local";
}
}
// Trust flag change (effect gets this type only if nothing else but flags has been changed)
else if (node.fieldsPrev.Flags) {
// Account set a noRipple flag
if (node.fields.Flags & ripple.Remote.flags.state[which] &&
!(node.fieldsPrev.Flags & ripple.Remote.flags.state[which])) {
effect.type = "trust_change_no_ripple";
}
// Account removed the noRipple flag
else if (node.fieldsPrev.Flags & ripple.Remote.flags.state[which] &&
!(node.fields.Flags & ripple.Remote.flags.state[which])) {
effect.type = "trust_change_no_ripple";
}
if (effect.type)
effect.flags = node.fields.Flags;
}
}
if (!$.isEmptyObject(effect)) {
effect.counterparty = high.issuer === account ? low.issuer : high.issuer;
effect.currency = high.currency;
effect.balance = high.issuer === account
? ripple.Amount.from_json(node.fields.Balance).negate(true)
: ripple.Amount.from_json(node.fields.Balance);
if (obj.transaction && obj.transaction.type === "trust_change_balance") {
obj.transaction.balance = effect.balance;
}
// noRipple flag
if (node.fields.Flags & ripple.Remote.flags.state[which]) {
effect.noRipple = true;
}
}
}
// Offer
else if (node.entryType === "Offer") {
// For new and cancelled offers we use "fields"
var fieldSet = node.fields;
// Current account offer
if (node.fields.Account === account) {
// Partially funded offer [and deleted.. no more funds]
/* Offer has been partially funded and deleted (because of the luck of funds)
if the node is deleted and the TakerGets/TakerPays field has been changed */
if (node.diffType === "ModifiedNode" ||
(node.diffType === "DeletedNode"
&& node.fieldsPrev.TakerGets
&& !ripple.Amount.from_json(node.fieldsFinal.TakerGets).is_zero())) {
effect.type = 'offer_partially_funded';
if (node.diffType !== "DeletedNode") {
effect.remaining = ripple.Amount.from_json(node.fields.TakerGets);
}
else {
effect.cancelled = true;
}
}
else {
// New / Funded / Cancelled offer
effect.type = node.diffType === "CreatedNode"
? 'offer_created'
: node.fieldsPrev.TakerPays
? 'offer_funded'
: 'offer_cancelled';
// For funded offers we use "fieldsPrev".
if (effect.type === 'offer_funded')
fieldSet = node.fieldsPrev;
// We don't count cancelling an offer as a side effect if it's
// already the primary effect of the transaction.
if (effect.type === 'offer_cancelled' &&
obj.transaction &&
obj.transaction.type === "offercancel") {
// Fill in remaining information about offer
obj.transaction.gets = fieldSet.TakerGets;
obj.transaction.pays = fieldSet.TakerPays;
}
}
effect.seq = +node.fields.Sequence;
}
// Another account offer. We care about it only if our transaction changed the offer amount (we bought currency)
else if(tx.Account === account && !$.isEmptyObject(node.fieldsPrev) /* Offer is unfunded if node.fieldsPrev is empty */) {
effect.type = 'offer_bought';
}
if (effect.type) {
effect.gets = ripple.Amount.from_json(fieldSet.TakerGets);
effect.pays = ripple.Amount.from_json(fieldSet.TakerPays);
if ('offer_partially_funded' === effect.type || 'offer_bought' === effect.type) {
effect.got = ripple.Amount.from_json(node.fieldsPrev.TakerGets).subtract(node.fields.TakerGets);
effect.paid = ripple.Amount.from_json(node.fieldsPrev.TakerPays).subtract(node.fields.TakerPays);
}
}
if (effect.gets && effect.pays) {
effect.price = getPrice(effect, tx.date);
}
// Flags
if (node.fields.Flags) {
effect.flags = node.fields.Flags;
effect.sell = node.fields.Flags & ripple.Remote.flags.offer.Sell;
}
}
if (!$.isEmptyObject(effect)) {
if (node.diffType === "DeletedNode") {
effect.deleted = true;
}
if (!obj.effects) obj.effects = [];
obj.effects.push(effect);
}
// Fee effect
if (feeEff) {
if (!obj.effects) obj.effects = [];
obj.effects.push(feeEff);
}
});
}
// Balance after the transaction
if (obj.accountRoot && obj.transaction && "undefined" === typeof obj.transaction.balance) {
obj.transaction.balance = ripple.Amount.from_json(obj.accountRoot.Balance);
}
if ($.isEmptyObject(obj))
return;
// If the transaction didn't wind up cancelling an offer
if (tx.TransactionType === 'OfferCancel' && obj.transaction &&
(!obj.transaction.gets || !obj.transaction.pays)) {
return;
}
// Rippling transaction
if (isRippling(obj.effects)) {
if (!obj.transaction) {
obj.transaction = {};
}
obj.transaction.type = 'rippling';
}
obj.tx_type = tx.TransactionType;
obj.tx_result = meta.TransactionResult;
obj.fee = tx.Fee;
obj.date = ripple.utils.toTimestamp(tx.date);
obj.dateRaw = tx.date;
obj.hash = tx.hash;
obj.affected_currencies = affected_currencies ? affected_currencies : [];
obj.ledger_index = tx.ledger_index;
return obj;
}
};