@hsuite/helpers

A comprehensive collection of helper utilities for working with Hedera Hashgraph services, transactions, and configurations. This library provides a set of tools to simplify common operations and standardize interactions with the Hedera network.

Features

  • Transaction Management

    • Process and parse different types of transfers (HBAR, tokens, NFTs)

    • Handle offline transaction workflows

    • Support multi-signature operations

    • Transaction ID generation and management

  • Client Configuration

    • Network selection (Testnet/Mainnet)

    • Operator account management

    • Mirror node connectivity

    • Automatic network updates

  • RESTful API Integration

    • Authenticated mirror node interactions

    • Standardized request/response handling

    • Error management and logging

    • API key support

  • Module Configuration

    • Type-safe configuration management

    • Generic configuration templates

    • Error handling and validation

    • Configuration instance control

Installation

npm install @helpers

Usage

Transaction Helper

Process and extract transfer information from Hedera transactions:

import { TransactionHelper } from '@helpers';

const helper = new TransactionHelper();

// Extract HBAR transfers
const hbarTransfers = helper.getHbarTransfers(transaction);
// Returns: [{token: 'HBAR', account: '0.0.123', amount: 50}, ...]

// Extract fungible token transfers
const tokenTransfers = helper.getTokenTransfers(transaction);
// Returns: [{token: '0.0.456', account: '0.0.123', amount: 100}, ...]

// Extract NFT transfers
const nftTransfers = helper.getNftTransfers(transaction);
// Returns: [{token: '0.0.789', sender: '0.0.123', receiver: '0.0.456', serialNumber: 1}, ...]

Hedera Client Helper

Initialize and configure Hedera clients:

import { HashgraphClientHelper } from '@helpers';
import { LedgerId } from '@hashgraph/sdk';

const helper = new HashgraphClientHelper(
  LedgerId.TESTNET,
  {
    accountId: '0.0.123456',
    privateKey: 'privateKey'
  },
  {
    grpc: 'testnet.mirrornode.hedera.com:443'
  }
);

// Get configured client
const client = helper.getClient();

// Access network information
const network = helper.getNetwork();
const operator = helper.getNodeOperator();

RESTful Helper

Make API calls to Hedera mirror nodes:

import { HashgraphRestfulHelper } from '@helpers';

const helper = new HashgraphRestfulHelper(
  {
    url: 'https://testnet.mirrornode.hedera.com',
    apiKey: 'your-api-key'
  },
  httpService
);

// Make API calls
const accountInfo = await helper.call('accounts/0.0.123456');
const transactionInfo = await helper.call('transactions/[email protected]');
const tokenInfo = await helper.call('tokens/0.0.123456');

Offline Transaction Helper

Handle transactions in offline scenarios:

import { OfflineTransactionHelper } from '@helpers';

const helper = new OfflineTransactionHelper(configService);

// Generate multi-signature keylist
const keyList = await helper.generateMultisigOfMultisig(appPublicKey);

// Get random node for network
const nodeId = helper.getRandomNodeForNetwork(LedgerId.MAINNET);

// Generate transaction ID
const txId = helper.generateTransactionId("0.0.123456");

// Convert transaction to bytes
const bytes = await helper.makeBytes(
  LedgerId.MAINNET,
  transaction,
  "0.0.123456"
);

Configurable Module Helper

Create type-safe configurable modules:

import { ConfigurableModuleHelper } from '@helpers';

interface ConfigInput {
  apiKey: string;
  endpoint: string;
}

class ModuleConfig {
  constructor(data: ConfigInput) {
    // Configuration logic
  }
}

// Create helper instance
const helper = new ConfigurableModuleHelper<ConfigInput, ModuleConfig>(
  { 
    apiKey: "abc123", 
    endpoint: "https://api.example.com" 
  },
  ModuleConfig
);

// Access configuration
const config = helper.config;

API Reference

TransactionHelper

  • getHbarTransfers(transaction): Extract HBAR transfers from a transaction

  • getTokenTransfers(transaction): Extract fungible token transfers from a transaction

  • getNftTransfers(transaction): Extract NFT transfers from a transaction

HashgraphClientHelper

  • getClient(): Get configured Hedera client instance

  • getNetwork(): Get current network identifier

  • getNodeOperator(): Get operator account configuration

HashgraphRestfulHelper

  • call(endpoint): Make GET request to mirror node endpoint

OfflineTransactionHelper

  • generateMultisigOfMultisig(smartAppPublicKey?): Generate multi-signature KeyList

  • getRandomNodeForNetwork(network): Get random node for specified network

  • generateTransactionId(accountId): Generate new transaction ID

  • makeBytes(network, transaction, accountId, transactionId?): Convert transaction to bytes

ConfigurableModuleHelper

  • config: Access the instantiated configuration object

Contributing

Please read our contributing guidelines and code of conduct before submitting pull requests or issues.

License

This project is licensed under the terms specified in the LICENSE file.


Built with ❤️ by the HbarSuite Team Copyright © 2024 HbarSuite. All rights reserved.

Last updated