@bithomp/xrpl-api 3.6.16 → 3.6.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,6 +15,7 @@ export interface BalanceChanges {
15
15
  }
16
16
  interface ParseBalanceChangesOptions {
17
17
  adjustBalancesForNativeEscrow?: boolean;
18
+ adjustBalancesForPaymentChannel?: boolean;
18
19
  }
19
20
  declare function parseBalanceChanges(metadata: TransactionMetadata, nativeCurrency?: string, tx?: any, options?: ParseBalanceChangesOptions): BalanceChanges;
20
21
  declare function parseFinalBalances(metadata: TransactionMetadata, nativeCurrency?: string): {
@@ -13,7 +13,9 @@ const utils_1 = require("../utils");
13
13
  const mptoken_1 = require("../../models/mptoken");
14
14
  const mptoken_normalize_1 = require("../mptoken_normalize");
15
15
  const amount_1 = __importDefault(require("../ledger/amount"));
16
+ const channel_changes_1 = require("./channel_changes");
16
17
  const ESCROW_TYPES = ["EscrowFinish", "EscrowCreate", "EscrowCancel"];
18
+ const PAYMENT_CHANNEL_TYPES = ["PaymentChannelClaim", "PaymentChannelCreate", "PaymentChannelFund"];
17
19
  function groupByAddress(balanceChanges) {
18
20
  const grouped = lodash_1.default.groupBy(balanceChanges, function (node) {
19
21
  return node.address;
@@ -217,6 +219,45 @@ function adjustBalancesForNativeEscrow(balanceChanges, metadata, nativeCurrency,
217
219
  adjustBalancesChanges(balanceChanges, source, [{ currency: amount.currency, value: `-${amount.value}` }]);
218
220
  }
219
221
  }
222
+ function adjustBalancesForPaymentChannel(balanceChanges, metadata, nativeCurrency, tx) {
223
+ const channelChanges = (0, channel_changes_1.parseChannelChanges)(metadata);
224
+ if (!channelChanges) {
225
+ return;
226
+ }
227
+ if (!channelChanges.source?.address) {
228
+ return;
229
+ }
230
+ if (tx.TransactionType === "PaymentChannelCreate") {
231
+ adjustBalancesChanges(balanceChanges, channelChanges.source.address, [
232
+ { currency: channelChanges.amount.currency, value: channelChanges.amount.value },
233
+ ]);
234
+ }
235
+ else if (tx.TransactionType === "PaymentChannelFund") {
236
+ if (channelChanges.amountChange) {
237
+ adjustBalancesChanges(balanceChanges, channelChanges.source.address, [
238
+ { currency: channelChanges.amountChange.currency, value: channelChanges.amountChange.value },
239
+ ]);
240
+ }
241
+ }
242
+ else if (tx.TransactionType === "PaymentChannelClaim") {
243
+ if (tx.Account === channelChanges.source.address && channelChanges.amountChange) {
244
+ adjustBalancesChanges(balanceChanges, channelChanges.source.address, [
245
+ { currency: channelChanges.amountChange.currency, value: channelChanges.amountChange.value },
246
+ ]);
247
+ }
248
+ else if (channelChanges.status === "deleted") {
249
+ const unlockedAmount = new bignumber_js_1.default(channelChanges.amount.value).minus(new bignumber_js_1.default(channelChanges?.balance?.value || "0"));
250
+ adjustBalancesChanges(balanceChanges, channelChanges.source.address, [
251
+ { currency: channelChanges.amount.currency, value: `-${unlockedAmount.toString()}` },
252
+ ]);
253
+ }
254
+ else if (channelChanges.balanceChange) {
255
+ adjustBalancesChanges(balanceChanges, channelChanges.source.address, [
256
+ { currency: channelChanges.balanceChange.currency, value: channelChanges.balanceChange.value },
257
+ ]);
258
+ }
259
+ }
260
+ }
220
261
  function adjustBalancesChanges(balanceChanges, address, changes) {
221
262
  const existingChanges = balanceChanges[address] || [];
222
263
  const change = existingChanges.find((c) => c.currency === changes[0].currency && c.issuer === changes[0].issuer);
@@ -242,10 +283,13 @@ function parseBalanceChanges(metadata, nativeCurrency, tx, options) {
242
283
  (0, mptoken_normalize_1.normalizeMPTokensPreviousFields)(metadata, tx);
243
284
  }
244
285
  const balanceChanges = parseQuantities(metadata, computeBalanceChange, nativeCurrency);
245
- if (tx && options && options.adjustBalancesForNativeEscrow) {
246
- if (tx.TransactionType && ESCROW_TYPES.includes(tx.TransactionType)) {
286
+ if (tx && tx.TransactionType && options) {
287
+ if (options.adjustBalancesForNativeEscrow && ESCROW_TYPES.includes(tx.TransactionType)) {
247
288
  adjustBalancesForNativeEscrow(balanceChanges, metadata, nativeCurrency, tx);
248
289
  }
290
+ else if (options.adjustBalancesForPaymentChannel && PAYMENT_CHANNEL_TYPES.includes(tx.TransactionType)) {
291
+ adjustBalancesForPaymentChannel(balanceChanges, metadata, nativeCurrency, tx);
292
+ }
249
293
  }
250
294
  return balanceChanges;
251
295
  }
@@ -39,8 +39,8 @@ function summarizePaymentChannel(node) {
39
39
  if (prev.Amount) {
40
40
  summary.channelAmountChangeDrops = new bignumber_js_1.default(final.Amount).minus(new bignumber_js_1.default(prev.Amount || 0)).toString(10);
41
41
  summary.amountChange = (0, amount_1.default)(prev.Amount);
42
- summary.amountChange.value = new bignumber_js_1.default(summary.amountChange.value)
43
- .minus(new bignumber_js_1.default(summary.amount.value))
42
+ summary.amountChange.value = new bignumber_js_1.default(summary.amount.value)
43
+ .minus(new bignumber_js_1.default(summary.amountChange.value))
44
44
  .toString(10);
45
45
  }
46
46
  if (prev.Balance) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bithomp/xrpl-api",
3
- "version": "3.6.16",
3
+ "version": "3.6.17",
4
4
  "description": "A Bithomp JavaScript/TypeScript library for interacting with the XRP Ledger",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -43,7 +43,7 @@
43
43
  "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
44
44
  "lint": "eslint",
45
45
  "prepare": "npm run build",
46
- "-prepublishOnly": "npm test && npm run lint",
46
+ "prepublishOnly": "npm test && npm run lint",
47
47
  "preversion": "npm run lint",
48
48
  "version": "npm run format && git add -A src",
49
49
  "postversion": "git push && git push --tags"
@@ -74,7 +74,7 @@
74
74
  "@typescript-eslint/parser": "^8.46.1",
75
75
  "chai": "^6.2.0",
76
76
  "chai-as-promised": "^8.0.2",
77
- "eslint": "^9.37.0",
77
+ "eslint": "^9.38.0",
78
78
  "eslint-config-prettier": "^10.1.8",
79
79
  "eslint-plugin-chai-friendly": "^1.1.0",
80
80
  "eslint-plugin-import": "^2.32.0",