@ledgerhq/coin-polkadot 0.0.2

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 (181) hide show
  1. package/.eslintrc.js +57 -0
  2. package/.turbo/turbo-build.log +4 -0
  3. package/CHANGELOG.md +12 -0
  4. package/jest.config.js +10 -0
  5. package/lib/account.d.ts +15 -0
  6. package/lib/account.d.ts.map +1 -0
  7. package/lib/account.js +133 -0
  8. package/lib/account.js.map +1 -0
  9. package/lib/address.d.ts +7 -0
  10. package/lib/address.d.ts.map +1 -0
  11. package/lib/address.js +28 -0
  12. package/lib/address.js.map +1 -0
  13. package/lib/api/bisontrails.d.ts +11 -0
  14. package/lib/api/bisontrails.d.ts.map +1 -0
  15. package/lib/api/bisontrails.js +351 -0
  16. package/lib/api/bisontrails.js.map +1 -0
  17. package/lib/api/common.d.ts +12 -0
  18. package/lib/api/common.d.ts.map +1 -0
  19. package/lib/api/common.js +41 -0
  20. package/lib/api/common.js.map +1 -0
  21. package/lib/api/index.d.ts +3 -0
  22. package/lib/api/index.d.ts.map +1 -0
  23. package/lib/api/index.js +53 -0
  24. package/lib/api/index.js.map +1 -0
  25. package/lib/api/sidecar.d.ts +193 -0
  26. package/lib/api/sidecar.d.ts.map +1 -0
  27. package/lib/api/sidecar.js +844 -0
  28. package/lib/api/sidecar.js.map +1 -0
  29. package/lib/api/sidecar.types.d.ts +163 -0
  30. package/lib/api/sidecar.types.d.ts.map +1 -0
  31. package/lib/api/sidecar.types.js +3 -0
  32. package/lib/api/sidecar.types.js.map +1 -0
  33. package/lib/api/subscan.d.ts +6 -0
  34. package/lib/api/subscan.d.ts.map +1 -0
  35. package/lib/api/subscan.js +375 -0
  36. package/lib/api/subscan.js.map +1 -0
  37. package/lib/api/websocket.d.ts +92 -0
  38. package/lib/api/websocket.d.ts.map +1 -0
  39. package/lib/api/websocket.js +759 -0
  40. package/lib/api/websocket.js.map +1 -0
  41. package/lib/bridge/js.d.ts +6 -0
  42. package/lib/bridge/js.d.ts.map +1 -0
  43. package/lib/bridge/js.js +120 -0
  44. package/lib/bridge/js.js.map +1 -0
  45. package/lib/bridge.integration.test.d.ts +4 -0
  46. package/lib/bridge.integration.test.d.ts.map +1 -0
  47. package/lib/bridge.integration.test.js +795 -0
  48. package/lib/bridge.integration.test.js.map +1 -0
  49. package/lib/cache.d.ts +65 -0
  50. package/lib/cache.d.ts.map +1 -0
  51. package/lib/cache.js +182 -0
  52. package/lib/cache.js.map +1 -0
  53. package/lib/cli-transaction.d.ts +44 -0
  54. package/lib/cli-transaction.d.ts.map +1 -0
  55. package/lib/cli-transaction.js +148 -0
  56. package/lib/cli-transaction.js.map +1 -0
  57. package/lib/deviceTransactionConfig.d.ts +16 -0
  58. package/lib/deviceTransactionConfig.d.ts.map +1 -0
  59. package/lib/deviceTransactionConfig.js +171 -0
  60. package/lib/deviceTransactionConfig.js.map +1 -0
  61. package/lib/errors.d.ts +37 -0
  62. package/lib/errors.d.ts.map +1 -0
  63. package/lib/errors.js +17 -0
  64. package/lib/errors.js.map +1 -0
  65. package/lib/hw-getAddress.d.ts +4 -0
  66. package/lib/hw-getAddress.d.ts.map +1 -0
  67. package/lib/hw-getAddress.js +64 -0
  68. package/lib/hw-getAddress.js.map +1 -0
  69. package/lib/js-buildTransaction.d.ts +25 -0
  70. package/lib/js-buildTransaction.d.ts.map +1 -0
  71. package/lib/js-buildTransaction.js +265 -0
  72. package/lib/js-buildTransaction.js.map +1 -0
  73. package/lib/js-createTransaction.d.ts +9 -0
  74. package/lib/js-createTransaction.d.ts.map +1 -0
  75. package/lib/js-createTransaction.js +22 -0
  76. package/lib/js-createTransaction.js.map +1 -0
  77. package/lib/js-estimateMaxSpendable.d.ts +15 -0
  78. package/lib/js-estimateMaxSpendable.d.ts.map +1 -0
  79. package/lib/js-estimateMaxSpendable.js +89 -0
  80. package/lib/js-estimateMaxSpendable.js.map +1 -0
  81. package/lib/js-getFeesForTransaction.d.ts +14 -0
  82. package/lib/js-getFeesForTransaction.d.ts.map +1 -0
  83. package/lib/js-getFeesForTransaction.js +94 -0
  84. package/lib/js-getFeesForTransaction.js.map +1 -0
  85. package/lib/js-getTransactionStatus.d.ts +18 -0
  86. package/lib/js-getTransactionStatus.d.ts.map +1 -0
  87. package/lib/js-getTransactionStatus.js +316 -0
  88. package/lib/js-getTransactionStatus.js.map +1 -0
  89. package/lib/js-prepareTransaction.d.ts +9 -0
  90. package/lib/js-prepareTransaction.d.ts.map +1 -0
  91. package/lib/js-prepareTransaction.js +83 -0
  92. package/lib/js-prepareTransaction.js.map +1 -0
  93. package/lib/js-signOperation.d.ts +27 -0
  94. package/lib/js-signOperation.d.ts.map +1 -0
  95. package/lib/js-signOperation.js +235 -0
  96. package/lib/js-signOperation.js.map +1 -0
  97. package/lib/js-synchronisation.d.ts +3 -0
  98. package/lib/js-synchronisation.d.ts.map +1 -0
  99. package/lib/js-synchronisation.js +104 -0
  100. package/lib/js-synchronisation.js.map +1 -0
  101. package/lib/logic.d.ts +107 -0
  102. package/lib/logic.d.ts.map +1 -0
  103. package/lib/logic.js +242 -0
  104. package/lib/logic.js.map +1 -0
  105. package/lib/platformAdapter.d.ts +11 -0
  106. package/lib/platformAdapter.d.ts.map +1 -0
  107. package/lib/platformAdapter.js +25 -0
  108. package/lib/platformAdapter.js.map +1 -0
  109. package/lib/platformAdapter.test.d.ts +2 -0
  110. package/lib/platformAdapter.test.d.ts.map +1 -0
  111. package/lib/platformAdapter.test.js +51 -0
  112. package/lib/platformAdapter.test.js.map +1 -0
  113. package/lib/polkadot-crypto.d.ts +2 -0
  114. package/lib/polkadot-crypto.d.ts.map +1 -0
  115. package/lib/polkadot-crypto.js +14 -0
  116. package/lib/polkadot-crypto.js.map +1 -0
  117. package/lib/preload.d.ts +14 -0
  118. package/lib/preload.d.ts.map +1 -0
  119. package/lib/preload.js +173 -0
  120. package/lib/preload.js.map +1 -0
  121. package/lib/serialization.d.ts +4 -0
  122. package/lib/serialization.d.ts.map +1 -0
  123. package/lib/serialization.js +51 -0
  124. package/lib/serialization.js.map +1 -0
  125. package/lib/specs.d.ts +7 -0
  126. package/lib/specs.d.ts.map +1 -0
  127. package/lib/specs.js +274 -0
  128. package/lib/specs.js.map +1 -0
  129. package/lib/speculos-deviceActions.d.ts +4 -0
  130. package/lib/speculos-deviceActions.d.ts.map +1 -0
  131. package/lib/speculos-deviceActions.js +85 -0
  132. package/lib/speculos-deviceActions.js.map +1 -0
  133. package/lib/transaction.d.ts +15 -0
  134. package/lib/transaction.d.ts.map +1 -0
  135. package/lib/transaction.js +51 -0
  136. package/lib/transaction.js.map +1 -0
  137. package/lib/types.d.ts +96 -0
  138. package/lib/types.d.ts.map +1 -0
  139. package/lib/types.js +3 -0
  140. package/lib/types.js.map +1 -0
  141. package/lib/validators.d.ts +3 -0
  142. package/lib/validators.d.ts.map +1 -0
  143. package/lib/validators.js +7 -0
  144. package/lib/validators.js.map +1 -0
  145. package/package.json +95 -0
  146. package/src/account.ts +170 -0
  147. package/src/address.ts +28 -0
  148. package/src/api/bisontrails.ts +343 -0
  149. package/src/api/common.ts +52 -0
  150. package/src/api/index.ts +38 -0
  151. package/src/api/sidecar.ts +735 -0
  152. package/src/api/sidecar.types.ts +162 -0
  153. package/src/api/subscan.ts +362 -0
  154. package/src/api/websocket.ts +535 -0
  155. package/src/bridge/js.ts +81 -0
  156. package/src/bridge.integration.test.ts +764 -0
  157. package/src/cache.ts +202 -0
  158. package/src/cli-transaction.ts +184 -0
  159. package/src/deviceTransactionConfig.ts +209 -0
  160. package/src/errors.ts +39 -0
  161. package/src/hw-getAddress.ts +14 -0
  162. package/src/js-buildTransaction.ts +206 -0
  163. package/src/js-createTransaction.ts +22 -0
  164. package/src/js-estimateMaxSpendable.ts +37 -0
  165. package/src/js-getFeesForTransaction.ts +41 -0
  166. package/src/js-getTransactionStatus.ts +350 -0
  167. package/src/js-prepareTransaction.ts +27 -0
  168. package/src/js-signOperation.ts +232 -0
  169. package/src/js-synchronisation.ts +57 -0
  170. package/src/logic.ts +266 -0
  171. package/src/platformAdapter.test.ts +61 -0
  172. package/src/platformAdapter.ts +29 -0
  173. package/src/polkadot-crypto.ts +10 -0
  174. package/src/preload.ts +148 -0
  175. package/src/serialization.ts +48 -0
  176. package/src/specs.ts +305 -0
  177. package/src/speculos-deviceActions.ts +84 -0
  178. package/src/transaction.ts +64 -0
  179. package/src/types.ts +118 -0
  180. package/src/validators.ts +2 -0
  181. package/tsconfig.json +21 -0
@@ -0,0 +1,764 @@
1
+ import { disconnect } from "./api";
2
+ import { BigNumber } from "bignumber.js";
3
+ import { canUnbond, MAX_UNLOCKINGS } from "./logic";
4
+ import {
5
+ NotEnoughBalance,
6
+ RecipientRequired,
7
+ InvalidAddress,
8
+ InvalidAddressBecauseDestinationIsAlsoSource,
9
+ AmountRequired,
10
+ NotEnoughBalanceBecauseDestinationNotCreated,
11
+ } from "@ledgerhq/errors";
12
+ import {
13
+ PolkadotUnauthorizedOperation,
14
+ PolkadotNotValidator,
15
+ PolkadotBondMinimumAmount,
16
+ PolkadotValidatorsRequired,
17
+ PolkadotAllFundsWarning,
18
+ PolkadotDoMaxSendInstead,
19
+ } from "./errors";
20
+ import type { CurrenciesData, DatasetTest } from "@ledgerhq/types-live";
21
+ import { fromTransactionRaw } from "./transaction";
22
+ import type { PolkadotAccount, Transaction } from "./types";
23
+ const ACCOUNT_SAME_STASHCONTROLLER =
24
+ "12YA86tRQhHgwU3SSj56aesUKB7GKvdnZTTTXRop4vd3YgDV";
25
+ const ACCOUNT_STASH = "13jAJfhpFkRZj1TSSdFopaiFeKnof2q7g4GNdcxcg8Lvx6QN";
26
+ const ACCOUNT_CONTROLLER = "15oodc5d8DWJodZhTD6qsxxSQRYWhdkWCrwqNHajDirXRrAD";
27
+ const ACCOUNT_EMPTY = "111111111111111111111111111111111HC1";
28
+ const ACCOUNT_WITH_NO_OPERATION =
29
+ "12EsPA79dvhtjp1bYvCiEWPsQmmdKGss44GzE3CT9tTo9g4Q";
30
+
31
+ const polkadot: CurrenciesData<Transaction> = {
32
+ FIXME_ignoreAccountFields: [
33
+ "polkadotResources.unlockings", // Due to completion date that change everyday (estimated time)
34
+ "polkadotResources.nominations", // TODO: try to only ignore status
35
+ "polkadotResources.unlockedBalance", // It moved when an unbond is finished
36
+ ],
37
+ scanAccounts: [
38
+ {
39
+ name: "polkadot seed 1",
40
+ apdus: `
41
+ => 90010000142c00008062010080000000800000000000000000
42
+ <= c71b1e00ca34fdd14ea065917a67168675828927e3adc3b68f444bf85b4d24be3135573461755239707652335a5765616f50516f4562616d54535557347571335072515369564a4e4e7953385868314e9000
43
+ => 90010000142c00008062010080000000800000008000000080
44
+ <= 845175a888f0ced372bd352d59cb51242dff8438c3dd12a703835c25bdfa3f9231337a565838337a74575939446d69757446525738374262676b627631517031666366706f4a6964626448776b3335649000
45
+ => 90010000142c00008062010080010000800000008000000080
46
+ <= 3a521fa830998568da9c205c987dcd157a78b2bd30ece3d1b6c32213986864ca31324b5542335272346163346469354a47707a545935474e516b44564e6b50556b636a324d4746546d7a617a4d5364739000
47
+ => 90010000142c00008062010080020000800000008000000080
48
+ <= 7e52138bf614dddff1116623b027ea92d6015d8fe741575b78ac7773545066bd31337264523573727657463533566b4e654a757a35576b476b4736465358515141375a423551424769673441544373399000
49
+ => 90010000142c00008062010080030000800000008000000080
50
+ <= dfd77108b64b8173805852f7550f1f2d6c166968d31d5c4984187c9cb1969d9331363456674b366f6f5a3466557555346946567770537558774658474c55356e5454577a4736347672447167697077689000
51
+ `,
52
+ },
53
+ ],
54
+ accounts: [
55
+ {
56
+ // Account which is stash and controller
57
+ raw: {
58
+ id: `js:2:polkadot:${ACCOUNT_SAME_STASHCONTROLLER}:polkadotbip44`,
59
+ seedIdentifier: ACCOUNT_SAME_STASHCONTROLLER,
60
+ name: "Polkadot 1",
61
+ derivationMode: "polkadotbip44",
62
+ index: 0,
63
+ freshAddress: ACCOUNT_SAME_STASHCONTROLLER,
64
+ freshAddressPath: "44'/354'/0'/0'/0'",
65
+ freshAddresses: [],
66
+ blockHeight: 0,
67
+ operations: [],
68
+ pendingOperations: [],
69
+ currencyId: "polkadot",
70
+ unitMagnitude: 10,
71
+ lastSyncDate: "",
72
+ balance: "21000310",
73
+ },
74
+ transactions: [
75
+ {
76
+ name: "recipient and sender must not be the same",
77
+ transaction: fromTransactionRaw({
78
+ family: "polkadot",
79
+ recipient: ACCOUNT_SAME_STASHCONTROLLER,
80
+ amount: "100000000",
81
+ mode: "send",
82
+ era: null,
83
+ validators: [],
84
+ fees: null,
85
+ rewardDestination: null,
86
+ numSlashingSpans: 0,
87
+ }),
88
+ expectedStatus: {
89
+ amount: new BigNumber("100000000"),
90
+ errors: {
91
+ recipient: new InvalidAddressBecauseDestinationIsAlsoSource(),
92
+ },
93
+ warnings: {},
94
+ },
95
+ },
96
+ {
97
+ name: "Not a valid address",
98
+ transaction: fromTransactionRaw({
99
+ family: "polkadot",
100
+ recipient: "ewrererewrew",
101
+ amount: "100000000",
102
+ mode: "send",
103
+ era: null,
104
+ validators: [],
105
+ fees: null,
106
+ rewardDestination: null,
107
+ numSlashingSpans: 0,
108
+ }),
109
+ expectedStatus: {
110
+ errors: {
111
+ recipient: new InvalidAddress(),
112
+ },
113
+ warnings: {},
114
+ },
115
+ },
116
+ {
117
+ name: "Amount Required",
118
+ transaction: fromTransactionRaw({
119
+ family: "polkadot",
120
+ recipient: ACCOUNT_CONTROLLER,
121
+ amount: "0",
122
+ mode: "send",
123
+ era: null,
124
+ validators: [],
125
+ fees: null,
126
+ rewardDestination: null,
127
+ numSlashingSpans: 0,
128
+ }),
129
+ expectedStatus: {
130
+ errors: {
131
+ amount: new AmountRequired(),
132
+ },
133
+ warnings: {},
134
+ },
135
+ },
136
+ {
137
+ name: "Not enough balance",
138
+ transaction: fromTransactionRaw({
139
+ family: "polkadot",
140
+ recipient: ACCOUNT_CONTROLLER,
141
+ amount: "1000000000000000",
142
+ mode: "send",
143
+ era: null,
144
+ validators: [],
145
+ fees: null,
146
+ rewardDestination: null,
147
+ numSlashingSpans: 0,
148
+ }),
149
+ expectedStatus: {
150
+ errors: {
151
+ amount: new NotEnoughBalance(),
152
+ },
153
+ warnings: {},
154
+ },
155
+ },
156
+ {
157
+ name: "Not created account and deposit not existing",
158
+ transaction: fromTransactionRaw({
159
+ family: "polkadot",
160
+ recipient: ACCOUNT_WITH_NO_OPERATION,
161
+ amount: "1000000",
162
+ mode: "send",
163
+ era: null,
164
+ validators: [],
165
+ fees: null,
166
+ rewardDestination: null,
167
+ numSlashingSpans: 0,
168
+ }),
169
+ expectedStatus: {
170
+ errors: {
171
+ amount: new NotEnoughBalanceBecauseDestinationNotCreated(),
172
+ },
173
+ warnings: {},
174
+ },
175
+ },
176
+ {
177
+ name: "New account and suffisent deposit",
178
+ transaction: fromTransactionRaw({
179
+ family: "polkadot",
180
+ recipient: ACCOUNT_WITH_NO_OPERATION,
181
+ amount: "10000000000",
182
+ mode: "send",
183
+ era: null,
184
+ validators: [],
185
+ fees: null,
186
+ rewardDestination: null,
187
+ numSlashingSpans: 0,
188
+ }),
189
+ expectedStatus: {
190
+ amount: new BigNumber("10000000000"),
191
+ errors: {},
192
+ warnings: {},
193
+ },
194
+ },
195
+ {
196
+ name: "nominate without true validator",
197
+ transaction: fromTransactionRaw({
198
+ family: "polkadot",
199
+ recipient: ACCOUNT_SAME_STASHCONTROLLER,
200
+ amount: "0",
201
+ mode: "nominate",
202
+ era: null,
203
+ validators: [ACCOUNT_SAME_STASHCONTROLLER],
204
+ fees: null,
205
+ rewardDestination: null,
206
+ numSlashingSpans: 0,
207
+ }),
208
+ expectedStatus: {
209
+ errors: {
210
+ staking: new PolkadotNotValidator(),
211
+ },
212
+ warnings: {},
213
+ },
214
+ },
215
+ {
216
+ name: "nominate is empty",
217
+ transaction: fromTransactionRaw({
218
+ family: "polkadot",
219
+ recipient: ACCOUNT_SAME_STASHCONTROLLER,
220
+ amount: "0",
221
+ mode: "nominate",
222
+ era: null,
223
+ validators: [],
224
+ fees: null,
225
+ rewardDestination: null,
226
+ numSlashingSpans: 0,
227
+ }),
228
+ expectedStatus: {
229
+ errors: {
230
+ staking: new PolkadotValidatorsRequired(),
231
+ },
232
+ warnings: {},
233
+ },
234
+ },
235
+ {
236
+ name: "bond extra - not enough spendable",
237
+ transaction: fromTransactionRaw({
238
+ family: "polkadot",
239
+ recipient: ACCOUNT_SAME_STASHCONTROLLER,
240
+ amount: "2000000000000000000",
241
+ mode: "bond",
242
+ era: null,
243
+ validators: null,
244
+ fees: null,
245
+ rewardDestination: null,
246
+ numSlashingSpans: 0,
247
+ }),
248
+ expectedStatus: {
249
+ errors: {
250
+ amount: new NotEnoughBalance(),
251
+ },
252
+ warnings: {},
253
+ },
254
+ },
255
+ {
256
+ name: "[Bond] New controller and suffisent deposit",
257
+ transaction: fromTransactionRaw({
258
+ family: "polkadot",
259
+ recipient: ACCOUNT_EMPTY,
260
+ amount: "10000000000",
261
+ mode: "bond",
262
+ era: null,
263
+ validators: [],
264
+ fees: null,
265
+ rewardDestination: null,
266
+ numSlashingSpans: 0,
267
+ }),
268
+ expectedStatus: {
269
+ amount: new BigNumber("10000000000"),
270
+ errors: {},
271
+ warnings: {},
272
+ },
273
+ },
274
+ {
275
+ name: "[unbond] no amount",
276
+ transaction: fromTransactionRaw({
277
+ family: "polkadot",
278
+ recipient: "",
279
+ amount: "0",
280
+ mode: "unbond",
281
+ era: null,
282
+ validators: [],
283
+ fees: null,
284
+ rewardDestination: null,
285
+ numSlashingSpans: 0,
286
+ }),
287
+ expectedStatus: {
288
+ errors: {
289
+ amount: new AmountRequired(),
290
+ },
291
+ warnings: {},
292
+ },
293
+ },
294
+ {
295
+ name: "[unbond] not enough locked balance",
296
+ transaction: fromTransactionRaw({
297
+ family: "polkadot",
298
+ recipient: "",
299
+ amount: "2000000000000000",
300
+ mode: "unbond",
301
+ era: null,
302
+ validators: [],
303
+ fees: null,
304
+ rewardDestination: null,
305
+ numSlashingSpans: 0,
306
+ }),
307
+ expectedStatus: {
308
+ errors: {
309
+ amount: new NotEnoughBalance(),
310
+ },
311
+ warnings: {},
312
+ },
313
+ },
314
+ {
315
+ name: "[rebond] no amount",
316
+ transaction: fromTransactionRaw({
317
+ family: "polkadot",
318
+ recipient: "",
319
+ amount: "0",
320
+ mode: "rebond",
321
+ era: null,
322
+ validators: [],
323
+ fees: null,
324
+ rewardDestination: null,
325
+ numSlashingSpans: 0,
326
+ }),
327
+ expectedStatus: {
328
+ errors: {
329
+ amount: new AmountRequired(),
330
+ },
331
+ warnings: {},
332
+ },
333
+ },
334
+ {
335
+ name: "[unbond] use all amount",
336
+ transaction: (t) => ({
337
+ ...t,
338
+ useAllAmount: true,
339
+ mode: "unbond",
340
+ }),
341
+ expectedStatus: (a) => ({
342
+ errors: {},
343
+ warnings: {},
344
+ amount: (
345
+ a as PolkadotAccount
346
+ ).polkadotResources?.lockedBalance.minus(
347
+ (a as PolkadotAccount).polkadotResources.unlockingBalance
348
+ ),
349
+ }),
350
+ },
351
+ {
352
+ name: "[rebond] use all amount",
353
+ transaction: (t) => ({
354
+ ...t,
355
+ useAllAmount: true,
356
+ mode: "rebond",
357
+ }),
358
+ expectedStatus: (a) => ({
359
+ errors: {},
360
+ warnings: {},
361
+ amount: (a as PolkadotAccount).polkadotResources?.unlockingBalance,
362
+ }),
363
+ },
364
+ ],
365
+ },
366
+ {
367
+ raw: {
368
+ id: `js:2:polkadot:${ACCOUNT_STASH}:polkadotbip44`,
369
+ seedIdentifier: ACCOUNT_STASH,
370
+ name: "Polkadot 2",
371
+ derivationMode: "polkadotbip44",
372
+ index: 0,
373
+ freshAddress: ACCOUNT_STASH,
374
+ freshAddressPath: "44'/354'/2'/0'/0'",
375
+ freshAddresses: [],
376
+ blockHeight: 0,
377
+ operations: [],
378
+ pendingOperations: [],
379
+ currencyId: "polkadot",
380
+ unitMagnitude: 10,
381
+ lastSyncDate: "",
382
+ balance: "11000000000",
383
+ },
384
+ transactions: [
385
+ {
386
+ name: "[send] use all amount - should warn all funds",
387
+ transaction: (t) => ({
388
+ ...t,
389
+ useAllAmount: true,
390
+ mode: "send",
391
+ recipient: ACCOUNT_SAME_STASHCONTROLLER,
392
+ }),
393
+ expectedStatus: (account) => ({
394
+ errors: {},
395
+ warnings: {
396
+ amount: new PolkadotAllFundsWarning(),
397
+ },
398
+ totalSpent: account.spendableBalance,
399
+ }),
400
+ },
401
+ {
402
+ name: "stash can't nominate",
403
+ transaction: fromTransactionRaw({
404
+ family: "polkadot",
405
+ recipient: ACCOUNT_SAME_STASHCONTROLLER,
406
+ amount: "0",
407
+ mode: "nominate",
408
+ era: null,
409
+ validators: [ACCOUNT_SAME_STASHCONTROLLER],
410
+ fees: null,
411
+ rewardDestination: null,
412
+ numSlashingSpans: 0,
413
+ }),
414
+ expectedStatus: {
415
+ errors: {
416
+ staking: new PolkadotUnauthorizedOperation(),
417
+ },
418
+ warnings: {},
419
+ },
420
+ },
421
+ {
422
+ name: "[chill] unauthorized",
423
+ transaction: fromTransactionRaw({
424
+ family: "polkadot",
425
+ recipient: "",
426
+ amount: "1000",
427
+ mode: "chill",
428
+ era: null,
429
+ validators: [],
430
+ fees: null,
431
+ rewardDestination: null,
432
+ numSlashingSpans: 0,
433
+ }),
434
+ expectedStatus: {
435
+ errors: {
436
+ staking: new PolkadotUnauthorizedOperation(),
437
+ },
438
+ warnings: {},
439
+ },
440
+ },
441
+ {
442
+ name: "[rebond] unauthorized",
443
+ transaction: fromTransactionRaw({
444
+ family: "polkadot",
445
+ recipient: "",
446
+ amount: "1000",
447
+ mode: "rebond",
448
+ era: null,
449
+ validators: [],
450
+ fees: null,
451
+ rewardDestination: null,
452
+ numSlashingSpans: 0,
453
+ }),
454
+ expectedStatus: {
455
+ errors: {
456
+ staking: new PolkadotUnauthorizedOperation(),
457
+ },
458
+ warnings: {},
459
+ },
460
+ },
461
+ {
462
+ name: "[withdrawUnbonded] unauthorized",
463
+ transaction: fromTransactionRaw({
464
+ family: "polkadot",
465
+ recipient: "",
466
+ amount: "1000",
467
+ mode: "withdrawUnbonded",
468
+ era: null,
469
+ validators: [],
470
+ fees: null,
471
+ rewardDestination: null,
472
+ numSlashingSpans: 0,
473
+ }),
474
+ expectedStatus: {
475
+ errors: {
476
+ staking: new PolkadotUnauthorizedOperation(),
477
+ },
478
+ warnings: {},
479
+ },
480
+ },
481
+ ],
482
+ },
483
+ // TODO: Write a setController test
484
+ {
485
+ raw: {
486
+ id: `js:2:polkadot:${ACCOUNT_CONTROLLER}:polkadotbip44`,
487
+ seedIdentifier: ACCOUNT_CONTROLLER,
488
+ name: "Polkadot 3",
489
+ derivationMode: "polkadotbip44",
490
+ index: 0,
491
+ freshAddress: ACCOUNT_CONTROLLER,
492
+ freshAddressPath: "44'/354'/3'/0'/0'",
493
+ freshAddresses: [],
494
+ blockHeight: 0,
495
+ operations: [],
496
+ pendingOperations: [],
497
+ currencyId: "polkadot",
498
+ unitMagnitude: 10,
499
+ lastSyncDate: "",
500
+ balance: "11000000000",
501
+ },
502
+ transactions: [
503
+ {
504
+ name: "[send] Not enough balance",
505
+ transaction: fromTransactionRaw({
506
+ family: "polkadot",
507
+ recipient: ACCOUNT_SAME_STASHCONTROLLER,
508
+ amount: "12000000000",
509
+ mode: "send",
510
+ era: null,
511
+ validators: [],
512
+ fees: null,
513
+ rewardDestination: null,
514
+ numSlashingSpans: 0,
515
+ }),
516
+ expectedStatus: {
517
+ errors: {
518
+ amount: new NotEnoughBalance(),
519
+ },
520
+ warnings: {},
521
+ },
522
+ },
523
+ {
524
+ name: "[send] Do Max Send Instead error",
525
+ transaction: fromTransactionRaw({
526
+ family: "polkadot",
527
+ recipient: ACCOUNT_SAME_STASHCONTROLLER,
528
+ amount: "5000000000",
529
+ mode: "send",
530
+ era: null,
531
+ validators: [],
532
+ fees: null,
533
+ rewardDestination: null,
534
+ numSlashingSpans: 0,
535
+ }),
536
+ expectedStatus: {
537
+ errors: {
538
+ amount: new PolkadotDoMaxSendInstead(),
539
+ },
540
+ warnings: {},
541
+ },
542
+ },
543
+ {
544
+ name: "[send] use all amount no warn",
545
+ transaction: fromTransactionRaw({
546
+ family: "polkadot",
547
+ recipient: ACCOUNT_SAME_STASHCONTROLLER,
548
+ amount: "0",
549
+ useAllAmount: true,
550
+ mode: "send",
551
+ era: null,
552
+ validators: [],
553
+ fees: null,
554
+ rewardDestination: null,
555
+ numSlashingSpans: 0,
556
+ }),
557
+ expectedStatus: {
558
+ errors: {},
559
+ warnings: {},
560
+ },
561
+ },
562
+ {
563
+ name: "[bond] no recipient",
564
+ transaction: fromTransactionRaw({
565
+ family: "polkadot",
566
+ recipient: "",
567
+ amount: "0",
568
+ mode: "bond",
569
+ era: null,
570
+ validators: [],
571
+ fees: null,
572
+ rewardDestination: null,
573
+ numSlashingSpans: 0,
574
+ }),
575
+ expectedStatus: {
576
+ errors: {
577
+ recipient: new RecipientRequired(),
578
+ },
579
+ warnings: {},
580
+ },
581
+ },
582
+ {
583
+ name: "[bond] recipient with invalid address",
584
+ transaction: fromTransactionRaw({
585
+ family: "polkadot",
586
+ recipient: "not a valid address",
587
+ amount: "0",
588
+ mode: "bond",
589
+ era: null,
590
+ validators: [],
591
+ fees: null,
592
+ rewardDestination: null,
593
+ numSlashingSpans: 0,
594
+ }),
595
+ expectedStatus: {
596
+ errors: {
597
+ recipient: new InvalidAddress(),
598
+ },
599
+ warnings: {},
600
+ },
601
+ },
602
+ {
603
+ name: "[bond] is already controller",
604
+ transaction: fromTransactionRaw({
605
+ family: "polkadot",
606
+ recipient: ACCOUNT_CONTROLLER,
607
+ amount: "100000000",
608
+ mode: "bond",
609
+ era: null,
610
+ validators: [],
611
+ fees: null,
612
+ rewardDestination: null,
613
+ numSlashingSpans: 0,
614
+ }),
615
+ expectedStatus: {
616
+ errors: {
617
+ recipient: new PolkadotUnauthorizedOperation(
618
+ "Recipient is already a controller"
619
+ ),
620
+ },
621
+ warnings: {},
622
+ },
623
+ },
624
+ {
625
+ name: "[bond] not minimum amount",
626
+ transaction: fromTransactionRaw({
627
+ family: "polkadot",
628
+ recipient: ACCOUNT_CONTROLLER,
629
+ amount: "1000000",
630
+ mode: "bond",
631
+ era: null,
632
+ validators: [],
633
+ fees: null,
634
+ rewardDestination: null,
635
+ numSlashingSpans: 0,
636
+ }),
637
+ expectedStatus: {
638
+ errors: {
639
+ amount: new PolkadotBondMinimumAmount(),
640
+ },
641
+ warnings: {},
642
+ },
643
+ },
644
+ {
645
+ name: "[bond] success",
646
+ transaction: fromTransactionRaw({
647
+ family: "polkadot",
648
+ recipient: ACCOUNT_CONTROLLER,
649
+ amount: "10000000000",
650
+ mode: "bond",
651
+ era: null,
652
+ validators: [],
653
+ fees: null,
654
+ rewardDestination: null,
655
+ numSlashingSpans: 0,
656
+ }),
657
+ expectedStatus: {
658
+ errors: {},
659
+ warnings: {},
660
+ },
661
+ },
662
+ {
663
+ name: "[unbond] haveEnoughLockedBalance",
664
+ transaction: fromTransactionRaw({
665
+ family: "polkadot",
666
+ recipient: "",
667
+ amount: "100000",
668
+ mode: "unbond",
669
+ era: null,
670
+ validators: [],
671
+ fees: null,
672
+ rewardDestination: null,
673
+ numSlashingSpans: 0,
674
+ }),
675
+ expectedStatus: {
676
+ errors: {
677
+ amount: new NotEnoughBalance(),
678
+ staking: new PolkadotUnauthorizedOperation(),
679
+ },
680
+ warnings: {},
681
+ },
682
+ },
683
+ ],
684
+ },
685
+ ],
686
+ };
687
+
688
+ export const dataset: DatasetTest<Transaction> = {
689
+ implementations: ["js"],
690
+ currencies: {
691
+ polkadot,
692
+ },
693
+ };
694
+
695
+ // Disconnect all api clients that could be open.
696
+ afterAll(async () => {
697
+ await disconnect();
698
+ });
699
+
700
+ describe("canUnbond", () => {
701
+ test("can unbond", () => {
702
+ const account: Partial<PolkadotAccount> = {
703
+ polkadotResources: {
704
+ controller: "",
705
+ stash: "",
706
+ nonce: 0,
707
+ numSlashingSpans: 0,
708
+ lockedBalance: new BigNumber(10000),
709
+ unlockedBalance: new BigNumber(0),
710
+ unlockingBalance: new BigNumber(0),
711
+ nominations: [],
712
+ unlockings: [
713
+ ...Array(MAX_UNLOCKINGS - 1).map(() => ({
714
+ amount: new BigNumber(100000),
715
+ completionDate: new Date(),
716
+ })),
717
+ ],
718
+ },
719
+ };
720
+ expect(canUnbond(account as PolkadotAccount)).toBeTruthy();
721
+ });
722
+ test("can't unbond because unlockings is too much", () => {
723
+ const account: Partial<PolkadotAccount> = {
724
+ polkadotResources: {
725
+ controller: "",
726
+ stash: "",
727
+ nonce: 0,
728
+ numSlashingSpans: 0,
729
+ lockedBalance: new BigNumber(1000000),
730
+ unlockedBalance: new BigNumber(0),
731
+ unlockingBalance: new BigNumber(0),
732
+ nominations: [],
733
+ unlockings: [
734
+ ...Array(MAX_UNLOCKINGS).map(() => ({
735
+ amount: new BigNumber(100000),
736
+ completionDate: new Date(),
737
+ })),
738
+ ],
739
+ },
740
+ };
741
+ expect(canUnbond(account as PolkadotAccount)).toBeFalsy();
742
+ });
743
+ test("can't unbond because not enough lockedBalance", () => {
744
+ const account: Partial<PolkadotAccount> = {
745
+ polkadotResources: {
746
+ controller: "",
747
+ stash: "",
748
+ nonce: 0,
749
+ numSlashingSpans: 0,
750
+ lockedBalance: new BigNumber(100),
751
+ unlockedBalance: new BigNumber(0),
752
+ unlockingBalance: new BigNumber(100),
753
+ nominations: [],
754
+ unlockings: [
755
+ ...Array(MAX_UNLOCKINGS).map(() => ({
756
+ amount: new BigNumber(100000),
757
+ completionDate: new Date(),
758
+ })),
759
+ ],
760
+ },
761
+ };
762
+ expect(canUnbond(account as PolkadotAccount)).toBeFalsy();
763
+ });
764
+ });