@wallfree-dev/polkadot 0.13.42

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 (122) hide show
  1. package/LICENSE.md +7 -0
  2. package/index.d.ts +1 -0
  3. package/index.js +18 -0
  4. package/index.js.map +1 -0
  5. package/package.json +65 -0
  6. package/readme.md +121 -0
  7. package/v0/index.d.ts +5 -0
  8. package/v0/index.js +31 -0
  9. package/v0/index.js.map +1 -0
  10. package/v0/protocol/kusama/KusamaProtocol.d.ts +22 -0
  11. package/v0/protocol/kusama/KusamaProtocol.js +71 -0
  12. package/v0/protocol/kusama/KusamaProtocol.js.map +1 -0
  13. package/v0/protocol/kusama/KusamaProtocolOptions.d.ts +23 -0
  14. package/v0/protocol/kusama/KusamaProtocolOptions.js +85 -0
  15. package/v0/protocol/kusama/KusamaProtocolOptions.js.map +1 -0
  16. package/v0/protocol/polkadot/PolkadotProtocol.d.ts +23 -0
  17. package/v0/protocol/polkadot/PolkadotProtocol.js +72 -0
  18. package/v0/protocol/polkadot/PolkadotProtocol.js.map +1 -0
  19. package/v0/protocol/polkadot/PolkadotProtocolOptions.d.ts +23 -0
  20. package/v0/protocol/polkadot/PolkadotProtocolOptions.js +85 -0
  21. package/v0/protocol/polkadot/PolkadotProtocolOptions.js.map +1 -0
  22. package/v1/controller/PolkadotAccountController.d.ts +30 -0
  23. package/v1/controller/PolkadotAccountController.js +675 -0
  24. package/v1/controller/PolkadotAccountController.js.map +1 -0
  25. package/v1/controller/PolkadotTransactionController.d.ts +5 -0
  26. package/v1/controller/PolkadotTransactionController.js +28 -0
  27. package/v1/controller/PolkadotTransactionController.js.map +1 -0
  28. package/v1/data/staking/PolkadotActiveEraInfo.d.ts +8 -0
  29. package/v1/data/staking/PolkadotActiveEraInfo.js +19 -0
  30. package/v1/data/staking/PolkadotActiveEraInfo.js.map +1 -0
  31. package/v1/data/staking/PolkadotEraRewardPoints.d.ts +8 -0
  32. package/v1/data/staking/PolkadotEraRewardPoints.js +21 -0
  33. package/v1/data/staking/PolkadotEraRewardPoints.js.map +1 -0
  34. package/v1/data/staking/PolkadotExposure.d.ts +9 -0
  35. package/v1/data/staking/PolkadotExposure.js +23 -0
  36. package/v1/data/staking/PolkadotExposure.js.map +1 -0
  37. package/v1/data/staking/PolkadotNominationStatus.d.ts +5 -0
  38. package/v1/data/staking/PolkadotNominationStatus.js +10 -0
  39. package/v1/data/staking/PolkadotNominationStatus.js.map +1 -0
  40. package/v1/data/staking/PolkadotNominations.d.ts +9 -0
  41. package/v1/data/staking/PolkadotNominations.js +21 -0
  42. package/v1/data/staking/PolkadotNominations.js.map +1 -0
  43. package/v1/data/staking/PolkadotNominatorDetails.d.ts +24 -0
  44. package/v1/data/staking/PolkadotNominatorDetails.js +3 -0
  45. package/v1/data/staking/PolkadotNominatorDetails.js.map +1 -0
  46. package/v1/data/staking/PolkadotPayee.d.ts +5 -0
  47. package/v1/data/staking/PolkadotPayee.js +10 -0
  48. package/v1/data/staking/PolkadotPayee.js.map +1 -0
  49. package/v1/data/staking/PolkadotSlashingSpans.d.ts +10 -0
  50. package/v1/data/staking/PolkadotSlashingSpans.js +23 -0
  51. package/v1/data/staking/PolkadotSlashingSpans.js.map +1 -0
  52. package/v1/data/staking/PolkadotStakingActionType.d.ts +14 -0
  53. package/v1/data/staking/PolkadotStakingActionType.js +19 -0
  54. package/v1/data/staking/PolkadotStakingActionType.js.map +1 -0
  55. package/v1/data/staking/PolkadotStakingBalance.d.ts +5 -0
  56. package/v1/data/staking/PolkadotStakingBalance.js +3 -0
  57. package/v1/data/staking/PolkadotStakingBalance.js.map +1 -0
  58. package/v1/data/staking/PolkadotStakingLedger.d.ts +11 -0
  59. package/v1/data/staking/PolkadotStakingLedger.js +27 -0
  60. package/v1/data/staking/PolkadotStakingLedger.js.map +1 -0
  61. package/v1/data/staking/PolkadotValidatorDetails.d.ts +16 -0
  62. package/v1/data/staking/PolkadotValidatorDetails.js +3 -0
  63. package/v1/data/staking/PolkadotValidatorDetails.js.map +1 -0
  64. package/v1/data/staking/PolkadotValidatorPrefs.d.ts +7 -0
  65. package/v1/data/staking/PolkadotValidatorPrefs.js +17 -0
  66. package/v1/data/staking/PolkadotValidatorPrefs.js.map +1 -0
  67. package/v1/data/transaction/method/args.d.ts +2 -0
  68. package/v1/data/transaction/method/args.js +441 -0
  69. package/v1/data/transaction/method/args.js.map +1 -0
  70. package/v1/data/transaction/transaction.d.ts +2 -0
  71. package/v1/data/transaction/transaction.js +28 -0
  72. package/v1/data/transaction/transaction.js.map +1 -0
  73. package/v1/index.d.ts +15 -0
  74. package/v1/index.js +16 -0
  75. package/v1/index.js.map +1 -0
  76. package/v1/module/PolkadotModule.d.ts +19 -0
  77. package/v1/module/PolkadotModule.js +113 -0
  78. package/v1/module/PolkadotModule.js.map +1 -0
  79. package/v1/module.d.ts +3 -0
  80. package/v1/module.js +24 -0
  81. package/v1/module.js.map +1 -0
  82. package/v1/node/PolkadotNodeClient.d.ts +45 -0
  83. package/v1/node/PolkadotNodeClient.js +319 -0
  84. package/v1/node/PolkadotNodeClient.js.map +1 -0
  85. package/v1/protocol/KusamaProtocol.d.ts +14 -0
  86. package/v1/protocol/KusamaProtocol.js +106 -0
  87. package/v1/protocol/KusamaProtocol.js.map +1 -0
  88. package/v1/protocol/PolkadotBaseProtocol.d.ts +57 -0
  89. package/v1/protocol/PolkadotBaseProtocol.js +737 -0
  90. package/v1/protocol/PolkadotBaseProtocol.js.map +1 -0
  91. package/v1/protocol/PolkadotProtocol.d.ts +14 -0
  92. package/v1/protocol/PolkadotProtocol.js +119 -0
  93. package/v1/protocol/PolkadotProtocol.js.map +1 -0
  94. package/v1/serializer/v3/schemas/converter/transaction-converter.d.ts +7 -0
  95. package/v1/serializer/v3/schemas/converter/transaction-converter.js +21 -0
  96. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -0
  97. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-polkadot.d.ts +3 -0
  98. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-polkadot.js +3 -0
  99. package/v1/serializer/v3/schemas/definitions/transaction-sign-request-polkadot.js.map +1 -0
  100. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-polkadot.d.ts +3 -0
  101. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-polkadot.js +3 -0
  102. package/v1/serializer/v3/schemas/definitions/transaction-sign-response-polkadot.js.map +1 -0
  103. package/v1/serializer/v3/schemas/generated/transaction-sign-request-polkadot.json +36 -0
  104. package/v1/serializer/v3/schemas/generated/transaction-sign-response-polkadot.json +19 -0
  105. package/v1/serializer/v3/serializer-companion.d.ts +14 -0
  106. package/v1/serializer/v3/serializer-companion.js +189 -0
  107. package/v1/serializer/v3/serializer-companion.js.map +1 -0
  108. package/v1/serializer/v3/validators/transaction-validator.d.ts +4 -0
  109. package/v1/serializer/v3/validators/transaction-validator.js +28 -0
  110. package/v1/serializer/v3/validators/transaction-validator.js.map +1 -0
  111. package/v1/serializer/v3/validators/validators.d.ts +4 -0
  112. package/v1/serializer/v3/validators/validators.js +6 -0
  113. package/v1/serializer/v3/validators/validators.js.map +1 -0
  114. package/v1/types/configuration.d.ts +11 -0
  115. package/v1/types/configuration.js +3 -0
  116. package/v1/types/configuration.js.map +1 -0
  117. package/v1/types/crypto.d.ts +2 -0
  118. package/v1/types/crypto.js +3 -0
  119. package/v1/types/crypto.js.map +1 -0
  120. package/v1/types/protocol.d.ts +13 -0
  121. package/v1/types/protocol.js +3 -0
  122. package/v1/types/protocol.js.map +1 -0
@@ -0,0 +1,675 @@
1
+ "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
17
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
+ return new (P || (P = Promise))(function (resolve, reject) {
20
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
24
+ });
25
+ };
26
+ var __generator = (this && this.__generator) || function (thisArg, body) {
27
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
+ function verb(n) { return function (v) { return step([n, v]); }; }
30
+ function step(op) {
31
+ if (f) throw new TypeError("Generator is already executing.");
32
+ while (_) try {
33
+ 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;
34
+ if (y = 0, t) op = [op[0] & 2, t.value];
35
+ switch (op[0]) {
36
+ case 0: case 1: t = op; break;
37
+ case 4: _.label++; return { value: op[1], done: false };
38
+ case 5: _.label++; y = op[1]; op = [0]; continue;
39
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
+ default:
41
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
+ if (t[2]) _.ops.pop();
46
+ _.trys.pop(); continue;
47
+ }
48
+ op = body.call(thisArg, _);
49
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
+ }
52
+ };
53
+ var __importDefault = (this && this.__importDefault) || function (mod) {
54
+ return (mod && mod.__esModule) ? mod : { "default": mod };
55
+ };
56
+ Object.defineProperty(exports, "__esModule", { value: true });
57
+ exports.PolkadotAccountController = void 0;
58
+ var bignumber_1 = __importDefault(require("@wallfree-dev/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
59
+ var v1_1 = require("@wallfree-dev/substrate/v1");
60
+ var PolkadotNominationStatus_1 = require("../data/staking/PolkadotNominationStatus");
61
+ var PolkadotStakingActionType_1 = require("../data/staking/PolkadotStakingActionType");
62
+ var PolkadotAccountController = /** @class */ (function (_super) {
63
+ __extends(PolkadotAccountController, _super);
64
+ function PolkadotAccountController() {
65
+ return _super !== null && _super.apply(this, arguments) || this;
66
+ }
67
+ PolkadotAccountController.prototype.getStakingBalance = function (accountId) {
68
+ return __awaiter(this, void 0, void 0, function () {
69
+ var stakingDetails, bonded, unlocking;
70
+ return __generator(this, function (_a) {
71
+ switch (_a.label) {
72
+ case 0: return [4 /*yield*/, this.nodeClient.getStakingLedger(this.substrateAddressFrom(accountId))];
73
+ case 1:
74
+ stakingDetails = _a.sent();
75
+ if (stakingDetails === undefined) {
76
+ return [2 /*return*/, undefined];
77
+ }
78
+ bonded = stakingDetails.active.value;
79
+ unlocking = stakingDetails.unlocking.elements
80
+ .map(function (entry) { return entry.first.value; })
81
+ .reduce(function (sum, next) { return sum.plus(next); }, new bignumber_1.default(0));
82
+ return [2 /*return*/, { bonded: bonded, unlocking: unlocking }];
83
+ }
84
+ });
85
+ });
86
+ };
87
+ PolkadotAccountController.prototype.getUnlockingBalance = function (accountId) {
88
+ var _a;
89
+ return __awaiter(this, void 0, void 0, function () {
90
+ var stakingBalance;
91
+ return __generator(this, function (_b) {
92
+ switch (_b.label) {
93
+ case 0: return [4 /*yield*/, this.getStakingBalance(accountId)];
94
+ case 1:
95
+ stakingBalance = _b.sent();
96
+ return [2 /*return*/, (_a = stakingBalance === null || stakingBalance === void 0 ? void 0 : stakingBalance.unlocking) !== null && _a !== void 0 ? _a : new bignumber_1.default(0)];
97
+ }
98
+ });
99
+ });
100
+ };
101
+ PolkadotAccountController.prototype.isBonded = function (accountId) {
102
+ return __awaiter(this, void 0, void 0, function () {
103
+ var bonded;
104
+ return __generator(this, function (_a) {
105
+ switch (_a.label) {
106
+ case 0: return [4 /*yield*/, this.nodeClient.getBonded(this.substrateAddressFrom(accountId))];
107
+ case 1:
108
+ bonded = _a.sent();
109
+ return [2 /*return*/, bonded != undefined];
110
+ }
111
+ });
112
+ });
113
+ };
114
+ PolkadotAccountController.prototype.isDelegating = function (accountId) {
115
+ return __awaiter(this, void 0, void 0, function () {
116
+ var nominations;
117
+ return __generator(this, function (_a) {
118
+ switch (_a.label) {
119
+ case 0: return [4 /*yield*/, this.nodeClient.getNominations(this.substrateAddressFrom(accountId))];
120
+ case 1:
121
+ nominations = _a.sent();
122
+ return [2 /*return*/, nominations != undefined];
123
+ }
124
+ });
125
+ });
126
+ };
127
+ PolkadotAccountController.prototype.getCurrentValidators = function (accountId) {
128
+ return __awaiter(this, void 0, void 0, function () {
129
+ var nominations;
130
+ return __generator(this, function (_a) {
131
+ switch (_a.label) {
132
+ case 0: return [4 /*yield*/, this.nodeClient.getNominations(this.substrateAddressFrom(accountId))];
133
+ case 1:
134
+ nominations = _a.sent();
135
+ if (nominations) {
136
+ return [2 /*return*/, nominations.targets.elements.map(function (target) { return target.asAddress(); })];
137
+ }
138
+ return [2 /*return*/, []];
139
+ }
140
+ });
141
+ });
142
+ };
143
+ PolkadotAccountController.prototype.getValidatorDetails = function (accountId) {
144
+ var _a;
145
+ return __awaiter(this, void 0, void 0, function () {
146
+ var address, activeEra, identity, status, exposure, validatorPrefs, lastEraReward, activeEraIndex, results, currentValidators;
147
+ return __generator(this, function (_b) {
148
+ switch (_b.label) {
149
+ case 0:
150
+ address = this.substrateAddressFrom(accountId);
151
+ return [4 /*yield*/, this.nodeClient.getActiveEraInfo()];
152
+ case 1:
153
+ activeEra = _b.sent();
154
+ if (!activeEra) return [3 /*break*/, 4];
155
+ activeEraIndex = activeEra.index.toNumber();
156
+ return [4 /*yield*/, Promise.all([
157
+ this.getAccountIdentityInfo(address),
158
+ this.nodeClient.getValidators(),
159
+ this.nodeClient.getValidatorPrefs(activeEraIndex, address),
160
+ this.nodeClient.getValidatorExposure(activeEraIndex, address)
161
+ ])];
162
+ case 2:
163
+ results = _b.sent();
164
+ identity = results[0];
165
+ currentValidators = results[1];
166
+ validatorPrefs = results[2];
167
+ exposure = results[3];
168
+ return [4 /*yield*/, this.getEraValidatorReward(address, activeEraIndex - 1)];
169
+ case 3:
170
+ lastEraReward = _b.sent();
171
+ if (currentValidators && currentValidators.find(function (current) { return current.compare(address) == 0; })) {
172
+ status = 'Active';
173
+ }
174
+ else if (currentValidators) {
175
+ status = 'Inactive';
176
+ }
177
+ _b.label = 4;
178
+ case 4: return [2 /*return*/, {
179
+ address: address.asString(),
180
+ name: identity ? identity.display.toString() : undefined,
181
+ status: status,
182
+ ownStash: exposure ? exposure.own.toString() : undefined,
183
+ totalStakingBalance: exposure ? exposure.total.toString() : undefined,
184
+ commission: validatorPrefs
185
+ ? validatorPrefs.commission.value.dividedBy(1000000000).toString() // commission is Perbill (parts per billion)
186
+ : undefined,
187
+ lastEraReward: lastEraReward,
188
+ nominators: (_a = exposure === null || exposure === void 0 ? void 0 : exposure.others.elements.length) !== null && _a !== void 0 ? _a : 0
189
+ }];
190
+ }
191
+ });
192
+ });
193
+ };
194
+ PolkadotAccountController.prototype.getNominatorDetails = function (accountId, validatorIds) {
195
+ var _a, _b;
196
+ return __awaiter(this, void 0, void 0, function () {
197
+ var address, results, balance, stakingLedger, nominations, activeEra, expectedEraDuration, validators, stakingDetails, availableActions;
198
+ return __generator(this, function (_c) {
199
+ switch (_c.label) {
200
+ case 0:
201
+ address = this.substrateAddressFrom(accountId);
202
+ return [4 /*yield*/, Promise.all([
203
+ this.getBalance(address),
204
+ this.nodeClient.getStakingLedger(address),
205
+ this.nodeClient.getNominations(address),
206
+ this.nodeClient.getActiveEraInfo(),
207
+ this.nodeClient.getExpectedEraDuration(),
208
+ this.nodeClient.getExistentialDeposit()
209
+ ])];
210
+ case 1:
211
+ results = _c.sent();
212
+ balance = results[0];
213
+ stakingLedger = results[1];
214
+ nominations = results[2];
215
+ activeEra = results[3];
216
+ expectedEraDuration = results[4];
217
+ if (!balance || !activeEra || !expectedEraDuration) {
218
+ return [2 /*return*/, Promise.reject('Could not fetch nominator details.')];
219
+ }
220
+ validators = ((_b = (_a = nominations === null || nominations === void 0 ? void 0 : nominations.targets) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.map(function (target) { return target.asAddress(); })) || [];
221
+ return [4 /*yield*/, this.getStakingDetails(accountId, stakingLedger, nominations, activeEra, expectedEraDuration)];
222
+ case 2:
223
+ stakingDetails = _c.sent();
224
+ return [4 /*yield*/, this.getAvailableStakingActions(stakingDetails, nominations, validatorIds !== null && validatorIds !== void 0 ? validatorIds : validators, balance.transferable)];
225
+ case 3:
226
+ availableActions = _c.sent();
227
+ return [2 /*return*/, {
228
+ address: address.asString(),
229
+ balance: balance.total.toString(),
230
+ delegatees: validators,
231
+ availableActions: availableActions,
232
+ stakingDetails: stakingDetails
233
+ }];
234
+ }
235
+ });
236
+ });
237
+ };
238
+ PolkadotAccountController.prototype.getNominationStatus = function (nominator, validator, era) {
239
+ var _a;
240
+ return __awaiter(this, void 0, void 0, function () {
241
+ var eraIndex, _b, nominations, exposure, isActive, isOversubscribed, position;
242
+ return __generator(this, function (_c) {
243
+ switch (_c.label) {
244
+ case 0:
245
+ if (!(era !== undefined)) return [3 /*break*/, 1];
246
+ _b = era;
247
+ return [3 /*break*/, 3];
248
+ case 1: return [4 /*yield*/, this.nodeClient.getActiveEraInfo()];
249
+ case 2:
250
+ _b = (_a = (_c.sent())) === null || _a === void 0 ? void 0 : _a.index.toNumber();
251
+ _c.label = 3;
252
+ case 3:
253
+ eraIndex = _b;
254
+ if (eraIndex === undefined) {
255
+ return [2 /*return*/, Promise.reject('Could not fetch active era')];
256
+ }
257
+ return [4 /*yield*/, this.nodeClient.getNominations(this.substrateAddressFrom(nominator))];
258
+ case 4:
259
+ nominations = _c.sent();
260
+ if (nominations === undefined ||
261
+ !nominations.targets.elements.some(function (target) { return target.asAddress() === validator; })) {
262
+ return [2 /*return*/, undefined];
263
+ }
264
+ return [4 /*yield*/, this.nodeClient.getValidatorExposure(eraIndex, this.substrateAddressFrom(validator))];
265
+ case 5:
266
+ exposure = _c.sent();
267
+ if (!exposure) {
268
+ return [2 /*return*/, PolkadotNominationStatus_1.PolkadotNominationStatus.INACTIVE];
269
+ }
270
+ isActive = exposure.others.elements.some(function (element) { return element.first.asAddress() === nominator.toString(); });
271
+ if (!isActive) {
272
+ return [2 /*return*/, PolkadotNominationStatus_1.PolkadotNominationStatus.INACTIVE];
273
+ }
274
+ isOversubscribed = exposure.others.elements.length > 256;
275
+ if (isOversubscribed) {
276
+ position = exposure.others.elements
277
+ .sort(function (a, b) { return b.second.value.minus(a.second.value).toNumber(); })
278
+ .map(function (exposure) { return exposure.first.asAddress(); })
279
+ .indexOf(nominator.toString());
280
+ if (position > 256) {
281
+ return [2 /*return*/, PolkadotNominationStatus_1.PolkadotNominationStatus.OVERSUBSCRIBED];
282
+ }
283
+ }
284
+ return [2 /*return*/, PolkadotNominationStatus_1.PolkadotNominationStatus.ACTIVE];
285
+ }
286
+ });
287
+ });
288
+ };
289
+ PolkadotAccountController.prototype.getSlashingSpansNumber = function (accountId) {
290
+ return __awaiter(this, void 0, void 0, function () {
291
+ var slashingSpans;
292
+ return __generator(this, function (_a) {
293
+ switch (_a.label) {
294
+ case 0: return [4 /*yield*/, this.nodeClient.getSlashingSpan(this.substrateAddressFrom(accountId))];
295
+ case 1:
296
+ slashingSpans = _a.sent();
297
+ return [2 /*return*/, slashingSpans ? slashingSpans.prior.elements.length + 1 : 0];
298
+ }
299
+ });
300
+ });
301
+ };
302
+ PolkadotAccountController.prototype.getStakingDetails = function (accountId, stakingLedger, nominations, activeEra, expectedEraDuration) {
303
+ var _a, _b;
304
+ return __awaiter(this, void 0, void 0, function () {
305
+ var unlockingDetails, results, stakingStatus, rewards;
306
+ return __generator(this, function (_c) {
307
+ switch (_c.label) {
308
+ case 0:
309
+ if (!stakingLedger) {
310
+ return [2 /*return*/, undefined];
311
+ }
312
+ unlockingDetails = this.getUnlockingDetails(stakingLedger.unlocking.elements.map(function (tuple) { return [tuple.first.value, tuple.second.value]; }), activeEra, expectedEraDuration);
313
+ return [4 /*yield*/, Promise.all([
314
+ this.getStakingStatus(accountId, nominations, activeEra.index.toNumber()),
315
+ nominations
316
+ ? this.getNominatorRewards(accountId, nominations.targets.elements.map(function (id) { return id.address; }), activeEra, 5)
317
+ : []
318
+ ])];
319
+ case 1:
320
+ results = _c.sent();
321
+ stakingStatus = results[0];
322
+ rewards = results[1];
323
+ return [2 /*return*/, {
324
+ total: stakingLedger.total.toString(),
325
+ active: stakingLedger.active.toString(),
326
+ locked: unlockingDetails.locked,
327
+ unlocked: unlockingDetails.unlocked,
328
+ status: stakingStatus,
329
+ nextEra: ((_b = (_a = activeEra.start.value) === null || _a === void 0 ? void 0 : _a.plus(expectedEraDuration)) === null || _b === void 0 ? void 0 : _b.toNumber()) || 0,
330
+ rewards: rewards
331
+ }];
332
+ }
333
+ });
334
+ });
335
+ };
336
+ PolkadotAccountController.prototype.getUnlockingDetails = function (unlocking, activeEra, expectedEraDuration) {
337
+ var _a = this.partitionArray(unlocking, function (_a) {
338
+ var _ = _a[0], era = _a[1];
339
+ return activeEra.index.lt(era);
340
+ }), locked = _a[0], unlocked = _a[1];
341
+ var lockedDetails = locked.map(function (_a) {
342
+ var _b;
343
+ var value = _a[0], era = _a[1];
344
+ var eraStart = ((_b = activeEra.start.value) === null || _b === void 0 ? void 0 : _b.value) || new bignumber_1.default(0);
345
+ var estimatedDuration = era.minus(activeEra.index.value).multipliedBy(expectedEraDuration);
346
+ var expectedUnlock = eraStart.plus(estimatedDuration);
347
+ return {
348
+ value: value.toString(10),
349
+ expectedUnlock: expectedUnlock.toNumber()
350
+ };
351
+ });
352
+ var totalUnlocked = unlocked.reduce(function (total, _a) {
353
+ var value = _a[0], _ = _a[1];
354
+ return total.plus(value);
355
+ }, new bignumber_1.default(0));
356
+ return {
357
+ locked: lockedDetails,
358
+ unlocked: totalUnlocked.toString()
359
+ };
360
+ };
361
+ PolkadotAccountController.prototype.getStakingStatus = function (nominator, nominations, eraIndex) {
362
+ var _a;
363
+ return __awaiter(this, void 0, void 0, function () {
364
+ var isWaitingForNomination, hasActiveNominations;
365
+ var _this = this;
366
+ return __generator(this, function (_b) {
367
+ switch (_b.label) {
368
+ case 0:
369
+ isWaitingForNomination = (_a = nominations === null || nominations === void 0 ? void 0 : nominations.submittedIn.gte(eraIndex)) !== null && _a !== void 0 ? _a : false;
370
+ hasActiveNominations = false;
371
+ if (!(!isWaitingForNomination && nominations)) return [3 /*break*/, 2];
372
+ return [4 /*yield*/, Promise.all(nominations.targets.elements.map(function (target) {
373
+ return _this.getNominationStatus(nominator, target.asAddress(), eraIndex);
374
+ }))];
375
+ case 1:
376
+ hasActiveNominations = (_b.sent()).some(function (status) { return status === PolkadotNominationStatus_1.PolkadotNominationStatus.ACTIVE; });
377
+ _b.label = 2;
378
+ case 2:
379
+ if (nominations === undefined) {
380
+ return [2 /*return*/, 'bonded'];
381
+ }
382
+ else if (hasActiveNominations) {
383
+ return [2 /*return*/, 'nominating'];
384
+ }
385
+ else if (!isWaitingForNomination && !hasActiveNominations) {
386
+ return [2 /*return*/, 'nominating_inactive'];
387
+ }
388
+ else {
389
+ return [2 /*return*/, 'nominating_waiting'];
390
+ }
391
+ return [2 /*return*/];
392
+ }
393
+ });
394
+ });
395
+ };
396
+ PolkadotAccountController.prototype.getEraValidatorReward = function (accountId, eraIndex) {
397
+ var _a, _b, _c, _d;
398
+ return __awaiter(this, void 0, void 0, function () {
399
+ var address, results, eraReward, eraPoints, exposureClipped, validatorPrefs, validatorPoints, validatorReward;
400
+ var _this = this;
401
+ return __generator(this, function (_e) {
402
+ switch (_e.label) {
403
+ case 0:
404
+ address = this.substrateAddressFrom(accountId);
405
+ return [4 /*yield*/, Promise.all([
406
+ this.nodeClient.getValidatorReward(eraIndex).then(function (result) { return __awaiter(_this, void 0, void 0, function () {
407
+ return __generator(this, function (_a) {
408
+ return [2 /*return*/, result ? result : this.nodeClient.getValidatorReward(eraIndex - 1)];
409
+ });
410
+ }); }),
411
+ this.nodeClient.getRewardPoints(eraIndex),
412
+ this.nodeClient.getStakersClipped(eraIndex, address),
413
+ this.nodeClient.getValidatorPrefs(eraIndex, address)
414
+ ])];
415
+ case 1:
416
+ results = _e.sent();
417
+ if (results.some(function (result) { return !result; })) {
418
+ return [2 /*return*/, undefined];
419
+ }
420
+ eraReward = results[0];
421
+ eraPoints = results[1];
422
+ exposureClipped = results[2];
423
+ validatorPrefs = results[3];
424
+ validatorPoints = (_d = (_c = (_b = (_a = eraPoints === null || eraPoints === void 0 ? void 0 : eraPoints.individual) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.find(function (element) { return element.first.address.compare(address); })) === null || _c === void 0 ? void 0 : _c.second) === null || _d === void 0 ? void 0 : _d.value;
425
+ if (!eraReward || !eraPoints || !exposureClipped || !validatorPrefs || !validatorPoints) {
426
+ return [2 /*return*/, undefined];
427
+ }
428
+ validatorReward = this.calculateValidatorReward(eraReward, eraPoints.total.value, validatorPoints);
429
+ return [2 /*return*/, {
430
+ amount: validatorReward.toFixed(),
431
+ totalStake: exposureClipped.total.toString(),
432
+ ownStake: exposureClipped.own.toString(),
433
+ commission: validatorPrefs.commission.toString()
434
+ }];
435
+ }
436
+ });
437
+ });
438
+ };
439
+ PolkadotAccountController.prototype.getNominatorRewards = function (accountId, validators, activeEra, eras) {
440
+ return __awaiter(this, void 0, void 0, function () {
441
+ var address, expectedEraDuration, eraIndices, rewards;
442
+ var _this = this;
443
+ return __generator(this, function (_a) {
444
+ switch (_a.label) {
445
+ case 0:
446
+ address = this.substrateAddressFrom(accountId);
447
+ return [4 /*yield*/, this.nodeClient.getExpectedEraDuration()];
448
+ case 1:
449
+ expectedEraDuration = _a.sent();
450
+ if (!expectedEraDuration) {
451
+ return [2 /*return*/, Promise.reject('Could not fetch all necessary data.')];
452
+ }
453
+ eraIndices = Array.isArray(eras) ? eras : Array.from(Array(eras).keys()).map(function (index) { return activeEra.index.toNumber() - 1 - index; });
454
+ return [4 /*yield*/, Promise.all(eraIndices.map(function (era) {
455
+ return _this.calculateEraNominatorReward(address, validators.map(function (validator) { return _this.substrateAddressFrom(validator); }), era).then(function (partial) {
456
+ if (partial) {
457
+ var rewardEra = partial.eraIndex || activeEra.index.toNumber();
458
+ var erasPassed = activeEra.index.minus(rewardEra).toNumber();
459
+ partial.timestamp = activeEra.start.value
460
+ ? activeEra.start.value.minus(expectedEraDuration.multipliedBy(erasPassed - 1)).toNumber()
461
+ : 0;
462
+ }
463
+ return partial;
464
+ });
465
+ }))];
466
+ case 2:
467
+ rewards = _a.sent();
468
+ return [2 /*return*/, rewards.filter(function (reward) { return reward; })];
469
+ }
470
+ });
471
+ });
472
+ };
473
+ PolkadotAccountController.prototype.calculateEraNominatorReward = function (accountId, validators, eraIndex) {
474
+ return __awaiter(this, void 0, void 0, function () {
475
+ var results, reward, rewardPoints, exposuresWithValidators, partialRewards;
476
+ var _this = this;
477
+ return __generator(this, function (_a) {
478
+ switch (_a.label) {
479
+ case 0: return [4 /*yield*/, Promise.all([
480
+ this.nodeClient.getValidatorReward(eraIndex),
481
+ this.nodeClient.getRewardPoints(eraIndex),
482
+ Promise.all(validators.map(function (validator) { return __awaiter(_this, void 0, void 0, function () {
483
+ var _a;
484
+ return __generator(this, function (_b) {
485
+ switch (_b.label) {
486
+ case 0:
487
+ _a = [this.substrateAddressFrom(validator)];
488
+ return [4 /*yield*/, this.nodeClient
489
+ .getValidatorPrefs(eraIndex, this.substrateAddressFrom(validator))
490
+ .then(function (prefs) { var _a; return (_a = prefs === null || prefs === void 0 ? void 0 : prefs.commission) === null || _a === void 0 ? void 0 : _a.value; })];
491
+ case 1:
492
+ _a = _a.concat([
493
+ _b.sent()
494
+ ]);
495
+ return [4 /*yield*/, this.nodeClient.getStakersClipped(eraIndex, this.substrateAddressFrom(validator))];
496
+ case 2: return [2 /*return*/, _a.concat([
497
+ _b.sent()
498
+ ])];
499
+ }
500
+ });
501
+ }); }))
502
+ ])];
503
+ case 1:
504
+ results = _a.sent();
505
+ reward = results[0];
506
+ rewardPoints = results[1];
507
+ exposuresWithValidators = results[2];
508
+ if (!reward || !rewardPoints || !exposuresWithValidators) {
509
+ return [2 /*return*/, undefined];
510
+ }
511
+ partialRewards = exposuresWithValidators
512
+ .map(function (_a) {
513
+ var _b, _c, _d, _e;
514
+ var validator = _a[0], commission = _a[1], exposure = _a[2];
515
+ var validatorPoints = (_c = (_b = rewardPoints.individual.elements.find(function (element) { return element.first.compare(validator) === 0; })) === null || _b === void 0 ? void 0 : _b.second) === null || _c === void 0 ? void 0 : _c.value;
516
+ var nominatorStake = (_e = (_d = exposure === null || exposure === void 0 ? void 0 : exposure.others.elements.find(function (element) { return element.first.compare(accountId) === 0; })) === null || _d === void 0 ? void 0 : _d.second) === null || _e === void 0 ? void 0 : _e.value;
517
+ if (commission && exposure && validatorPoints && nominatorStake) {
518
+ var validatorReward = _this.calculateValidatorReward(reward, rewardPoints.total.value, validatorPoints);
519
+ return _this.calculateNominatorReward(validatorReward, commission, exposure.total.value, nominatorStake);
520
+ }
521
+ else {
522
+ return undefined;
523
+ }
524
+ })
525
+ .filter(function (reward) { return reward !== undefined; });
526
+ if (partialRewards.every(function (reward) { return !reward; })) {
527
+ return [2 /*return*/, undefined];
528
+ }
529
+ return [2 /*return*/, {
530
+ eraIndex: eraIndex,
531
+ amount: partialRewards.reduce(function (sum, next) { return sum.plus(next); }, new bignumber_1.default(0)).toFixed(0),
532
+ exposures: exposuresWithValidators === null || exposuresWithValidators === void 0 ? void 0 : exposuresWithValidators.map(function (_a) {
533
+ var validator = _a[0], _ = _a[1], exposure = _a[2];
534
+ return [
535
+ validator.asString(),
536
+ exposure === null || exposure === void 0 ? void 0 : exposure.others.elements.findIndex(function (element) { return element.first.compare(accountId) === 0; })
537
+ ];
538
+ }).filter(function (_a) {
539
+ var _ = _a[0], index = _a[1];
540
+ return index !== undefined;
541
+ })
542
+ }];
543
+ }
544
+ });
545
+ });
546
+ };
547
+ PolkadotAccountController.prototype.calculateValidatorReward = function (totalReward, totalPoints, validatorPoints) {
548
+ return validatorPoints.dividedBy(totalPoints).multipliedBy(totalReward);
549
+ };
550
+ PolkadotAccountController.prototype.calculateNominatorReward = function (validatorReward, validatorCommission, totalStake, nominatorStake) {
551
+ var nominatorShare = nominatorStake.dividedBy(totalStake);
552
+ return new bignumber_1.default(1).minus(validatorCommission.dividedBy(1000000000)).multipliedBy(validatorReward).multipliedBy(nominatorShare);
553
+ };
554
+ // tslint:disable-next-line: cyclomatic-complexity
555
+ PolkadotAccountController.prototype.getAvailableStakingActions = function (stakingDetails, nominations, validatorIds, maxDelegationValue) {
556
+ var _a, _b, _c, _d;
557
+ return __awaiter(this, void 0, void 0, function () {
558
+ var availableActions, currentValidators, validatorAddresses, bondedValue, minDelegationValue, isBonded, hasEnoughBond, isDelegating, isUnbonding, hasFundsToWithdraw;
559
+ var _this = this;
560
+ return __generator(this, function (_e) {
561
+ switch (_e.label) {
562
+ case 0:
563
+ availableActions = [];
564
+ currentValidators = ((_b = (_a = nominations === null || nominations === void 0 ? void 0 : nominations.targets) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.map(function (target) { return target.asAddress(); })) || [];
565
+ validatorAddresses = validatorIds.map(function (id) { return _this.substrateAddressFrom(id).asString(); });
566
+ bondedValue = new bignumber_1.default((_c = stakingDetails === null || stakingDetails === void 0 ? void 0 : stakingDetails.active) !== null && _c !== void 0 ? _c : 0);
567
+ return [4 /*yield*/, this.nodeClient.getMinNominatorBond()];
568
+ case 1:
569
+ minDelegationValue = _e.sent();
570
+ isBonded = bondedValue.gt(0);
571
+ hasEnoughBond = bondedValue.gte(minDelegationValue !== null && minDelegationValue !== void 0 ? minDelegationValue : 0);
572
+ isDelegating = nominations !== undefined;
573
+ isUnbonding = stakingDetails && stakingDetails.locked.length > 0;
574
+ hasFundsToWithdraw = new bignumber_1.default((_d = stakingDetails === null || stakingDetails === void 0 ? void 0 : stakingDetails.unlocked) !== null && _d !== void 0 ? _d : 0).gt(0);
575
+ if (maxDelegationValue.gt(0) && !isBonded && !isUnbonding) {
576
+ availableActions.push({
577
+ type: PolkadotStakingActionType_1.PolkadotStakingActionType.BOND_NOMINATE,
578
+ args: ['targets', 'controller', 'value', 'payee']
579
+ });
580
+ }
581
+ if (isBonded && !isDelegating && hasEnoughBond && !isUnbonding) {
582
+ availableActions.push({
583
+ type: PolkadotStakingActionType_1.PolkadotStakingActionType.NOMINATE,
584
+ args: ['targets']
585
+ });
586
+ }
587
+ if (maxDelegationValue.gt(0) && isBonded && !isUnbonding) {
588
+ availableActions.push({
589
+ type: PolkadotStakingActionType_1.PolkadotStakingActionType.BOND_EXTRA,
590
+ args: ['value']
591
+ });
592
+ }
593
+ if (isBonded && !isDelegating && !isUnbonding) {
594
+ availableActions.push({
595
+ type: PolkadotStakingActionType_1.PolkadotStakingActionType.UNBOND,
596
+ args: ['value']
597
+ });
598
+ }
599
+ if (isUnbonding && !isDelegating) {
600
+ availableActions.push({
601
+ type: PolkadotStakingActionType_1.PolkadotStakingActionType.REBOND_NOMINATE,
602
+ args: ['targets', 'value']
603
+ });
604
+ }
605
+ if (isUnbonding && isDelegating) {
606
+ availableActions.push({
607
+ type: PolkadotStakingActionType_1.PolkadotStakingActionType.REBOND_EXTRA,
608
+ args: ['value']
609
+ });
610
+ }
611
+ if (isDelegating) {
612
+ if (validatorAddresses.every(function (validator) { return currentValidators.includes(validator); }) &&
613
+ validatorAddresses.length === currentValidators.length) {
614
+ availableActions.push({
615
+ type: PolkadotStakingActionType_1.PolkadotStakingActionType.CANCEL_NOMINATION,
616
+ args: ['value']
617
+ });
618
+ }
619
+ else if (validatorAddresses.length > 0) {
620
+ availableActions.push({
621
+ type: PolkadotStakingActionType_1.PolkadotStakingActionType.CHANGE_NOMINATION,
622
+ args: ['targets']
623
+ });
624
+ }
625
+ }
626
+ if (hasFundsToWithdraw) {
627
+ availableActions.push({
628
+ type: PolkadotStakingActionType_1.PolkadotStakingActionType.WITHDRAW_UNBONDED,
629
+ args: []
630
+ });
631
+ }
632
+ availableActions.sort(function (a, b) { return a.type - b.type; });
633
+ return [2 /*return*/, availableActions];
634
+ }
635
+ });
636
+ });
637
+ };
638
+ PolkadotAccountController.prototype.getAccountIdentityInfo = function (address) {
639
+ return __awaiter(this, void 0, void 0, function () {
640
+ var registration, superOf, _a;
641
+ return __generator(this, function (_b) {
642
+ switch (_b.label) {
643
+ case 0:
644
+ _b.trys.push([0, 3, , 4]);
645
+ return [4 /*yield*/, this.nodeClient.getIdentityOf(address)];
646
+ case 1:
647
+ registration = _b.sent();
648
+ if (registration) {
649
+ return [2 /*return*/, registration.identityInfo];
650
+ }
651
+ return [4 /*yield*/, this.nodeClient.getSuperOf(address)];
652
+ case 2:
653
+ superOf = _b.sent();
654
+ return [2 /*return*/, superOf ? this.getAccountIdentityInfo(superOf.first.address) : undefined];
655
+ case 3:
656
+ _a = _b.sent();
657
+ return [2 /*return*/, undefined];
658
+ case 4: return [2 /*return*/];
659
+ }
660
+ });
661
+ });
662
+ };
663
+ PolkadotAccountController.prototype.partitionArray = function (array, predicate) {
664
+ var partitioned = [[], []];
665
+ for (var _i = 0, array_1 = array; _i < array_1.length; _i++) {
666
+ var item = array_1[_i];
667
+ var index = predicate(item) ? 0 : 1;
668
+ partitioned[index].push(item);
669
+ }
670
+ return partitioned;
671
+ };
672
+ return PolkadotAccountController;
673
+ }(v1_1.SubstrateCommonAccountController));
674
+ exports.PolkadotAccountController = PolkadotAccountController;
675
+ //# sourceMappingURL=PolkadotAccountController.js.map