@ledgerhq/coin-polkadot 0.1.0 → 0.1.1-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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @ledgerhq/coin-polkadot
2
2
 
3
+ ## 0.1.1-next.0
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [[`5785155282`](https://github.com/LedgerHQ/ledger-live/commit/5785155282d61d0dbdc30f7a66d3243a74fce117), [`197d28697b`](https://github.com/LedgerHQ/ledger-live/commit/197d28697b173c3f6b2badfe4d1deddeadc912d4), [`d4834aad3f`](https://github.com/LedgerHQ/ledger-live/commit/d4834aad3f58d904850be9a3ab40b46260d9f7d4)]:
8
+ - @ledgerhq/cryptoassets@9.3.0-next.0
9
+ - @ledgerhq/types-live@6.32.0-next.0
10
+ - @ledgerhq/coin-framework@0.2.2-next.0
11
+ - @ledgerhq/live-portfolio@0.0.2-next.0
12
+
3
13
  ## 0.1.0
4
14
 
5
15
  ### Minor Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ledgerhq/coin-polkadot",
3
- "version": "0.1.0",
3
+ "version": "0.1.1-next.0",
4
4
  "description": "Ledger Polkadot Coin integration",
5
5
  "keywords": [
6
6
  "Ledger",
@@ -54,20 +54,22 @@
54
54
  "invariant": "^2.2.2",
55
55
  "lodash": "^4.17.21",
56
56
  "rxjs": "^6.6.7",
57
- "@ledgerhq/coin-framework": "^0.2.1",
58
- "@ledgerhq/cryptoassets": "^9.2.0",
57
+ "@ledgerhq/coin-framework": "^0.2.2-next.0",
58
+ "@ledgerhq/cryptoassets": "^9.3.0-next.0",
59
59
  "@ledgerhq/devices": "^8.0.1",
60
60
  "@ledgerhq/errors": "^6.12.4",
61
61
  "@ledgerhq/hw-app-polkadot": "^6.27.13",
62
62
  "@ledgerhq/live-env": "^0.0.1",
63
- "@ledgerhq/live-portfolio": "^0.0.1",
63
+ "@ledgerhq/live-portfolio": "^0.0.2-next.0",
64
64
  "@ledgerhq/live-promise": "^0.0.1",
65
65
  "@ledgerhq/logs": "^6.10.1",
66
66
  "@ledgerhq/types-cryptoassets": "^7.0.0",
67
- "@ledgerhq/types-live": "^6.31.0"
67
+ "@ledgerhq/types-live": "^6.32.0-next.0"
68
68
  },
69
69
  "devDependencies": {
70
+ "@types/invariant": "^2.2.2",
70
71
  "@types/jest": "^29.2.4",
72
+ "@types/lodash": "^4.14.191",
71
73
  "@typescript-eslint/eslint-plugin": "^5.46.1",
72
74
  "@typescript-eslint/parser": "^5.46.1",
73
75
  "eslint": "^7.32.0",
package/src/account.ts CHANGED
@@ -18,7 +18,7 @@ function formatOperationSpecifics(
18
18
  validatorStash,
19
19
  amount,
20
20
  } = op.extra;
21
- let str = (validators || []).map((v) => `\n ${v}`).join("");
21
+ let str = (validators || []).map((v: any) => `\n ${v}`).join("");
22
22
  const formatConfig = {
23
23
  disableRounding: true,
24
24
  alwaysShowSign: false,
@@ -38,9 +38,9 @@ const getAccountOperationUrl = (
38
38
  startAt,
39
39
  })}`;
40
40
 
41
- const getWithdrawUnbondedAmount = (extrinsic) => {
41
+ const getWithdrawUnbondedAmount = (extrinsic: any) => {
42
42
  return (
43
- extrinsic?.staking?.eventStaking.reduce((acc, curr) => {
43
+ extrinsic?.staking?.eventStaking.reduce((acc: any, curr: any) => {
44
44
  return curr.method === "Withdrawn"
45
45
  ? new BigNumber(acc).plus(curr.value)
46
46
  : new BigNumber(0);
@@ -48,7 +48,7 @@ const getWithdrawUnbondedAmount = (extrinsic) => {
48
48
  );
49
49
  };
50
50
 
51
- const getController = (_extrinsic) => {
51
+ const getController = (_extrinsic: any) => {
52
52
  // TODO: ask BisonTrails to provide the info
53
53
  return "";
54
54
  };
@@ -126,7 +126,7 @@ const getExtra = (type: OperationType, extrinsic: any): Record<string, any> => {
126
126
  extra = {
127
127
  ...extra,
128
128
  validators:
129
- extrinsic.staking?.validators?.reduce((acc, current) => {
129
+ extrinsic.staking?.validators?.reduce((acc: any, current: any) => {
130
130
  return [...acc, current.address];
131
131
  }, []) ?? [],
132
132
  };
@@ -144,7 +144,7 @@ const getExtra = (type: OperationType, extrinsic: any): Record<string, any> => {
144
144
  *
145
145
  * @returns {BigNumber}
146
146
  */
147
- const getValue = (extrinsic, type: OperationType): BigNumber => {
147
+ const getValue = (extrinsic: any, type: OperationType): BigNumber => {
148
148
  if (!extrinsic.isSuccess) {
149
149
  return type === "IN"
150
150
  ? new BigNumber(0)
@@ -178,7 +178,7 @@ const getValue = (extrinsic, type: OperationType): BigNumber => {
178
178
  const extrinsicToOperation = (
179
179
  addr: string,
180
180
  accountId: string,
181
- extrinsic
181
+ extrinsic: any
182
182
  ): Partial<Operation> | null => {
183
183
  let type = getOperationType(extrinsic.section, extrinsic.method);
184
184
 
@@ -226,7 +226,7 @@ const extrinsicToOperation = (
226
226
  const rewardToOperation = (
227
227
  addr: string,
228
228
  accountId: string,
229
- reward
229
+ reward: any
230
230
  ): Partial<Operation> => {
231
231
  const hash = reward.extrinsicHash;
232
232
  const type = "REWARD_PAYOUT";
@@ -257,7 +257,7 @@ const rewardToOperation = (
257
257
  const slashToOperation = (
258
258
  addr: string,
259
259
  accountId: string,
260
- slash
260
+ slash: any
261
261
  ): Partial<Operation> => {
262
262
  const hash = `${slash.blockNumber}`;
263
263
  const type = "SLASH";
@@ -294,18 +294,18 @@ const fetchOperationList =
294
294
  startAt: number,
295
295
  offset = 0,
296
296
  prevOperations: Operation[] = []
297
- ) => {
297
+ ): Promise<Operation[]> => {
298
298
  const { data } = await network({
299
299
  method: "GET",
300
300
  url: getAccountOperationUrl(addr, offset, startAt),
301
301
  });
302
- const operations = data.extrinsics.map((extrinsic) =>
302
+ const operations = data.extrinsics.map((extrinsic: any) =>
303
303
  extrinsicToOperation(addr, accountId, extrinsic)
304
304
  );
305
- const rewards = data.rewards.map((reward) =>
305
+ const rewards = data.rewards.map((reward: any) =>
306
306
  rewardToOperation(addr, accountId, reward)
307
307
  );
308
- const slashes = data.slashes.map((slash) =>
308
+ const slashes = data.slashes.map((slash: any) =>
309
309
  slashToOperation(addr, accountId, slash)
310
310
  );
311
311
  const mergedOp = [...prevOperations, ...operations, ...rewards, ...slashes];
@@ -44,7 +44,8 @@ const getBaseSidecarUrl = (): string => getEnv("API_POLKADOT_SIDECAR");
44
44
  *
45
45
  * @returns {string}
46
46
  */
47
- const getSidecarUrl = (route): string => `${getBaseSidecarUrl()}${route || ""}`;
47
+ const getSidecarUrl = (route: string): string =>
48
+ `${getBaseSidecarUrl()}${route || ""}`;
48
49
 
49
50
  const VALIDATOR_COMISSION_RATIO = 1000000000;
50
51
  const ELECTION_STATUS_OPTIMISTIC_THRESHOLD =
package/src/bridge/js.ts CHANGED
@@ -27,7 +27,10 @@ import { getPreloadStrategy, hydrate, preload } from "../preload";
27
27
  import type { Transaction } from "../types";
28
28
  import { LRUCacheFn } from "@ledgerhq/coin-framework/cache";
29
29
 
30
- const updateTransaction = (t, patch) => ({ ...t, ...patch });
30
+ const updateTransaction = (t: Transaction, patch: Partial<Transaction>) => ({
31
+ ...t,
32
+ ...patch,
33
+ });
31
34
 
32
35
  /**
33
36
  * Broadcast the signed transaction
@@ -12,7 +12,7 @@ import {
12
12
  SidecarValidatorsParamStatus,
13
13
  } from "./api/sidecar.types";
14
14
  import { AccountLike } from "@ledgerhq/types-live";
15
- import { PolkadotAccount, Transaction } from "./types";
15
+ import { PolkadotAccount, PolkadotValidator, Transaction } from "./types";
16
16
  import { PolkadotAPI } from "./api";
17
17
  import { NetworkRequestCall } from "@ledgerhq/coin-framework/network";
18
18
  import { LRUCacheFn } from "@ledgerhq/coin-framework/cache";
@@ -50,59 +50,61 @@ const options = [
50
50
  desc: "Reward destination",
51
51
  },
52
52
  ];
53
- const polkadotValidatorsFormatters = {
54
- json: (list) => JSON.stringify(list),
55
- csv: (list) => {
56
- const polkadotUnit = getCryptoCurrencyById("polkadot").units[0];
57
- const csvHeader =
58
- "ADDRESS IDENTITY TOTAL_BONDED SELF_BONDED NOMINATORS_COUNT COMMISSION REWARD_POINTS\n";
59
- const csvList = list
60
- .map(
61
- (v) =>
62
- `${v.address} "${v.identity}" ${formatCurrencyUnit(
63
- polkadotUnit,
64
- v.totalBonded,
65
- {
53
+ type PolkadotValidatorFormatter = (list: PolkadotValidator[]) => string;
54
+ const polkadotValidatorsFormatters: Record<string, PolkadotValidatorFormatter> =
55
+ {
56
+ json: (list: PolkadotValidator[]) => JSON.stringify(list),
57
+ csv: (list: PolkadotValidator[]) => {
58
+ const polkadotUnit = getCryptoCurrencyById("polkadot").units[0];
59
+ const csvHeader =
60
+ "ADDRESS IDENTITY TOTAL_BONDED SELF_BONDED NOMINATORS_COUNT COMMISSION REWARD_POINTS\n";
61
+ const csvList = list
62
+ .map(
63
+ (v) =>
64
+ `${v.address} "${v.identity}" ${formatCurrencyUnit(
65
+ polkadotUnit,
66
+ v.totalBonded,
67
+ {
68
+ showCode: false,
69
+ disableRounding: true,
70
+ useGrouping: false,
71
+ }
72
+ )} ${formatCurrencyUnit(polkadotUnit, v.selfBonded, {
66
73
  showCode: false,
67
74
  disableRounding: true,
68
75
  useGrouping: false,
69
- }
70
- )} ${formatCurrencyUnit(polkadotUnit, v.selfBonded, {
71
- showCode: false,
72
- disableRounding: true,
73
- useGrouping: false,
74
- })} ${v.nominatorsCount} ${v.commission
75
- .multipliedBy(100)
76
- .toFixed(2)} ${v.rewardPoints}`
77
- )
78
- .join("\n");
79
- return csvHeader + csvList;
80
- },
81
- default: (list) => {
82
- const polkadotUnit = getCryptoCurrencyById("polkadot").units[0];
83
- const tableList = list
84
- .map(
85
- (v) =>
86
- `${v.address} "${v.identity}" ${formatCurrencyUnit(
87
- polkadotUnit,
88
- v.totalBonded,
89
- {
76
+ })} ${v.nominatorsCount} ${v.commission
77
+ .multipliedBy(100)
78
+ .toFixed(2)} ${v.rewardPoints}`
79
+ )
80
+ .join("\n");
81
+ return csvHeader + csvList;
82
+ },
83
+ default: (list: PolkadotValidator[]) => {
84
+ const polkadotUnit = getCryptoCurrencyById("polkadot").units[0];
85
+ const tableList = list
86
+ .map(
87
+ (v) =>
88
+ `${v.address} "${v.identity}" ${formatCurrencyUnit(
89
+ polkadotUnit,
90
+ v.totalBonded,
91
+ {
92
+ showCode: true,
93
+ disableRounding: true,
94
+ useGrouping: false,
95
+ }
96
+ )} ${formatCurrencyUnit(polkadotUnit, v.selfBonded, {
90
97
  showCode: true,
91
98
  disableRounding: true,
92
99
  useGrouping: false,
93
- }
94
- )} ${formatCurrencyUnit(polkadotUnit, v.selfBonded, {
95
- showCode: true,
96
- disableRounding: true,
97
- useGrouping: false,
98
- })} ${v.nominatorsCount} ${
99
- v.commission.multipliedBy(100).toFixed(2) + "%"
100
- } ${v.rewardPoints + " PTS"}`
101
- )
102
- .join("\n");
103
- return tableList;
104
- },
105
- };
100
+ })} ${v.nominatorsCount} ${
101
+ v.commission.multipliedBy(100).toFixed(2) + "%"
102
+ } ${v.rewardPoints + " PTS"}`
103
+ )
104
+ .join("\n");
105
+ return tableList;
106
+ },
107
+ };
106
108
  function createValidators(polkadotAPI: PolkadotAPI) {
107
109
  return {
108
110
  args: [
@@ -139,8 +141,9 @@ function createValidators(polkadotAPI: PolkadotAPI) {
139
141
  ).pipe(
140
142
  map((validators) => {
141
143
  const f =
142
- (format && polkadotValidatorsFormatters[format]) ||
143
- polkadotValidatorsFormatters.default;
144
+ format && format in polkadotValidatorsFormatters
145
+ ? polkadotValidatorsFormatters[format]
146
+ : polkadotValidatorsFormatters.default;
144
147
  return f(validators);
145
148
  })
146
149
  ),
@@ -155,30 +158,39 @@ function inferTransactions(
155
158
  opts: Record<string, any>,
156
159
  { inferAmount }: any
157
160
  ): Transaction[] {
158
- return flatMap(transactions, ({ transaction, account }) => {
159
- invariant(transaction.family === "polkadot", "polkadot family");
161
+ return flatMap(
162
+ transactions,
163
+ ({
164
+ transaction,
165
+ account,
166
+ }: {
167
+ account: AccountLike;
168
+ transaction: Transaction;
169
+ }) => {
170
+ invariant(transaction.family === "polkadot", "polkadot family");
160
171
 
161
- if (isAccount(account)) {
162
- invariant(
163
- (account as PolkadotAccount).polkadotResources,
164
- "unactivated account"
165
- );
166
- }
172
+ if (isAccount(account)) {
173
+ invariant(
174
+ (account as PolkadotAccount).polkadotResources,
175
+ "unactivated account"
176
+ );
177
+ }
167
178
 
168
- const validators: string[] = opts["validator"] || [];
169
- return {
170
- ...transaction,
171
- family: "polkadot",
172
- fees: opts.fees ? inferAmount(account, opts.fees) : null,
173
- mode: opts.mode || "send",
174
- validators,
175
- era: opts.era || null,
176
- rewardDestination: opts.rewardDestination || null,
177
- numSlashingSpans: isAccount(account)
178
- ? (account as PolkadotAccount).polkadotResources?.numSlashingSpans
179
- : null,
180
- };
181
- });
179
+ const validators: string[] = opts["validator"] || [];
180
+ return {
181
+ ...transaction,
182
+ family: "polkadot",
183
+ fees: opts.fees ? inferAmount(account, opts.fees) : null,
184
+ mode: opts.mode || "send",
185
+ validators,
186
+ era: opts.era || null,
187
+ rewardDestination: opts.rewardDestination || null,
188
+ numSlashingSpans: isAccount(account)
189
+ ? (account as PolkadotAccount).polkadotResources?.numSlashingSpans
190
+ : null,
191
+ };
192
+ }
193
+ );
182
194
  }
183
195
 
184
196
  export default function makeCliTools(
@@ -10,7 +10,15 @@ const DEFAULTS = {
10
10
  eraPeriod: 64,
11
11
  };
12
12
 
13
- const getExtrinsicParams = (a: PolkadotAccount, t: Transaction) => {
13
+ type ExtrinsicParams = {
14
+ name: string;
15
+ pallet: "staking" | "balances";
16
+ args: Record<string, any>;
17
+ };
18
+ const getExtrinsicParams = (
19
+ a: PolkadotAccount,
20
+ t: Transaction
21
+ ): ExtrinsicParams => {
14
22
  const validator = t.validators ? t.validators[0] : null;
15
23
 
16
24
  switch (t.mode) {
@@ -170,10 +178,8 @@ export const buildTransaction =
170
178
  const methodArgs = methodFunction.meta.args;
171
179
  const method = methodFunction(
172
180
  ...methodArgs.map((arg) => {
173
- if (
174
- extrinsicParams.args[stringCamelCase(arg.name.toString())] ===
175
- undefined
176
- ) {
181
+ const param = extrinsicParams.args[stringCamelCase(arg.name)];
182
+ if (param === undefined) {
177
183
  throw new Error(
178
184
  `Method ${extrinsicParams.pallet}::${
179
185
  extrinsicParams.name
@@ -181,7 +187,7 @@ export const buildTransaction =
181
187
  );
182
188
  }
183
189
 
184
- return extrinsicParams.args[stringCamelCase(arg.name.toString())];
190
+ return param;
185
191
  })
186
192
  ).toHex();
187
193
  const unsigned = {
@@ -2,8 +2,10 @@ import type { PolkadotAccount, Transaction } from "./types";
2
2
  import getEstimatedFees from "./js-getFeesForTransaction";
3
3
  import { loadPolkadotCrypto } from "./polkadot-crypto";
4
4
  import { PolkadotAPI } from "./api";
5
+ import BigNumber from "bignumber.js";
5
6
 
6
- const sameFees = (a, b) => (!a || !b ? a === b : a.eq(b));
7
+ const sameFees = (a: BigNumber, b?: BigNumber | null) =>
8
+ !a || !b ? a === b : a.eq(b);
7
9
 
8
10
  /**
9
11
  * Calculate fees for the current transaction
@@ -19,7 +21,7 @@ const prepareTransaction =
19
21
  t,
20
22
  });
21
23
 
22
- if (!sameFees(t.fees, fees)) {
24
+ if (!sameFees(fees, t.fees)) {
23
25
  return { ...t, fees };
24
26
  }
25
27
 
package/src/preload.ts CHANGED
@@ -2,7 +2,11 @@
2
2
  import { BigNumber } from "bignumber.js";
3
3
  import { Observable, Subject } from "rxjs";
4
4
  import { log } from "@ledgerhq/logs";
5
- import type { PolkadotPreloadData, PolkadotValidator } from "./types";
5
+ import type {
6
+ PolkadotPreloadData,
7
+ PolkadotStakingProgress,
8
+ PolkadotValidator,
9
+ } from "./types";
6
10
  import { loadPolkadotCrypto } from "./polkadot-crypto";
7
11
  import { PolkadotAPI } from "./api";
8
12
 
@@ -101,7 +105,10 @@ export const getPreloadStrategy = () => ({
101
105
  preloadMaxAge: PRELOAD_MAX_AGE,
102
106
  });
103
107
 
104
- const shouldRefreshValidators = (previousState, currentState) => {
108
+ const shouldRefreshValidators = (
109
+ previousState: PolkadotStakingProgress | undefined,
110
+ currentState: PolkadotStakingProgress
111
+ ) => {
105
112
  return !previousState || currentState.activeEra !== previousState.activeEra;
106
113
  };
107
114
 
package/src/specs.ts CHANGED
@@ -39,7 +39,7 @@ const polkadot: AppSpec<Transaction> = {
39
39
  name: "Polkadot",
40
40
  currency: getCryptoCurrencyById("polkadot"),
41
41
  appQuery: {
42
- model: DeviceModelId.nanoS,
42
+ model: DeviceModelId.nanoSP,
43
43
  appName: "Polkadot",
44
44
  },
45
45
  testTimeout: 2 * 60 * 1000,
package/tsconfig.json CHANGED
@@ -4,8 +4,6 @@
4
4
  "declarationMap": true,
5
5
  "sourceMap": true,
6
6
  "strict": true,
7
- "noImplicitAny": false,
8
- "noImplicitThis": false,
9
7
  "allowSyntheticDefaultImports": true,
10
8
  "esModuleInterop": true,
11
9
  "skipLibCheck": true,
@@ -16,6 +14,6 @@
16
14
  "lib": ["es2020", "dom"],
17
15
  "outDir": "lib"
18
16
  },
19
- "include": ["src/**/*"]
17
+ "include": ["src/**/*"],
18
+ "exclude": ["src/api/subscan.ts", "src/api/websocket.ts"]
20
19
  }
21
-