import { AddressClient, GetAccountStatsResponse, GetAccountStatsV1Response, AggregateMessageClient, AlephSocket, BalanceClient, BaseMessageClient, ChannelsListResponse, CursorMessagesResponse, CursorPostsResponse, EstimateInstanceCostConfiguration, EstimatedCostsResponse, FileMetadataResponse, ForgetMessageClient, GetAggregatesConfiguration, GetAccountBalanceConfiguration, GetAccountBalanceResponse, GetAccountChannelsResponse, GetAccountCreditHistoryResponse, GetAccountFilesConfiguration, GetAccountFilesResponse, GetAccountPostTypesResponse, GetAccountStatsConfiguration, GetAccountStatsV1Configuration, GetBalancesConfiguration, GetCreditBalancesConfiguration, GetCreditHistoryConfiguration, GetMessagesConfiguration, GetMessagesCursorConfiguration, GetResourceConsumedCreditsResponse, InstanceMessageClient, MessageContent, MessageError, MessageStatusInfo, MessageType, NodeClient, NodePublicInfo, PaginatedAggregates, PaginatedBalances, PaginatedCreditBalances, PostGetConfiguration, PostGetCursorConfiguration, PostMessageClient, PostResponse, PriceClient, ProgramMessageClient, PublishedMessage, RecalculateCostsResponse, StorageHashResponse, StoreMessageClient, VerifiableProgramMessageClient, VersionResponse, } from '@aleph-sdk/message' export class AlephHttpClient { postClient: PostMessageClient forgetClient: ForgetMessageClient aggregateClient: AggregateMessageClient programClient: ProgramMessageClient instanceClient: InstanceMessageClient vProgramClient: VerifiableProgramMessageClient storeClient: StoreMessageClient messageClient: BaseMessageClient balanceClient: BalanceClient priceClient: PriceClient addressClient: AddressClient nodeClient: NodeClient constructor(apiServer?: string) { this.postClient = new PostMessageClient(apiServer) this.forgetClient = new ForgetMessageClient(apiServer) this.aggregateClient = new AggregateMessageClient(apiServer) this.programClient = new ProgramMessageClient(apiServer) this.instanceClient = new InstanceMessageClient(apiServer) this.vProgramClient = new VerifiableProgramMessageClient(apiServer) this.storeClient = new StoreMessageClient(apiServer) this.messageClient = new BaseMessageClient(apiServer) this.balanceClient = new BalanceClient(apiServer) this.priceClient = new PriceClient(apiServer) this.addressClient = new AddressClient(apiServer) this.nodeClient = new NodeClient(apiServer) } /** * Fetches an aggregate value from the Aleph network. Aggregates are a key-value store on the Aleph network. * Aggregates are bound by address and each address can have multiple keys. * By default, only the address that signs the aggregate can modify it. * * @param address The address of the aggregate's owner * @param key The key of the aggregate to fetch */ async fetchAggregate(address: string, key: string): Promise { const params = { address, keys: [key] } const aggregate = await this.aggregateClient.get(params) return aggregate[key] } /** * Fetches multiple aggregates from the Aleph network. Aggregates are a key-value store on the Aleph network. * Aggregates are bound by address and each address can have multiple keys. * By default, only the address that signs the aggregate can modify it. * * @param address The address of the aggregate's owner * @param keys The keys of the aggregates to fetch */ async fetchAggregates(address: string, keys?: string[]): Promise> { const params = { address, keys } const result = await this.aggregateClient.get(params) return result } /** * Fetches a POST message on the Aleph network. POST messages are used to store arbitrary JSON data on the Aleph network. * They can be queried by type, channel, reference, address, tag, or hash. * POSTs can be modified by amend messages. This endpoint returns the latest version of the POST. * * @param config The configuration used to fetch a post message. */ async getPost(config: PostGetConfiguration) { return await this.postClient.get(config) } /** * Fetches multiple POST messages on the Aleph network. POST messages are used to store arbitrary JSON data on the Aleph network. * * @param config The configuration used to fetch a post message. */ async getPosts(config: PostGetConfiguration) { return await this.postClient.getAll(config) } /** * Downloads a file from the Aleph network. * * @param file_hash The hash of the STORE message of the file to retrieve. */ async downloadFile(file_hash: string): Promise { return await this.storeClient.download(file_hash) } /** * Fetches a stored file by its hash, returning the base64 encoded content and its storage metadata. * * @param fileHash The hash of the file to retrieve. */ async getFile(fileHash: string): Promise { return await this.storeClient.getFile(fileHash) } /** * Fetches a file's metadata from the hash of the STORE message that references it. * * @param messageHash The hash of the STORE message. */ async getFileMetadataByMessageHash(messageHash: string): Promise { return await this.storeClient.getFileMetadataByMessageHash(messageHash) } /** * Fetches a file's metadata from its reference. * * @param ref The reference of the file. */ async getFileMetadataByRef(ref: string): Promise { return await this.storeClient.getFileMetadataByRef(ref) } /** * Fetches the raw stored metadata of a file by its hash. * * @param fileHash The hash of the file. */ async getFileMetadata(fileHash: string): Promise> { return await this.storeClient.getFileMetadata(fileHash) } /** * Fetches the number of pins referencing a given file hash. * * @param hash The hash of the file. */ async getFilePinsCount(hash: string): Promise { return await this.storeClient.getFilePinsCount(hash) } /** * Retrieves raw messages from the Aleph network. Similar to getPosts, but with more options. * * @param config The configuration used to fetch messages. */ async getMessages(config: GetMessagesConfiguration) { return await this.messageClient.getAll(config) } /** * Tries to fetch a specific message from the Aleph network. Throws an error if the message is not found or forgotten. * * @param itemHash The hash of the message to fetch. */ async getMessage(itemHash: string) { return await this.messageClient.get({ hash: itemHash }) } /** * Fetches the reason why a message has been rejected from the network. If the message has no error, returns null. * * @param itemHash The hash of the message to fetch the error from. */ async getMessageError(itemHash: string): Promise { return await this.messageClient.getError(itemHash) } /** * Fetches the processing status of a message (pending, processed, rejected or forgotten), * along with its reception time, without fetching the message content. * * @param itemHash The hash of the message to query. */ async getMessageStatus(itemHash: string): Promise { return await this.messageClient.getStatus(itemHash) } /** * Fetches only the content of a message, without the surrounding message envelope. * * @param itemHash The hash of the message to query. */ async getMessageContent(itemHash: string): Promise { return await this.messageClient.getContent(itemHash) } /** * Retrieves messages using cursor-based pagination. More efficient than page-based pagination for large result sets. * * @param config The configuration used to fetch messages (same filters as getMessages, minus `page`). */ async getMessagesCursor(config: GetMessagesCursorConfiguration): Promise { return await this.messageClient.getCursor(config) } /** * Returns an async iterator over all messages matching the given filters. * Handles cursor-based pagination automatically, yielding individual messages. * * @param config The filters used to query messages (same as getMessages, minus `page`). */ getMessagesIterator( config: Omit, ): AsyncGenerator> { return this.messageClient.getAsyncIterator(config) } /** * Retrieves posts using cursor-based pagination. More efficient than page-based pagination for large result sets. * * @param config The configuration used to fetch posts (same filters as getPosts, minus `page`). */ async getPostsCursor(config: PostGetCursorConfiguration): Promise> { return await this.postClient.getCursor(config) } /** * Returns an async iterator over all posts matching the given filters. * Handles cursor-based pagination automatically, yielding individual posts. * * @param config The filters used to query posts (same as getPosts, minus `page`). */ getPostsIterator(config: Omit): AsyncGenerator> { return this.postClient.getAsyncIterator(config) } /** * Watches for new messages on the Aleph network. This method returns a socket that can be used to listen for new messages. * @param config The filters used to watch for new messages, similar to getMessages. */ async watchMessages(config: Omit): Promise { return this.messageClient.getMessagesSocket(config) } /** * Fetches the token balance of an address, including the locked amount and credit balance. * * @param address The address to query. * @param config Optional chain filter and credit-details toggle. */ async getBalance(address: string, config: GetAccountBalanceConfiguration = {}): Promise { return await this.balanceClient.getBalance(address, config) } /** * Fetches a paginated list of token balances across chains. * * @param config Optional chain filter, minimum balance and pagination. */ async getBalances(config: GetBalancesConfiguration = {}): Promise { return await this.balanceClient.getBalances(config) } /** * Fetches a paginated list of credit balances. * * @param config Optional minimum balance and pagination. */ async getCreditBalances(config: GetCreditBalancesConfiguration = {}): Promise { return await this.balanceClient.getCreditBalances(config) } /** * Fetches the credit history of an address. * * @param address The address to query. * @param config Optional filters and pagination. */ async getCreditHistory( address: string, config: GetCreditHistoryConfiguration = {}, ): Promise { return await this.balanceClient.getCreditHistory(address, config) } /** * Fetches the amount of credits consumed by a resource, identified by its message hash. * * @param itemHash The hash of the resource's message. */ async getConsumedCredits(itemHash: string): Promise { return await this.balanceClient.getConsumedCredits(itemHash) } /** * Estimates the cost of an instance from its content, without publishing a message. * * @param config The instance content to estimate. */ async estimateInstanceCost(config: EstimateInstanceCostConfiguration): Promise { return await this.priceClient.estimateInstanceCost(config) } /** * Triggers a recalculation of message costs and returns a summary of the changes found. */ async recalculateCosts(): Promise { return await this.priceClient.recalculate() } /** * Fetches the files stored by an address, along with their total size. * * @param address The address to query. * @param config Optional file-hash filter, sort order and pagination. */ async getAddressFiles(address: string, config: GetAccountFilesConfiguration = {}): Promise { return await this.addressClient.getFiles(address, config) } /** * Fetches the POST types used by an address. * * @param address The address to query. */ async getAddressPostTypes(address: string): Promise { return await this.addressClient.getPostTypes(address) } /** * Fetches the channels used by an address. * * @param address The address to query. */ async getAddressChannels(address: string): Promise { return await this.addressClient.getChannels(address) } /** * Fetches message statistics for one or more addresses (v0 endpoint). * * @param config Optional list of addresses to filter on. */ async getAddressStats(config: GetAccountStatsConfiguration = {}): Promise { return await this.addressClient.getStats(config) } /** * Fetches paginated message statistics across addresses (v1 endpoint). * * @param config Optional substring filter, sort and pagination. */ async getAddressStatsV1(config: GetAccountStatsV1Configuration = {}): Promise { return await this.addressClient.getStatsV1(config) } /** * Fetches a paginated list of aggregates across addresses. * * @param config Optional key/address filters, sorting and pagination. */ async getAggregatesList(config: GetAggregatesConfiguration = {}): Promise { return await this.aggregateClient.getAggregates(config) } /** * Fetches the node's public information, including its P2P multiaddresses. */ async getNodePublicInfo(): Promise { return await this.nodeClient.getPublicInfo() } /** * Fetches the list of channels known to the node. */ async listChannels(): Promise { return await this.nodeClient.listChannels() } /** * Fetches the node software version. */ async getNodeVersion(): Promise { return await this.nodeClient.getVersion() } } export default AlephHttpClient