@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,844 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
29
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __read = (this && this.__read) || function (o, n) {
50
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
51
+ if (!m) return o;
52
+ var i = m.call(o), r, ar = [], e;
53
+ try {
54
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
55
+ }
56
+ catch (error) { e = { error: error }; }
57
+ finally {
58
+ try {
59
+ if (r && !r.done && (m = i["return"])) m.call(i);
60
+ }
61
+ finally { if (e) throw e.error; }
62
+ }
63
+ return ar;
64
+ };
65
+ var __importDefault = (this && this.__importDefault) || function (mod) {
66
+ return (mod && mod.__esModule) ? mod : { "default": mod };
67
+ };
68
+ exports.__esModule = true;
69
+ exports.getTransactionMaterialWithMetadata = exports.getConstants = exports.getRegistry = exports.getStakingProgress = exports.getValidators = exports.paymentInfo = exports.submitExtrinsic = exports.getTransactionParams = exports.getNominations = exports.getStakingInfo = exports.getAccount = exports.verifyValidatorAddresses = exports.isControllerAddress = exports.isNewAccount = exports.isElectionClosed = exports.disconnect = exports.getMinimumBondBalance = void 0;
70
+ var bignumber_js_1 = require("bignumber.js");
71
+ var querystring_1 = __importDefault(require("querystring"));
72
+ var types_1 = require("@polkadot/types");
73
+ var types_known_1 = require("@polkadot/types-known");
74
+ var metadata_1 = require("@polkadot/types/metadata");
75
+ var decorate_1 = require("@polkadot/types/metadata/decorate");
76
+ var cache_1 = require("@ledgerhq/coin-framework/cache");
77
+ var env_1 = require("@ledgerhq/coin-framework/env");
78
+ var network_1 = __importDefault(require("@ledgerhq/coin-framework/network"));
79
+ /**
80
+ * Get indexer base url.
81
+ *
82
+ * @returns {string}
83
+ */
84
+ var getBaseSidecarUrl = function () { return (0, env_1.getEnv)("API_POLKADOT_SIDECAR"); };
85
+ /**
86
+ * Returns the full indexer url for en route endpoint.
87
+ *
88
+ * @param {*} route
89
+ *
90
+ * @returns {string}
91
+ */
92
+ var getSidecarUrl = function (route) { return "".concat(getBaseSidecarUrl()).concat(route || ""); };
93
+ var VALIDATOR_COMISSION_RATIO = 1000000000;
94
+ var ELECTION_STATUS_OPTIMISTIC_THRESHOLD = (0, env_1.getEnv)("POLKADOT_ELECTION_STATUS_THRESHOLD") || 25;
95
+ // blocks = 2 minutes 30
96
+ /**
97
+ * Fetch Balance from the api.
98
+ *
99
+ * @async
100
+ * @param {string} addr
101
+ *
102
+ * @returns {SidecarAccountBalanceInfo}
103
+ */
104
+ var fetchBalanceInfo = function (addr) { return __awaiter(void 0, void 0, void 0, function () {
105
+ var data;
106
+ return __generator(this, function (_a) {
107
+ switch (_a.label) {
108
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
109
+ method: "GET",
110
+ url: getSidecarUrl("/accounts/".concat(addr, "/balance-info"))
111
+ })];
112
+ case 1:
113
+ data = (_a.sent()).data;
114
+ return [2 /*return*/, data];
115
+ }
116
+ });
117
+ }); };
118
+ /**
119
+ * Fetch the stash address associated to an account.
120
+ *
121
+ * @async
122
+ * @param {string} addr
123
+ *
124
+ * @returns {string}
125
+ */
126
+ var fetchStashAddr = function (addr) { return __awaiter(void 0, void 0, void 0, function () {
127
+ var data;
128
+ var _a, _b;
129
+ return __generator(this, function (_c) {
130
+ switch (_c.label) {
131
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
132
+ method: "GET",
133
+ url: getSidecarUrl("/pallets/staking/storage/ledger?keys[]=".concat(addr, "&key1=").concat(addr))
134
+ })];
135
+ case 1:
136
+ data = (_c.sent()).data;
137
+ return [2 /*return*/, (_b = (_a = data.value) === null || _a === void 0 ? void 0 : _a.stash) !== null && _b !== void 0 ? _b : null];
138
+ }
139
+ });
140
+ }); };
141
+ /**
142
+ * Fetch the controller address associated to an account.
143
+ *
144
+ * @async
145
+ * @param {string} addr
146
+ *
147
+ * @returns {string}
148
+ */
149
+ var fetchControllerAddr = function (addr) { return __awaiter(void 0, void 0, void 0, function () {
150
+ var data;
151
+ var _a;
152
+ return __generator(this, function (_b) {
153
+ switch (_b.label) {
154
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
155
+ method: "GET",
156
+ url: getSidecarUrl("/pallets/staking/storage/bonded?keys[]=".concat(addr, "&key1=").concat(addr))
157
+ })];
158
+ case 1:
159
+ data = (_b.sent()).data;
160
+ return [2 /*return*/, (_a = data.value) !== null && _a !== void 0 ? _a : null];
161
+ }
162
+ });
163
+ }); };
164
+ /**
165
+ * Fetch the staking info for an account.
166
+ *
167
+ * @async
168
+ * @param {string} addr
169
+ *
170
+ * @returns {SidecarStakingInfo}
171
+ */
172
+ var fetchStakingInfo = function (addr) { return __awaiter(void 0, void 0, void 0, function () {
173
+ var data;
174
+ return __generator(this, function (_a) {
175
+ switch (_a.label) {
176
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
177
+ method: "GET",
178
+ url: getSidecarUrl("/accounts/".concat(addr, "/staking-info"))
179
+ })];
180
+ case 1:
181
+ data = (_a.sent()).data;
182
+ return [2 /*return*/, data];
183
+ }
184
+ });
185
+ }); };
186
+ /**
187
+ * Returns the list of nominations for an account, with status and associated stake if relevant.
188
+ *
189
+ * @async
190
+ * @param {string} addr
191
+ *
192
+ * @returns {SidecarNominations}
193
+ */
194
+ var fetchNominations = function (addr) { return __awaiter(void 0, void 0, void 0, function () {
195
+ var data;
196
+ return __generator(this, function (_a) {
197
+ switch (_a.label) {
198
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
199
+ method: "GET",
200
+ url: getSidecarUrl("/accounts/".concat(addr, "/nominations"))
201
+ })];
202
+ case 1:
203
+ data = (_a.sent()).data;
204
+ return [2 /*return*/, data];
205
+ }
206
+ });
207
+ }); };
208
+ /**
209
+ * Returns the blockchain's runtime constants.
210
+ *
211
+ * @async
212
+ * @param {string} addr
213
+ *
214
+ * @returns {Object}
215
+ */
216
+ var fetchConstants = function () { return __awaiter(void 0, void 0, void 0, function () {
217
+ var data;
218
+ return __generator(this, function (_a) {
219
+ switch (_a.label) {
220
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
221
+ method: "GET",
222
+ url: getSidecarUrl("/runtime/constants")
223
+ })];
224
+ case 1:
225
+ data = (_a.sent()).data;
226
+ return [2 /*return*/, data.consts];
227
+ }
228
+ });
229
+ }); };
230
+ /**
231
+ * Returns the activeEra info
232
+ *
233
+ * @async
234
+ *
235
+ * @returns {SidecarPalletStorageItem}
236
+ */
237
+ var fetchActiveEra = function () { return __awaiter(void 0, void 0, void 0, function () {
238
+ var data;
239
+ return __generator(this, function (_a) {
240
+ switch (_a.label) {
241
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
242
+ method: "GET",
243
+ url: getSidecarUrl("/pallets/staking/storage/activeEra")
244
+ })];
245
+ case 1:
246
+ data = (_a.sent()).data;
247
+ return [2 /*return*/, data];
248
+ }
249
+ });
250
+ }); };
251
+ /**
252
+ * Fetch the minimum value allowed for a bond
253
+ *
254
+ * @async
255
+ * @param {string} addr
256
+ *
257
+ * @returns {string}
258
+ */
259
+ var getMinimumBondBalance = function () { return __awaiter(void 0, void 0, void 0, function () {
260
+ var data;
261
+ return __generator(this, function (_a) {
262
+ switch (_a.label) {
263
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
264
+ method: "GET",
265
+ url: getSidecarUrl("/pallets/staking/storage/minNominatorBond")
266
+ })];
267
+ case 1:
268
+ data = (_a.sent()).data;
269
+ return [2 /*return*/, (data.value && new bignumber_js_1.BigNumber(data.value)) || new bignumber_js_1.BigNumber(0)];
270
+ }
271
+ });
272
+ }); };
273
+ exports.getMinimumBondBalance = getMinimumBondBalance;
274
+ /**
275
+ * Fetch a list of validators with some info and indentity.
276
+ * It fetches the list providing a status (all, elected, waiting) and/or a list of
277
+ * addresses (comma-separated or as multiple query params).
278
+ *
279
+ * @async
280
+ * @param {string} status
281
+ * @param {string[]} addresses
282
+ *
283
+ * @returns {SidecarValidators}
284
+ */
285
+ var fetchValidators = function (status, addresses) {
286
+ if (status === void 0) { status = "all"; }
287
+ return __awaiter(void 0, void 0, void 0, function () {
288
+ var params, data;
289
+ return __generator(this, function (_a) {
290
+ switch (_a.label) {
291
+ case 0:
292
+ params = {};
293
+ if (status) {
294
+ params = __assign(__assign({}, params), { status: status });
295
+ }
296
+ if (addresses && addresses.length) {
297
+ params = __assign(__assign({}, params), { addresses: addresses.join(",") });
298
+ }
299
+ return [4 /*yield*/, (0, network_1["default"])({
300
+ method: "GET",
301
+ url: getSidecarUrl("/validators?".concat(querystring_1["default"].stringify(params)))
302
+ })];
303
+ case 1:
304
+ data = (_a.sent()).data;
305
+ return [2 /*return*/, data];
306
+ }
307
+ });
308
+ });
309
+ };
310
+ /**
311
+ * Fetch the progress info concerning staking.
312
+ *
313
+ * @async
314
+ *
315
+ * @returns {SidecarPalletStakingProgress}
316
+ */
317
+ var fetchStakingProgress = function () { return __awaiter(void 0, void 0, void 0, function () {
318
+ var data;
319
+ return __generator(this, function (_a) {
320
+ switch (_a.label) {
321
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
322
+ method: "GET",
323
+ url: getSidecarUrl("/pallets/staking/progress")
324
+ })];
325
+ case 1:
326
+ data = (_a.sent()).data;
327
+ return [2 /*return*/, data];
328
+ }
329
+ });
330
+ }); };
331
+ /**
332
+ * Fetch the transaction params needed to sign a transaction
333
+ *
334
+ * @async
335
+ *
336
+ * @returns {SidecarTransactionMaterial}
337
+ */
338
+ var fetchTransactionMaterial = function (
339
+ // By default we don't want any metadata.
340
+ withMetadata) {
341
+ if (withMetadata === void 0) { withMetadata = false; }
342
+ return __awaiter(void 0, void 0, void 0, function () {
343
+ var params, data;
344
+ return __generator(this, function (_a) {
345
+ switch (_a.label) {
346
+ case 0:
347
+ params = withMetadata ? "?metadata=scale" : "?noMeta=true";
348
+ return [4 /*yield*/, (0, network_1["default"])({
349
+ method: "GET",
350
+ url: getSidecarUrl("/transaction/material".concat(params))
351
+ })];
352
+ case 1:
353
+ data = (_a.sent()).data;
354
+ return [2 /*return*/, data];
355
+ }
356
+ });
357
+ });
358
+ };
359
+ /**
360
+ * Fetch the blockchain specs
361
+ *
362
+ * @async
363
+ *
364
+ * @returns {SidecarRuntimeSpec}
365
+ */
366
+ var fetchChainSpec = function () { return __awaiter(void 0, void 0, void 0, function () {
367
+ var data;
368
+ return __generator(this, function (_a) {
369
+ switch (_a.label) {
370
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
371
+ method: "GET",
372
+ url: getSidecarUrl("/runtime/spec")
373
+ })];
374
+ case 1:
375
+ data = (_a.sent()).data;
376
+ return [2 /*return*/, data];
377
+ }
378
+ });
379
+ }); };
380
+ // Not relevant when not using websocket
381
+ var disconnect = function () { };
382
+ exports.disconnect = disconnect;
383
+ /**
384
+ * Returns true if ElectionStatus is Close.
385
+ * If ElectionStatus is Open, some features must be disabled.
386
+ *
387
+ * @async
388
+ *
389
+ * @returns {boolean}
390
+ */
391
+ var isElectionClosed = function () { return __awaiter(void 0, void 0, void 0, function () {
392
+ var progress;
393
+ var _a, _b;
394
+ return __generator(this, function (_c) {
395
+ switch (_c.label) {
396
+ case 0: return [4 /*yield*/, fetchStakingProgress()];
397
+ case 1:
398
+ progress = _c.sent();
399
+ return [2 /*return*/, !((_b = (_a = progress.electionStatus) === null || _a === void 0 ? void 0 : _a.status) === null || _b === void 0 ? void 0 : _b.Open)];
400
+ }
401
+ });
402
+ }); };
403
+ exports.isElectionClosed = isElectionClosed;
404
+ /**
405
+ * Returns true if the address is a new account with no balance.
406
+ *
407
+ * @async
408
+ * @param {*} addr
409
+ *
410
+ * @returns {boolean}
411
+ */
412
+ var isNewAccount = function (addr) { return __awaiter(void 0, void 0, void 0, function () {
413
+ var _a, nonce, free;
414
+ return __generator(this, function (_b) {
415
+ switch (_b.label) {
416
+ case 0: return [4 /*yield*/, fetchBalanceInfo(addr)];
417
+ case 1:
418
+ _a = _b.sent(), nonce = _a.nonce, free = _a.free;
419
+ return [2 /*return*/, new bignumber_js_1.BigNumber(0).isEqualTo(nonce) && new bignumber_js_1.BigNumber(0).isEqualTo(free)];
420
+ }
421
+ });
422
+ }); };
423
+ exports.isNewAccount = isNewAccount;
424
+ /**
425
+ * Returns true if the address is a controller, i.e. it is associated to a stash.
426
+ *
427
+ * @async
428
+ * @param {*} addr
429
+ *
430
+ * @returns {boolean}
431
+ */
432
+ var isControllerAddress = function (addr) { return __awaiter(void 0, void 0, void 0, function () {
433
+ var stash;
434
+ return __generator(this, function (_a) {
435
+ switch (_a.label) {
436
+ case 0: return [4 /*yield*/, fetchStashAddr(addr)];
437
+ case 1:
438
+ stash = _a.sent();
439
+ return [2 /*return*/, !!stash];
440
+ }
441
+ });
442
+ }); };
443
+ exports.isControllerAddress = isControllerAddress;
444
+ /**
445
+ * Returns all addresses that are not validators.
446
+ *
447
+ * @async
448
+ * @param {string[]} - address to verify
449
+ *
450
+ * @returns {string[]} - addresses that are not validators
451
+ */
452
+ var verifyValidatorAddresses = function (validators) { return __awaiter(void 0, void 0, void 0, function () {
453
+ var existingValidators, existingIds;
454
+ return __generator(this, function (_a) {
455
+ switch (_a.label) {
456
+ case 0: return [4 /*yield*/, fetchValidators("all", validators)];
457
+ case 1:
458
+ existingValidators = _a.sent();
459
+ existingIds = existingValidators.map(function (v) { return v.accountId; });
460
+ return [2 /*return*/, validators.filter(function (v) { return !existingIds.includes(v); })];
461
+ }
462
+ });
463
+ }); };
464
+ exports.verifyValidatorAddresses = verifyValidatorAddresses;
465
+ /**
466
+ * Get all account-related data
467
+ *
468
+ * @async
469
+ * @param {*} addr
470
+ */
471
+ var getAccount = function (addr) { return __awaiter(void 0, void 0, void 0, function () {
472
+ var balances, stakingInfo, nominations;
473
+ return __generator(this, function (_a) {
474
+ switch (_a.label) {
475
+ case 0: return [4 /*yield*/, getBalances(addr)];
476
+ case 1:
477
+ balances = _a.sent();
478
+ return [4 /*yield*/, (0, exports.getStakingInfo)(addr)];
479
+ case 2:
480
+ stakingInfo = _a.sent();
481
+ return [4 /*yield*/, (0, exports.getNominations)(addr)];
482
+ case 3:
483
+ nominations = _a.sent();
484
+ return [2 /*return*/, __assign(__assign(__assign({}, balances), stakingInfo), { nominations: nominations })];
485
+ }
486
+ });
487
+ }); };
488
+ exports.getAccount = getAccount;
489
+ /**
490
+ * Returns all the balances for an account
491
+ *
492
+ * @async
493
+ * @param {*} addr - the account address
494
+ */
495
+ var getBalances = function (addr) { return __awaiter(void 0, void 0, void 0, function () {
496
+ var balanceInfo, totalLocked, balance, spendableBalance;
497
+ var _a;
498
+ return __generator(this, function (_b) {
499
+ switch (_b.label) {
500
+ case 0: return [4 /*yield*/, fetchBalanceInfo(addr)];
501
+ case 1:
502
+ balanceInfo = _b.sent();
503
+ totalLocked = balanceInfo.locks.reduce(function (total, lock) {
504
+ var amount = new bignumber_js_1.BigNumber(lock.amount);
505
+ if (amount.gt(total)) {
506
+ return amount;
507
+ }
508
+ return total;
509
+ }, new bignumber_js_1.BigNumber(0));
510
+ balance = new bignumber_js_1.BigNumber(balanceInfo.free);
511
+ spendableBalance = totalLocked.gt(balance)
512
+ ? new bignumber_js_1.BigNumber(0)
513
+ : balance.minus(totalLocked);
514
+ return [2 /*return*/, {
515
+ blockHeight: ((_a = balanceInfo.at) === null || _a === void 0 ? void 0 : _a.height) ? Number(balanceInfo.at.height) : null,
516
+ balance: balance,
517
+ spendableBalance: spendableBalance,
518
+ nonce: Number(balanceInfo.nonce),
519
+ lockedBalance: new bignumber_js_1.BigNumber(balanceInfo.miscFrozen)
520
+ }];
521
+ }
522
+ });
523
+ }); };
524
+ /**
525
+ * Returns all staking-related data for an account
526
+ *
527
+ * @async
528
+ * @param {*} addr
529
+ */
530
+ var getStakingInfo = function (addr) { return __awaiter(void 0, void 0, void 0, function () {
531
+ var _a, stash, controller, _b, stakingInfo, activeEra, consts, activeEraIndex, activeEraStart, blockTime, epochDuration, sessionsPerEra, eraLength, unlockings, now, unlocked, unlockingBalance, unlockedBalance, numSlashingSpans;
532
+ var _c, _d, _e, _f, _g, _h;
533
+ return __generator(this, function (_j) {
534
+ switch (_j.label) {
535
+ case 0: return [4 /*yield*/, Promise.all([
536
+ fetchStashAddr(addr),
537
+ fetchControllerAddr(addr),
538
+ ])];
539
+ case 1:
540
+ _a = __read.apply(void 0, [_j.sent(), 2]), stash = _a[0], controller = _a[1];
541
+ // If account is not a stash, no need to fetch staking-info (it would return an error)
542
+ if (!controller) {
543
+ return [2 /*return*/, {
544
+ controller: null,
545
+ stash: stash || null,
546
+ unlockedBalance: new bignumber_js_1.BigNumber(0),
547
+ unlockingBalance: new bignumber_js_1.BigNumber(0),
548
+ unlockings: []
549
+ }];
550
+ }
551
+ return [4 /*yield*/, Promise.all([
552
+ fetchStakingInfo(addr),
553
+ fetchActiveEra(),
554
+ (0, exports.getConstants)(),
555
+ ])];
556
+ case 2:
557
+ _b = __read.apply(void 0, [_j.sent(), 3]), stakingInfo = _b[0], activeEra = _b[1], consts = _b[2];
558
+ activeEraIndex = Number(((_c = activeEra.value) === null || _c === void 0 ? void 0 : _c.index) || 0);
559
+ activeEraStart = Number(((_d = activeEra.value) === null || _d === void 0 ? void 0 : _d.start) || 0);
560
+ blockTime = new bignumber_js_1.BigNumber(((_e = consts === null || consts === void 0 ? void 0 : consts.babe) === null || _e === void 0 ? void 0 : _e.expectedBlockTime) || 6000);
561
+ epochDuration = new bignumber_js_1.BigNumber(((_f = consts === null || consts === void 0 ? void 0 : consts.babe) === null || _f === void 0 ? void 0 : _f.epochDuration) || 2400);
562
+ sessionsPerEra = new bignumber_js_1.BigNumber(((_g = consts === null || consts === void 0 ? void 0 : consts.staking) === null || _g === void 0 ? void 0 : _g.sessionsPerEra) || 6);
563
+ eraLength = sessionsPerEra
564
+ .multipliedBy(epochDuration)
565
+ .multipliedBy(blockTime)
566
+ .toNumber();
567
+ unlockings = (stakingInfo === null || stakingInfo === void 0 ? void 0 : stakingInfo.staking.unlocking)
568
+ ? (_h = stakingInfo === null || stakingInfo === void 0 ? void 0 : stakingInfo.staking) === null || _h === void 0 ? void 0 : _h.unlocking.map(function (lock) { return ({
569
+ amount: new bignumber_js_1.BigNumber(lock.value),
570
+ completionDate: new Date(activeEraStart + (Number(lock.era) - activeEraIndex) * eraLength)
571
+ }); })
572
+ : [];
573
+ now = new Date();
574
+ unlocked = unlockings.filter(function (lock) { return lock.completionDate <= now; });
575
+ unlockingBalance = unlockings.reduce(function (sum, lock) { return sum.plus(lock.amount); }, new bignumber_js_1.BigNumber(0));
576
+ unlockedBalance = unlocked.reduce(function (sum, lock) { return sum.plus(lock.amount); }, new bignumber_js_1.BigNumber(0));
577
+ numSlashingSpans = Number((stakingInfo === null || stakingInfo === void 0 ? void 0 : stakingInfo.numSlashingSpans) || 0);
578
+ return [2 /*return*/, {
579
+ controller: controller || null,
580
+ stash: stash || null,
581
+ unlockedBalance: unlockedBalance,
582
+ unlockingBalance: unlockingBalance,
583
+ unlockings: unlockings,
584
+ numSlashingSpans: numSlashingSpans
585
+ }];
586
+ }
587
+ });
588
+ }); };
589
+ exports.getStakingInfo = getStakingInfo;
590
+ /**
591
+ * Returns nominations for an account including validator address, status and associated stake.
592
+ *
593
+ * @async
594
+ * @param {*} addr
595
+ *
596
+ * @returns {PolkadotNomination[}
597
+ */
598
+ var getNominations = function (addr) { return __awaiter(void 0, void 0, void 0, function () {
599
+ var nominations;
600
+ return __generator(this, function (_a) {
601
+ switch (_a.label) {
602
+ case 0: return [4 /*yield*/, fetchNominations(addr)];
603
+ case 1:
604
+ nominations = _a.sent();
605
+ if (!nominations)
606
+ return [2 /*return*/, []];
607
+ return [2 /*return*/, nominations.targets.map(function (nomination) { return ({
608
+ address: nomination.address,
609
+ value: new bignumber_js_1.BigNumber(nomination.value || 0),
610
+ status: nomination.status
611
+ }); })];
612
+ }
613
+ });
614
+ }); };
615
+ exports.getNominations = getNominations;
616
+ /**
617
+ * Returns all the params from the chain to build an extrinsic (a transaction on Substrate)
618
+ *
619
+ * @async
620
+ */
621
+ var getTransactionParams = function () { return __awaiter(void 0, void 0, void 0, function () {
622
+ var material;
623
+ return __generator(this, function (_a) {
624
+ switch (_a.label) {
625
+ case 0: return [4 /*yield*/, fetchTransactionMaterial()];
626
+ case 1:
627
+ material = _a.sent();
628
+ return [2 /*return*/, {
629
+ blockHash: material.at.hash,
630
+ blockNumber: material.at.height,
631
+ chainName: material.chainName,
632
+ genesisHash: material.genesisHash,
633
+ specName: material.specName,
634
+ transactionVersion: material.txVersion,
635
+ specVersion: material.specVersion
636
+ }];
637
+ }
638
+ });
639
+ }); };
640
+ exports.getTransactionParams = getTransactionParams;
641
+ /**
642
+ * Broadcast the transaction to the substrate node
643
+ *
644
+ * @async
645
+ * @param {string} extrinsic - the encoded extrinsic to send
646
+ *
647
+ * @returns {string>} - the broadcasted transaction's hah
648
+ */
649
+ var submitExtrinsic = function (extrinsic) { return __awaiter(void 0, void 0, void 0, function () {
650
+ var data;
651
+ return __generator(this, function (_a) {
652
+ switch (_a.label) {
653
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
654
+ method: "POST",
655
+ url: getSidecarUrl("/transaction"),
656
+ data: {
657
+ tx: extrinsic
658
+ }
659
+ })];
660
+ case 1:
661
+ data = (_a.sent()).data;
662
+ return [2 /*return*/, data.hash];
663
+ }
664
+ });
665
+ }); };
666
+ exports.submitExtrinsic = submitExtrinsic;
667
+ /**
668
+ * Retrieve the transaction fees and weights
669
+ * Note: fees on Substrate are not set by the signer, but directly by the blockchain runtime.
670
+ *
671
+ * @async
672
+ * @param {string} extrinsic - the encoded extrinsic to send with a fake signing
673
+ *
674
+ * @returns {SidecarPaymentInfo}
675
+ */
676
+ var paymentInfo = function (extrinsic) { return __awaiter(void 0, void 0, void 0, function () {
677
+ var data;
678
+ return __generator(this, function (_a) {
679
+ switch (_a.label) {
680
+ case 0: return [4 /*yield*/, (0, network_1["default"])({
681
+ method: "POST",
682
+ url: getSidecarUrl("/transaction/fee-estimate"),
683
+ data: {
684
+ tx: extrinsic
685
+ }
686
+ })];
687
+ case 1:
688
+ data = (_a.sent()).data;
689
+ return [2 /*return*/, data];
690
+ }
691
+ });
692
+ }); };
693
+ exports.paymentInfo = paymentInfo;
694
+ /**
695
+ * List all validators for the current era, and their exposure, and identity.
696
+ *
697
+ * @async
698
+ * @param {string | string[]} stashes - stashes or validator status
699
+ *
700
+ * @returns {PolkadotValidator[]}
701
+ */
702
+ var getValidators = function (stashes) {
703
+ if (stashes === void 0) { stashes = "elected"; }
704
+ return __awaiter(void 0, void 0, void 0, function () {
705
+ var validators;
706
+ return __generator(this, function (_a) {
707
+ switch (_a.label) {
708
+ case 0:
709
+ if (!Array.isArray(stashes)) return [3 /*break*/, 2];
710
+ return [4 /*yield*/, fetchValidators("all", stashes)];
711
+ case 1:
712
+ validators = _a.sent();
713
+ return [3 /*break*/, 4];
714
+ case 2: return [4 /*yield*/, fetchValidators(stashes)];
715
+ case 3:
716
+ validators = _a.sent();
717
+ _a.label = 4;
718
+ case 4: return [2 /*return*/, validators.map(function (v) { return ({
719
+ address: v.accountId,
720
+ identity: v.identity
721
+ ? [v.identity.displayParent, v.identity.display]
722
+ .filter(Boolean)
723
+ .join(" - ")
724
+ .trim()
725
+ : "",
726
+ nominatorsCount: Number(v.nominatorsCount),
727
+ rewardPoints: v.rewardsPoints ? new bignumber_js_1.BigNumber(v.rewardsPoints) : null,
728
+ commission: new bignumber_js_1.BigNumber(v.commission).dividedBy(VALIDATOR_COMISSION_RATIO),
729
+ totalBonded: new bignumber_js_1.BigNumber(v.total),
730
+ selfBonded: new bignumber_js_1.BigNumber(v.own),
731
+ isElected: v.isElected,
732
+ isOversubscribed: v.isOversubscribed
733
+ }); })];
734
+ }
735
+ });
736
+ });
737
+ };
738
+ exports.getValidators = getValidators;
739
+ /**
740
+ * Get Active Era progress
741
+ *
742
+ * @async
743
+ *
744
+ * @returns {PolkadotStakingProgress}
745
+ */
746
+ var getStakingProgress = function () { return __awaiter(void 0, void 0, void 0, function () {
747
+ var _a, progress, consts, activeEra, currentBlock, toggleEstimate, electionClosed, optimisticElectionClosed;
748
+ var _b, _c, _d;
749
+ return __generator(this, function (_e) {
750
+ switch (_e.label) {
751
+ case 0: return [4 /*yield*/, Promise.all([
752
+ fetchStakingProgress(),
753
+ (0, exports.getConstants)(),
754
+ ])];
755
+ case 1:
756
+ _a = __read.apply(void 0, [_e.sent(), 2]), progress = _a[0], consts = _a[1];
757
+ activeEra = Number(progress.activeEra);
758
+ currentBlock = Number(progress.at.height);
759
+ toggleEstimate = Number((_b = progress.electionStatus) === null || _b === void 0 ? void 0 : _b.toggleEstimate);
760
+ electionClosed = !((_d = (_c = progress.electionStatus) === null || _c === void 0 ? void 0 : _c.status) === null || _d === void 0 ? void 0 : _d.Open);
761
+ optimisticElectionClosed = electionClosed &&
762
+ activeEra &&
763
+ currentBlock &&
764
+ toggleEstimate &&
765
+ currentBlock >= toggleEstimate - ELECTION_STATUS_OPTIMISTIC_THRESHOLD
766
+ ? false
767
+ : electionClosed;
768
+ return [2 /*return*/, {
769
+ activeEra: activeEra,
770
+ electionClosed: optimisticElectionClosed,
771
+ maxNominatorRewardedPerValidator: Number(consts.staking.maxNominatorRewardedPerValidator) || 128,
772
+ bondingDuration: Number(consts.staking.bondingDuration) || 28
773
+ }];
774
+ }
775
+ });
776
+ }); };
777
+ exports.getStakingProgress = getStakingProgress;
778
+ /**
779
+ * Create a new Registry for creating Polkadot.JS types (or any Substrate)
780
+ *
781
+ * @async
782
+ *
783
+ * @returns {Object} - { registry, extrinsics }
784
+ */
785
+ var getRegistry = function () { return __awaiter(void 0, void 0, void 0, function () {
786
+ var _a, material, spec, registry, metadata, extrinsics;
787
+ return __generator(this, function (_b) {
788
+ switch (_b.label) {
789
+ case 0: return [4 /*yield*/, Promise.all([
790
+ (0, exports.getTransactionMaterialWithMetadata)(),
791
+ fetchChainSpec(),
792
+ ])];
793
+ case 1:
794
+ _a = __read.apply(void 0, [_b.sent(), 2]), material = _a[0], spec = _a[1];
795
+ registry = new types_1.TypeRegistry();
796
+ metadata = new metadata_1.Metadata(registry, material.metadata);
797
+ // Register types specific to chain/runtimeVersion
798
+ registry.register((0, types_known_1.getSpecTypes)(registry, material.chainName, material.specName, Number(material.specVersion)));
799
+ // Register the chain properties for this registry
800
+ registry.setChainProperties(registry.createType("ChainProperties", {
801
+ ss58Format: Number(spec.properties.ss58Format),
802
+ tokenDecimals: Number(spec.properties.tokenDecimals),
803
+ tokenSymbol: spec.properties.tokenSymbol
804
+ }));
805
+ registry.setMetadata(metadata);
806
+ extrinsics = (0, decorate_1.expandMetadata)(registry, metadata).tx;
807
+ return [2 /*return*/, {
808
+ registry: registry,
809
+ extrinsics: extrinsics
810
+ }];
811
+ }
812
+ });
813
+ }); };
814
+ exports.getRegistry = getRegistry;
815
+ /*
816
+ * CACHED REQUESTS
817
+ * NOTE: we don't use the cache from family's `cache.js` to avoid cyclic imports.
818
+ */
819
+ /**
820
+ * Cache the fetchConstants to avoid too many calls
821
+ *
822
+ * @async
823
+ *
824
+ * @returns {Promise<Object>} consts
825
+ */
826
+ exports.getConstants = (0, cache_1.makeLRUCache)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
827
+ return [2 /*return*/, fetchConstants()];
828
+ }); }); }, function () { return "polkadot"; }, {
829
+ max: 1,
830
+ ttl: 60 * 60 * 1000
831
+ });
832
+ /**
833
+ * Cache the fetchTransactionMaterial(true) to avoid too many calls
834
+ *
835
+ * @async
836
+ *
837
+ * @returns {Promise<Object>} consts
838
+ */
839
+ exports.getTransactionMaterialWithMetadata = (0, cache_1.makeLRUCache)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
840
+ return [2 /*return*/, fetchTransactionMaterial(true)];
841
+ }); }); }, function () { return "polkadot"; }, {
842
+ ttl: 60 * 60 * 1000
843
+ });
844
+ //# sourceMappingURL=sidecar.js.map