@aetheras/agencejs
Version:
Agence Javascript Library
73 lines (72 loc) • 2.69 kB
JavaScript
;
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
exports.__esModule = true;
var polkadotUtil = require("@polkadot/util");
var polkadotUtilCrypto = require("@polkadot/util-crypto");
var types_1 = require("@polkadot/types");
var StorageKey_1 = require("@polkadot/types/primitive/StorageKey");
// function isValidApiUrl(url: string) {
// const protocol = new URL(url).protocol
// return protocol == 'ws:' || protocol == 'wss:'
// }
function getCallArgs(moduleMetadata, callName) {
var idx = moduleMetadata.calls.find(function (data) { return data === callName; });
return idx && moduleMetadata.calls[idx].args;
}
function getModules(api) {
return Object.keys(api.query).sort();
}
function parseTypeDef(typeName, registry) {
if (!typeName || !registry)
return null;
try {
var typeDef = (0, types_1.getTypeDef)(typeName);
// create the type and get the raw
var type = (0, types_1.createType)(registry, typeName);
var raw = type.toRawType();
var rawTypeDef = (0, types_1.getTypeDef)(raw);
return {
name: typeName,
type: typeDef,
rawType: rawTypeDef,
registry: registry
};
}
catch (e) {
console.error(e);
return null;
}
}
function storageResultTypeString(registry, storageCreatorMeta) {
var typeStr = (0, StorageKey_1.unwrapStorageType)(registry, storageCreatorMeta.type);
return storageCreatorMeta.modifier.isOptional
? "Option<".concat(typeStr, ">")
: typeStr;
}
function storageResultString(typeName, result) {
if (polkadotUtil.isNull(result) || polkadotUtil.isUndefined(result))
return '<unknown>';
if (['Bytes', 'Raw', 'Option<Keys>', 'Keys'].includes(typeName)) {
return polkadotUtil.u8aToHex(result.toU8a(true));
}
if (result instanceof types_1.Raw && result.isEmpty) {
return '<empty>';
}
else if (result instanceof types_1.Option && result.isNone) {
return '<none>';
}
return result.toString();
}
module.exports = __assign(__assign({}, polkadotUtil), {
// isValidApiUrl,
getCallArgs: getCallArgs, getModules: getModules, parseTypeDef: parseTypeDef, storageResultTypeString: storageResultTypeString, storageResultString: storageResultString, crypto: polkadotUtilCrypto });