Utilities and Services
The Standards SDK includes several utility classes and services that provide essential functionality across the SDK components.
Mirror Node Service
The HederaMirrorNode
class provides a streamlined interface for interacting with Hedera Mirror Nodes, offering methods to retrieve account information, topic messages, and pricing data.
Initialization
import { HederaMirrorNode } from '@hashgraphonline/standards-sdk';
import { Logger } from '@hashgraphonline/standards-sdk';
const logger = new Logger({ module: 'MyApp', level: 'info' });
// Basic initialization
const mirrorNode = new HederaMirrorNode('testnet', logger);
// With custom mirror node configuration
const customMirrorNode = new HederaMirrorNode('mainnet', logger, {
customUrl: 'https://mainnet.hedera.api.hgraph.dev/v1/<API-KEY>',
apiKey: 'your-api-key-here'
});
Custom Mirror Node Providers
The SDK supports integration with custom mirror node providers through flexible configuration:
// Using HGraph with API key in URL
const hgraphMirrorNode = new HederaMirrorNode('mainnet', logger, {
customUrl: 'https://mainnet.hedera.api.hgraph.dev/v1/<API-KEY>',
apiKey: 'your-hgraph-api-key'
});
// Using custom provider with headers
const customProviderMirrorNode = new HederaMirrorNode('mainnet', logger, {
customUrl: 'https://custom-mirror-node.com',
apiKey: 'your-api-key',
headers: {
'X-Custom-Header': 'value',
'X-Another-Header': 'another-value'
}
});
The configuration supports:
- Custom URLs: Override the default Hedera mirror node endpoints
- API Keys: Authentication via URL replacement or headers
- Custom Headers: Additional headers for custom provider requirements
Account Information
Retrieve account information, including balances:
// Get account details including balance
const accountInfo = await mirrorNode.requestAccount('0.0.123456');
console.log(
'Account balance:',
accountInfo.balance.balance / 100_000_000,
'HBAR'
);
// Get account memo
const memo = await mirrorNode.getAccountMemo('0.0.123456');
console.log('Account memo:', memo);
// Get account public key
const publicKey = await mirrorNode.getPublicKey('0.0.123456');
console.log('Public key:', publicKey.toString());
Topic Information
Retrieve topic information and messages:
// Get topic info
const topicInfo = await mirrorNode.getTopicInfo('0.0.123456');
console.log('Topic memo:', topicInfo.memo);
// Get topic fees
const fees = await mirrorNode.getTopicFees('0.0.123456');
console.log('Topic fees:', fees);
// Get topic messages with filtering options
const messages = await mirrorNode.getTopicMessages('0.0.123456', {
sequenceNumber: 'gt:100', // Messages after sequence number 100
limit: 50,
order: 'desc'
});
console.log('Topic messages:', messages);
// Get topic messages by filter with time range
const filteredMessages = await mirrorNode.getTopicMessagesByFilter('0.0.123456', {
startTime: '1629400000.000000000',
endTime: '1629500000.000000000',
limit: 100,
order: 'asc'
});
HBAR Pricing
Get current HBAR price for calculating fees:
// Get current HBAR price in USD
const hbarPrice = await mirrorNode.getHBARPrice(new Date());
console.log('Current HBAR price: $', hbarPrice);
// Calculate cost of HBAR needed for operations
const hbarNeeded = 5; // 5 HBAR
console.log('Cost in USD:', hbarNeeded * hbarPrice);
Transaction Information
Retrieve transaction details and scheduled transactions:
// Get transaction by ID or hash
const transaction = await mirrorNode.getTransaction('[email protected]');
console.log('Transaction details:', transaction);
// Get transaction by timestamp
const transactions = await mirrorNode.getTransactionByTimestamp('1629400000.000000000');
console.log('Transactions at timestamp:', transactions);
// Get scheduled transaction info
const scheduleInfo = await mirrorNode.getScheduleInfo('0.0.123456');
console.log('Schedule info:', scheduleInfo);
// Check scheduled transaction status
const status = await mirrorNode.getScheduledTransactionStatus('0.0.123456');
console.log('Schedule executed:', status.executed);
console.log('Executed date:', status.executedDate);
console.log('Schedule deleted:', status.deleted);
Token and NFT Operations
Manage tokens and NFTs:
// Get token information
const tokenInfo = await mirrorNode.getTokenInfo('0.0.123456');
console.log('Token name:', tokenInfo?.name);
console.log('Token symbol:', tokenInfo?.symbol);
// Get account balance in HBAR
const hbarBalance = await mirrorNode.getAccountBalance('0.0.123456');
console.log('HBAR balance:', hbarBalance);
// Get account tokens
const tokens = await mirrorNode.getAccountTokens('0.0.123456', 100);
console.log('Account tokens:', tokens);
// Get account NFTs
const nfts = await mirrorNode.getAccountNfts('0.0.123456', '0.0.789012', 50);
console.log('Account NFTs:', nfts);
// Get specific NFT info
const nftInfo = await mirrorNode.getNftInfo('0.0.123456', 1);
console.log('NFT metadata:', nftInfo?.metadata);
// Get NFTs by token
const tokenNfts = await mirrorNode.getNftsByToken('0.0.123456', {
accountId: '0.0.789012',
limit: 100,
order: 'desc'
});
// Validate NFT ownership
const ownedNft = await mirrorNode.validateNFTOwnership(
'0.0.123456', // accountId
'0.0.789012', // tokenId
1 // serialNumber
);
console.log('NFT owned:', ownedNft !== null);
Airdrop Operations
Manage token airdrops:
// Get outstanding airdrops sent by an account
const sentAirdrops = await mirrorNode.getOutstandingTokenAirdrops('0.0.123456', {
limit: 50,
order: 'desc',
tokenId: '0.0.789012'
});
// Get pending airdrops received by an account
const receivedAirdrops = await mirrorNode.getPendingTokenAirdrops('0.0.123456', {
limit: 50,
order: 'asc',
senderId: '0.0.789012'
});
Smart Contract Operations
Interact with smart contracts:
// Read from a smart contract (eth_call equivalent)
const contractResult = await mirrorNode.readSmartContractQuery(
'0.0.123456', // contract ID or EVM address
'0x06fdde03', // function selector (e.g., name())
'0.0.789012', // payer account ID
{
gas: 50000,
value: 0
}
);
console.log('Contract result:', contractResult?.result);
// Get contract information
const contract = await mirrorNode.getContract('0.0.123456');
console.log('Contract bytecode:', contract?.bytecode);
// Get contract results
const contractResults = await mirrorNode.getContractResults({
from: '0x1234567890abcdef',
limit: 10,
order: 'desc'
});
// Get specific contract result
const result = await mirrorNode.getContractResult('0x1234567890abcdef');
// Get contract state
const state = await mirrorNode.getContractState('0.0.123456', {
slot: '0x0',
limit: 100
});
// Get contract logs
const logs = await mirrorNode.getContractLogs({
topic0: '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef',
limit: 50
});
// Get contract actions
const actions = await mirrorNode.getContractActions('0x1234567890abcdef', {
limit: 25
});
// Get opcode traces
const opcodes = await mirrorNode.getOpcodeTraces('0x1234567890abcdef', {
stack: true,
memory: true,
storage: true
});
Network and Block Information
Retrieve network and blockchain data:
// Get blocks
const blocks = await mirrorNode.getBlocks({
limit: 10,
order: 'desc'
});
// Get specific block
const block = await mirrorNode.getBlock('12345'); // by number or hash
console.log('Block hash:', block?.hash);
// Get network information
const networkInfo = await mirrorNode.getNetworkInfo();
console.log('Network nodes:', networkInfo?.nodes);
// Get network fees
const fees = await mirrorNode.getNetworkFees();
console.log('Current fees:', fees?.fees);
// Get network supply
const supply = await mirrorNode.getNetworkSupply();
console.log('Total supply:', supply?.total_supply);
// Get network stake
const stake = await mirrorNode.getNetworkStake();
console.log('Total stake:', stake?.total_stake_rewarded);