blockchain-api
Version:
API utilities for interacting with the Exatechl2 blockchain
45 lines (44 loc) • 1.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.search = search;
const explorerApi_1 = require("../../core/explorerApi");
const logger_1 = require("../../utils/logger");
/**
* Search for blocks, transactions, addresses, or tokens
*
* @param {string} query - The search query (block number, transaction hash, address, etc.)
* @returns {Promise<SearchResult | null>} - Basic search result with type and ID, or null if not found
*/
async function search(query) {
var _a, _b, _c, _d;
try {
logger_1.logger.debug(`Searching for: ${query}`);
const response = await (0, explorerApi_1.callExplorerApi)('search', { q: query });
if (response.status !== 'success' || !response.type) {
return null;
}
let id;
switch (response.type) {
case 'block':
id = ((_a = response.data) === null || _a === void 0 ? void 0 : _a.number) || ((_b = response.data) === null || _b === void 0 ? void 0 : _b.hash);
break;
case 'transaction':
id = (_c = response.data) === null || _c === void 0 ? void 0 : _c.hash;
break;
case 'address':
case 'token':
id = (_d = response.data) === null || _d === void 0 ? void 0 : _d.address;
break;
default:
id = query;
}
return {
type: response.type,
id: id
};
}
catch (error) {
logger_1.logger.error('Error in search:', error);
return null;
}
}