airgap-coin-lib
Version:
The airgap-coin-lib is a protocol agnostic library to prepare, sign and broadcast cryptocurrency transactions.
496 lines • 28.2 kB
JavaScript
"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 RPCBody_1 = require("../../../../data/RPCBody");
var axios_0_19_0_1 = require("../../../../dependencies/src/axios-0.19.0");
var bignumber_1 = require("../../../../dependencies/src/bignumber.js-9.0.0/bignumber");
var hex_1 = require("../../../../utils/hex");
var SubstrateAccountInfo_1 = require("../data/account/SubstrateAccountInfo");
var SubstrateRegistration_1 = require("../data/account/SubstrateRegistration");
var Metadata_1 = require("../data/metadata/Metadata");
var SCALEAccountId_1 = require("../data/scale/type/SCALEAccountId");
var SCALEArray_1 = require("../data/scale/type/SCALEArray");
var SCALECompactInt_1 = require("../data/scale/type/SCALECompactInt");
var SCALEData_1 = require("../data/scale/type/SCALEData");
var SCALEEnum_1 = require("../data/scale/type/SCALEEnum");
var SCALEInt_1 = require("../data/scale/type/SCALEInt");
var SCALETuple_1 = require("../data/scale/type/SCALETuple");
var SubstrateActiveEraInfo_1 = require("../data/staking/SubstrateActiveEraInfo");
var SubstrateEraElectionStatus_1 = require("../data/staking/SubstrateEraElectionStatus");
var SubstrateEraRewardPoints_1 = require("../data/staking/SubstrateEraRewardPoints");
var SubstrateExposure_1 = require("../data/staking/SubstrateExposure");
var SubstrateNominations_1 = require("../data/staking/SubstrateNominations");
var SubstratePayee_1 = require("../data/staking/SubstratePayee");
var SubstrateSlashingSpans_1 = require("../data/staking/SubstrateSlashingSpans");
var SubstrateStakingLedger_1 = require("../data/staking/SubstrateStakingLedger");
var SubstrateValidatorPrefs_1 = require("../data/staking/SubstrateValidatorPrefs");
var SubstrateNodeCache_1 = require("./SubstrateNodeCache");
var supported_1 = require("./supported");
var CACHE_DEFAULT_EXPIRATION_TIME = 3000; // 3s
var SubstrateNodeClient = /** @class */ (function () {
function SubstrateNodeClient(network, baseURL, cache) {
if (cache === void 0) { cache = new SubstrateNodeCache_1.SubstrateNodeCache(CACHE_DEFAULT_EXPIRATION_TIME); }
this.network = network;
this.baseURL = baseURL;
this.cache = cache;
this.lastFees = new Map();
this.initApiPromise = null;
}
SubstrateNodeClient.prototype.getAccountInfo = function (address) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('System', 'Account', SCALEAccountId_1.SCALEAccountId.from(address, this.network)).then(function (item) {
return item ? SubstrateAccountInfo_1.SubstrateAccountInfo.decode(_this.network, _this.runtimeVersion, item) : null;
})];
});
});
};
SubstrateNodeClient.prototype.getExistentialDeposit = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.getConstant('Balances', 'ExistentialDeposit').then(function (constant) { return SCALEInt_1.SCALEInt.decode(constant).decoded.value; })];
});
});
};
SubstrateNodeClient.prototype.getTransactionMetadata = function (type) {
return __awaiter(this, void 0, void 0, function () {
var _a, methodName, callName, call;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
_a = supported_1.supportedCallEndpoints.get(type) || [null, null], methodName = _a[0], callName = _a[1];
call = null;
if (!(methodName && callName)) return [3 /*break*/, 2];
return [4 /*yield*/, this.getCall(methodName, callName)];
case 1:
call = _b.sent();
_b.label = 2;
case 2: return [2 /*return*/, call ? call : Promise.reject('Could not find requested item.')];
}
});
});
};
SubstrateNodeClient.prototype.getTransferFeeEstimate = function (transactionBytes) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.send('payment', 'queryInfo', [hex_1.bytesToHex(transactionBytes)]).then(function (result) {
return result ? new bignumber_1.default(result.partialFee) : null;
})];
});
});
};
SubstrateNodeClient.prototype.saveLastFee = function (type, fee) {
this.lastFees.set(type, fee);
};
SubstrateNodeClient.prototype.getSavedLastFee = function (type, defaultValue) {
if (defaultValue === void 0) { defaultValue = 'null'; }
var fee = this.lastFees.get(type) || null;
if (!fee && defaultValue === 'largest') {
var savedFees = Array.from(this.lastFees.values());
fee = savedFees.length > 0 ? bignumber_1.default.max.apply(bignumber_1.default, savedFees) : null;
}
return fee;
};
SubstrateNodeClient.prototype.getFirstBlockHash = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.getBlockHash(0)];
});
});
};
SubstrateNodeClient.prototype.getLastBlockHash = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.getBlockHash()];
});
});
};
SubstrateNodeClient.prototype.getCurrentHeight = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.send('chain', 'getBlock').then(function (result) { return new bignumber_1.default(hex_1.stripHexPrefix(result.block.header.number), 16); })];
});
});
};
SubstrateNodeClient.prototype.getCurrentEraIndex = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'CurrentEra').then(function (item) { return (item ? SCALEInt_1.SCALEInt.decode(item).decoded.value : null); })];
});
});
};
SubstrateNodeClient.prototype.getRuntimeVersion = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.send('state', 'getRuntimeVersion').catch(function () { return null; })];
});
});
};
SubstrateNodeClient.prototype.getBonded = function (address) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'Bonded', SCALEAccountId_1.SCALEAccountId.from(address, this.network)).then(function (item) {
return item ? SCALEAccountId_1.SCALEAccountId.decode(_this.network, item).decoded.address : null;
})];
});
});
};
SubstrateNodeClient.prototype.getNominations = function (address) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'Nominators', SCALEAccountId_1.SCALEAccountId.from(address, this.network)).then(function (item) {
return item ? SubstrateNominations_1.SubstrateNominations.decode(_this.network, _this.runtimeVersion, item) : null;
})];
});
});
};
SubstrateNodeClient.prototype.getRewardPoints = function (eraIndex) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'ErasRewardPoints', SCALEInt_1.SCALEInt.from(eraIndex, 32)).then(function (item) {
return item ? SubstrateEraRewardPoints_1.SubstrateEraRewardPoints.decode(_this.network, _this.runtimeVersion, item) : null;
})];
});
});
};
SubstrateNodeClient.prototype.getValidatorReward = function (eraIndex) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'ErasValidatorReward', SCALEInt_1.SCALEInt.from(eraIndex, 32)).then(function (item) {
return item ? SCALEInt_1.SCALEInt.decode(item).decoded.value : null;
})];
});
});
};
SubstrateNodeClient.prototype.getStakersClipped = function (eraIndex, validator) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'ErasStakersClipped', SCALEInt_1.SCALEInt.from(eraIndex, 32), SCALEAccountId_1.SCALEAccountId.from(validator, this.network)).then(function (item) { return (item ? SubstrateExposure_1.SubstrateExposure.decode(_this.network, _this.runtimeVersion, item) : null); })];
});
});
};
SubstrateNodeClient.prototype.getRewardDestination = function (address) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'Payee', SCALEAccountId_1.SCALEAccountId.from(address, this.network)).then(function (item) {
return item ? SCALEEnum_1.SCALEEnum.decode(item, function (hex) { return SubstratePayee_1.SubstratePayee[SubstratePayee_1.SubstratePayee[hex]]; }).decoded.value : null;
})];
});
});
};
SubstrateNodeClient.prototype.getStakingLedger = function (address) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'Ledger', SCALEAccountId_1.SCALEAccountId.from(address, this.network)).then(function (item) {
return item ? SubstrateStakingLedger_1.SubstrateStakingLedger.decode(_this.network, _this.runtimeVersion, item) : null;
})];
});
});
};
SubstrateNodeClient.prototype.getValidators = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Session', 'Validators').then(function (items) {
return items ? SCALEArray_1.SCALEArray.decode(_this.network, items, SCALEAccountId_1.SCALEAccountId.decode).decoded.elements.map(function (encoded) { return encoded.address; }) : null;
})];
});
});
};
SubstrateNodeClient.prototype.getValidatorExposure = function (eraIndex, address) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'ErasStakers', SCALEInt_1.SCALEInt.from(eraIndex, 32), SCALEAccountId_1.SCALEAccountId.from(address, this.network)).then(function (item) { return (item ? SubstrateExposure_1.SubstrateExposure.decode(_this.network, _this.runtimeVersion, item) : null); })];
});
});
};
SubstrateNodeClient.prototype.getElectionStatus = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'EraElectionStatus').then(function (item) {
return item ? SubstrateEraElectionStatus_1.SubstrateEraElectionStatus.decode(_this.network, _this.runtimeVersion, item) : null;
})];
});
});
};
SubstrateNodeClient.prototype.getIdentityOf = function (address) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Identity', 'IdentityOf', SCALEAccountId_1.SCALEAccountId.from(address, this.network)).then(function (item) {
return item ? SubstrateRegistration_1.SubstrateRegistration.decode(_this.network, _this.runtimeVersion, item) : null;
})];
});
});
};
SubstrateNodeClient.prototype.getSuperOf = function (address) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Identity', 'SuperOf', SCALEAccountId_1.SCALEAccountId.from(address, this.network)).then(function (item) {
return item
? SCALETuple_1.SCALETuple.decode(_this.network, item, function (network, hex) { return SCALEAccountId_1.SCALEAccountId.decode(network, hex); }, function (_, hex) { return SCALEData_1.SCALEData.decode(hex); }).decoded
: null;
})];
});
});
};
SubstrateNodeClient.prototype.getSubsOf = function (address) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Identity', 'SubsOf', SCALEAccountId_1.SCALEAccountId.from(address, this.network)).then(function (item) {
return item
? SCALETuple_1.SCALETuple.decode(_this.network, item, function (_, hex) { return SCALECompactInt_1.SCALECompactInt.decode(hex); }, function (network, hex) { return SCALEArray_1.SCALEArray.decode(network, hex, function (innerNetwork, innerHex) { return SCALEAccountId_1.SCALEAccountId.decode(innerNetwork, innerHex); }); }).decoded
: null;
})];
});
});
};
SubstrateNodeClient.prototype.getValidatorPrefs = function (eraIndex, address) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'ErasValidatorPrefs', SCALEInt_1.SCALEInt.from(eraIndex, 32), SCALEAccountId_1.SCALEAccountId.from(address, this.network)).then(function (item) { return (item ? SubstrateValidatorPrefs_1.SubstrateValidatorPrefs.decode(_this.network, _this.runtimeVersion, item) : null); })];
});
});
};
SubstrateNodeClient.prototype.getExpectedEraDuration = function () {
return __awaiter(this, void 0, void 0, function () {
var constants, expectedBlockTime, epochDuration, sessionsPerEra;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Promise.all([
this.getConstant('Babe', 'ExpectedBlockTime'),
this.getConstant('Babe', 'EpochDuration'),
this.getConstant('Staking', 'SessionsPerEra')
]).then(function (constants) { return constants.map(function (constant) { return (constant ? SCALEInt_1.SCALEInt.decode(constant).decoded.value : null); }); })];
case 1:
constants = _a.sent();
if (constants.some(function (constant) { return constant === null; })) {
return [2 /*return*/, null];
}
expectedBlockTime = constants[0];
epochDuration = constants[1];
sessionsPerEra = constants[2];
return [2 /*return*/, expectedBlockTime.multipliedBy(epochDuration).multipliedBy(sessionsPerEra)];
}
});
});
};
SubstrateNodeClient.prototype.getActiveEraInfo = function () {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'ActiveEra').then(function (item) {
return item ? SubstrateActiveEraInfo_1.SubstrateActiveEraInfo.decode(_this.network, _this.runtimeVersion, item) : null;
})];
});
});
};
SubstrateNodeClient.prototype.getSlashingSpan = function (address) {
return __awaiter(this, void 0, void 0, function () {
var _this = this;
return __generator(this, function (_a) {
return [2 /*return*/, this.fromStorage('Staking', 'SlashingSpans', SCALEAccountId_1.SCALEAccountId.from(address, this.network)).then(function (item) {
return item ? SubstrateSlashingSpans_1.SubstrateSlashingSpans.decode(_this.network, _this.runtimeVersion, item) : null;
})];
});
});
};
SubstrateNodeClient.prototype.submitTransaction = function (encoded) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.send('author', 'submitExtrinsic', [encoded])];
});
});
};
SubstrateNodeClient.prototype.getBlockHash = function (blockNumber) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.send('chain', 'getBlockHash', blockNumber !== undefined ? [hex_1.toHexString(blockNumber)] : [])];
});
});
};
SubstrateNodeClient.prototype.fromStorage = function (moduleName, entryName) {
var args = [];
for (var _i = 2; _i < arguments.length; _i++) {
args[_i - 2] = arguments[_i];
}
var _a;
return __awaiter(this, void 0, void 0, function () {
var storageEntry, hash, result;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.initApi()];
case 1:
_b.sent();
storageEntry = (_a = this.metadata) === null || _a === void 0 ? void 0 : _a.storageEntry(moduleName, entryName);
if (!storageEntry) {
return [2 /*return*/, Promise.reject("Could not find requested item: " + moduleName + " " + entryName)];
}
return [4 /*yield*/, storageEntry.hash.apply(storageEntry, args)];
case 2:
hash = _b.sent();
return [4 /*yield*/, this.send('state', 'getStorage', [hash])];
case 3:
result = _b.sent();
return [2 /*return*/, result];
}
});
});
};
SubstrateNodeClient.prototype.getCall = function (moduleName, callName) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var call;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.initApi()];
case 1:
_b.sent();
call = (_a = this.metadata) === null || _a === void 0 ? void 0 : _a.call(moduleName, callName);
return [2 /*return*/, call ? call : Promise.reject("Could not find requested item: " + moduleName + " " + callName)];
}
});
});
};
SubstrateNodeClient.prototype.getConstant = function (moduleName, constantName) {
var _a;
return __awaiter(this, void 0, void 0, function () {
var constant;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this.initApi()];
case 1:
_b.sent();
constant = (_a = this.metadata) === null || _a === void 0 ? void 0 : _a.constant(moduleName, constantName);
return [2 /*return*/, constant ? constant.value.toString('hex') : Promise.reject("Could not find requested item: " + moduleName + " " + constantName)];
}
});
});
};
SubstrateNodeClient.prototype.initApi = function () {
return __awaiter(this, void 0, void 0, function () {
var initApiPromise_1;
var _this = this;
return __generator(this, function (_a) {
if (!this.initApiPromise) {
initApiPromise_1 = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
var _a, metadataEncoded, runtimeVersion;
var _b;
return __generator(this, function (_c) {
switch (_c.label) {
case 0: return [4 /*yield*/, Promise.all([
this.send('state', 'getMetadata'),
this.getRuntimeVersion()
])];
case 1:
_a = _c.sent(), metadataEncoded = _a[0], runtimeVersion = _a[1];
this.metadata = Metadata_1.Metadata.decode(this.network, metadataEncoded).decorate();
if (!runtimeVersion) {
reject("Could not fetch runtime version from the node");
}
this.runtimeVersion = (_b = runtimeVersion) === null || _b === void 0 ? void 0 : _b.specVersion;
resolve();
return [2 /*return*/];
}
});
}); }).then(function () { return __awaiter(_this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
this.initApiPromise = Promise.resolve();
return [4 /*yield*/, this.initCache()];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); });
this.initApiPromise = initApiPromise_1.catch(function (error) {
console.warn(error);
_this.initApiPromise = initApiPromise_1; // retry once
});
}
return [2 /*return*/, this.initApiPromise];
});
});
};
SubstrateNodeClient.prototype.initCache = function () {
return __awaiter(this, void 0, void 0, function () {
var blockTime;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.getConstant('Babe', 'ExpectedBlockTime').then(function (constant) { return SCALEInt_1.SCALEInt.decode(constant).decoded.toNumber(); })];
case 1:
blockTime = _a.sent();
this.cache.expirationTime = Math.floor(blockTime / 3);
return [2 /*return*/];
}
});
});
};
SubstrateNodeClient.prototype.send = function (module, method, params, config) {
if (params === void 0) { params = []; }
if (config === void 0) { config = { allowCache: true }; }
return __awaiter(this, void 0, void 0, function () {
var endpoint, key;
var _this = this;
return __generator(this, function (_a) {
endpoint = module + "_" + method;
key = endpoint + "$" + params.join('');
return [2 /*return*/, this.cache.get(key).catch(function () {
var promise = axios_0_19_0_1.default.post(_this.baseURL, new RPCBody_1.RPCBody(endpoint, params.map(hex_1.addHexPrefix))).then(function (response) { return response.data.result; });
return _this.cache.save(key, promise, { cacheValue: config.allowCache });
})];
});
});
};
return SubstrateNodeClient;
}());
exports.SubstrateNodeClient = SubstrateNodeClient;
//# sourceMappingURL=SubstrateNodeClient.js.map