From dab988f1f57748efed815c6aab69d6c99bfcf8d1 Mon Sep 17 00:00:00 2001 From: Eulentier <63156923+Eulentier161@users.noreply.github.com> Date: Thu, 11 Dec 2025 01:34:35 +0100 Subject: [PATCH] added rpc calls get_stats and get_confirmation_history (#14) * add get_stats method and StatsType definitions * add get_confirmation_history method and ConfirmationHistoryRPC definitions * added documentation comment for get_confirmation_history method --- rpc.ts | 15 ++++++++++++- rpc_types.ts | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/rpc.ts b/rpc.ts index 231b216..8db5a0d 100644 --- a/rpc.ts +++ b/rpc.ts @@ -1,4 +1,4 @@ -import type { Address, BlockHash, BlockCountRPC, BlockInfoRPC, BlocksRPC, BlocksInfoRPC, RepresentativesRPC, RepresentativesOnlineRPC, RepresentativesOnlineWeightRPC, AccountHistoryRPC, AccountHistoryRawRPC, AccountInfoRPC, AccountBalanceRPC, AccountsBalancesRPC, AccountRepresentativeRPC, AccountsRepresentativesRPC, AccountWeightRPC, AccountReceivableRPC, AccountReceivableThresholdRPC, AccountReceivableSourceRPC, DelegatorsRPC, DelegatorsCountRPC, TelemetryRPC, TelemetryRawRPC, TelemetryAddressRPC, VersionRPC } from "./rpc_types"; +import type { Address, BlockHash, BlockCountRPC, BlockInfoRPC, BlocksRPC, BlocksInfoRPC, RepresentativesRPC, RepresentativesOnlineRPC, RepresentativesOnlineWeightRPC, AccountHistoryRPC, AccountHistoryRawRPC, AccountInfoRPC, AccountBalanceRPC, AccountsBalancesRPC, AccountRepresentativeRPC, AccountsRepresentativesRPC, AccountWeightRPC, AccountReceivableRPC, AccountReceivableThresholdRPC, AccountReceivableSourceRPC, DelegatorsRPC, DelegatorsCountRPC, TelemetryRPC, TelemetryRawRPC, TelemetryAddressRPC, VersionRPC, StatsType, StatsRPC, ConfirmationHistoryRPC } from "./rpc_types"; /** Implement this interface if the built-in RPC class does not fit your needs. The easiest way to do this is by just extending the built-in RPC class */ export interface RPCInterface { @@ -197,6 +197,19 @@ export class RPC implements RPCInterface { action: "version", })) as VersionRPC; } + + /** https://docs.nano.org/commands/rpc-protocol/#stats */ + async get_stats(type: T): Promise> { + return (await this.call({ + action: "stats", + type, + })) as StatsRPC; + } + + /** https://docs.nano.org/commands/rpc-protocol/#confirmation_history */ + async get_confirmation_history(hash?: string): Promise { + return (await this.call({ action: "confirmation_history" })) as ConfirmationHistoryRPC; + } } export class RPCWithBackup extends RPC { diff --git a/rpc_types.ts b/rpc_types.ts index 666881d..d61e1ed 100644 --- a/rpc_types.ts +++ b/rpc_types.ts @@ -10,6 +10,8 @@ export type BlockSubtype = BlockBasicTypes | "epoch"; export type BlockLegacyTypes = BlockBasicTypes | "open"; export type BlockAllTypes = BlockLegacyTypes | "state"; +export type StatsType = "counters" | "samples" | "objects" | "database"; + export interface BlockNoSignature { type: BlockAllTypes; account: Address; @@ -218,4 +220,64 @@ export interface VersionRPC { build_info: string; } +export interface StatsCountersRPC { + type: "counters"; + created: string; + entries: { + time: string; + type: string; + detail: string; + dir: "in" | "out"; + value: `${number}`; + }[]; +} +export interface StatsSamplesRPC { + type: "samples"; + created: string; + entries: + | "" + | { + time: string; + type: string; + detail: string; + dir: string; + value: string; + }[]; + stat_duration_seconds: `${number}`; +} +export interface StatsUnstableRPC { + [key: string]: string | StatsUnstableRPC; +} + +export type StatsRPC = T extends "counters" ? StatsCountersRPC : T extends "samples" ? StatsSamplesRPC : T extends "objects" | "database" ? StatsUnstableRPC : never; + +export interface ConfirmationHistoryRPC { + confirmation_stats: { + count: `${number}`; + average: `${number}`; + }; + confirmations: + | "" + | [ + { + hash: BlockHash; + duration: `${number}`; + time: `${number}`; + tally: `${number}`; + blocks: `${number}`; + voters: `${number}`; + request_count: `${number}`; + }, + { + hash: BlockHash; + duration: `${number}`; + time: `${number}`; + tally: `${number}`; + blocks: `${number}`; + voters: `${number}`; + request_count: `${number}`; + }, + ]; +} + //