UNPKG

@oap75/api

Version:
44 lines (43 loc) 2.32 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getSubstrateApi = void 0; const api_1 = require("@polkadot/api"); const utils_1 = require("@subsocial/utils"); const registry_1 = __importDefault(require("@subsocial/types/substrate/registry")); const util_1 = require("@polkadot/util"); const types_1 = require("@subsocial/types"); const logger = (0, utils_1.newLogger)('SubstrateConnection'); let api = undefined; /** Get the current open connection to Substrate node. */ const getSubstrateApi = (nodeUrl) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b; if (api) return api; const rpcEndpoint = nodeUrl || 'ws://127.0.0.1:9944/'; const provider = new api_1.WsProvider(rpcEndpoint); logger.info(`Connecting to Substrate node at ${rpcEndpoint}...`); api = new api_1.ApiPromise({ provider, typesBundle: types_1.typesBundle }); yield api.isReady; const properties = yield api.rpc.system.properties(); const tokenSymbol = (_a = properties.tokenSymbol.unwrapOr(undefined)) === null || _a === void 0 ? void 0 : _a.map((x) => x.toString()); const tokenDecimals = (_b = properties.tokenDecimals.unwrapOr(undefined)) === null || _b === void 0 ? void 0 : _b.map((x) => x.toNumber()); registry_1.default.setChainProperties(properties); util_1.formatBalance.setDefaults({ decimals: tokenDecimals, unit: tokenSymbol }); return api; }); exports.getSubstrateApi = getSubstrateApi;