Skip to content

Commit

Permalink
feat: explorer stats api
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-moore committed Sep 10, 2024
1 parent 83a405d commit 9fdd911
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,23 @@ describe('IndexerGrpcExplorerApi', () => {
)
}
})

test('fetchExplorerStats', async () => {
try {
const response = await indexerGrpcExplorerApi.fetchExplorerStats()

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof IndexerGrpcExplorerTransformer.getExplorerStatsResponseToExplorerStats
>
>(response),
)
} catch (e) {
console.error(
'IndexerGrpcExplorerApi.fetchExplorerStats => ' + (e as any).message,
)
}
})
})
28 changes: 28 additions & 0 deletions packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcExplorerApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -540,4 +540,32 @@ export class IndexerGrpcExplorerApi extends BaseGrpcConsumer {
})
}
}

async fetchExplorerStats() {
const request = InjectiveExplorerRpc.GetStatsRequest.create()

try {
const response = await this.retry<InjectiveExplorerRpc.GetStatsResponse>(
() => this.client.GetStats(request),
)

return IndexerGrpcExplorerTransformer.getExplorerStatsResponseToExplorerStats(
response,
)
} catch (e: unknown) {
if (e instanceof InjectiveExplorerRpc.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'GetExplorerStats',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'GetExplorerStats',
contextModule: this.module,
})
}
}
}
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import {
BankMsgSendTransaction,
Block,
BlockWithTxs,
GasFee,
GrpcBankMsgSendMessage,
GrpcGasFee,
GrpcIndexerValidatorDescription,
GrpcValidatorSlashingEvent,
GrpcValidatorUptime,
Transaction,
ExplorerValidator,
ExplorerValidatorDescription,
ValidatorSlashingEvent,
ValidatorUptime,
BlockWithTxs,
IBCTransferTx,
ExplorerStats,
PeggyDepositTx,
ValidatorUptime,
ExplorerValidator,
PeggyWithdrawalTx,
GrpcIBCTransferTx,
GrpcPeggyDepositTx,
GrpcValidatorUptime,
GrpcPeggyWithdrawalTx,
BankMsgSendTransaction,
GrpcBankMsgSendMessage,
ValidatorSlashingEvent,
IndexerStreamTransaction,
GrpcValidatorSlashingEvent,
ExplorerValidatorDescription,
GrpcIndexerValidatorDescription,
} from '../types/explorer'
import { grpcPagingToPaging } from '../../../utils'
import { InjectiveExplorerRpc } from '@injectivelabs/indexer-proto-ts'
Expand Down Expand Up @@ -395,4 +396,19 @@ export class IndexerGrpcExplorerTransformer {
updatedAt: grpcPeggyWithdrawalTx.updatedAt,
}
}

static getExplorerStatsResponseToExplorerStats(
response: InjectiveExplorerRpc.GetStatsResponse,
): ExplorerStats {
return {
assets: response.assets,
txsTotal: response.txsTotal,
addresses: response.addresses,
injSupply: response.injSupply,
txsInPast24Hours: response.txs24H,
blockCountInPast24Hours: response.blockCount24H,
txsPerSecondInPast24Hours: response.txsPs24H,
txsPerSecondInPast100Blocks: response.txsPs100B,
}
}
}
12 changes: 12 additions & 0 deletions packages/sdk-ts/src/client/indexer/types/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,17 @@ export interface CosmWasmChecksum {
hash: string
}

export interface ExplorerStats {
assets: string
txsTotal: string
addresses: string
injSupply: string
txsInPast24Hours: string
blockCountInPast24Hours: string
txsPerSecondInPast24Hours: string
txsPerSecondInPast100Blocks: string
}

export type GrpcIBCTransferTx = InjectiveExplorerRpc.IBCTransferTx
export type GrpcPeggyDepositTx = InjectiveExplorerRpc.PeggyDepositTx
export type GrpcPeggyWithdrawalTx = InjectiveExplorerRpc.PeggyWithdrawalTx
Expand All @@ -387,3 +398,4 @@ export type GrpcValidatorUptime = InjectiveExplorerRpc.ValidatorUptime
export type GrpcIndexerValidatorDescription =
InjectiveExplorerRpc.ValidatorDescription
export type GrpcValidatorSlashingEvent = InjectiveExplorerRpc.SlashingEvent
export type GrpcExplorerStats = InjectiveExplorerRpc.GetStatsResponse

0 comments on commit 9fdd911

Please sign in to comment.