@substrate/api-sidecar
Version:
REST service that makes it easy to interact with blockchain nodes built using Substrate's FRAME framework.
95 lines • 4.17 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.PalletsForeignAssetsService = void 0;
const util_1 = require("@polkadot/util");
const AbstractService_1 = require("../AbstractService");
class PalletsForeignAssetsService extends AbstractService_1.AbstractService {
constructor(api) {
super(api);
}
/**
* Fetch all foreign asset's `AssetDetails` and `AssetMetadata`.
*
* @param hash `BlockHash` to make call at
*/
async fetchForeignAssets(hash) {
const { api } = this;
const [{ number }, foreignAssetInfo] = await Promise.all([
api.rpc.chain.getHeader(hash),
api.query.foreignAssets.asset.entries(),
]);
const items = [];
/**
* This will iterate through all the foreign asset entries and for each entry it will create
* the `foreignAssetMultiLocation` variable based on the MultiLocation of the foreign asset.
* This variable will then be used as the key to get the corresponding metadata of the foreign asset.
*
* This is based on the logic implemented by marshacb in asset-transfer-api-registry
* https://github.com/paritytech/asset-transfer-api-registry/blob/main/src/fetchSystemParachainForeignAssetInfo.ts#L25L36
*/
for (const [assetStorageKeyData, assetInfo] of foreignAssetInfo) {
let foreignAssetData = '';
if ((0, util_1.isHex)(assetStorageKeyData)) {
foreignAssetData = assetStorageKeyData;
}
else {
foreignAssetData = assetStorageKeyData.toHuman();
}
let multiLocation = '';
if (foreignAssetData) {
let assetMetadata;
// Checking if the foreign asset data is an array or not because there is a case that the
// Multilocation is given as a hexadecimal value (see foreign assets in Westend Asset Hub).
if (Array.isArray(foreignAssetData)) {
multiLocation = foreignAssetData[0];
// remove any commas from multilocation key values e.g. Parachain: 2,125 -> Parachain: 2125
const MultiLocationStr = JSON.stringify(foreignAssetData[0]).replace(/(\d),/g, '$1');
assetMetadata = await api.query.foreignAssets.metadata(JSON.parse(MultiLocationStr));
}
else {
multiLocation = foreignAssetData;
assetMetadata = {};
}
if (assetInfo.isSome) {
items.push({
multiLocation,
foreignAssetInfo: assetInfo.unwrap(),
foreignAssetMetadata: assetMetadata,
});
}
else {
items.push({
multiLocation,
foreignAssetInfo: {},
foreignAssetMetadata: assetMetadata,
});
}
}
}
const at = {
hash,
height: number.unwrap().toString(10),
};
return {
at,
items,
};
}
}
exports.PalletsForeignAssetsService = PalletsForeignAssetsService;
//# sourceMappingURL=PalletsForeignAssetsService.js.map