@airgap/moonbeam 0.13.45-beta.3 → 0.13.45-beta.5

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 (86) hide show
  1. package/package.json +5 -5
  2. package/v0/index.js +7 -7
  3. package/v0/index.js.map +1 -1
  4. package/v0/protocol/moonbase/MoonbaseProtocol.js +36 -17
  5. package/v0/protocol/moonbase/MoonbaseProtocol.js.map +1 -1
  6. package/v0/protocol/moonbase/MoonbaseProtocolOptions.js +67 -28
  7. package/v0/protocol/moonbase/MoonbaseProtocolOptions.js.map +1 -1
  8. package/v0/protocol/moonbeam/MoonbeamProtocol.js +589 -334
  9. package/v0/protocol/moonbeam/MoonbeamProtocol.js.map +1 -1
  10. package/v0/protocol/moonbeam/MoonbeamProtocolOptions.js +87 -41
  11. package/v0/protocol/moonbeam/MoonbeamProtocolOptions.js.map +1 -1
  12. package/v0/protocol/moonbeam/controllers/MoonbeamAccountController.js +495 -287
  13. package/v0/protocol/moonbeam/controllers/MoonbeamAccountController.js.map +1 -1
  14. package/v0/protocol/moonbeam/controllers/MoonbeamTransactionController.d.ts +1 -0
  15. package/v0/protocol/moonbeam/controllers/MoonbeamTransactionController.js +69 -9
  16. package/v0/protocol/moonbeam/controllers/MoonbeamTransactionController.js.map +1 -1
  17. package/v0/protocol/moonbeam/data/staking/MoonbeamBond.js +34 -16
  18. package/v0/protocol/moonbeam/data/staking/MoonbeamBond.js.map +1 -1
  19. package/v0/protocol/moonbeam/data/staking/MoonbeamCandidateBondLessRequest.js +34 -16
  20. package/v0/protocol/moonbeam/data/staking/MoonbeamCandidateBondLessRequest.js.map +1 -1
  21. package/v0/protocol/moonbeam/data/staking/MoonbeamCandidateMetadata.js +23 -22
  22. package/v0/protocol/moonbeam/data/staking/MoonbeamCandidateMetadata.js.map +1 -1
  23. package/v0/protocol/moonbeam/data/staking/MoonbeamDelegationScheduledRequests.js +107 -64
  24. package/v0/protocol/moonbeam/data/staking/MoonbeamDelegationScheduledRequests.js.map +1 -1
  25. package/v0/protocol/moonbeam/data/staking/MoonbeamDelegator.js +90 -50
  26. package/v0/protocol/moonbeam/data/staking/MoonbeamDelegator.js.map +1 -1
  27. package/v0/protocol/moonbeam/data/staking/MoonbeamRoundInfo.js +13 -12
  28. package/v0/protocol/moonbeam/data/staking/MoonbeamRoundInfo.js.map +1 -1
  29. package/v0/protocol/moonbeam/data/staking/MoonbeamStakingActionType.js +1 -1
  30. package/v0/protocol/moonbeam/data/staking/MoonbeamStakingActionType.js.map +1 -1
  31. package/v0/protocol/moonbeam/node/MoonbeamNodeClient.js +215 -60
  32. package/v0/protocol/moonbeam/node/MoonbeamNodeClient.js.map +1 -1
  33. package/v0/protocol/moonriver/MoonriverProtocol.js +36 -17
  34. package/v0/protocol/moonriver/MoonriverProtocol.js.map +1 -1
  35. package/v0/protocol/moonriver/MoonriverProtocolOptions.js +67 -28
  36. package/v0/protocol/moonriver/MoonriverProtocolOptions.js.map +1 -1
  37. package/v1/controller/MoonbeamAccountController.js +456 -252
  38. package/v1/controller/MoonbeamAccountController.js.map +1 -1
  39. package/v1/controller/MoonbeamTransactionController.js +23 -3
  40. package/v1/controller/MoonbeamTransactionController.js.map +1 -1
  41. package/v1/data/staking/MoonbeamBond.js +32 -14
  42. package/v1/data/staking/MoonbeamBond.js.map +1 -1
  43. package/v1/data/staking/MoonbeamCandidateBondLessRequest.js +32 -14
  44. package/v1/data/staking/MoonbeamCandidateBondLessRequest.js.map +1 -1
  45. package/v1/data/staking/MoonbeamCandidateMetadata.js +24 -21
  46. package/v1/data/staking/MoonbeamCandidateMetadata.js.map +1 -1
  47. package/v1/data/staking/MoonbeamDelegationScheduledRequests.js +107 -62
  48. package/v1/data/staking/MoonbeamDelegationScheduledRequests.js.map +1 -1
  49. package/v1/data/staking/MoonbeamDelegator.js +90 -48
  50. package/v1/data/staking/MoonbeamDelegator.js.map +1 -1
  51. package/v1/data/staking/MoonbeamRoundInfo.js +12 -11
  52. package/v1/data/staking/MoonbeamRoundInfo.js.map +1 -1
  53. package/v1/data/staking/MoonbeamStakingActionType.js +1 -1
  54. package/v1/data/staking/MoonbeamStakingActionType.js.map +1 -1
  55. package/v1/data/transaction/method/args.js +293 -160
  56. package/v1/data/transaction/method/args.js.map +1 -1
  57. package/v1/data/transaction/transaction.js +23 -12
  58. package/v1/data/transaction/transaction.js.map +1 -1
  59. package/v1/index.js +5 -5
  60. package/v1/index.js.map +1 -1
  61. package/v1/module/MoonbeamModule.d.ts +1 -1
  62. package/v1/module/MoonbeamModule.js +98 -40
  63. package/v1/module/MoonbeamModule.js.map +1 -1
  64. package/v1/module.js +3 -2
  65. package/v1/module.js.map +1 -1
  66. package/v1/node/MoonbeamNodeClient.js +222 -71
  67. package/v1/node/MoonbeamNodeClient.js.map +1 -1
  68. package/v1/protocol/MoonbaseProtocol.js +49 -18
  69. package/v1/protocol/MoonbaseProtocol.js.map +1 -1
  70. package/v1/protocol/MoonbeamBaseProtocol.js +625 -340
  71. package/v1/protocol/MoonbeamBaseProtocol.js.map +1 -1
  72. package/v1/protocol/MoonbeamProtocol.js +51 -20
  73. package/v1/protocol/MoonbeamProtocol.js.map +1 -1
  74. package/v1/protocol/MoonriverProtocol.js +49 -18
  75. package/v1/protocol/MoonriverProtocol.js.map +1 -1
  76. package/v1/serializer/v3/schemas/converter/transaction-converter.js +6 -5
  77. package/v1/serializer/v3/schemas/converter/transaction-converter.js.map +1 -1
  78. package/v1/serializer/v3/serializer-companion.js +154 -75
  79. package/v1/serializer/v3/serializer-companion.js.map +1 -1
  80. package/v1/serializer/v3/validators/transaction-validator.js +23 -3
  81. package/v1/serializer/v3/validators/transaction-validator.js.map +1 -1
  82. package/v1/serializer/v3/validators/validators.js +1 -1
  83. package/v1/serializer/v3/validators/validators.js.map +1 -1
  84. package/v1/types/configuration.d.ts +1 -1
  85. package/v1/types/crypto.d.ts +1 -1
  86. package/v1/types/protocol.d.ts +3 -3
@@ -1,210 +1,388 @@
1
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
+ };
2
53
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
54
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
55
  };
5
56
  Object.defineProperty(exports, "__esModule", { value: true });
6
57
  exports.MoonbeamAccountController = void 0;
7
- const coinlib_core_1 = require("@airgap/coinlib-core");
8
- const bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
9
- const errors_1 = require("@airgap/coinlib-core/errors");
10
- const array_1 = require("@airgap/coinlib-core/utils/array");
11
- const v1_1 = require("@airgap/substrate/v1");
12
- const MoonbeamCandidateMetadata_1 = require("../data/staking/MoonbeamCandidateMetadata");
13
- const MoonbeamDelegationScheduledRequests_1 = require("../data/staking/MoonbeamDelegationScheduledRequests");
14
- const MoonbeamDelegator_1 = require("../data/staking/MoonbeamDelegator");
15
- const MoonbeamStakingActionType_1 = require("../data/staking/MoonbeamStakingActionType");
16
- class MoonbeamAccountController extends v1_1.SubstrateCommonAccountController {
17
- async isDelegating(accountId) {
18
- const delegatorState = await this.nodeClient.getDelegatorState(v1_1.SubstrateEthAddress.from(accountId));
19
- return delegatorState ? delegatorState.delegations.elements.length > 0 : false;
20
- }
21
- async getMinDelegationAmount(accountId) {
22
- const isDelegating = await this.isDelegating(accountId);
23
- const minAmount = isDelegating ? await this.nodeClient.getMinDelegation() : await this.nodeClient.getMinDelegatorStake();
24
- if (!minAmount) {
25
- throw new errors_1.OperationFailedError(coinlib_core_1.Domain.SUBSTRATE, 'Could not fetch network constants');
26
- }
27
- return minAmount.toFixed();
28
- }
29
- async getCurrentCollators(accountId) {
30
- const delegatorState = await this.nodeClient.getDelegatorState(v1_1.SubstrateEthAddress.from(accountId));
31
- if (delegatorState) {
32
- return delegatorState.delegations.elements.map((collatorDetails) => collatorDetails.owner.asAddress());
33
- }
34
- return [];
58
+ var coinlib_core_1 = require("@airgap/coinlib-core");
59
+ var bignumber_1 = __importDefault(require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber"));
60
+ var errors_1 = require("@airgap/coinlib-core/errors");
61
+ var array_1 = require("@airgap/coinlib-core/utils/array");
62
+ var v1_1 = require("@airgap/substrate/v1");
63
+ var MoonbeamCandidateMetadata_1 = require("../data/staking/MoonbeamCandidateMetadata");
64
+ var MoonbeamDelegationScheduledRequests_1 = require("../data/staking/MoonbeamDelegationScheduledRequests");
65
+ var MoonbeamDelegator_1 = require("../data/staking/MoonbeamDelegator");
66
+ var MoonbeamStakingActionType_1 = require("../data/staking/MoonbeamStakingActionType");
67
+ var MoonbeamAccountController = /** @class */ (function (_super) {
68
+ __extends(MoonbeamAccountController, _super);
69
+ function MoonbeamAccountController() {
70
+ return _super !== null && _super.apply(this, arguments) || this;
35
71
  }
36
- async getDelegatorDetails(accountId) {
37
- const address = v1_1.SubstrateEthAddress.from(accountId);
38
- const results = await Promise.all([this.getBalance(address), this.nodeClient.getDelegatorState(address)]);
39
- const balance = results[0];
40
- const delegatorState = results[1];
41
- if (!balance) {
42
- return Promise.reject('Could not fetch delegator details.');
43
- }
44
- const totalBond = delegatorState?.total.value ?? new bignumber_1.default(0);
45
- let status;
46
- switch (delegatorState?.status.type.value) {
47
- case MoonbeamDelegator_1.MoonbeamDelegatorStatusRaw.ACTIVE:
48
- status = 'Active';
49
- break;
50
- case MoonbeamDelegator_1.MoonbeamDelegatorStatusRaw.LEAVING:
51
- status = 'Leaving';
52
- break;
53
- }
54
- return {
55
- address: address.asString(),
56
- balance: balance.total.toString(),
57
- totalBond: totalBond.toString(),
58
- delegatees: delegatorState?.delegations.elements.map((bond) => bond.owner.asAddress()) ?? [],
59
- availableActions: await this.getStakingActions(delegatorState),
60
- status
61
- };
62
- }
63
- async getCollatorDetails(accountId) {
64
- const address = v1_1.SubstrateEthAddress.from(accountId);
65
- const results = await Promise.all([this.nodeClient.getCandidateInfo(address), this.nodeClient.getCollatorCommission()]);
66
- const candidateInfo = results[0];
67
- const commission = results[1];
68
- if (!candidateInfo || !commission) {
69
- return Promise.reject('Could not fetch collator details.');
70
- }
71
- let status;
72
- switch (candidateInfo.status.value) {
73
- case MoonbeamCandidateMetadata_1.MoonbeamCollatorStatus.ACTIVE:
74
- status = 'Active';
75
- break;
76
- case MoonbeamCandidateMetadata_1.MoonbeamCollatorStatus.IDLE:
77
- status = 'Idle';
78
- break;
79
- case MoonbeamCandidateMetadata_1.MoonbeamCollatorStatus.LEAVING:
80
- status = 'Leaving';
81
- }
82
- return {
83
- address: address.asString(),
84
- status,
85
- minEligibleBalance: candidateInfo.lowestTopDelegationAmount.toString(),
86
- ownStakingBalance: candidateInfo.bond.toString(),
87
- totalStakingBalance: candidateInfo.totalCounted.toString(),
88
- commission: commission.dividedBy(1_000_000_000).toString(), // commission is Perbill (parts per billion)
89
- delegators: candidateInfo.delegationCount.toNumber()
90
- };
91
- }
92
- async getDelegationDetails(accountId, collator) {
93
- const delegatorAddress = v1_1.SubstrateEthAddress.from(accountId);
94
- const collatorAddress = v1_1.SubstrateEthAddress.from(collator);
95
- const results = await Promise.all([
96
- this.getBalance(delegatorAddress),
97
- this.nodeClient.getDelegatorState(delegatorAddress),
98
- this.getCollatorDetails(collator),
99
- this.nodeClient.getDelegationScheduledRequests(collatorAddress),
100
- this.nodeClient.getRound()
101
- ]);
102
- const balance = results[0];
103
- const delegatorState = results[1];
104
- const collatorDetails = results[2];
105
- const delegationScheduledRequests = results[3];
106
- const currentRound = results[4]?.current;
107
- if (!balance || !collatorDetails) {
108
- return Promise.reject('Could not fetch delegation details.');
109
- }
110
- const bond = delegatorState?.delegations.elements.find((bond) => bond.owner.compare(collator) === 0)?.amount.value ?? new bignumber_1.default(0);
111
- const totalBond = delegatorState?.total.value ?? new bignumber_1.default(0);
112
- let status;
113
- switch (delegatorState?.status.type.value) {
114
- case MoonbeamDelegator_1.MoonbeamDelegatorStatusRaw.ACTIVE:
115
- status = 'Active';
116
- break;
117
- case MoonbeamDelegator_1.MoonbeamDelegatorStatusRaw.LEAVING:
118
- status = currentRound?.gte(delegatorState.status.roundIndex) ? 'ReadyToLeave' : 'Leaving';
119
- break;
120
- }
121
- const delegatorDetails = {
122
- address: delegatorAddress.asString(),
123
- balance: balance.total.toString(),
124
- totalBond: totalBond.toString(),
125
- delegatees: delegatorState?.delegations.elements.map((bond) => bond.owner.asAddress()) ?? [],
126
- availableActions: await this.getStakingActions(delegatorState, collatorDetails, delegationScheduledRequests),
127
- status
128
- };
129
- const changeRequest = delegationScheduledRequests?.requests.elements.find((request) => request.delegator.asAddress() === delegatorDetails.address);
130
- return {
131
- delegatorDetails,
132
- collatorDetails,
133
- bond: bond.toString(),
134
- pendingRequest: changeRequest
135
- ? {
136
- type: changeRequest.action.type.value === MoonbeamDelegationScheduledRequests_1.MoonbeamDelegationActionRaw.REVOKE ? 'revoke' : 'decrease',
137
- amount: changeRequest.action.amount.toString(),
138
- executableIn: currentRound
139
- ? bignumber_1.default.max(changeRequest.whenExecutable.value.minus(currentRound.value), 0).toNumber()
140
- : undefined
72
+ MoonbeamAccountController.prototype.isDelegating = function (accountId) {
73
+ return __awaiter(this, void 0, void 0, function () {
74
+ var delegatorState;
75
+ return __generator(this, function (_a) {
76
+ switch (_a.label) {
77
+ case 0: return [4 /*yield*/, this.nodeClient.getDelegatorState(v1_1.SubstrateEthAddress.from(accountId))];
78
+ case 1:
79
+ delegatorState = _a.sent();
80
+ return [2 /*return*/, delegatorState ? delegatorState.delegations.elements.length > 0 : false];
141
81
  }
142
- : undefined
143
- };
144
- }
145
- async getStakingActions(delegator, collator, delegationScheduledRequests) {
146
- const actions = [];
147
- const scheduledLeaving = delegator ? await this.getScheduledLeaving(delegator) : undefined;
148
- const scheduledRequests = delegationScheduledRequests
149
- ? delegator && collator
150
- ? delegationScheduledRequests.requests.elements.filter((value) => value.delegator.asAddress() === delegator.id.asAddress())
151
- : []
152
- : undefined;
153
- const delegations = delegator?.delegations.elements ?? [];
154
- const maxDelegations = await this.nodeClient.getMaxDelegationsPerDelegator();
155
- const canDelegateToCollator = maxDelegations?.gt(delegations.length) && collator && !delegations.some((bond) => bond.owner.compare(collator.address) === 0);
156
- const isDelegatingCollator = !!collator && delegations.some((bond) => bond.owner.compare(collator.address) === 0);
157
- const currentRound = scheduledLeaving || scheduledRequests ? (await this.nodeClient.getRound())?.current.value : undefined;
158
- if (canDelegateToCollator && !scheduledLeaving && scheduledRequests?.length === 0) {
159
- actions.push(...this.getUndelegatedActions());
160
- }
161
- if (delegations.length > 0 && !scheduledLeaving && scheduledRequests?.length === 0) {
162
- actions.push(...this.getDelegatedActions(isDelegatingCollator));
163
- }
164
- if (scheduledLeaving && currentRound) {
165
- actions.push(...(await this.getLeavingActions(currentRound, scheduledLeaving)));
166
- }
167
- if (scheduledRequests && scheduledRequests.length > 0 && currentRound) {
168
- actions.push(...(await this.getRequestActions(currentRound, scheduledRequests)));
169
- }
170
- return actions;
171
- }
172
- async getScheduledLeaving(delegator) {
173
- if (delegator.status instanceof MoonbeamDelegator_1.MoonbeamDelegatorStatusLeaving) {
174
- return delegator.status.roundIndex.value;
175
- }
176
- const allDelegationScheduledRequests = await Promise.all(delegator.delegations.elements.map((bond) => {
177
- const collator = v1_1.SubstrateEthAddress.from(bond.owner.address);
178
- return this.nodeClient.getDelegationScheduledRequests(collator);
179
- }));
180
- return allDelegationScheduledRequests.reduce((acc, requests) => {
181
- if (acc === undefined) {
182
- return undefined;
183
- }
184
- const whenExecutable = requests?.requests.elements.find((request) => {
185
- request.delegator.asAddress() === delegator.id.asAddress() && request.action.type.value === MoonbeamDelegationScheduledRequests_1.MoonbeamDelegationActionRaw.REVOKE;
186
- })?.whenExecutable.value;
187
- if (whenExecutable === undefined) {
188
- return undefined;
189
- }
190
- if (acc.eq(whenExecutable) || acc.eq(-1)) {
191
- return whenExecutable;
192
- }
193
- else {
194
- return undefined;
195
- }
196
- }, new bignumber_1.default(-1));
197
- }
198
- getUndelegatedActions() {
82
+ });
83
+ });
84
+ };
85
+ MoonbeamAccountController.prototype.getMinDelegationAmount = function (accountId) {
86
+ return __awaiter(this, void 0, void 0, function () {
87
+ var isDelegating, minAmount, _a;
88
+ return __generator(this, function (_b) {
89
+ switch (_b.label) {
90
+ case 0: return [4 /*yield*/, this.isDelegating(accountId)];
91
+ case 1:
92
+ isDelegating = _b.sent();
93
+ if (!isDelegating) return [3 /*break*/, 3];
94
+ return [4 /*yield*/, this.nodeClient.getMinDelegation()];
95
+ case 2:
96
+ _a = _b.sent();
97
+ return [3 /*break*/, 5];
98
+ case 3: return [4 /*yield*/, this.nodeClient.getMinDelegatorStake()];
99
+ case 4:
100
+ _a = _b.sent();
101
+ _b.label = 5;
102
+ case 5:
103
+ minAmount = _a;
104
+ if (!minAmount) {
105
+ throw new errors_1.OperationFailedError(coinlib_core_1.Domain.SUBSTRATE, 'Could not fetch network constants');
106
+ }
107
+ return [2 /*return*/, minAmount.toFixed()];
108
+ }
109
+ });
110
+ });
111
+ };
112
+ MoonbeamAccountController.prototype.getCurrentCollators = function (accountId) {
113
+ return __awaiter(this, void 0, void 0, function () {
114
+ var delegatorState;
115
+ return __generator(this, function (_a) {
116
+ switch (_a.label) {
117
+ case 0: return [4 /*yield*/, this.nodeClient.getDelegatorState(v1_1.SubstrateEthAddress.from(accountId))];
118
+ case 1:
119
+ delegatorState = _a.sent();
120
+ if (delegatorState) {
121
+ return [2 /*return*/, delegatorState.delegations.elements.map(function (collatorDetails) { return collatorDetails.owner.asAddress(); })];
122
+ }
123
+ return [2 /*return*/, []];
124
+ }
125
+ });
126
+ });
127
+ };
128
+ MoonbeamAccountController.prototype.getDelegatorDetails = function (accountId) {
129
+ var _a, _b;
130
+ return __awaiter(this, void 0, void 0, function () {
131
+ var address, results, balance, delegatorState, totalBond, status;
132
+ var _c;
133
+ return __generator(this, function (_d) {
134
+ switch (_d.label) {
135
+ case 0:
136
+ address = v1_1.SubstrateEthAddress.from(accountId);
137
+ return [4 /*yield*/, Promise.all([this.getBalance(address), this.nodeClient.getDelegatorState(address)])];
138
+ case 1:
139
+ results = _d.sent();
140
+ balance = results[0];
141
+ delegatorState = results[1];
142
+ if (!balance) {
143
+ return [2 /*return*/, Promise.reject('Could not fetch delegator details.')];
144
+ }
145
+ totalBond = (_a = delegatorState === null || delegatorState === void 0 ? void 0 : delegatorState.total.value) !== null && _a !== void 0 ? _a : new bignumber_1.default(0);
146
+ switch (delegatorState === null || delegatorState === void 0 ? void 0 : delegatorState.status.type.value) {
147
+ case MoonbeamDelegator_1.MoonbeamDelegatorStatusRaw.ACTIVE:
148
+ status = 'Active';
149
+ break;
150
+ case MoonbeamDelegator_1.MoonbeamDelegatorStatusRaw.LEAVING:
151
+ status = 'Leaving';
152
+ break;
153
+ }
154
+ _c = {
155
+ address: address.asString(),
156
+ balance: balance.total.toString(),
157
+ totalBond: totalBond.toString(),
158
+ delegatees: (_b = delegatorState === null || delegatorState === void 0 ? void 0 : delegatorState.delegations.elements.map(function (bond) { return bond.owner.asAddress(); })) !== null && _b !== void 0 ? _b : []
159
+ };
160
+ return [4 /*yield*/, this.getStakingActions(delegatorState)];
161
+ case 2: return [2 /*return*/, (_c.availableActions = _d.sent(),
162
+ _c.status = status,
163
+ _c)];
164
+ }
165
+ });
166
+ });
167
+ };
168
+ MoonbeamAccountController.prototype.getCollatorDetails = function (accountId) {
169
+ return __awaiter(this, void 0, void 0, function () {
170
+ var address, results, candidateInfo, commission, status;
171
+ return __generator(this, function (_a) {
172
+ switch (_a.label) {
173
+ case 0:
174
+ address = v1_1.SubstrateEthAddress.from(accountId);
175
+ return [4 /*yield*/, Promise.all([this.nodeClient.getCandidateInfo(address), this.nodeClient.getCollatorCommission()])];
176
+ case 1:
177
+ results = _a.sent();
178
+ candidateInfo = results[0];
179
+ commission = results[1];
180
+ if (!candidateInfo || !commission) {
181
+ return [2 /*return*/, Promise.reject('Could not fetch collator details.')];
182
+ }
183
+ switch (candidateInfo.status.value) {
184
+ case MoonbeamCandidateMetadata_1.MoonbeamCollatorStatus.ACTIVE:
185
+ status = 'Active';
186
+ break;
187
+ case MoonbeamCandidateMetadata_1.MoonbeamCollatorStatus.IDLE:
188
+ status = 'Idle';
189
+ break;
190
+ case MoonbeamCandidateMetadata_1.MoonbeamCollatorStatus.LEAVING:
191
+ status = 'Leaving';
192
+ }
193
+ return [2 /*return*/, {
194
+ address: address.asString(),
195
+ status: status,
196
+ minEligibleBalance: candidateInfo.lowestTopDelegationAmount.toString(),
197
+ ownStakingBalance: candidateInfo.bond.toString(),
198
+ totalStakingBalance: candidateInfo.totalCounted.toString(),
199
+ commission: commission.dividedBy(1000000000).toString(),
200
+ delegators: candidateInfo.delegationCount.toNumber()
201
+ }];
202
+ }
203
+ });
204
+ });
205
+ };
206
+ MoonbeamAccountController.prototype.getDelegationDetails = function (accountId, collator) {
207
+ var _a, _b, _c, _d, _e;
208
+ return __awaiter(this, void 0, void 0, function () {
209
+ var delegatorAddress, collatorAddress, results, balance, delegatorState, collatorDetails, delegationScheduledRequests, currentRound, bond, totalBond, status, delegatorDetails, changeRequest;
210
+ var _f;
211
+ return __generator(this, function (_g) {
212
+ switch (_g.label) {
213
+ case 0:
214
+ delegatorAddress = v1_1.SubstrateEthAddress.from(accountId);
215
+ collatorAddress = v1_1.SubstrateEthAddress.from(collator);
216
+ return [4 /*yield*/, Promise.all([
217
+ this.getBalance(delegatorAddress),
218
+ this.nodeClient.getDelegatorState(delegatorAddress),
219
+ this.getCollatorDetails(collator),
220
+ this.nodeClient.getDelegationScheduledRequests(collatorAddress),
221
+ this.nodeClient.getRound()
222
+ ])];
223
+ case 1:
224
+ results = _g.sent();
225
+ balance = results[0];
226
+ delegatorState = results[1];
227
+ collatorDetails = results[2];
228
+ delegationScheduledRequests = results[3];
229
+ currentRound = (_a = results[4]) === null || _a === void 0 ? void 0 : _a.current;
230
+ if (!balance || !collatorDetails) {
231
+ return [2 /*return*/, Promise.reject('Could not fetch delegation details.')];
232
+ }
233
+ bond = (_c = (_b = delegatorState === null || delegatorState === void 0 ? void 0 : delegatorState.delegations.elements.find(function (bond) { return bond.owner.compare(collator) === 0; })) === null || _b === void 0 ? void 0 : _b.amount.value) !== null && _c !== void 0 ? _c : new bignumber_1.default(0);
234
+ totalBond = (_d = delegatorState === null || delegatorState === void 0 ? void 0 : delegatorState.total.value) !== null && _d !== void 0 ? _d : new bignumber_1.default(0);
235
+ switch (delegatorState === null || delegatorState === void 0 ? void 0 : delegatorState.status.type.value) {
236
+ case MoonbeamDelegator_1.MoonbeamDelegatorStatusRaw.ACTIVE:
237
+ status = 'Active';
238
+ break;
239
+ case MoonbeamDelegator_1.MoonbeamDelegatorStatusRaw.LEAVING:
240
+ status = (currentRound === null || currentRound === void 0 ? void 0 : currentRound.gte(delegatorState.status.roundIndex)) ? 'ReadyToLeave' : 'Leaving';
241
+ break;
242
+ }
243
+ _f = {
244
+ address: delegatorAddress.asString(),
245
+ balance: balance.total.toString(),
246
+ totalBond: totalBond.toString(),
247
+ delegatees: (_e = delegatorState === null || delegatorState === void 0 ? void 0 : delegatorState.delegations.elements.map(function (bond) { return bond.owner.asAddress(); })) !== null && _e !== void 0 ? _e : []
248
+ };
249
+ return [4 /*yield*/, this.getStakingActions(delegatorState, collatorDetails, delegationScheduledRequests)];
250
+ case 2:
251
+ delegatorDetails = (_f.availableActions = _g.sent(),
252
+ _f.status = status,
253
+ _f);
254
+ changeRequest = delegationScheduledRequests === null || delegationScheduledRequests === void 0 ? void 0 : delegationScheduledRequests.requests.elements.find(function (request) { return request.delegator.asAddress() === delegatorDetails.address; });
255
+ return [2 /*return*/, {
256
+ delegatorDetails: delegatorDetails,
257
+ collatorDetails: collatorDetails,
258
+ bond: bond.toString(),
259
+ pendingRequest: changeRequest
260
+ ? {
261
+ type: changeRequest.action.type.value === MoonbeamDelegationScheduledRequests_1.MoonbeamDelegationActionRaw.REVOKE ? 'revoke' : 'decrease',
262
+ amount: changeRequest.action.amount.toString(),
263
+ executableIn: currentRound
264
+ ? bignumber_1.default.max(changeRequest.whenExecutable.value.minus(currentRound.value), 0).toNumber()
265
+ : undefined
266
+ }
267
+ : undefined
268
+ }];
269
+ }
270
+ });
271
+ });
272
+ };
273
+ MoonbeamAccountController.prototype.getStakingActions = function (delegator, collator, delegationScheduledRequests) {
274
+ var _a, _b;
275
+ return __awaiter(this, void 0, void 0, function () {
276
+ var actions, scheduledLeaving, _c, scheduledRequests, delegations, maxDelegations, canDelegateToCollator, isDelegatingCollator, currentRound, _d, _e, _f, _g, _h, _j, _k;
277
+ return __generator(this, function (_l) {
278
+ switch (_l.label) {
279
+ case 0:
280
+ actions = [];
281
+ if (!delegator) return [3 /*break*/, 2];
282
+ return [4 /*yield*/, this.getScheduledLeaving(delegator)];
283
+ case 1:
284
+ _c = _l.sent();
285
+ return [3 /*break*/, 3];
286
+ case 2:
287
+ _c = undefined;
288
+ _l.label = 3;
289
+ case 3:
290
+ scheduledLeaving = _c;
291
+ scheduledRequests = delegationScheduledRequests
292
+ ? delegator && collator
293
+ ? delegationScheduledRequests.requests.elements.filter(function (value) { return value.delegator.asAddress() === delegator.id.asAddress(); })
294
+ : []
295
+ : undefined;
296
+ delegations = (_a = delegator === null || delegator === void 0 ? void 0 : delegator.delegations.elements) !== null && _a !== void 0 ? _a : [];
297
+ return [4 /*yield*/, this.nodeClient.getMaxDelegationsPerDelegator()];
298
+ case 4:
299
+ maxDelegations = _l.sent();
300
+ canDelegateToCollator = (maxDelegations === null || maxDelegations === void 0 ? void 0 : maxDelegations.gt(delegations.length)) && collator && !delegations.some(function (bond) { return bond.owner.compare(collator.address) === 0; });
301
+ isDelegatingCollator = !!collator && delegations.some(function (bond) { return bond.owner.compare(collator.address) === 0; });
302
+ if (!(scheduledLeaving || scheduledRequests)) return [3 /*break*/, 6];
303
+ return [4 /*yield*/, this.nodeClient.getRound()];
304
+ case 5:
305
+ _d = (_b = (_l.sent())) === null || _b === void 0 ? void 0 : _b.current.value;
306
+ return [3 /*break*/, 7];
307
+ case 6:
308
+ _d = undefined;
309
+ _l.label = 7;
310
+ case 7:
311
+ currentRound = _d;
312
+ if (canDelegateToCollator && !scheduledLeaving && (scheduledRequests === null || scheduledRequests === void 0 ? void 0 : scheduledRequests.length) === 0) {
313
+ actions.push.apply(actions, this.getUndelegatedActions());
314
+ }
315
+ if (delegations.length > 0 && !scheduledLeaving && (scheduledRequests === null || scheduledRequests === void 0 ? void 0 : scheduledRequests.length) === 0) {
316
+ actions.push.apply(actions, this.getDelegatedActions(isDelegatingCollator));
317
+ }
318
+ if (!(scheduledLeaving && currentRound)) return [3 /*break*/, 9];
319
+ _f = (_e = actions.push).apply;
320
+ _g = [actions];
321
+ return [4 /*yield*/, this.getLeavingActions(currentRound, scheduledLeaving)];
322
+ case 8:
323
+ _f.apply(_e, _g.concat([(_l.sent())]));
324
+ _l.label = 9;
325
+ case 9:
326
+ if (!(scheduledRequests && scheduledRequests.length > 0 && currentRound)) return [3 /*break*/, 11];
327
+ _j = (_h = actions.push).apply;
328
+ _k = [actions];
329
+ return [4 /*yield*/, this.getRequestActions(currentRound, scheduledRequests)];
330
+ case 10:
331
+ _j.apply(_h, _k.concat([(_l.sent())]));
332
+ _l.label = 11;
333
+ case 11: return [2 /*return*/, actions];
334
+ }
335
+ });
336
+ });
337
+ };
338
+ MoonbeamAccountController.prototype.getScheduledLeaving = function (delegator) {
339
+ return __awaiter(this, void 0, void 0, function () {
340
+ var allDelegationScheduledRequests;
341
+ var _this = this;
342
+ return __generator(this, function (_a) {
343
+ switch (_a.label) {
344
+ case 0:
345
+ if (delegator.status instanceof MoonbeamDelegator_1.MoonbeamDelegatorStatusLeaving) {
346
+ return [2 /*return*/, delegator.status.roundIndex.value];
347
+ }
348
+ return [4 /*yield*/, Promise.all(delegator.delegations.elements.map(function (bond) {
349
+ var collator = v1_1.SubstrateEthAddress.from(bond.owner.address);
350
+ return _this.nodeClient.getDelegationScheduledRequests(collator);
351
+ }))];
352
+ case 1:
353
+ allDelegationScheduledRequests = _a.sent();
354
+ return [2 /*return*/, allDelegationScheduledRequests.reduce(function (acc, requests) {
355
+ var _a;
356
+ if (acc === undefined) {
357
+ return undefined;
358
+ }
359
+ var whenExecutable = (_a = requests === null || requests === void 0 ? void 0 : requests.requests.elements.find(function (request) {
360
+ request.delegator.asAddress() === delegator.id.asAddress() && request.action.type.value === MoonbeamDelegationScheduledRequests_1.MoonbeamDelegationActionRaw.REVOKE;
361
+ })) === null || _a === void 0 ? void 0 : _a.whenExecutable.value;
362
+ if (whenExecutable === undefined) {
363
+ return undefined;
364
+ }
365
+ if (acc.eq(whenExecutable) || acc.eq(-1)) {
366
+ return whenExecutable;
367
+ }
368
+ else {
369
+ return undefined;
370
+ }
371
+ }, new bignumber_1.default(-1))];
372
+ }
373
+ });
374
+ });
375
+ };
376
+ MoonbeamAccountController.prototype.getUndelegatedActions = function () {
199
377
  return [
200
378
  {
201
379
  type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.DELEGATE,
202
380
  args: ['candidate', 'amount']
203
381
  }
204
382
  ];
205
- }
206
- getDelegatedActions(isDelegatingCollator) {
207
- const actions = [];
383
+ };
384
+ MoonbeamAccountController.prototype.getDelegatedActions = function (isDelegatingCollator) {
385
+ var actions = [];
208
386
  actions.push({
209
387
  type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.SCHEDULE_UNDELEGATE_ALL
210
388
  });
@@ -221,70 +399,96 @@ class MoonbeamAccountController extends v1_1.SubstrateCommonAccountController {
221
399
  });
222
400
  }
223
401
  return actions;
224
- }
225
- async getLeavingActions(currentRound, scheduledLeaving) {
226
- if (currentRound.gte(scheduledLeaving)) {
227
- return [
228
- {
229
- type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.EXECUTE_UNDELEGATE_ALL
402
+ };
403
+ MoonbeamAccountController.prototype.getLeavingActions = function (currentRound, scheduledLeaving) {
404
+ return __awaiter(this, void 0, void 0, function () {
405
+ return __generator(this, function (_a) {
406
+ if (currentRound.gte(scheduledLeaving)) {
407
+ return [2 /*return*/, [
408
+ {
409
+ type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.EXECUTE_UNDELEGATE_ALL
410
+ }
411
+ ]];
230
412
  }
231
- ];
232
- }
233
- else {
234
- return [
235
- {
236
- type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.CANCEL_UNDELEGATE_ALL
413
+ else {
414
+ return [2 /*return*/, [
415
+ {
416
+ type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.CANCEL_UNDELEGATE_ALL
417
+ }
418
+ ]];
237
419
  }
238
- ];
239
- }
240
- }
241
- async getRequestActions(currentRound, scheduledRequests) {
242
- const actions = await Promise.all(scheduledRequests.map((request) => {
243
- if (request.action.type.value === MoonbeamDelegationScheduledRequests_1.MoonbeamDelegationActionRaw.REVOKE) {
244
- return this.getRevokeRequestActions(currentRound, request);
245
- }
246
- if (request.action.type.value === MoonbeamDelegationScheduledRequests_1.MoonbeamDelegationActionRaw.DECREASE) {
247
- return this.getDecreaseRequestActions(currentRound, request);
248
- }
249
- return [];
250
- }));
251
- return (0, array_1.flattenArray)(actions);
252
- }
253
- async getRevokeRequestActions(currentRound, scheduledRequest) {
254
- if (currentRound.gte(scheduledRequest.whenExecutable.value)) {
255
- return [
256
- {
257
- type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.EXECUTE_UNDELEGATE,
258
- args: ['candidate']
420
+ return [2 /*return*/];
421
+ });
422
+ });
423
+ };
424
+ MoonbeamAccountController.prototype.getRequestActions = function (currentRound, scheduledRequests) {
425
+ return __awaiter(this, void 0, void 0, function () {
426
+ var actions;
427
+ var _this = this;
428
+ return __generator(this, function (_a) {
429
+ switch (_a.label) {
430
+ case 0: return [4 /*yield*/, Promise.all(scheduledRequests.map(function (request) {
431
+ if (request.action.type.value === MoonbeamDelegationScheduledRequests_1.MoonbeamDelegationActionRaw.REVOKE) {
432
+ return _this.getRevokeRequestActions(currentRound, request);
433
+ }
434
+ if (request.action.type.value === MoonbeamDelegationScheduledRequests_1.MoonbeamDelegationActionRaw.DECREASE) {
435
+ return _this.getDecreaseRequestActions(currentRound, request);
436
+ }
437
+ return [];
438
+ }))];
439
+ case 1:
440
+ actions = _a.sent();
441
+ return [2 /*return*/, (0, array_1.flattenArray)(actions)];
259
442
  }
260
- ];
261
- }
262
- else {
263
- return [
264
- {
265
- type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.CANCEL_UNDELEGATE,
266
- args: ['candidate']
443
+ });
444
+ });
445
+ };
446
+ MoonbeamAccountController.prototype.getRevokeRequestActions = function (currentRound, scheduledRequest) {
447
+ return __awaiter(this, void 0, void 0, function () {
448
+ return __generator(this, function (_a) {
449
+ if (currentRound.gte(scheduledRequest.whenExecutable.value)) {
450
+ return [2 /*return*/, [
451
+ {
452
+ type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.EXECUTE_UNDELEGATE,
453
+ args: ['candidate']
454
+ }
455
+ ]];
267
456
  }
268
- ];
269
- }
270
- }
271
- async getDecreaseRequestActions(currentRound, scheduledRequest) {
272
- if (currentRound.gte(scheduledRequest.whenExecutable.value)) {
273
- return [
274
- {
275
- type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.EXECUTE_BOND_LESS,
276
- args: ['candidate']
457
+ else {
458
+ return [2 /*return*/, [
459
+ {
460
+ type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.CANCEL_UNDELEGATE,
461
+ args: ['candidate']
462
+ }
463
+ ]];
277
464
  }
278
- ];
279
- }
280
- else {
281
- return [
282
- {
283
- type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.CANCEL_BOND_LESS
465
+ return [2 /*return*/];
466
+ });
467
+ });
468
+ };
469
+ MoonbeamAccountController.prototype.getDecreaseRequestActions = function (currentRound, scheduledRequest) {
470
+ return __awaiter(this, void 0, void 0, function () {
471
+ return __generator(this, function (_a) {
472
+ if (currentRound.gte(scheduledRequest.whenExecutable.value)) {
473
+ return [2 /*return*/, [
474
+ {
475
+ type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.EXECUTE_BOND_LESS,
476
+ args: ['candidate']
477
+ }
478
+ ]];
284
479
  }
285
- ];
286
- }
287
- }
288
- }
480
+ else {
481
+ return [2 /*return*/, [
482
+ {
483
+ type: MoonbeamStakingActionType_1.MoonbeamStakingActionType.CANCEL_BOND_LESS
484
+ }
485
+ ]];
486
+ }
487
+ return [2 /*return*/];
488
+ });
489
+ });
490
+ };
491
+ return MoonbeamAccountController;
492
+ }(v1_1.SubstrateCommonAccountController));
289
493
  exports.MoonbeamAccountController = MoonbeamAccountController;
290
494
  //# sourceMappingURL=MoonbeamAccountController.js.map