UNPKG

airgap-coin-lib

Version:

The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.

632 lines 38.6 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { 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; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); var wasm_crypto_1 = require("@polkadot/wasm-crypto"); var bignumber_1 = require("../../../dependencies/src/bignumber.js-9.0.0/bignumber"); var sr25519_1 = require("../../../utils/sr25519"); var SubstrateAddress_1 = require("./data/account/SubstrateAddress"); var SubstrateEraElectionStatus_1 = require("./data/staking/SubstrateEraElectionStatus"); var SubstrateStakingActionType_1 = require("./data/staking/SubstrateStakingActionType"); var SubstrateAccountController = /** @class */ (function () { function SubstrateAccountController(network, nodeClient) { this.network = network; this.nodeClient = nodeClient; } SubstrateAccountController.prototype.createKeyPairFromMnemonic = function (mnemonic, derivationPath, password) { return __awaiter(this, void 0, void 0, function () { var secret; return __generator(this, function (_a) { secret = wasm_crypto_1.bip39ToMiniSecret(mnemonic, password || ''); return [2 /*return*/, this.createKeyPairFromHexSecret(Buffer.from(secret).toString('hex'), derivationPath)]; }); }); }; SubstrateAccountController.prototype.createKeyPairFromHexSecret = function (secret, derivationPath) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, sr25519_1.createSr25519KeyPair(secret, derivationPath)]; }); }); }; SubstrateAccountController.prototype.createAddressFromPublicKey = function (publicKey) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, SubstrateAddress_1.SubstrateAddress.from(publicKey, this.network).toString()]; }); }); }; SubstrateAccountController.prototype.getBalance = function (accountId) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var accountInfo; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, this.nodeClient.getAccountInfo(SubstrateAddress_1.SubstrateAddress.from(accountId, this.network))]; case 1: accountInfo = _c.sent(); return [2 /*return*/, (_b = (_a = accountInfo) === null || _a === void 0 ? void 0 : _a.data.free.value, (_b !== null && _b !== void 0 ? _b : new bignumber_1.default(0)))]; } }); }); }; SubstrateAccountController.prototype.getTransferableBalance = function (accountId, excludeExistentialDeposit, excludeLocks) { if (excludeExistentialDeposit === void 0) { excludeExistentialDeposit = true; } if (excludeLocks === void 0) { excludeLocks = true; } return __awaiter(this, void 0, void 0, function () { var results, accountInfo, minBalance, free, reserved, locked; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, Promise.all([ this.nodeClient.getAccountInfo(SubstrateAddress_1.SubstrateAddress.from(accountId, this.network)), excludeExistentialDeposit ? this.nodeClient.getExistentialDeposit() : null ])]; case 1: results = _a.sent(); accountInfo = results[0]; minBalance = results[1]; if (!accountInfo) { return [2 /*return*/, new bignumber_1.default(0)]; } free = accountInfo.data.free.value; reserved = accountInfo.data.reserved.value; locked = excludeLocks ? accountInfo.data.miscFrozen.value : new bignumber_1.default(0); return [2 /*return*/, free .minus(reserved) .minus(locked) .minus(minBalance || 0)]; } }); }); }; SubstrateAccountController.prototype.getUnlockingBalance = function (accountId) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var stakingDetails; return __generator(this, function (_c) { switch (_c.label) { case 0: return [4 /*yield*/, this.nodeClient.getStakingLedger(SubstrateAddress_1.SubstrateAddress.from(accountId, this.network))]; case 1: stakingDetails = _c.sent(); return [2 /*return*/, (_b = (_a = stakingDetails) === null || _a === void 0 ? void 0 : _a.unlocking.elements.map(function (entry) { return entry.first.value; }).reduce(function (sum, next) { return sum.plus(next); }, new bignumber_1.default(0)), (_b !== null && _b !== void 0 ? _b : new bignumber_1.default(0)))]; } }); }); }; SubstrateAccountController.prototype.isBonded = function (accountId) { return __awaiter(this, void 0, void 0, function () { var bonded; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.nodeClient.getBonded(SubstrateAddress_1.SubstrateAddress.from(accountId, this.network))]; case 1: bonded = _a.sent(); return [2 /*return*/, bonded != null]; } }); }); }; SubstrateAccountController.prototype.isNominating = function (accountId) { return __awaiter(this, void 0, void 0, function () { var nominations; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.nodeClient.getNominations(SubstrateAddress_1.SubstrateAddress.from(accountId, this.network))]; case 1: nominations = _a.sent(); return [2 /*return*/, nominations != null]; } }); }); }; SubstrateAccountController.prototype.getCurrentValidators = function (accountId) { return __awaiter(this, void 0, void 0, function () { var nominations; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.nodeClient.getNominations(SubstrateAddress_1.SubstrateAddress.from(accountId, this.network))]; case 1: nominations = _a.sent(); if (nominations) { return [2 /*return*/, nominations.targets.elements.map(function (target) { return target.asAddress(); })]; } return [2 /*return*/, []]; } }); }); }; SubstrateAccountController.prototype.getValidatorDetails = function (accountId) { return __awaiter(this, void 0, void 0, function () { var address, activeEra, identity, status, exposure, validatorPrefs, lastEraReward, activeEraIndex, results, currentValidators; return __generator(this, function (_a) { switch (_a.label) { case 0: address = SubstrateAddress_1.SubstrateAddress.from(accountId, this.network); return [4 /*yield*/, this.nodeClient.getActiveEraInfo()]; case 1: activeEra = _a.sent(); if (!activeEra) return [3 /*break*/, 4]; activeEraIndex = activeEra.index.toNumber(); return [4 /*yield*/, Promise.all([ this.getAccountIdentityInfo(address), this.nodeClient.getValidators(), this.nodeClient.getValidatorPrefs(activeEraIndex, address), this.nodeClient.getValidatorExposure(activeEraIndex, address) ])]; case 2: results = _a.sent(); identity = results[0] || undefined; currentValidators = results[1]; validatorPrefs = results[2] || undefined; exposure = results[3] || undefined; return [4 /*yield*/, this.getEraValidatorReward(address, activeEraIndex - 1)]; case 3: lastEraReward = (_a.sent()) || undefined; if (currentValidators && currentValidators.find(function (current) { return current.compare(address) == 0; })) { status = 'Active'; } else if (currentValidators) { status = 'Inactive'; } _a.label = 4; case 4: return [2 /*return*/, { address: address.toString(), name: identity ? identity.display.toString() : undefined, status: status || undefined, ownStash: exposure ? exposure.own.toString() : undefined, totalStakingBalance: exposure ? exposure.total.toString() : undefined, commission: validatorPrefs ? validatorPrefs.commission.value.dividedBy(1000000000).toString() // commission is Perbill (parts per billion) : undefined, lastEraReward: lastEraReward }]; } }); }); }; SubstrateAccountController.prototype.getNominatorDetails = function (accountId, validatorIds) { var _a, _b, _c; return __awaiter(this, void 0, void 0, function () { var address, results, balance, transferableBalance, stakingLedger, nominations, activeEra, expectedEraDuration, validators, stakingDetails, availableActions; return __generator(this, function (_d) { switch (_d.label) { case 0: address = SubstrateAddress_1.SubstrateAddress.from(accountId, this.network); return [4 /*yield*/, Promise.all([ this.getBalance(address), this.getTransferableBalance(address, false), this.nodeClient.getStakingLedger(address), this.nodeClient.getNominations(address), this.nodeClient.getActiveEraInfo(), this.nodeClient.getExpectedEraDuration(), this.nodeClient.getExistentialDeposit() ])]; case 1: results = _d.sent(); balance = results[0]; transferableBalance = results[1]; stakingLedger = results[2]; nominations = results[3]; activeEra = results[4]; expectedEraDuration = results[5]; if (!balance || !transferableBalance || !activeEra || !expectedEraDuration) { return [2 /*return*/, Promise.reject('Could not fetch nominator details.')]; } validators = ((_c = (_b = (_a = nominations) === null || _a === void 0 ? void 0 : _a.targets) === null || _b === void 0 ? void 0 : _b.elements) === null || _c === void 0 ? void 0 : _c.map(function (target) { return target.asAddress(); })) || []; return [4 /*yield*/, this.getStakingDetails(accountId, stakingLedger, nominations, activeEra, expectedEraDuration)]; case 2: stakingDetails = _d.sent(); return [4 /*yield*/, this.getAvailableStakingActions(stakingDetails, nominations, validatorIds || validators, transferableBalance)]; case 3: availableActions = _d.sent(); return [2 /*return*/, { address: address.toString(), balance: balance.toString(), delegatees: validators, availableActions: availableActions, stakingDetails: stakingDetails || undefined }]; } }); }); }; SubstrateAccountController.prototype.getSlashingSpansNumber = function (accountId) { return __awaiter(this, void 0, void 0, function () { var slashingSpans; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this.nodeClient.getSlashingSpan(SubstrateAddress_1.SubstrateAddress.from(accountId, this.network))]; case 1: slashingSpans = _a.sent(); return [2 /*return*/, slashingSpans ? slashingSpans.prior.elements.length + 1 : 0]; } }); }); }; SubstrateAccountController.prototype.getStakingDetails = function (accountId, stakingLedger, nominations, activeEra, expectedEraDuration) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var unlockingDetails, stakingStatus, rewards, _c; return __generator(this, function (_d) { switch (_d.label) { case 0: if (!stakingLedger) { return [2 /*return*/, null]; } unlockingDetails = this.getUnlockingDetails(stakingLedger.unlocking.elements.map(function (tuple) { return [tuple.first.value, tuple.second.value]; }), activeEra, expectedEraDuration); stakingStatus = this.getStakingStatus(nominations, activeEra.index.toNumber()); if (!nominations) return [3 /*break*/, 2]; return [4 /*yield*/, this.getNominatorRewards(accountId, nominations.targets.elements.map(function (id) { return id.address; }), activeEra, 5)]; case 1: _c = _d.sent(); return [3 /*break*/, 3]; case 2: _c = []; _d.label = 3; case 3: rewards = _c; return [2 /*return*/, { total: stakingLedger.total.toString(), active: stakingLedger.active.toString(), locked: unlockingDetails.locked, unlocked: unlockingDetails.unlocked, status: stakingStatus, nextEra: ((_b = (_a = activeEra.start.value) === null || _a === void 0 ? void 0 : _a.plus(expectedEraDuration)) === null || _b === void 0 ? void 0 : _b.toNumber()) || 0, rewards: rewards }]; } }); }); }; SubstrateAccountController.prototype.getUnlockingDetails = function (unlocking, activeEra, expectedEraDuration) { var _a = this.partitionArray(unlocking, function (_a) { var _ = _a[0], era = _a[1]; return activeEra.index.lt(era); }), locked = _a[0], unlocked = _a[1]; var lockedDetails = locked.map(function (_a) { var value = _a[0], era = _a[1]; var _b; var eraStart = ((_b = activeEra.start.value) === null || _b === void 0 ? void 0 : _b.value) || new bignumber_1.default(0); var estimatedDuration = era.minus(activeEra.index.value).multipliedBy(expectedEraDuration); var expectedUnlock = eraStart.plus(estimatedDuration); return { value: value.toString(10), expectedUnlock: expectedUnlock.toNumber() }; }); var totalUnlocked = unlocked.reduce(function (total, _a) { var value = _a[0], _ = _a[1]; return total.plus(value); }, new bignumber_1.default(0)); return { locked: lockedDetails, unlocked: totalUnlocked.toString() }; }; SubstrateAccountController.prototype.getStakingStatus = function (nominations, eraIndex) { if (nominations === null) { return 'bonded'; } else if (nominations.submittedIn.lt(eraIndex)) { return 'nominating'; } else { return 'nominating_inactive'; } }; SubstrateAccountController.prototype.getEraValidatorReward = function (accountId, eraIndex) { var _a, _b, _c, _d, _e; return __awaiter(this, void 0, void 0, function () { var address, results, eraReward, eraPoints, exposureClipped, validatorPrefs, validatorPoints, validatorReward; var _this = this; return __generator(this, function (_f) { switch (_f.label) { case 0: address = SubstrateAddress_1.SubstrateAddress.from(accountId, this.network); return [4 /*yield*/, Promise.all([ this.nodeClient.getValidatorReward(eraIndex).then(function (result) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { return [2 /*return*/, result ? result : this.nodeClient.getValidatorReward(eraIndex - 1)]; }); }); }), this.nodeClient.getRewardPoints(eraIndex), this.nodeClient.getStakersClipped(eraIndex, address), this.nodeClient.getValidatorPrefs(eraIndex, address) ])]; case 1: results = _f.sent(); if (results.some(function (result) { return !result; })) { return [2 /*return*/, null]; } eraReward = results[0]; eraPoints = results[1]; exposureClipped = results[2]; validatorPrefs = results[3]; validatorPoints = (_e = (_d = (_c = (_b = (_a = eraPoints) === null || _a === void 0 ? void 0 : _a.individual) === null || _b === void 0 ? void 0 : _b.elements) === null || _c === void 0 ? void 0 : _c.find(function (element) { return element.first.address.compare(address); })) === null || _d === void 0 ? void 0 : _d.second) === null || _e === void 0 ? void 0 : _e.value; if (!eraReward || !eraPoints || !exposureClipped || !validatorPrefs || !validatorPoints) { return [2 /*return*/, null]; } validatorReward = this.calculateValidatorReward(eraReward, eraPoints.total.value, validatorPoints); return [2 /*return*/, { amount: validatorReward.toFixed(), totalStake: exposureClipped.total.toString(), ownStake: exposureClipped.own.toString(), commission: validatorPrefs.commission.toString() }]; } }); }); }; SubstrateAccountController.prototype.getNominatorRewards = function (accountId, validators, activeEra, eras) { return __awaiter(this, void 0, void 0, function () { var address, expectedEraDuration, eraIndices, rewards; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: address = SubstrateAddress_1.SubstrateAddress.from(accountId, this.network); return [4 /*yield*/, this.nodeClient.getExpectedEraDuration()]; case 1: expectedEraDuration = _a.sent(); if (!expectedEraDuration) { return [2 /*return*/, Promise.reject('Could not fetch all necessary data.')]; } eraIndices = Array.isArray(eras) ? eras : Array.from(Array(eras).keys()).map(function (index) { return activeEra.index.toNumber() - 1 - index; }); return [4 /*yield*/, Promise.all(eraIndices.map(function (era) { return _this.calculateEraNominatorReward(address, validators.map(function (validator) { return SubstrateAddress_1.SubstrateAddress.from(validator, _this.network); }), era).then(function (partial) { if (partial) { var rewardEra = partial.eraIndex || activeEra.index.toNumber(); var erasPassed = activeEra.index.minus(rewardEra).toNumber(); partial.timestamp = activeEra.start.value ? activeEra.start.value.minus(expectedEraDuration.multipliedBy(erasPassed - 1)).toNumber() : 0; } return partial; }); }))]; case 2: rewards = _a.sent(); return [2 /*return*/, rewards.filter(function (reward) { return reward; })]; } }); }); }; SubstrateAccountController.prototype.calculateEraNominatorReward = function (accountId, validators, eraIndex) { var _a; return __awaiter(this, void 0, void 0, function () { var results, reward, rewardPoints, exposuresWithValidators, partialRewards; var _this = this; return __generator(this, function (_b) { switch (_b.label) { case 0: return [4 /*yield*/, Promise.all([ this.nodeClient.getValidatorReward(eraIndex), this.nodeClient.getRewardPoints(eraIndex), Promise.all(validators.map(function (validator) { return __awaiter(_this, void 0, void 0, function () { var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _a = [SubstrateAddress_1.SubstrateAddress.from(validator, this.network)]; return [4 /*yield*/, this.nodeClient .getValidatorPrefs(eraIndex, SubstrateAddress_1.SubstrateAddress.from(validator, this.network)) .then(function (prefs) { var _a, _b; return (_b = (_a = prefs) === null || _a === void 0 ? void 0 : _a.commission) === null || _b === void 0 ? void 0 : _b.value; })]; case 1: _a = _a.concat([ _b.sent() ]); return [4 /*yield*/, this.nodeClient.getStakersClipped(eraIndex, SubstrateAddress_1.SubstrateAddress.from(validator, this.network))]; case 2: return [2 /*return*/, _a.concat([ _b.sent() ])]; } }); }); })) ])]; case 1: results = _b.sent(); reward = results[0]; rewardPoints = results[1]; exposuresWithValidators = results[2]; if (!reward || !rewardPoints || !exposuresWithValidators) { return [2 /*return*/, null]; } partialRewards = exposuresWithValidators .map(function (_a) { var validator = _a[0], commission = _a[1], exposure = _a[2]; var _b, _c, _d, _e, _f; var validatorPoints = (_c = (_b = rewardPoints.individual.elements.find(function (element) { return element.first.address.compare(validator) === 0; })) === null || _b === void 0 ? void 0 : _b.second) === null || _c === void 0 ? void 0 : _c.value; var nominatorStake = (_f = (_e = (_d = exposure) === null || _d === void 0 ? void 0 : _d.others.elements.find(function (element) { return element.first.address.compare(accountId) === 0; })) === null || _e === void 0 ? void 0 : _e.second) === null || _f === void 0 ? void 0 : _f.value; if (commission && exposure && validatorPoints && nominatorStake) { var validatorReward = _this.calculateValidatorReward(reward, rewardPoints.total.value, validatorPoints); return _this.calculateNominatorReward(validatorReward, commission, exposure.total.value, nominatorStake); } else { return null; } }) .filter(function (reward) { return reward !== null; }); if (partialRewards.every(function (reward) { return !reward; })) { return [2 /*return*/, null]; } return [2 /*return*/, { eraIndex: eraIndex, amount: partialRewards.reduce(function (sum, next) { return sum.plus(next); }, new bignumber_1.default(0)).toFixed(0), exposures: (_a = exposuresWithValidators) === null || _a === void 0 ? void 0 : _a.map(function (_a) { var validator = _a[0], _ = _a[1], exposure = _a[2]; var _b; return [ validator.toString(), (_b = exposure) === null || _b === void 0 ? void 0 : _b.others.elements.findIndex(function (element) { return element.first.address.compare(accountId) === 0; }) ]; }).filter(function (_a) { var _ = _a[0], index = _a[1]; return index !== undefined; }) }]; } }); }); }; SubstrateAccountController.prototype.calculateValidatorReward = function (totalReward, totalPoints, validatorPoints) { return validatorPoints.dividedBy(totalPoints).multipliedBy(totalReward); }; SubstrateAccountController.prototype.calculateNominatorReward = function (validatorReward, validatorCommission, totalStake, nominatorStake) { var nominatorShare = nominatorStake.dividedBy(totalStake); return new bignumber_1.default(1).minus(validatorCommission.dividedBy(1000000000)).multipliedBy(validatorReward).multipliedBy(nominatorShare); }; // tslint:disable-next-line: cyclomatic-complexity SubstrateAccountController.prototype.getAvailableStakingActions = function (stakingDetails, nominations, validatorIds, maxDelegationValue) { var _a, _b, _c, _d, _e, _f, _g; return __awaiter(this, void 0, void 0, function () { var availableActions, currentValidators, validatorAddresses, isBonded, isDelegating, isUnbonding, minBondingValue, minDelegationValue, electionStatus, isElectionClosed, hasFundsToWithdraw; var _this = this; return __generator(this, function (_h) { switch (_h.label) { case 0: availableActions = []; currentValidators = ((_c = (_b = (_a = nominations) === null || _a === void 0 ? void 0 : _a.targets) === null || _b === void 0 ? void 0 : _b.elements) === null || _c === void 0 ? void 0 : _c.map(function (target) { return target.asAddress(); })) || []; validatorAddresses = validatorIds.map(function (id) { return SubstrateAddress_1.SubstrateAddress.from(id, _this.network).toString(); }); isBonded = new bignumber_1.default((_e = (_d = stakingDetails) === null || _d === void 0 ? void 0 : _d.active, (_e !== null && _e !== void 0 ? _e : 0))).gt(0); isDelegating = nominations !== null; isUnbonding = stakingDetails && stakingDetails.locked.length > 0; return [4 /*yield*/, this.nodeClient.getExistentialDeposit()]; case 1: minBondingValue = _h.sent(); minDelegationValue = new bignumber_1.default(1); return [4 /*yield*/, this.nodeClient.getElectionStatus().then(function (eraElectionStatus) { var _a; return (_a = eraElectionStatus) === null || _a === void 0 ? void 0 : _a.status.value; })]; case 2: electionStatus = _h.sent(); isElectionClosed = electionStatus === SubstrateEraElectionStatus_1.SubstrateElectionStatus.CLOSED; hasFundsToWithdraw = new bignumber_1.default((_g = (_f = stakingDetails) === null || _f === void 0 ? void 0 : _f.unlocked, (_g !== null && _g !== void 0 ? _g : 0))).gt(0); if (maxDelegationValue.gt(minBondingValue) && !isBonded && !isUnbonding && isElectionClosed) { availableActions.push({ type: SubstrateStakingActionType_1.SubstrateStakingActionType.BOND_NOMINATE, args: ['targets', 'controller', 'value', 'payee'] }); } if (maxDelegationValue.gt(minDelegationValue) && isBonded && !isUnbonding && isElectionClosed) { availableActions.push({ type: SubstrateStakingActionType_1.SubstrateStakingActionType.BOND_EXTRA, args: ['value'] }); } if (isBonded && !isDelegating && !isUnbonding && isElectionClosed) { availableActions.push({ type: SubstrateStakingActionType_1.SubstrateStakingActionType.NOMINATE, args: ['targets'] }, { type: SubstrateStakingActionType_1.SubstrateStakingActionType.UNBOND, args: ['value'] }); } if (isUnbonding && !isDelegating && isElectionClosed) { availableActions.push({ type: SubstrateStakingActionType_1.SubstrateStakingActionType.REBOND_NOMINATE, args: ['targets', 'value'] }); } if (isUnbonding && isDelegating && isElectionClosed) { availableActions.push({ type: SubstrateStakingActionType_1.SubstrateStakingActionType.REBOND_EXTRA, args: ['value'] }); } if (isDelegating && isElectionClosed) { if (validatorAddresses.every(function (validator) { return currentValidators.includes(validator); }) && validatorAddresses.length === currentValidators.length) { availableActions.push({ type: SubstrateStakingActionType_1.SubstrateStakingActionType.CANCEL_NOMINATION, args: ['value'] }); } else if (validatorAddresses.length > 0) { availableActions.push({ type: SubstrateStakingActionType_1.SubstrateStakingActionType.CHANGE_NOMINATION, args: ['targets'] }); } } if (hasFundsToWithdraw && isElectionClosed) { availableActions.push({ type: SubstrateStakingActionType_1.SubstrateStakingActionType.WITHDRAW_UNBONDED, args: [] }); } availableActions.sort(function (a, b) { return a.type - b.type; }); return [2 /*return*/, availableActions]; } }); }); }; SubstrateAccountController.prototype.getAccountIdentityInfo = function (address) { return __awaiter(this, void 0, void 0, function () { var registration, superOf, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 3, , 4]); return [4 /*yield*/, this.nodeClient.getIdentityOf(address)]; case 1: registration = _b.sent(); if (registration) { return [2 /*return*/, registration.identityInfo]; } return [4 /*yield*/, this.nodeClient.getSuperOf(address)]; case 2: superOf = _b.sent(); return [2 /*return*/, superOf ? this.getAccountIdentityInfo(superOf.first.address) : null]; case 3: _a = _b.sent(); return [2 /*return*/, null]; case 4: return [2 /*return*/]; } }); }); }; SubstrateAccountController.prototype.partitionArray = function (array, predicate) { var partitioned = [[], []]; for (var _i = 0, array_1 = array; _i < array_1.length; _i++) { var item = array_1[_i]; var index = predicate(item) ? 0 : 1; partitioned[index].push(item); } return partitioned; }; return SubstrateAccountController; }()); exports.SubstrateAccountController = SubstrateAccountController; //# sourceMappingURL=SubstrateAccountController.js.map