@bugbytes/hapi-mirror
Version:
Hedera Mirror Node Client
209 lines • 10.1 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MirrorRestClient = void 0;
const hapi_util_1 = require("@bugbytes/hapi-util");
const mirror_error_1 = require("./mirror-error");
class MirrorRestClient {
mirrorHostname;
constructor(mirrorHostname) {
if (!mirrorHostname) {
throw new Error('Mirror Node Hostname is required.');
}
this.mirrorHostname = (mirrorHostname.endsWith('/') ? mirrorHostname.substring(0, mirrorHostname.length - 1) : mirrorHostname);
}
async *getNodes() {
let path = '/api/v1/network/nodes';
while (path) {
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
const payload = await response.json();
for (const item of payload.nodes) {
yield item;
}
path = payload.links.next || '';
}
}
async getTransaction(transactionId) {
const txKey = (typeof transactionId === 'string') ? (0, hapi_util_1.keyString_to_transactionID)(transactionId) : transactionId;
let path = `/api/v1/transactions/${(0, hapi_util_1.transactionID_to_mirrorKeyString)(txKey)}`;
if (txKey.nonce > 0) {
path = `${path}/?nonce=${txKey.nonce}`;
}
if (txKey.scheduled) {
path = txKey.nonce > 0 ? `${path}&scheduled=true` : `${path}/?scheduled=true`;
}
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
const payload = await response.json();
if (payload.transactions && payload.transactions.length > 0) {
return payload.transactions[0];
}
throw new mirror_error_1.MirrorError('Transaction not found.', 400);
}
async getTransactionGroup(transactionId) {
const txKey = (typeof transactionId === 'string') ? (0, hapi_util_1.keyString_to_transactionID)(transactionId) : transactionId;
let path = `/api/v1/transactions/${(0, hapi_util_1.transactionID_to_mirrorKeyString)(txKey)}`;
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
const payload = await response.json();
if (payload.transactions && payload.transactions.length > 0) {
return payload.transactions;
}
throw new mirror_error_1.MirrorError('Transaction not found.', 400);
}
async *getTransactions(accountId) {
const accountKey = (typeof accountId === 'string') ? accountId : (0, hapi_util_1.accountID_to_keyString)(accountId);
let path = `/api/v1/transactions?account.id=${accountKey}&limit=100&order=asc`;
while (path) {
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
const payload = await response.json();
if (payload.transactions) {
for (const item of payload.transactions) {
yield item;
}
}
path = payload.links?.next || '';
}
}
async getLatestTransaction() {
const path = '/api/v1/transactions?limit=1&order=desc';
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
const payload = await response.json();
if (payload.transactions && payload.transactions.length > 0) {
return payload.transactions[0];
}
throw new mirror_error_1.MirrorError('No Transactions found.', 400);
}
async getAccountInfo(accountId) {
const accountKey = (typeof accountId === 'string') ? accountId : (0, hapi_util_1.accountID_to_keyString)(accountId);
const path = `/api/v1/accounts/${accountKey}`;
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
return await response.json();
}
async *getAccountTokens(accountId) {
const accountKey = (typeof accountId === 'string') ? accountId : (0, hapi_util_1.accountID_to_keyString)(accountId);
let path = `/api/v1/accounts/${accountKey}/tokens`;
while (path) {
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
const payload = await response.json();
if (payload.tokens) {
for (const item of payload.tokens) {
yield item;
}
}
path = payload.links?.next || '';
}
}
async *getAccountNfts(accountId) {
const accountKey = (typeof accountId === 'string') ? accountId : (0, hapi_util_1.accountID_to_keyString)(accountId);
let path = `/api/v1/accounts/${accountKey}/nfts`;
while (path) {
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
const payload = await response.json();
if (payload.nfts) {
for (const item of payload.nfts) {
yield item;
}
}
path = payload.links?.next || '';
}
}
async getContractInfo(contractId) {
const contractKey = (typeof contractId === 'string') ? contractId : (0, hapi_util_1.contractID_to_keyString)(contractId);
const path = `/api/v1/contracts/${contractKey}`;
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
return await response.json();
}
async getTokenInfo(tokenId, timestamp = undefined) {
const tokenKey = (typeof tokenId === 'string') ? tokenId : (0, hapi_util_1.tokenID_to_keyString)(tokenId);
const timestampKey = timestamp ? ((typeof timestamp === 'string') ? timestamp : (0, hapi_util_1.timestamp_to_keyString)(timestamp)) : undefined;
const path = timestampKey ?
`/api/v1/tokens/${tokenKey}?timestamp=${timestampKey}` :
`/api/v1/tokens/${tokenKey}`;
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
return await response.json();
}
async getTokenBalance(accountId, tokenId, timestamp = undefined) {
const accountKey = (typeof accountId === 'string') ? accountId : (0, hapi_util_1.accountID_to_keyString)(accountId);
const tokenKey = (typeof tokenId === 'string') ? tokenId : (0, hapi_util_1.tokenID_to_keyString)(tokenId);
const timestampKey = timestamp ? ((typeof timestamp === 'string') ? timestamp : (0, hapi_util_1.timestamp_to_keyString)(timestamp)) : undefined;
const path = timestampKey ?
`/api/v1/tokens/${tokenKey}/balances?account.id=${accountKey}×tamp=lte:${timestampKey}` :
`/api/v1/tokens/${tokenKey}/balances?account.id=${accountKey}`;
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
const payload = await response.json();
const record = payload.balances?.find(b => b.account === accountKey);
if (record) {
return {
timestamp: payload.timestamp,
account: accountKey,
token: tokenKey,
balance: record.balance
};
}
throw new mirror_error_1.MirrorError('No Associated Token Balance.', 404);
}
async getContractResult(transactionId) {
const tx = (typeof transactionId === 'string') ? (0, hapi_util_1.keyString_to_transactionID)(transactionId) : transactionId;
const path = `/api/v1/contracts/results/${(0, hapi_util_1.transactionID_to_mirrorKeyString)(tx)}`;
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
return await response.json();
}
async getMessage(topicId, sequenceNumber) {
const topicKey = (typeof topicId === 'string') ? topicId : (0, hapi_util_1.topicID_to_keyString)(topicId);
const path = `/api/v1/topics/${topicKey}/messages/${sequenceNumber}`;
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
return await response.json();
}
async getLatestMessage(topicId) {
const topicKey = (typeof topicId === 'string') ? topicId : (0, hapi_util_1.topicID_to_keyString)(topicId);
const path = `/api/v1/topics/${topicKey}/messages?limit=1&order=desc`;
const response = await fetch(this.mirrorHostname + path);
if (!response.ok) {
throw new mirror_error_1.MirrorError(response.statusText, response.status);
}
const payload = await response.json();
if (payload.messages && payload.messages.length > 0) {
return payload.messages[0];
}
throw new mirror_error_1.MirrorError('No Messages Found.', 404);
}
}
exports.MirrorRestClient = MirrorRestClient;
//# sourceMappingURL=client.js.map