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