Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/denom holders and stats #502

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/sdk-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"@injectivelabs/grpc-web": "^0.0.1",
"@injectivelabs/grpc-web-node-http-transport": "^0.0.2",
"@injectivelabs/grpc-web-react-native-transport": "^0.0.2",
"@injectivelabs/indexer-proto-ts": "1.11.49",
"@injectivelabs/indexer-proto-ts": "1.11.53",
"@injectivelabs/mito-proto-ts": "1.0.65",
"@injectivelabs/networks": "^1.14.14",
"@injectivelabs/test-utils": "^1.14.14",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'
import { INJ_DENOM } from '@injectivelabs/utils'
import { mockFactory } from '@injectivelabs/test-utils'
import { IndexerGrpcArchiverTransformer } from '../transformers'
import { IndexerGrpcArchiverApi } from './IndexerGrpcArchiverApi'

const account = mockFactory.injectiveAddress
const resolution = '1D'
const startDate = 1622505600
const endDate = 1625097600
const startDate = '1622505600'
const endDate = '1625097600'
const limit = 10
const endpoints = getNetworkEndpoints(Network.MainnetSentry)
const indexerGrpcArchiverApi = new IndexerGrpcArchiverApi(endpoints.indexer)
Expand Down Expand Up @@ -173,4 +174,26 @@ describe('IndexerGrpcArchiverApi', () => {
)
}
})

test('fetchDenomHolders', async () => {
try {
const response = await indexerGrpcArchiverApi.fetchDenomHolders({
denom: INJ_DENOM,
limit,
})

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof IndexerGrpcArchiverTransformer.grpcDenomHoldersResponseToDenomHolders
>
>(response),
)
} catch (e) {
console.error(
'IndexerGrpcArchiverApi.fetchDenomHolders => ' + (e as any).message,
)
}
})
})
47 changes: 47 additions & 0 deletions packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcArchiverApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,51 @@ export class IndexerGrpcArchiverApi extends BaseGrpcConsumer {
})
}
}

async fetchDenomHolders({
denom,
token,
limit,
}: {
denom: string
token?: string
Copy link
Collaborator

@ThomasRalee ThomasRalee Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the difference between token and symbol?
Maybe we should rename this as symbol, at least on the FE side it will be clearer

request.token = symbol

limit?: number
}) {
const request = InjectiveArchiverRpc.DenomHoldersRequest.create()

request.denom = denom

if (token) {
request.token = token
}

if (limit) {
request.limit = limit
}

try {
const response =
await this.retry<InjectiveArchiverRpc.DenomHoldersResponse>(() =>
this.client.DenomHolders(request),
)

return IndexerGrpcArchiverTransformer.grpcDenomHoldersResponseToDenomHolders(
response,
)
} catch (e: unknown) {
if (e instanceof InjectiveArchiverRpc.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'DenomHolders',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'DenomHolders',
contextModule: this.module,
})
}
}
}
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,
})
}
}
}
47 changes: 0 additions & 47 deletions packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcPortfolioApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,51 +104,4 @@ export class IndexerGrpcAccountPortfolioApi extends BaseGrpcConsumer {
})
}
}

async fetchAccountPortfolioTokenHolders({
denom,
cursor,
limit,
}: {
denom: string
cursor?: string
limit?: number
}) {
const request = InjectivePortfolioRpc.TokenHoldersRequest.create()

request.denom = denom

if (cursor) {
request.cursor = cursor
}

if (limit) {
request.limit = limit
}

try {
const response =
await this.retry<InjectivePortfolioRpc.TokenHoldersResponse>(() =>
this.client.TokenHolders(request),
)

return IndexerGrpcAccountPortfolioTransformer.tokenHoldersResponseToTokenHolders(
response,
)
} catch (e: unknown) {
if (e instanceof InjectivePortfolioRpc.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'TokenHolders',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'TokenHolders',
contextModule: this.module,
})
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Coin } from '@injectivelabs/ts-types'
import { GrpcCoin } from '../../../types'
import {
TokenHolders,
PositionsWithUPNL,
AccountPortfolioV2,
SubaccountDepositV2,
Expand Down Expand Up @@ -118,16 +117,4 @@ export class IndexerGrpcAccountPortfolioTransformer {
: undefined,
}
}

static tokenHoldersResponseToTokenHolders(
response: InjectivePortfolioRpc.TokenHoldersResponse,
): TokenHolders {
return {
holders: response.holders.map((holder) => ({
accountAddress: holder.accountAddress,
balance: holder.balance,
})),
nextCursors: response.nextCursors,
}
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
DenomHolders,
HistoricalRPNL,
HistoricalBalance,
HistoricalVolumes,
LeaderboardRow,
PnlLeaderboard,
VolLeaderboard,
HistoricalBalance,
HistoricalVolumes,
} from '../types/archiver'
import { InjectiveArchiverRpc } from '@injectivelabs/indexer-proto-ts'

Expand Down Expand Up @@ -133,4 +134,16 @@ export class IndexerGrpcArchiverTransformer {
),
}
}

static grpcDenomHoldersResponseToDenomHolders(
response: InjectiveArchiverRpc.DenomHoldersResponse,
): DenomHolders {
return {
holders: response.holders.map((holder) => ({
accountAddress: holder.accountAddress,
balance: holder.balance,
})),
next: response.next,
}
}
}
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,20 @@ 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,
txsInPast30Days: response.txs30D,
blockCountInPast24Hours: response.blockCount24H,
txsPerSecondInPast24Hours: response.txsPs24H,
txsPerSecondInPast100Blocks: response.txsPs100B,
}
}
}
11 changes: 0 additions & 11 deletions packages/sdk-ts/src/client/indexer/types/account-portfolio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,9 @@ export interface AccountPortfolioBalances {
subaccountsList: PortfolioSubaccountBalanceV2[]
}

export interface Holder {
accountAddress: string
balance: string
}

export interface TokenHolders {
holders: Holder[]
nextCursors: string[]
}

export type GrpcPositionV2 = InjectivePortfolioRpc.DerivativePosition
export type GrpcAccountPortfolioV2 = InjectivePortfolioRpc.Portfolio
export type GrpcSubaccountDepositV2 = InjectivePortfolioRpc.SubaccountDeposit
export type GrpcPositionsWithUPNL = InjectivePortfolioRpc.PositionsWithUPNL
export type GrpcPortfolioSubaccountBalanceV2 =
InjectivePortfolioRpc.SubaccountBalanceV2
export type GrpcTokenHolders = InjectivePortfolioRpc.TokenHoldersResponse
11 changes: 11 additions & 0 deletions packages/sdk-ts/src/client/indexer/types/archiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ export interface VolLeaderboard {
leaders: LeaderboardRow[]
}

export interface Holder {
accountAddress: string
balance: string
}

export interface DenomHolders {
holders: Holder[]
next: string[]
}

export type GrpcHistoricalRPNL = InjectiveArchiverRpc.HistoricalRPNL
export type GrpcHistoricalBalance = InjectiveArchiverRpc.HistoricalBalance
export type GrpcHistoricalVolumes = InjectiveArchiverRpc.HistoricalVolumes
Expand All @@ -44,3 +54,4 @@ export type GrpcVolLeaderboard =
| InjectiveArchiverRpc.VolLeaderboardResponse
| InjectiveArchiverRpc.VolLeaderboardFixedResolutionResponse
export type GrpcLeaderboardRow = InjectiveArchiverRpc.LeaderboardRow
export type GrpcDenomHolders = InjectiveArchiverRpc.DenomHoldersResponse
13 changes: 13 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,18 @@ export interface CosmWasmChecksum {
hash: string
}

export interface ExplorerStats {
assets: string
txsTotal: string
addresses: string
injSupply: string
txsInPast30Days: 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 +399,4 @@ export type GrpcValidatorUptime = InjectiveExplorerRpc.ValidatorUptime
export type GrpcIndexerValidatorDescription =
InjectiveExplorerRpc.ValidatorDescription
export type GrpcValidatorSlashingEvent = InjectiveExplorerRpc.SlashingEvent
export type GrpcExplorerStats = InjectiveExplorerRpc.GetStatsResponse
Loading