@ledgerhq/coin-xrp 0.7.1 → 0.8.0-next.0

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.
Files changed (73) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/lib/api/index.d.ts.map +1 -1
  3. package/lib/api/index.integ.test.js +13 -4
  4. package/lib/api/index.integ.test.js.map +1 -1
  5. package/lib/api/index.js +14 -7
  6. package/lib/api/index.js.map +1 -1
  7. package/lib/api/index.test.js +31 -13
  8. package/lib/api/index.test.js.map +1 -1
  9. package/lib/bridge/index.d.ts +1 -1
  10. package/lib/bridge/index.d.ts.map +1 -1
  11. package/lib/bridge/index.js +2 -1
  12. package/lib/bridge/index.js.map +1 -1
  13. package/lib/bridge/synchronization.js +5 -5
  14. package/lib/bridge/synchronization.js.map +1 -1
  15. package/lib/bridge/synchronization.test.js +5 -4
  16. package/lib/bridge/synchronization.test.js.map +1 -1
  17. package/lib/logic/listOperations.d.ts +5 -1
  18. package/lib/logic/listOperations.d.ts.map +1 -1
  19. package/lib/logic/listOperations.js +39 -13
  20. package/lib/logic/listOperations.js.map +1 -1
  21. package/lib/logic/listOperations.test.js +22 -10
  22. package/lib/logic/listOperations.test.js.map +1 -1
  23. package/lib/network/index.d.ts +1 -0
  24. package/lib/network/index.d.ts.map +1 -1
  25. package/lib/network/index.js +2 -2
  26. package/lib/network/index.js.map +1 -1
  27. package/lib/network/types.d.ts +4 -3
  28. package/lib/network/types.d.ts.map +1 -1
  29. package/lib/network/types.js.map +1 -1
  30. package/lib/types/model.d.ts +2 -0
  31. package/lib/types/model.d.ts.map +1 -1
  32. package/lib-es/api/index.d.ts.map +1 -1
  33. package/lib-es/api/index.integ.test.js +13 -4
  34. package/lib-es/api/index.integ.test.js.map +1 -1
  35. package/lib-es/api/index.js +14 -7
  36. package/lib-es/api/index.js.map +1 -1
  37. package/lib-es/api/index.test.js +31 -13
  38. package/lib-es/api/index.test.js.map +1 -1
  39. package/lib-es/bridge/index.d.ts +1 -1
  40. package/lib-es/bridge/index.d.ts.map +1 -1
  41. package/lib-es/bridge/index.js +3 -2
  42. package/lib-es/bridge/index.js.map +1 -1
  43. package/lib-es/bridge/synchronization.js +5 -5
  44. package/lib-es/bridge/synchronization.js.map +1 -1
  45. package/lib-es/bridge/synchronization.test.js +5 -4
  46. package/lib-es/bridge/synchronization.test.js.map +1 -1
  47. package/lib-es/logic/listOperations.d.ts +5 -1
  48. package/lib-es/logic/listOperations.d.ts.map +1 -1
  49. package/lib-es/logic/listOperations.js +39 -13
  50. package/lib-es/logic/listOperations.js.map +1 -1
  51. package/lib-es/logic/listOperations.test.js +22 -10
  52. package/lib-es/logic/listOperations.test.js.map +1 -1
  53. package/lib-es/network/index.d.ts +1 -0
  54. package/lib-es/network/index.d.ts.map +1 -1
  55. package/lib-es/network/index.js +2 -2
  56. package/lib-es/network/index.js.map +1 -1
  57. package/lib-es/network/types.d.ts +4 -3
  58. package/lib-es/network/types.d.ts.map +1 -1
  59. package/lib-es/network/types.js.map +1 -1
  60. package/lib-es/types/model.d.ts +2 -0
  61. package/lib-es/types/model.d.ts.map +1 -1
  62. package/package.json +5 -5
  63. package/src/api/index.integ.test.ts +15 -4
  64. package/src/api/index.test.ts +31 -13
  65. package/src/api/index.ts +20 -6
  66. package/src/bridge/index.ts +4 -2
  67. package/src/bridge/synchronization.test.ts +5 -4
  68. package/src/bridge/synchronization.ts +5 -5
  69. package/src/logic/listOperations.test.ts +22 -10
  70. package/src/logic/listOperations.ts +64 -15
  71. package/src/network/index.ts +3 -3
  72. package/src/network/types.ts +4 -3
  73. package/src/types/model.ts +2 -0
@@ -11,42 +11,89 @@ import { RIPPLE_EPOCH } from "./utils";
11
11
  */
12
12
  export async function listOperations(
13
13
  address: string,
14
- blockHeight: number,
15
- ): Promise<XrpOperation[]> {
14
+ {
15
+ limit,
16
+ mostRecentIndex,
17
+ startAt,
18
+ }: {
19
+ limit?: number;
20
+ mostRecentIndex?: number | undefined;
21
+ startAt?: number;
22
+ },
23
+ ): Promise<[XrpOperation[], number]> {
16
24
  const serverInfo = await getServerInfos();
17
25
  const ledgers = serverInfo.info.complete_ledgers.split("-");
18
26
  const minLedgerVersion = Number(ledgers[0]);
19
27
  const maxLedgerVersion = Number(ledgers[1]);
20
28
 
21
- // if there is no ops, it might be after a clear and we prefer to pull from the oldest possible history
22
- const startAt = Math.max(blockHeight, minLedgerVersion);
29
+ let options: {
30
+ ledger_index_min?: number;
31
+ ledger_index_max?: number;
32
+ limit?: number;
33
+ tx_type?: string;
34
+ } = {
35
+ ledger_index_max: mostRecentIndex ?? maxLedgerVersion,
36
+ tx_type: "Payment",
37
+ };
38
+
39
+ if (limit) {
40
+ options = {
41
+ ...options,
42
+ limit,
43
+ };
44
+ }
45
+ if (startAt) {
46
+ options = {
47
+ ...options,
48
+ // if there is no ops, it might be after a clear and we prefer to pull from the oldest possible history
49
+ ledger_index_min: Math.max(startAt, minLedgerVersion),
50
+ };
51
+ }
23
52
 
24
- const transactions = await getTransactions(address, {
25
- ledger_index_min: startAt,
26
- ledger_index_max: maxLedgerVersion,
27
- });
53
+ // We need to filter out the transactions that are not "Payment" type because the filter on "tx_type" of the node RPC
54
+ // is not working as expected. It returns all the transactions. We need to call the node RPC multiple times to get the
55
+ // desired number of transactions by the limiter.
56
+ let transactions: XrplOperation[] = [];
57
+ let needToStop = true;
58
+ do {
59
+ const newTransactions = await getTransactions(address, options);
60
+ const newPaymentsTxs = newTransactions.filter(tx => tx.tx_json.TransactionType === "Payment");
61
+ if (options.limit) {
62
+ needToStop = newTransactions.length < options.limit;
63
+ options.ledger_index_max = newTransactions.slice(-1)[0].tx_json.ledger_index - 1;
64
+ }
65
+ transactions = transactions.concat(newPaymentsTxs);
66
+ } while (
67
+ options.limit &&
68
+ !needToStop &&
69
+ transactions.length < options.limit &&
70
+ (options.limit -= transactions.length)
71
+ );
28
72
 
29
- return transactions
30
- .filter(op => op.tx.TransactionType === "Payment")
31
- .map(convertToCoreOperation(address));
73
+ return [
74
+ transactions.map(convertToCoreOperation(address)),
75
+ transactions.slice(-1)[0].tx_json.ledger_index - 1, // Returns the next index to start from for pagination
76
+ ];
32
77
  }
33
78
 
34
79
  const convertToCoreOperation =
35
80
  (address: string) =>
36
81
  (operation: XrplOperation): XrpOperation => {
37
82
  const {
83
+ ledger_hash,
84
+ hash,
85
+ close_time_iso,
38
86
  meta: { delivered_amount },
39
- tx: {
87
+ tx_json: {
40
88
  TransactionType,
41
89
  Fee,
42
- hash,
43
- inLedger,
44
90
  date,
45
91
  Account,
46
92
  Destination,
47
93
  DestinationTag,
48
94
  Sequence,
49
95
  Memos,
96
+ ledger_index,
50
97
  },
51
98
  } = operation;
52
99
 
@@ -90,13 +137,15 @@ const convertToCoreOperation =
90
137
  }
91
138
 
92
139
  let op: XrpOperation = {
140
+ blockTime: new Date(close_time_iso),
141
+ blockHash: ledger_hash,
93
142
  hash,
94
143
  address,
95
144
  type: TransactionType,
96
145
  simpleType: type,
97
146
  value,
98
147
  fee,
99
- blockHeight: inLedger,
148
+ blockHeight: ledger_index,
100
149
  senders: [Account],
101
150
  recipients: [Destination],
102
151
  date: new Date(toEpochDate),
@@ -68,12 +68,12 @@ export const getServerInfos = async (): Promise<ServerInfoResponse> => {
68
68
 
69
69
  export const getTransactions = async (
70
70
  address: string,
71
- options: { ledger_index_min?: number; ledger_index_max?: number } | undefined,
71
+ options: { ledger_index_min?: number; ledger_index_max?: number; limit?: number } | undefined,
72
72
  ): Promise<XrplOperation[]> => {
73
73
  const result = await rpcCall<AccountTxResponse>("account_tx", {
74
74
  account: address,
75
- ledger_index: "validated",
76
75
  ...options,
76
+ api_version: 2,
77
77
  });
78
78
 
79
79
  return result.transactions;
@@ -109,7 +109,7 @@ async function rpcCall<T extends object>(
109
109
  });
110
110
 
111
111
  if (isResponseStatus(result) && result.status !== "success") {
112
- throw new Error(`couldn't fetch ${method} with params ${params}`);
112
+ throw new Error(`couldn't fetch ${method} with params ${JSON.stringify(params)}`);
113
113
  }
114
114
 
115
115
  return result;
@@ -1,4 +1,7 @@
1
1
  export type XrplOperation = {
2
+ ledger_hash: string;
3
+ hash: string;
4
+ close_time_iso: string;
2
5
  meta: {
3
6
  AffectedNodes: {
4
7
  ModifiedNode: {
@@ -22,7 +25,7 @@ export type XrplOperation = {
22
25
  TransactionResult: string;
23
26
  delivered_amount: string;
24
27
  };
25
- tx: {
28
+ tx_json: {
26
29
  Account: string;
27
30
  Amount: string;
28
31
  DeliverMax: string;
@@ -44,8 +47,6 @@ export type XrplOperation = {
44
47
  TransactionType: string;
45
48
  TxnSignature: string;
46
49
  date: number;
47
- hash: string;
48
- inLedger: number;
49
50
  ledger_index: number;
50
51
  };
51
52
  validated: boolean;
@@ -11,6 +11,8 @@ export type XrpMemo = {
11
11
  type?: string;
12
12
  };
13
13
  export type XrpOperation = {
14
+ blockTime: Date;
15
+ blockHash: string;
14
16
  hash: string;
15
17
  address: string;
16
18
  type: string;