@substrate/api-sidecar
Version:
REST service that makes it easy to interact with blockchain nodes built using Substrate's FRAME framework.
180 lines • 8.28 kB
JavaScript
;
// Copyright 2017-2025 Parity Technologies (UK) Ltd.
// This file is part of Substrate API Sidecar.
//
// Substrate API Sidecar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
Object.defineProperty(exports, "__esModule", { value: true });
exports.AhmInfoService = void 0;
const apiRegistry_1 = require("../../apiRegistry");
const AbstractService_1 = require("../AbstractService");
const consts_1 = require("../consts");
class AhmInfoService extends AbstractService_1.AbstractService {
/**
* Fetch Asset Hub Migration information at a given block.
*
* @param hash `BlockHash` to make call at.
*/
async fetchAhmInfo() {
if (apiRegistry_1.ApiPromiseRegistry.assetHubInfo.isAssetHub) {
return this.handleFromAh();
}
else {
return this.handleFromRelay();
}
}
// Handle the AHM info When the main connection is asset hub
async handleFromAh() {
var _a, _b, _c, _d, _e, _f, _g;
const { specName } = this;
const migrationBoundaries = consts_1.MIGRATION_BOUNDARIES[specName];
if (migrationBoundaries) {
return {
relay: {
startBlock: migrationBoundaries.relayMigrationStartedAt,
endBlock: migrationBoundaries.relayMigrationEndedAt,
},
assetHub: {
startBlock: migrationBoundaries.assetHubMigrationStartedAt,
endBlock: migrationBoundaries.assetHubMigrationEndedAt,
},
};
}
else {
// Sanity check to ensure the proper pallet exists
if (!this.api.query.ahMigrator) {
return {
relay: {
startBlock: null,
endBlock: null,
},
assetHub: {
startBlock: null,
endBlock: null,
},
};
}
const rcApi = (_a = apiRegistry_1.ApiPromiseRegistry.getApiByType('relay')[0]) === null || _a === void 0 ? void 0 : _a.api;
// We have a multi-chain connection so we can get all values from the relay and asset hub
if (rcApi) {
const [ahStart, ahEnd, rcStart, rcEnd] = await Promise.all([
(_b = this.api.query.ahMigrator) === null || _b === void 0 ? void 0 : _b.migrationStartBlock(),
(_c = this.api.query.ahMigrator) === null || _c === void 0 ? void 0 : _c.migrationEndBlock(),
(_d = rcApi.query.rcMigrator) === null || _d === void 0 ? void 0 : _d.migrationStartBlock(),
(_e = rcApi.query.rcMigrator) === null || _e === void 0 ? void 0 : _e.migrationEndBlock(),
]);
return {
relay: {
startBlock: rcStart.isSome ? rcStart.unwrap().toNumber() : null,
endBlock: rcEnd.isSome ? rcEnd.unwrap().toNumber() : null,
},
assetHub: {
startBlock: ahStart.isSome ? ahStart.unwrap().toNumber() : null,
endBlock: ahEnd.isSome ? ahEnd.unwrap().toNumber() : null,
},
};
}
else {
const [ahStart, ahEnd] = await Promise.all([
(_f = this.api.query.ahMigrator) === null || _f === void 0 ? void 0 : _f.migrationStartBlock(),
(_g = this.api.query.ahMigrator) === null || _g === void 0 ? void 0 : _g.migrationEndBlock(),
]);
return {
relay: {
startBlock: null,
endBlock: null,
},
assetHub: {
startBlock: ahStart.isSome ? ahStart.unwrap().toNumber() : null,
endBlock: ahEnd.isSome ? ahEnd.unwrap().toNumber() : null,
},
};
}
}
}
async handleFromRelay() {
var _a, _b, _c, _d, _e, _f, _g;
const { specName } = this;
const hasRelaySpec = consts_1.relayToSpecMapping.has(specName);
if (!hasRelaySpec) {
throw new Error("Invalid chain specName. Can't map specName to asset hub spec");
}
const assetHubSpecName = consts_1.relayToSpecMapping.get(specName);
const migrationBoundaries = consts_1.MIGRATION_BOUNDARIES[assetHubSpecName];
if (migrationBoundaries) {
return {
relay: {
startBlock: migrationBoundaries.relayMigrationStartedAt,
endBlock: migrationBoundaries.relayMigrationEndedAt,
},
assetHub: {
startBlock: migrationBoundaries.assetHubMigrationStartedAt,
endBlock: migrationBoundaries.assetHubMigrationEndedAt,
},
};
}
else {
// Sanity check to ensure the proper pallet exists
if (!this.api.query.rcMigrator) {
return {
relay: {
startBlock: null,
endBlock: null,
},
assetHub: {
startBlock: null,
endBlock: null,
},
};
}
const ahApi = (_a = apiRegistry_1.ApiPromiseRegistry.getApiByType('assetHub')[0]) === null || _a === void 0 ? void 0 : _a.api;
// We have a multi-chain connection so we can get all values from the relay and asset hub
if (ahApi) {
const [rcStart, rcEnd, ahStart, ahEnd] = await Promise.all([
(_b = this.api.query.rcMigrator) === null || _b === void 0 ? void 0 : _b.migrationStartBlock(),
(_c = this.api.query.rcMigrator) === null || _c === void 0 ? void 0 : _c.migrationEndBlock(),
(_d = ahApi.query.ahMigrator) === null || _d === void 0 ? void 0 : _d.migrationStartBlock(),
(_e = ahApi.query.ahMigrator) === null || _e === void 0 ? void 0 : _e.migrationEndBlock(),
]);
return {
relay: {
startBlock: rcStart.isSome ? rcStart.unwrap().toNumber() : null,
endBlock: rcEnd.isSome ? rcEnd.unwrap().toNumber() : null,
},
assetHub: {
startBlock: ahStart.isSome ? ahStart.unwrap().toNumber() : null,
endBlock: ahEnd.isSome ? ahEnd.unwrap().toNumber() : null,
},
};
}
else {
const [rcStart, rcEnd] = await Promise.all([
(_f = this.api.query.rcMigrator) === null || _f === void 0 ? void 0 : _f.migrationStartBlock(),
(_g = this.api.query.rcMigrator) === null || _g === void 0 ? void 0 : _g.migrationEndBlock(),
]);
return {
relay: {
startBlock: rcStart.isSome ? rcStart.unwrap().toNumber() : null,
endBlock: rcEnd.isSome ? rcEnd.unwrap().toNumber() : null,
},
assetHub: {
startBlock: null,
endBlock: null,
},
};
}
}
}
}
exports.AhmInfoService = AhmInfoService;
//# sourceMappingURL=AhmInfoService.js.map