UNPKG

@yoroi/api

Version:
153 lines (150 loc) 4.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.emptyOnChainMetadataRecord = void 0; exports.findMetadataRecord = findMetadataRecord; exports.getMetadataResult = getMetadataResult; exports.getOnChainMetadatas = void 0; var _common = require("@yoroi/common"); var _constants = require("./constants"); var _parsers = require("./parsers"); var _getTokenIdentity = require("../translators/helpers/getTokenIdentity"); const getOnChainMetadatas = (baseUrl, request = _common.fetcher) => { return (tokenIds, fetcherConfig) => { if (tokenIds.length === 0) { return Promise.resolve({}); } const assets = tokenIds.map(id => { const [policy, nameHex] = id.split('.'); return { policy, nameHex }; }); const payload = { assets }; return request({ ...fetcherConfig, url: `${baseUrl}/multiAsset/metadata`, method: 'POST', headers: { 'Content-Type': 'application/json' }, data: payload }).then(response => { if (!(0, _common.isRecord)(response)) return Promise.reject(new Error('Invalid asset metadatas')); const responseRecords = response; const result = {}; for (const id of tokenIds) { const { policyId, name, assetName } = (0, _getTokenIdentity.getTokenIdentity)(id); // API returns with utf8 name, the request sends with hex name const tokenId = `${policyId}.${name}`; const records = responseRecords[tokenId]; if (!(0, _common.isArray)(records)) { result[id] = emptyOnChainMetadataRecord; continue; } const tokenIdentity = { policyId, name, nameHex: assetName }; result[id] = getMetadataResult(records, tokenIdentity); } return Promise.resolve(result); }); }; }; exports.getOnChainMetadatas = getOnChainMetadatas; function getMetadataResult(records, tokenIdentity) { let ftMetadataResult = { mintFtMetadata: undefined, mintFtRecordSelected: undefined }; let nftMetadataResult = { mintNftMetadata: undefined, mintNftRecordSelected: undefined }; for (const record of records) { if (!(0, _common.isRecord)(record)) continue; const possibleMetadatas = record; // avoid casting in every usage if (possibleMetadatas?.key === _constants.CIP26_KEY_FT && !ftMetadataResult.mintFtRecordSelected) { ftMetadataResult = getFtRecord(possibleMetadatas, tokenIdentity); continue; } if (possibleMetadatas?.key === _constants.CIP25_KEY_NFT && !nftMetadataResult.mintNftRecordSelected) { nftMetadataResult = getNftRecord(possibleMetadatas, tokenIdentity); } } return { ...ftMetadataResult, ...nftMetadataResult }; } function getFtRecord(record, tokenIdentity) { const possibleFtMetadataRecord = findMetadataRecord(record, tokenIdentity); if (possibleFtMetadataRecord !== undefined && (0, _common.isRecord)(possibleFtMetadataRecord)) { const parsedFtMetadataRecord = (0, _parsers.parseFtMetadataRecord)(possibleFtMetadataRecord); if (parsedFtMetadataRecord !== undefined) { return { // record holds original tx mint metadata is safe to cast here mintFtMetadata: record, mintFtRecordSelected: parsedFtMetadataRecord }; } } return { mintFtMetadata: record, mintFtRecordSelected: undefined }; } function getNftRecord(record, tokenIdentity) { const possibleNftMetadataRecord = findMetadataRecord(record, tokenIdentity); if (possibleNftMetadataRecord !== undefined && (0, _common.isRecord)(possibleNftMetadataRecord)) { const parsedNftMetadataRecord = (0, _parsers.parseNftMetadataRecord)(possibleNftMetadataRecord); if (parsedNftMetadataRecord !== undefined) { return { // record holds original tx mint metadata is safe to cast here mintNftMetadata: record, mintNftRecordSelected: parsedNftMetadataRecord }; } } return { mintNftMetadata: record, mintNftRecordSelected: undefined }; } function findMetadataRecord(possibleMetadataRecord, tokenIdentity) { const { policyId, name, nameHex } = tokenIdentity; const metadataRecord = possibleMetadataRecord; const { metadata } = metadataRecord; if (!(0, _common.isRecord)(metadata)) return undefined; const { version, ...policyRecords } = metadata; const isV2 = version === _constants.CIP25_V2; const assetRecords = policyRecords[policyId]; if (!(0, _common.isRecord)(assetRecords)) return undefined; return isV2 ? assetRecords[nameHex] : assetRecords[name]; } const emptyOnChainMetadataRecord = exports.emptyOnChainMetadataRecord = { mintFtMetadata: undefined, mintFtRecordSelected: undefined, mintNftMetadata: undefined, mintNftRecordSelected: undefined }; //# sourceMappingURL=token-onchain-metadata.js.map