@ledgerhq/coin-canton 0.7.0-nightly.2 → 0.7.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.
@@ -1,446 +0,0 @@
1
- import BigNumber from "bignumber.js";
2
- import { Account } from "@ledgerhq/types-live";
3
- import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
4
- import {
5
- AmountRequired,
6
- FeeNotLoaded,
7
- FeeRequired,
8
- FeeTooHigh,
9
- InvalidAddress,
10
- InvalidAddressBecauseDestinationIsAlsoSource,
11
- NotEnoughBalanceBecauseDestinationNotCreated,
12
- NotEnoughSpendableBalance,
13
- RecipientRequired,
14
- } from "@ledgerhq/errors";
15
- import { getTransactionStatus } from "./getTransactionStatus";
16
- import { Transaction } from "../types";
17
- import coinConfig from "../config";
18
-
19
- // Mock the coin config
20
- jest.mock("../config", () => ({
21
- getCoinConfig: jest.fn(),
22
- }));
23
-
24
- const mockCoinConfig = jest.mocked(coinConfig);
25
-
26
- describe("getTransactionStatus", () => {
27
- const mockCurrency: CryptoCurrency = {
28
- id: "canton_network",
29
- name: "Canton Network",
30
- family: "canton",
31
- units: [
32
- {
33
- name: "Canton",
34
- code: "CANTON",
35
- magnitude: 8,
36
- },
37
- ],
38
- ticker: "CANTON",
39
- scheme: "canton",
40
- color: "#000000",
41
- type: "CryptoCurrency",
42
- managerAppName: "Canton",
43
- coinType: 0,
44
- disableCountervalue: false,
45
- delisted: false,
46
- keywords: ["canton"],
47
- explorerViews: [],
48
- terminated: {
49
- link: "",
50
- },
51
- };
52
-
53
- const mockAccount: Account = {
54
- id: "test-account-id",
55
- seedIdentifier: "test-seed-identifier",
56
- currency: mockCurrency,
57
- balance: new BigNumber(1000), // 1000 units
58
- spendableBalance: new BigNumber(1000),
59
- freshAddress: "test::123",
60
- freshAddressPath: "44'/60'/0'/0/0",
61
- index: 0,
62
- derivationMode: "canton",
63
- used: true,
64
- operations: [],
65
- pendingOperations: [],
66
- lastSyncDate: new Date(),
67
- creationDate: new Date(),
68
- operationsCount: 0,
69
- blockHeight: 100,
70
- balanceHistoryCache: {
71
- HOUR: { latestDate: null, balances: [] },
72
- DAY: { latestDate: null, balances: [] },
73
- WEEK: { latestDate: null, balances: [] },
74
- },
75
- swapHistory: [],
76
- nfts: [],
77
- subAccounts: [],
78
- type: "Account",
79
- };
80
-
81
- beforeEach(() => {
82
- jest.clearAllMocks();
83
- mockCoinConfig.getCoinConfig.mockReturnValue({
84
- minReserve: 100, // 100 units minimum reserve
85
- networkType: "mainnet",
86
- status: { type: "active" },
87
- nativeInstrumentId: "Amulet",
88
- });
89
- });
90
-
91
- describe("fee validation", () => {
92
- it("should return FeeNotLoaded error when fee is not provided", async () => {
93
- const transaction: Transaction = {
94
- family: "canton",
95
- amount: new BigNumber(100),
96
- recipient: "valid::123",
97
- fee: null,
98
- tokenId: "",
99
- };
100
-
101
- const result = await getTransactionStatus(mockAccount, transaction);
102
-
103
- expect(result.errors.fee).toBeInstanceOf(FeeNotLoaded);
104
- expect(result.warnings).toEqual({});
105
- });
106
-
107
- it("should return FeeRequired error when fee is zero", async () => {
108
- const transaction: Transaction = {
109
- family: "canton",
110
- amount: new BigNumber(100),
111
- recipient: "valid::123",
112
- fee: new BigNumber(0),
113
- tokenId: "",
114
- };
115
-
116
- const result = await getTransactionStatus(mockAccount, transaction);
117
-
118
- expect(result.errors.fee).toBeInstanceOf(FeeRequired);
119
- expect(result.warnings).toEqual({});
120
- });
121
-
122
- it("should add FeeTooHigh warning when fee is more than 10 times the amount", async () => {
123
- const transaction: Transaction = {
124
- family: "canton",
125
- amount: new BigNumber(100), // Use larger amount to avoid balance issues
126
- recipient: "valid::123",
127
- fee: new BigNumber(1500), // 15x the amount
128
- tokenId: "",
129
- };
130
-
131
- const result = await getTransactionStatus(mockAccount, transaction);
132
-
133
- expect(result.warnings.feeTooHigh).toBeInstanceOf(FeeTooHigh);
134
- // Don't check for empty errors since there might be balance issues
135
- });
136
-
137
- it("should not add FeeTooHigh warning when fee is reasonable", async () => {
138
- const transaction: Transaction = {
139
- family: "canton",
140
- amount: new BigNumber(100),
141
- recipient: "valid::123",
142
- fee: new BigNumber(10), // 0.1x the amount
143
- tokenId: "",
144
- };
145
-
146
- const result = await getTransactionStatus(mockAccount, transaction);
147
-
148
- expect(result.warnings).toEqual({});
149
- expect(result.errors).toEqual({});
150
- });
151
- });
152
-
153
- describe("balance validation", () => {
154
- it("should return NotEnoughSpendableBalance error when total spent exceeds balance minus reserve", async () => {
155
- const transaction: Transaction = {
156
- family: "canton",
157
- amount: new BigNumber(950), // 950 + 10 fee = 960, but balance is 1000 and reserve is 100
158
- recipient: "valid::123",
159
- fee: new BigNumber(10),
160
- tokenId: "",
161
- };
162
-
163
- const result = await getTransactionStatus(mockAccount, transaction);
164
-
165
- expect(result.errors.amount).toBeInstanceOf(NotEnoughSpendableBalance);
166
- });
167
-
168
- it("should return NotEnoughBalanceBecauseDestinationNotCreated error when amount is below reserve", async () => {
169
- const transaction: Transaction = {
170
- family: "canton",
171
- amount: new BigNumber(50), // Below reserve amount of 100
172
- recipient: "valid::123",
173
- fee: new BigNumber(10),
174
- tokenId: "",
175
- };
176
-
177
- const result = await getTransactionStatus(mockAccount, transaction);
178
-
179
- expect(result.errors.amount).toBeInstanceOf(NotEnoughBalanceBecauseDestinationNotCreated);
180
- });
181
-
182
- it("should pass balance validation when transaction is within limits", async () => {
183
- const transaction: Transaction = {
184
- family: "canton",
185
- amount: new BigNumber(800), // 800 + 10 fee = 810, balance is 1000, reserve is 100, so 900 available
186
- recipient: "valid::123",
187
- fee: new BigNumber(10),
188
- tokenId: "",
189
- };
190
-
191
- const result = await getTransactionStatus(mockAccount, transaction);
192
-
193
- expect(result.errors.amount).toBeUndefined();
194
- });
195
- });
196
-
197
- describe("recipient validation", () => {
198
- it("should return RecipientRequired error when recipient is missing", async () => {
199
- const transaction: Transaction = {
200
- family: "canton",
201
- amount: new BigNumber(100),
202
- recipient: "",
203
- fee: new BigNumber(10),
204
- tokenId: "",
205
- };
206
-
207
- const result = await getTransactionStatus(mockAccount, transaction);
208
-
209
- expect(result.errors.recipient).toBeInstanceOf(RecipientRequired);
210
- });
211
-
212
- it("should return InvalidAddressBecauseDestinationIsAlsoSource error when sending to self", async () => {
213
- const transaction: Transaction = {
214
- family: "canton",
215
- amount: new BigNumber(100),
216
- recipient: "test::123", // Same as account.freshAddress
217
- fee: new BigNumber(10),
218
- tokenId: "",
219
- };
220
-
221
- const result = await getTransactionStatus(mockAccount, transaction);
222
-
223
- expect(result.errors.recipient).toBeInstanceOf(InvalidAddressBecauseDestinationIsAlsoSource);
224
- });
225
-
226
- it("should return InvalidAddress error when recipient is invalid", async () => {
227
- const transaction: Transaction = {
228
- family: "canton",
229
- amount: new BigNumber(100),
230
- recipient: "invalid-address",
231
- fee: new BigNumber(10),
232
- tokenId: "",
233
- };
234
-
235
- const result = await getTransactionStatus(mockAccount, transaction);
236
-
237
- expect(result.errors.recipient).toBeInstanceOf(InvalidAddress);
238
- });
239
-
240
- it("should pass recipient validation when recipient is valid", async () => {
241
- const transaction: Transaction = {
242
- family: "canton",
243
- amount: new BigNumber(100),
244
- recipient: "valid::456",
245
- fee: new BigNumber(10),
246
- tokenId: "",
247
- };
248
-
249
- const result = await getTransactionStatus(mockAccount, transaction);
250
-
251
- expect(result.errors.recipient).toBeUndefined();
252
- });
253
- });
254
-
255
- describe("amount validation", () => {
256
- it("should return AmountRequired error when amount is zero", async () => {
257
- // Create a scenario where there are no other amount errors
258
- // Use a high balance and amount above reserve to avoid other amount errors
259
- const accountWithHighBalance = {
260
- ...mockAccount,
261
- balance: new BigNumber(10000), // High balance to avoid balance errors
262
- };
263
-
264
- // Set a high reserve to avoid the NotEnoughBalanceBecauseDestinationNotCreated error
265
- mockCoinConfig.getCoinConfig.mockReturnValue({
266
- minReserve: 0, // Set reserve to 0 to avoid reserve-related errors
267
- networkType: "mainnet",
268
- status: { type: "active" },
269
- nativeInstrumentId: "Amulet",
270
- });
271
-
272
- const transaction: Transaction = {
273
- family: "canton",
274
- amount: new BigNumber(0),
275
- recipient: "valid::123",
276
- fee: new BigNumber(10),
277
- tokenId: "",
278
- };
279
-
280
- const result = await getTransactionStatus(accountWithHighBalance, transaction);
281
-
282
- expect(result.errors.amount).toBeInstanceOf(AmountRequired);
283
- });
284
-
285
- it("should not return AmountRequired error when amount is positive", async () => {
286
- const transaction: Transaction = {
287
- family: "canton",
288
- amount: new BigNumber(100),
289
- recipient: "valid::123",
290
- fee: new BigNumber(10),
291
- tokenId: "",
292
- };
293
-
294
- const result = await getTransactionStatus(mockAccount, transaction);
295
-
296
- expect(result.errors.amount).toBeUndefined();
297
- });
298
- });
299
-
300
- describe("return values", () => {
301
- it("should return correct estimatedFees, amount, and totalSpent", async () => {
302
- const transaction: Transaction = {
303
- family: "canton",
304
- amount: new BigNumber(100),
305
- recipient: "valid::123",
306
- fee: new BigNumber(10),
307
- tokenId: "",
308
- };
309
-
310
- const result = await getTransactionStatus(mockAccount, transaction);
311
-
312
- expect(result.estimatedFees).toEqual(new BigNumber(10));
313
- expect(result.amount).toEqual(new BigNumber(100));
314
- expect(result.totalSpent).toEqual(new BigNumber(110));
315
- });
316
-
317
- it("should return empty errors and warnings when transaction is valid", async () => {
318
- const transaction: Transaction = {
319
- family: "canton",
320
- amount: new BigNumber(100),
321
- recipient: "valid::123",
322
- fee: new BigNumber(10),
323
- tokenId: "",
324
- };
325
-
326
- const result = await getTransactionStatus(mockAccount, transaction);
327
-
328
- expect(result.errors).toEqual({});
329
- expect(result.warnings).toEqual({});
330
- });
331
- });
332
-
333
- describe("edge cases", () => {
334
- it("should handle account with zero balance", async () => {
335
- const accountWithZeroBalance = {
336
- ...mockAccount,
337
- balance: new BigNumber(0),
338
- };
339
-
340
- const transaction: Transaction = {
341
- family: "canton",
342
- amount: new BigNumber(50),
343
- recipient: "valid::123",
344
- fee: new BigNumber(10),
345
- tokenId: "",
346
- };
347
-
348
- const result = await getTransactionStatus(accountWithZeroBalance, transaction);
349
-
350
- expect(result.errors.amount).toBeInstanceOf(NotEnoughSpendableBalance);
351
- });
352
-
353
- it("should handle account with balance exactly equal to reserve", async () => {
354
- const accountWithReserveBalance = {
355
- ...mockAccount,
356
- balance: new BigNumber(100), // Exactly equal to reserve
357
- };
358
-
359
- const transaction: Transaction = {
360
- family: "canton",
361
- amount: new BigNumber(50),
362
- recipient: "valid::123",
363
- fee: new BigNumber(10),
364
- tokenId: "",
365
- };
366
-
367
- const result = await getTransactionStatus(accountWithReserveBalance, transaction);
368
-
369
- expect(result.errors.amount).toBeInstanceOf(NotEnoughSpendableBalance);
370
- });
371
-
372
- it("should handle zero reserve amount", async () => {
373
- mockCoinConfig.getCoinConfig.mockReturnValue({
374
- minReserve: 0,
375
- networkType: "mainnet",
376
- status: { type: "active" },
377
- nativeInstrumentId: "Amulet",
378
- });
379
-
380
- const transaction: Transaction = {
381
- family: "canton",
382
- amount: new BigNumber(50),
383
- recipient: "valid::123",
384
- fee: new BigNumber(10),
385
- tokenId: "",
386
- };
387
-
388
- const result = await getTransactionStatus(mockAccount, transaction);
389
-
390
- expect(result.errors.amount).toBeUndefined();
391
- });
392
-
393
- it("should handle undefined reserve amount", async () => {
394
- mockCoinConfig.getCoinConfig.mockReturnValue({
395
- networkType: "mainnet",
396
- status: { type: "active" },
397
- nativeInstrumentId: "Amulet",
398
- });
399
-
400
- const transaction: Transaction = {
401
- family: "canton",
402
- amount: new BigNumber(50),
403
- recipient: "valid::123",
404
- fee: new BigNumber(10),
405
- tokenId: "",
406
- };
407
-
408
- const result = await getTransactionStatus(mockAccount, transaction);
409
-
410
- expect(result.errors.amount).toBeUndefined();
411
- });
412
- });
413
-
414
- describe("multiple validation errors", () => {
415
- it("should return multiple errors when multiple validations fail", async () => {
416
- const transaction: Transaction = {
417
- family: "canton",
418
- amount: new BigNumber(0), // AmountRequired
419
- recipient: "", // RecipientRequired
420
- fee: null, // FeeNotLoaded
421
- tokenId: "",
422
- };
423
-
424
- const result = await getTransactionStatus(mockAccount, transaction);
425
-
426
- expect(result.errors.amount).toBeInstanceOf(AmountRequired);
427
- expect(result.errors.recipient).toBeInstanceOf(RecipientRequired);
428
- expect(result.errors.fee).toBeInstanceOf(FeeNotLoaded);
429
- });
430
-
431
- it("should return both errors and warnings", async () => {
432
- const transaction: Transaction = {
433
- family: "canton",
434
- amount: new BigNumber(5), // Small amount
435
- recipient: "valid::123",
436
- fee: new BigNumber(100), // High fee relative to amount
437
- tokenId: "",
438
- };
439
-
440
- const result = await getTransactionStatus(mockAccount, transaction);
441
-
442
- expect(result.warnings.feeTooHigh).toBeInstanceOf(FeeTooHigh);
443
- expect(result.errors.amount).toBeInstanceOf(NotEnoughBalanceBecauseDestinationNotCreated);
444
- });
445
- });
446
- });
@@ -1,93 +0,0 @@
1
- import { isRecipientValid, validateTag } from "./utils";
2
- import BigNumber from "bignumber.js";
3
-
4
- describe("utils", () => {
5
- describe("isRecipientValid", () => {
6
- it("should return true for valid Canton addresses", () => {
7
- const validAddresses = [
8
- "abc::123",
9
- "hello::1",
10
- "test123::456",
11
- "a::0",
12
- "party::999",
13
- "user123::42",
14
- "canton_1a2b3c4d5e6f7g8h9i0j1k2l3m4n5o6p7q8r9s0t1u2v3w4x5y6z::123",
15
- "test::123456789",
16
- ];
17
-
18
- validAddresses.forEach(address => {
19
- expect(isRecipientValid(address)).toBe(true);
20
- });
21
- });
22
-
23
- it("should return false for invalid Canton addresses", () => {
24
- const invalidAddresses = [
25
- "", // empty string
26
- "::123", // no characters before ::
27
- "abc::", // no numbers after ::
28
- "abc:123", // single colon instead of double
29
- "abc::abc", // letters instead of numbers after ::
30
- "abc::", // empty after ::
31
- "::", // only colons
32
- "abc", // no colons
33
- "123", // no colons
34
- "abc::123abc", // mixed letters and numbers after ::
35
- "abc::123.456", // decimal numbers
36
- "abc::-123", // negative numbers
37
- "abc::+123", // positive sign
38
- "abc:: 123", // space before number
39
- "abc::123 ", // space after number
40
- "abc:: 123", // space after ::
41
- ];
42
-
43
- invalidAddresses.forEach(address => {
44
- expect(isRecipientValid(address)).toBe(false);
45
- });
46
- });
47
-
48
- it("should handle edge cases", () => {
49
- expect(isRecipientValid("a::1")).toBe(true); // minimum valid case
50
- expect(isRecipientValid("1::1")).toBe(true); // number before ::
51
- expect(isRecipientValid("_::1")).toBe(true); // underscore before ::
52
- expect(isRecipientValid("-::1")).toBe(true); // dash before ::
53
- expect(isRecipientValid(".::1")).toBe(true); // dot before ::
54
- });
55
-
56
- it("should handle addresses with spaces and multiple colons", () => {
57
- // These are valid according to our regex but might not be ideal Canton addresses
58
- expect(isRecipientValid(" abc::123")).toBe(true); // space before address
59
- expect(isRecipientValid("abc ::123")).toBe(true); // space before ::
60
- expect(isRecipientValid("abc::123::456")).toBe(true); // multiple ::
61
- });
62
- });
63
-
64
- describe("validateTag", () => {
65
- it("should return true for valid tags", () => {
66
- const validTags = [
67
- new BigNumber(1),
68
- new BigNumber(42),
69
- new BigNumber(4294967295), // UINT32_MAX
70
- new BigNumber(0),
71
- ];
72
-
73
- validTags.forEach(tag => {
74
- expect(validateTag(tag)).toBe(true);
75
- });
76
- });
77
-
78
- it("should return false for invalid tags", () => {
79
- const invalidTags = [
80
- new BigNumber(-1), // negative
81
- new BigNumber(4294967296), // greater than UINT32_MAX
82
- new BigNumber(1.5), // decimal
83
- new BigNumber(NaN), // NaN
84
- new BigNumber(Infinity), // Infinity
85
- new BigNumber(-Infinity), // -Infinity
86
- ];
87
-
88
- invalidTags.forEach(tag => {
89
- expect(validateTag(tag)).toBe(false);
90
- });
91
- });
92
- });
93
- });