@airgap/moonbeam 0.13.7-beta.11

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 (64) hide show
  1. package/LICENSE.md +7 -0
  2. package/airgap-coinlib-moonbeam.min.js +103205 -0
  3. package/index.d.ts +6 -0
  4. package/index.js +22 -0
  5. package/index.js.map +1 -0
  6. package/package.json +62 -0
  7. package/protocol/moonbase/MoonbaseProtocol.d.ts +17 -0
  8. package/protocol/moonbase/MoonbaseProtocol.js +73 -0
  9. package/protocol/moonbase/MoonbaseProtocol.js.map +1 -0
  10. package/protocol/moonbase/MoonbaseProtocolOptions.d.ts +21 -0
  11. package/protocol/moonbase/MoonbaseProtocolOptions.js +78 -0
  12. package/protocol/moonbase/MoonbaseProtocolOptions.js.map +1 -0
  13. package/protocol/moonbeam/MoonbeamProtocol.d.ts +55 -0
  14. package/protocol/moonbeam/MoonbeamProtocol.js +639 -0
  15. package/protocol/moonbeam/MoonbeamProtocol.js.map +1 -0
  16. package/protocol/moonbeam/MoonbeamProtocolOptions.d.ts +32 -0
  17. package/protocol/moonbeam/MoonbeamProtocolOptions.js +102 -0
  18. package/protocol/moonbeam/MoonbeamProtocolOptions.js.map +1 -0
  19. package/protocol/moonbeam/controllers/MoonbeamAccountController.d.ts +28 -0
  20. package/protocol/moonbeam/controllers/MoonbeamAccountController.js +543 -0
  21. package/protocol/moonbeam/controllers/MoonbeamAccountController.js.map +1 -0
  22. package/protocol/moonbeam/controllers/MoonbeamTransactionController.d.ts +9 -0
  23. package/protocol/moonbeam/controllers/MoonbeamTransactionController.js +73 -0
  24. package/protocol/moonbeam/controllers/MoonbeamTransactionController.js.map +1 -0
  25. package/protocol/moonbeam/data/staking/MoonbeamBond.d.ts +13 -0
  26. package/protocol/moonbeam/data/staking/MoonbeamBond.js +41 -0
  27. package/protocol/moonbeam/data/staking/MoonbeamBond.js.map +1 -0
  28. package/protocol/moonbeam/data/staking/MoonbeamCandidateBondLessRequest.d.ts +11 -0
  29. package/protocol/moonbeam/data/staking/MoonbeamCandidateBondLessRequest.js +41 -0
  30. package/protocol/moonbeam/data/staking/MoonbeamCandidateBondLessRequest.js.map +1 -0
  31. package/protocol/moonbeam/data/staking/MoonbeamCandidateMetadata.d.ts +28 -0
  32. package/protocol/moonbeam/data/staking/MoonbeamCandidateMetadata.js +49 -0
  33. package/protocol/moonbeam/data/staking/MoonbeamCandidateMetadata.js.map +1 -0
  34. package/protocol/moonbeam/data/staking/MoonbeamCollatorDetails.d.ts +9 -0
  35. package/protocol/moonbeam/data/staking/MoonbeamCollatorDetails.js +3 -0
  36. package/protocol/moonbeam/data/staking/MoonbeamCollatorDetails.js.map +1 -0
  37. package/protocol/moonbeam/data/staking/MoonbeamDelegationDetails.d.ts +13 -0
  38. package/protocol/moonbeam/data/staking/MoonbeamDelegationDetails.js +3 -0
  39. package/protocol/moonbeam/data/staking/MoonbeamDelegationDetails.js.map +1 -0
  40. package/protocol/moonbeam/data/staking/MoonbeamDelegationScheduledRequests.d.ts +46 -0
  41. package/protocol/moonbeam/data/staking/MoonbeamDelegationScheduledRequests.js +144 -0
  42. package/protocol/moonbeam/data/staking/MoonbeamDelegationScheduledRequests.js.map +1 -0
  43. package/protocol/moonbeam/data/staking/MoonbeamDelegator.d.ts +40 -0
  44. package/protocol/moonbeam/data/staking/MoonbeamDelegator.js +128 -0
  45. package/protocol/moonbeam/data/staking/MoonbeamDelegator.js.map +1 -0
  46. package/protocol/moonbeam/data/staking/MoonbeamDelegatorDetails.d.ts +5 -0
  47. package/protocol/moonbeam/data/staking/MoonbeamDelegatorDetails.js +3 -0
  48. package/protocol/moonbeam/data/staking/MoonbeamDelegatorDetails.js.map +1 -0
  49. package/protocol/moonbeam/data/staking/MoonbeamRoundInfo.d.ts +8 -0
  50. package/protocol/moonbeam/data/staking/MoonbeamRoundInfo.js +22 -0
  51. package/protocol/moonbeam/data/staking/MoonbeamRoundInfo.js.map +1 -0
  52. package/protocol/moonbeam/data/staking/MoonbeamStakingActionType.d.ts +13 -0
  53. package/protocol/moonbeam/data/staking/MoonbeamStakingActionType.js +18 -0
  54. package/protocol/moonbeam/data/staking/MoonbeamStakingActionType.js.map +1 -0
  55. package/protocol/moonbeam/node/MoonbeamNodeClient.d.ts +26 -0
  56. package/protocol/moonbeam/node/MoonbeamNodeClient.js +218 -0
  57. package/protocol/moonbeam/node/MoonbeamNodeClient.js.map +1 -0
  58. package/protocol/moonriver/MoonriverProtocol.d.ts +17 -0
  59. package/protocol/moonriver/MoonriverProtocol.js +73 -0
  60. package/protocol/moonriver/MoonriverProtocol.js.map +1 -0
  61. package/protocol/moonriver/MoonriverProtocolOptions.d.ts +21 -0
  62. package/protocol/moonriver/MoonriverProtocolOptions.js +78 -0
  63. package/protocol/moonriver/MoonriverProtocolOptions.js.map +1 -0
  64. package/readme.md +121 -0
@@ -0,0 +1,639 @@
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
+ extendStatics(d, b);
11
+ function __() { this.constructor = d; }
12
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
+ };
14
+ })();
15
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
16
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
17
+ return new (P || (P = Promise))(function (resolve, reject) {
18
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
19
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
20
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
21
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
22
+ });
23
+ };
24
+ var __generator = (this && this.__generator) || function (thisArg, body) {
25
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
26
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
27
+ function verb(n) { return function (v) { return step([n, v]); }; }
28
+ function step(op) {
29
+ if (f) throw new TypeError("Generator is already executing.");
30
+ while (_) try {
31
+ 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;
32
+ if (y = 0, t) op = [op[0] & 2, t.value];
33
+ switch (op[0]) {
34
+ case 0: case 1: t = op; break;
35
+ case 4: _.label++; return { value: op[1], done: false };
36
+ case 5: _.label++; y = op[1]; op = [0]; continue;
37
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
38
+ default:
39
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
40
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
41
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
42
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
43
+ if (t[2]) _.ops.pop();
44
+ _.trys.pop(); continue;
45
+ }
46
+ op = body.call(thisArg, _);
47
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
48
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
49
+ }
50
+ };
51
+ Object.defineProperty(exports, "__esModule", { value: true });
52
+ exports.MoonbeamProtocol = void 0;
53
+ var bignumber_1 = require("@airgap/coinlib-core/dependencies/src/bignumber.js-9.0.0/bignumber");
54
+ var errors_1 = require("@airgap/coinlib-core/errors");
55
+ var coinlib_error_1 = require("@airgap/coinlib-core/errors/coinlib-error");
56
+ var assert_1 = require("@airgap/coinlib-core/utils/assert");
57
+ var ProtocolSymbols_1 = require("@airgap/coinlib-core/utils/ProtocolSymbols");
58
+ var SubstrateTransaction_1 = require("@airgap/substrate/protocol/common/data/transaction/SubstrateTransaction");
59
+ var MoonbeamAddress_1 = require("@airgap/substrate/protocol/moonbeam/MoonbeamAddress");
60
+ var SubstrateDelegateProtocol_1 = require("@airgap/substrate/protocol/SubstrateDelegateProtocol");
61
+ var MoonbeamStakingActionType_1 = require("./data/staking/MoonbeamStakingActionType");
62
+ var MoonbeamProtocolOptions_1 = require("./MoonbeamProtocolOptions");
63
+ var MoonbeamProtocol = /** @class */ (function (_super) {
64
+ __extends(MoonbeamProtocol, _super);
65
+ function MoonbeamProtocol(options) {
66
+ if (options === void 0) { options = new MoonbeamProtocolOptions_1.MoonbeamProtocolOptions(new MoonbeamProtocolOptions_1.MoonbeamProtocolNetwork()); }
67
+ var _this = _super.call(this, options) || this;
68
+ _this.options = options;
69
+ _this.symbol = 'GLMR';
70
+ _this.name = 'Moonbeam';
71
+ _this.marketSymbol = 'GLMR';
72
+ _this.feeSymbol = 'GLMR';
73
+ _this.decimals = 18;
74
+ _this.feeDecimals = 18;
75
+ _this.identifier = ProtocolSymbols_1.MainProtocolSymbols.MOONBEAM;
76
+ _this.feeDefaults = {
77
+ low: '0.000000000125',
78
+ medium: '0.000000000125',
79
+ high: '0.000000000125'
80
+ };
81
+ _this.units = [
82
+ {
83
+ unitSymbol: 'GLMR',
84
+ factor: '1'
85
+ },
86
+ {
87
+ unitSymbol: 'mGLMR',
88
+ factor: '0.001'
89
+ },
90
+ {
91
+ unitSymbol: 'uGLMR',
92
+ factor: '0.000001'
93
+ },
94
+ {
95
+ unitSymbol: 'GWEI',
96
+ factor: '0.000000001'
97
+ },
98
+ {
99
+ unitSymbol: 'MWEI',
100
+ factor: '0.000000000001'
101
+ },
102
+ {
103
+ unitSymbol: 'kWEI',
104
+ factor: '0.000000000000001'
105
+ },
106
+ {
107
+ unitSymbol: 'WEI',
108
+ factor: '0.000000000000000001'
109
+ }
110
+ ];
111
+ _this.standardDerivationPath = "m/44'/60'/0'/0/0";
112
+ _this.addressIsCaseSensitive = false;
113
+ _this.addressValidationPattern = '^0x[a-fA-F0-9]{40}$';
114
+ _this.addressPlaceholder = "0xabc...";
115
+ return _this;
116
+ }
117
+ MoonbeamProtocol.prototype.getDefaultDelegatee = function () {
118
+ return __awaiter(this, void 0, void 0, function () {
119
+ var collators;
120
+ return __generator(this, function (_a) {
121
+ switch (_a.label) {
122
+ case 0:
123
+ if (this.defaultValidator) {
124
+ return [2 /*return*/, this.defaultValidator];
125
+ }
126
+ return [4 /*yield*/, this.options.nodeClient.getCollators()];
127
+ case 1:
128
+ collators = _a.sent();
129
+ return [2 /*return*/, collators ? collators[0].asString() : ''];
130
+ }
131
+ });
132
+ });
133
+ };
134
+ MoonbeamProtocol.prototype.getCurrentDelegateesForPublicKey = function (publicKey) {
135
+ return __awaiter(this, void 0, void 0, function () {
136
+ return __generator(this, function (_a) {
137
+ return [2 /*return*/, this.options.accountController.getCurrentCollators(publicKey)];
138
+ });
139
+ });
140
+ };
141
+ MoonbeamProtocol.prototype.getCurrentDelegateesForAddress = function (address) {
142
+ return __awaiter(this, void 0, void 0, function () {
143
+ return __generator(this, function (_a) {
144
+ return [2 /*return*/, this.options.accountController.getCurrentCollators(address)];
145
+ });
146
+ });
147
+ };
148
+ MoonbeamProtocol.prototype.getDelegateeDetails = function (address) {
149
+ var _a, _b;
150
+ return __awaiter(this, void 0, void 0, function () {
151
+ var collatorDetails;
152
+ return __generator(this, function (_c) {
153
+ switch (_c.label) {
154
+ case 0: return [4 /*yield*/, this.options.accountController.getCollatorDetails(address)];
155
+ case 1:
156
+ collatorDetails = _c.sent();
157
+ return [2 /*return*/, {
158
+ name: (_a = collatorDetails.name) !== null && _a !== void 0 ? _a : '',
159
+ status: (_b = collatorDetails.status) !== null && _b !== void 0 ? _b : '',
160
+ address: collatorDetails.address
161
+ }];
162
+ }
163
+ });
164
+ });
165
+ };
166
+ MoonbeamProtocol.prototype.isPublicKeyDelegating = function (publicKey) {
167
+ return __awaiter(this, void 0, void 0, function () {
168
+ return __generator(this, function (_a) {
169
+ return [2 /*return*/, this.options.accountController.isDelegating(publicKey)];
170
+ });
171
+ });
172
+ };
173
+ MoonbeamProtocol.prototype.isAddressDelegating = function (address) {
174
+ return __awaiter(this, void 0, void 0, function () {
175
+ return __generator(this, function (_a) {
176
+ return [2 /*return*/, this.options.accountController.isDelegating(address)];
177
+ });
178
+ });
179
+ };
180
+ MoonbeamProtocol.prototype.getDelegatorDetailsFromPublicKey = function (publicKey) {
181
+ return __awaiter(this, void 0, void 0, function () {
182
+ return __generator(this, function (_a) {
183
+ return [2 /*return*/, this.options.accountController.getDelegatorDetails(publicKey)];
184
+ });
185
+ });
186
+ };
187
+ MoonbeamProtocol.prototype.getDelegatorDetailsFromAddress = function (address) {
188
+ return __awaiter(this, void 0, void 0, function () {
189
+ return __generator(this, function (_a) {
190
+ return [2 /*return*/, this.options.accountController.getDelegatorDetails(address)];
191
+ });
192
+ });
193
+ };
194
+ MoonbeamProtocol.prototype.getDelegationDetailsFromPublicKey = function (publicKey, delegatees) {
195
+ return __awaiter(this, void 0, void 0, function () {
196
+ var address;
197
+ return __generator(this, function (_a) {
198
+ switch (_a.label) {
199
+ case 0: return [4 /*yield*/, this.getAddressFromPublicKey(publicKey)];
200
+ case 1:
201
+ address = _a.sent();
202
+ return [2 /*return*/, this.getDelegationDetailsFromAddress(address.address, delegatees)];
203
+ }
204
+ });
205
+ });
206
+ };
207
+ MoonbeamProtocol.prototype.getDelegationDetailsFromAddress = function (address, delegatees) {
208
+ return __awaiter(this, void 0, void 0, function () {
209
+ var collator, delegationDetails;
210
+ return __generator(this, function (_a) {
211
+ switch (_a.label) {
212
+ case 0:
213
+ if (delegatees.length > 1) {
214
+ throw new errors_1.UnsupportedError(coinlib_error_1.Domain.SUBSTRATE, 'Multiple validators for a single delegation are not supported');
215
+ }
216
+ collator = delegatees[0];
217
+ return [4 /*yield*/, this.options.accountController.getDelegationDetails(address, collator)];
218
+ case 1:
219
+ delegationDetails = _a.sent();
220
+ return [2 /*return*/, {
221
+ delegator: delegationDetails.delegatorDetails,
222
+ delegatees: [delegationDetails.collatorDetails]
223
+ }];
224
+ }
225
+ });
226
+ });
227
+ };
228
+ MoonbeamProtocol.prototype.prepareDelegatorActionFromPublicKey = function (publicKey, type, data) {
229
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
230
+ return __awaiter(this, void 0, void 0, function () {
231
+ return __generator(this, function (_m) {
232
+ if (!data) {
233
+ data = {};
234
+ }
235
+ switch (type) {
236
+ case MoonbeamStakingActionType_1.MoonbeamStakingActionType.DELEGATE:
237
+ assert_1.assertFields(MoonbeamStakingActionType_1.MoonbeamStakingActionType[type] + " action", data, 'candidate', 'amount');
238
+ return [2 /*return*/, this.prepareDelegation(publicKey, (_a = data.tip) !== null && _a !== void 0 ? _a : 0, data.candidate, data.amount)];
239
+ case MoonbeamStakingActionType_1.MoonbeamStakingActionType.BOND_MORE:
240
+ assert_1.assertFields(MoonbeamStakingActionType_1.MoonbeamStakingActionType[type] + " action", data, 'candidate', 'more');
241
+ return [2 /*return*/, this.prepareDelegatorBondMore(publicKey, (_b = data.tip) !== null && _b !== void 0 ? _b : 0, data.candidate, data.more)];
242
+ case MoonbeamStakingActionType_1.MoonbeamStakingActionType.SCHEDULE_BOND_LESS:
243
+ assert_1.assertFields(MoonbeamStakingActionType_1.MoonbeamStakingActionType[type] + " action", data, 'candidate', 'less');
244
+ return [2 /*return*/, this.prepareScheduleDelegatorBondLess(publicKey, (_c = data.tip) !== null && _c !== void 0 ? _c : 0, data.candidate, data.less)];
245
+ case MoonbeamStakingActionType_1.MoonbeamStakingActionType.EXECUTE_BOND_LESS:
246
+ assert_1.assertFields(MoonbeamStakingActionType_1.MoonbeamStakingActionType[type] + " action", data, 'candidate');
247
+ return [2 /*return*/, this.prepareExecuteDelegatorBondLess(publicKey, (_d = data.tip) !== null && _d !== void 0 ? _d : 0, data.candidate)];
248
+ case MoonbeamStakingActionType_1.MoonbeamStakingActionType.CANCEL_BOND_LESS:
249
+ assert_1.assertFields(MoonbeamStakingActionType_1.MoonbeamStakingActionType[type] + " action", data, 'candidate');
250
+ return [2 /*return*/, this.prepareCancelDelegatorBondLess(publicKey, (_e = data.tip) !== null && _e !== void 0 ? _e : 0, data.candidate)];
251
+ case MoonbeamStakingActionType_1.MoonbeamStakingActionType.SCHEDULE_UNDELEGATE:
252
+ assert_1.assertFields(MoonbeamStakingActionType_1.MoonbeamStakingActionType[type] + " action", data, 'collator');
253
+ return [2 /*return*/, this.prepareScheduleUndelegate(publicKey, (_f = data.tip) !== null && _f !== void 0 ? _f : 0, data.collator)];
254
+ case MoonbeamStakingActionType_1.MoonbeamStakingActionType.EXECUTE_UNDELEGATE:
255
+ assert_1.assertFields(MoonbeamStakingActionType_1.MoonbeamStakingActionType[type] + " action", data, 'candidate');
256
+ return [2 /*return*/, this.prepareExecuteUndelegate(publicKey, (_g = data.tip) !== null && _g !== void 0 ? _g : 0, data.candidate)];
257
+ case MoonbeamStakingActionType_1.MoonbeamStakingActionType.CANCEL_UNDELEGATE:
258
+ assert_1.assertFields(MoonbeamStakingActionType_1.MoonbeamStakingActionType[type] + " action", data, 'candidate');
259
+ return [2 /*return*/, this.prepareCancelUndelegate(publicKey, (_h = data.tip) !== null && _h !== void 0 ? _h : 0, data.candidate)];
260
+ case MoonbeamStakingActionType_1.MoonbeamStakingActionType.SCHEDULE_UNDELEGATE_ALL:
261
+ return [2 /*return*/, this.prepareScheduleUndelegateAll(publicKey, (_j = data.tip) !== null && _j !== void 0 ? _j : 0)];
262
+ case MoonbeamStakingActionType_1.MoonbeamStakingActionType.EXECUTE_UNDELEGATE_ALL:
263
+ return [2 /*return*/, this.prepareExecuteUndelegateAll(publicKey, (_k = data.tip) !== null && _k !== void 0 ? _k : 0)];
264
+ case MoonbeamStakingActionType_1.MoonbeamStakingActionType.CANCEL_UNDELEGATE_ALL:
265
+ return [2 /*return*/, this.prepareCancelUndelegateAll(publicKey, (_l = data.tip) !== null && _l !== void 0 ? _l : 0)];
266
+ default:
267
+ throw new errors_1.UnsupportedError(coinlib_error_1.Domain.SUBSTRATE, 'Unsupported delegator action.');
268
+ }
269
+ return [2 /*return*/];
270
+ });
271
+ });
272
+ };
273
+ MoonbeamProtocol.prototype.prepareDelegation = function (publicKey, tip, candidate, amount) {
274
+ return __awaiter(this, void 0, void 0, function () {
275
+ var requestedAmount, minAmount, results, delegatorDetails, collatorDetails, maxDelegations, balance, available, encoded;
276
+ return __generator(this, function (_a) {
277
+ switch (_a.label) {
278
+ case 0:
279
+ requestedAmount = new bignumber_1.BigNumber(amount);
280
+ return [4 /*yield*/, this.getMinDelegationAmount(publicKey)];
281
+ case 1:
282
+ minAmount = _a.sent();
283
+ if (requestedAmount.lt(minAmount)) {
284
+ throw new errors_1.ConditionViolationError(coinlib_error_1.Domain.SUBSTRATE, "The amount is too low, it has to be at least " + minAmount.toString());
285
+ }
286
+ return [4 /*yield*/, Promise.all([
287
+ this.options.accountController.getDelegatorDetails(publicKey),
288
+ this.options.accountController.getCollatorDetails(candidate),
289
+ this.options.nodeClient.getMaxDelegationsPerDelegator(),
290
+ this.getBalanceOfPublicKey(publicKey)
291
+ ])];
292
+ case 2:
293
+ results = _a.sent();
294
+ delegatorDetails = results[0];
295
+ collatorDetails = results[1];
296
+ maxDelegations = results[2];
297
+ balance = results[3];
298
+ if (maxDelegations === null || maxDelegations === void 0 ? void 0 : maxDelegations.lte(delegatorDetails.delegatees.length)) {
299
+ throw new errors_1.ConditionViolationError(coinlib_error_1.Domain.SUBSTRATE, 'This delegator cannot nominate more collators.');
300
+ }
301
+ available = new bignumber_1.BigNumber(balance).minus(amount);
302
+ return [4 /*yield*/, this.options.transactionController.prepareSubmittableTransactions(publicKey, available, [
303
+ {
304
+ type: SubstrateTransaction_1.SubstrateTransactionType.M_DELEGATE,
305
+ tip: tip,
306
+ args: {
307
+ candidate: candidate,
308
+ amount: new bignumber_1.BigNumber(amount),
309
+ candidateDelegationCount: collatorDetails.delegators,
310
+ delegationCount: delegatorDetails.delegatees.length
311
+ }
312
+ }
313
+ ])];
314
+ case 3:
315
+ encoded = _a.sent();
316
+ return [2 /*return*/, [{ encoded: encoded }]];
317
+ }
318
+ });
319
+ });
320
+ };
321
+ MoonbeamProtocol.prototype.prepareScheduleUndelegate = function (publicKey, tip, collator) {
322
+ return __awaiter(this, void 0, void 0, function () {
323
+ var balance, encoded;
324
+ return __generator(this, function (_a) {
325
+ switch (_a.label) {
326
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
327
+ case 1:
328
+ balance = _a.sent();
329
+ return [4 /*yield*/, this.options.transactionController.prepareSubmittableTransactions(publicKey, balance, [
330
+ {
331
+ type: SubstrateTransaction_1.SubstrateTransactionType.M_SCHEDULE_REVOKE_DELGATION,
332
+ tip: tip,
333
+ args: {
334
+ collator: collator
335
+ }
336
+ }
337
+ ])];
338
+ case 2:
339
+ encoded = _a.sent();
340
+ return [2 /*return*/, [{ encoded: encoded }]];
341
+ }
342
+ });
343
+ });
344
+ };
345
+ MoonbeamProtocol.prototype.prepareExecuteUndelegate = function (publicKey, tip, candidate) {
346
+ return __awaiter(this, void 0, void 0, function () {
347
+ return __generator(this, function (_a) {
348
+ return [2 /*return*/, this.prepareExecuteDelegationRequest(publicKey, tip, candidate)];
349
+ });
350
+ });
351
+ };
352
+ MoonbeamProtocol.prototype.prepareCancelUndelegate = function (publicKey, tip, candidate) {
353
+ return __awaiter(this, void 0, void 0, function () {
354
+ return __generator(this, function (_a) {
355
+ return [2 /*return*/, this.prepareCancelDelegationRequest(publicKey, tip, candidate)];
356
+ });
357
+ });
358
+ };
359
+ MoonbeamProtocol.prototype.prepareScheduleUndelegateAll = function (publicKey, tip) {
360
+ return __awaiter(this, void 0, void 0, function () {
361
+ var balance, encoded;
362
+ return __generator(this, function (_a) {
363
+ switch (_a.label) {
364
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
365
+ case 1:
366
+ balance = _a.sent();
367
+ return [4 /*yield*/, this.options.transactionController.prepareSubmittableTransactions(publicKey, balance, [
368
+ {
369
+ type: SubstrateTransaction_1.SubstrateTransactionType.M_SCHEDULE_LEAVE_DELEGATORS,
370
+ tip: tip,
371
+ args: {}
372
+ }
373
+ ])];
374
+ case 2:
375
+ encoded = _a.sent();
376
+ return [2 /*return*/, [{ encoded: encoded }]];
377
+ }
378
+ });
379
+ });
380
+ };
381
+ MoonbeamProtocol.prototype.prepareExecuteUndelegateAll = function (publicKey, tip) {
382
+ return __awaiter(this, void 0, void 0, function () {
383
+ var results, delegatorDetails, balance, delegator, encoded;
384
+ return __generator(this, function (_a) {
385
+ switch (_a.label) {
386
+ case 0: return [4 /*yield*/, Promise.all([
387
+ this.options.accountController.getDelegatorDetails(publicKey),
388
+ this.getBalanceOfPublicKey(publicKey),
389
+ this.getAddressFromPublicKey(publicKey)
390
+ ])];
391
+ case 1:
392
+ results = _a.sent();
393
+ delegatorDetails = results[0];
394
+ balance = results[1];
395
+ delegator = results[2].address;
396
+ return [4 /*yield*/, this.options.transactionController.prepareSubmittableTransactions(publicKey, balance, [
397
+ {
398
+ type: SubstrateTransaction_1.SubstrateTransactionType.M_EXECUTE_LEAVE_DELEGATORS,
399
+ tip: tip,
400
+ args: {
401
+ delegator: delegator,
402
+ delegationCount: delegatorDetails.delegatees.length
403
+ }
404
+ }
405
+ ])];
406
+ case 2:
407
+ encoded = _a.sent();
408
+ return [2 /*return*/, [{ encoded: encoded }]];
409
+ }
410
+ });
411
+ });
412
+ };
413
+ MoonbeamProtocol.prototype.prepareCancelUndelegateAll = function (publicKey, tip) {
414
+ return __awaiter(this, void 0, void 0, function () {
415
+ var balance, encoded;
416
+ return __generator(this, function (_a) {
417
+ switch (_a.label) {
418
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
419
+ case 1:
420
+ balance = _a.sent();
421
+ return [4 /*yield*/, this.options.transactionController.prepareSubmittableTransactions(publicKey, balance, [
422
+ {
423
+ type: SubstrateTransaction_1.SubstrateTransactionType.M_CANCEL_LEAVE_DELEGATORS,
424
+ tip: tip,
425
+ args: {}
426
+ }
427
+ ])];
428
+ case 2:
429
+ encoded = _a.sent();
430
+ return [2 /*return*/, [{ encoded: encoded }]];
431
+ }
432
+ });
433
+ });
434
+ };
435
+ MoonbeamProtocol.prototype.prepareDelegatorBondMore = function (publicKey, tip, candidate, more) {
436
+ return __awaiter(this, void 0, void 0, function () {
437
+ var balance, available, encoded;
438
+ return __generator(this, function (_a) {
439
+ switch (_a.label) {
440
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
441
+ case 1:
442
+ balance = _a.sent();
443
+ available = new bignumber_1.BigNumber(balance).minus(more);
444
+ return [4 /*yield*/, this.options.transactionController.prepareSubmittableTransactions(publicKey, available, [
445
+ {
446
+ type: SubstrateTransaction_1.SubstrateTransactionType.M_DELEGATOR_BOND_MORE,
447
+ tip: tip,
448
+ args: {
449
+ candidate: candidate,
450
+ more: new bignumber_1.BigNumber(more)
451
+ }
452
+ }
453
+ ])];
454
+ case 2:
455
+ encoded = _a.sent();
456
+ return [2 /*return*/, [{ encoded: encoded }]];
457
+ }
458
+ });
459
+ });
460
+ };
461
+ MoonbeamProtocol.prototype.prepareScheduleDelegatorBondLess = function (publicKey, tip, candidate, less) {
462
+ return __awaiter(this, void 0, void 0, function () {
463
+ var _a, balance, delegationDetails, bondAmount, requestedAmount, encoded;
464
+ return __generator(this, function (_b) {
465
+ switch (_b.label) {
466
+ case 0: return [4 /*yield*/, Promise.all([
467
+ this.getBalanceOfPublicKey(publicKey),
468
+ this.options.accountController.getDelegationDetails(publicKey, candidate)
469
+ ])];
470
+ case 1:
471
+ _a = _b.sent(), balance = _a[0], delegationDetails = _a[1];
472
+ bondAmount = new bignumber_1.BigNumber(delegationDetails.bond);
473
+ requestedAmount = new bignumber_1.BigNumber(less);
474
+ if (requestedAmount.gt(bondAmount)) {
475
+ throw new errors_1.ConditionViolationError(coinlib_error_1.Domain.SUBSTRATE, 'Bond less amount too high');
476
+ }
477
+ else if (requestedAmount.eq(bondAmount)) {
478
+ return [2 /*return*/, this.prepareScheduleUndelegate(publicKey, tip, candidate)];
479
+ }
480
+ return [4 /*yield*/, this.options.transactionController.prepareSubmittableTransactions(publicKey, balance, [
481
+ {
482
+ type: SubstrateTransaction_1.SubstrateTransactionType.M_SCHEDULE_DELEGATOR_BOND_LESS,
483
+ tip: tip,
484
+ args: {
485
+ candidate: candidate,
486
+ less: requestedAmount
487
+ }
488
+ }
489
+ ])];
490
+ case 2:
491
+ encoded = _b.sent();
492
+ return [2 /*return*/, [{ encoded: encoded }]];
493
+ }
494
+ });
495
+ });
496
+ };
497
+ MoonbeamProtocol.prototype.prepareExecuteDelegatorBondLess = function (publicKey, tip, candidate) {
498
+ return __awaiter(this, void 0, void 0, function () {
499
+ return __generator(this, function (_a) {
500
+ return [2 /*return*/, this.prepareExecuteDelegationRequest(publicKey, tip, candidate)];
501
+ });
502
+ });
503
+ };
504
+ MoonbeamProtocol.prototype.prepareCancelDelegatorBondLess = function (publicKey, tip, candidate) {
505
+ return __awaiter(this, void 0, void 0, function () {
506
+ return __generator(this, function (_a) {
507
+ return [2 /*return*/, this.prepareCancelDelegationRequest(publicKey, tip, candidate)];
508
+ });
509
+ });
510
+ };
511
+ MoonbeamProtocol.prototype.prepareExecuteDelegationRequest = function (publicKey, tip, candidate) {
512
+ return __awaiter(this, void 0, void 0, function () {
513
+ var results, balance, delegator, encoded;
514
+ return __generator(this, function (_a) {
515
+ switch (_a.label) {
516
+ case 0: return [4 /*yield*/, Promise.all([this.getBalanceOfPublicKey(publicKey), this.getAddressFromPublicKey(publicKey)])];
517
+ case 1:
518
+ results = _a.sent();
519
+ balance = results[0];
520
+ delegator = results[1].address;
521
+ return [4 /*yield*/, this.options.transactionController.prepareSubmittableTransactions(publicKey, balance, [
522
+ {
523
+ type: SubstrateTransaction_1.SubstrateTransactionType.M_EXECUTE_DELGATION_REQUEST,
524
+ tip: tip,
525
+ args: {
526
+ delegator: delegator,
527
+ candidate: candidate
528
+ }
529
+ }
530
+ ])];
531
+ case 2:
532
+ encoded = _a.sent();
533
+ return [2 /*return*/, [{ encoded: encoded }]];
534
+ }
535
+ });
536
+ });
537
+ };
538
+ MoonbeamProtocol.prototype.prepareCancelDelegationRequest = function (publicKey, tip, candidate) {
539
+ return __awaiter(this, void 0, void 0, function () {
540
+ var balance, encoded;
541
+ return __generator(this, function (_a) {
542
+ switch (_a.label) {
543
+ case 0: return [4 /*yield*/, this.getBalanceOfPublicKey(publicKey)];
544
+ case 1:
545
+ balance = _a.sent();
546
+ return [4 /*yield*/, this.options.transactionController.prepareSubmittableTransactions(publicKey, balance, [
547
+ {
548
+ type: SubstrateTransaction_1.SubstrateTransactionType.M_CANCEL_DELEGATION_REQUEST,
549
+ tip: tip,
550
+ args: {
551
+ candidate: candidate
552
+ }
553
+ }
554
+ ])];
555
+ case 2:
556
+ encoded = _a.sent();
557
+ return [2 /*return*/, [{ encoded: encoded }]];
558
+ }
559
+ });
560
+ });
561
+ };
562
+ MoonbeamProtocol.prototype.getMinDelegationAmount = function (accountId) {
563
+ return __awaiter(this, void 0, void 0, function () {
564
+ return __generator(this, function (_a) {
565
+ return [2 /*return*/, this.options.accountController.getMinDelegationAmount(accountId)];
566
+ });
567
+ });
568
+ };
569
+ MoonbeamProtocol.prototype.getFutureRequiredTransactions = function (accountId, intention) {
570
+ return __awaiter(this, void 0, void 0, function () {
571
+ var results, isDelegating, transferableBalance, stakingBalance, requiredTransactions;
572
+ return __generator(this, function (_a) {
573
+ switch (_a.label) {
574
+ case 0: return [4 /*yield*/, Promise.all([
575
+ this.options.accountController.isDelegating(accountId),
576
+ this.options.accountController.getTransferableBalance(accountId),
577
+ this.options.accountController.getTransferableBalance(accountId, false, false)
578
+ ])];
579
+ case 1:
580
+ results = _a.sent();
581
+ isDelegating = results[0];
582
+ transferableBalance = results[1];
583
+ stakingBalance = results[2];
584
+ requiredTransactions = [];
585
+ if (intention === 'transfer') {
586
+ requiredTransactions.push([
587
+ SubstrateTransaction_1.SubstrateTransactionType.TRANSFER,
588
+ {
589
+ to: MoonbeamAddress_1.MoonbeamAddress.getPlaceholder(),
590
+ value: transferableBalance
591
+ }
592
+ ]);
593
+ }
594
+ if (!isDelegating && intention === 'delegate') {
595
+ // not delegated
596
+ requiredTransactions.push([
597
+ SubstrateTransaction_1.SubstrateTransactionType.M_DELEGATE,
598
+ {
599
+ candidate: MoonbeamAddress_1.MoonbeamAddress.getPlaceholder(),
600
+ amount: stakingBalance,
601
+ candidateDelegationCount: 0,
602
+ delegationCount: 0
603
+ }
604
+ ], [
605
+ SubstrateTransaction_1.SubstrateTransactionType.M_SCHEDULE_REVOKE_DELGATION,
606
+ {
607
+ collator: MoonbeamAddress_1.MoonbeamAddress.getPlaceholder()
608
+ }
609
+ ], [
610
+ SubstrateTransaction_1.SubstrateTransactionType.M_EXECUTE_DELGATION_REQUEST,
611
+ {
612
+ delegator: MoonbeamAddress_1.MoonbeamAddress.getPlaceholder(),
613
+ candidate: MoonbeamAddress_1.MoonbeamAddress.getPlaceholder()
614
+ }
615
+ ]);
616
+ }
617
+ if (isDelegating && intention === 'delegate') {
618
+ requiredTransactions.push([
619
+ SubstrateTransaction_1.SubstrateTransactionType.M_SCHEDULE_REVOKE_DELGATION,
620
+ {
621
+ collator: MoonbeamAddress_1.MoonbeamAddress.getPlaceholder()
622
+ }
623
+ ], [
624
+ SubstrateTransaction_1.SubstrateTransactionType.M_EXECUTE_DELGATION_REQUEST,
625
+ {
626
+ delegator: MoonbeamAddress_1.MoonbeamAddress.getPlaceholder(),
627
+ candidate: MoonbeamAddress_1.MoonbeamAddress.getPlaceholder()
628
+ }
629
+ ]);
630
+ }
631
+ return [2 /*return*/, requiredTransactions];
632
+ }
633
+ });
634
+ });
635
+ };
636
+ return MoonbeamProtocol;
637
+ }(SubstrateDelegateProtocol_1.SubstrateDelegateProtocol));
638
+ exports.MoonbeamProtocol = MoonbeamProtocol;
639
+ //# sourceMappingURL=MoonbeamProtocol.js.map