From b49ea431d2cea203c3b091c8cd1990adbfd19667 Mon Sep 17 00:00:00 2001 From: thomasRalee Date: Wed, 4 Sep 2024 15:42:46 +0800 Subject: [PATCH] refactor: explorer rest typing --- .../indexer/rest/IndexerRestExplorerApi.ts | 34 +++--- .../IndexerRestExplorerTransformer.ts | 44 +++---- .../src/client/indexer/types/explorer-rest.ts | 76 ++----------- .../src/client/indexer/types/explorer.ts | 107 +++++++++++++----- 4 files changed, 134 insertions(+), 127 deletions(-) diff --git a/packages/sdk-ts/src/client/indexer/rest/IndexerRestExplorerApi.ts b/packages/sdk-ts/src/client/indexer/rest/IndexerRestExplorerApi.ts index c778ebb35..2975fb335 100644 --- a/packages/sdk-ts/src/client/indexer/rest/IndexerRestExplorerApi.ts +++ b/packages/sdk-ts/src/client/indexer/rest/IndexerRestExplorerApi.ts @@ -1,35 +1,35 @@ import { MsgStatus, MsgType } from '@injectivelabs/ts-types' -import BaseRestConsumer from '../../base/BaseRestConsumer' +import { + HttpRequestException, + UnspecifiedErrorCode, +} from '@injectivelabs/exceptions' import { Paging, + Contract, + WasmCode, + BankTransfer, ExplorerTransaction, - ExplorerApiResponse, + ContractTransaction, ExplorerBlockWithTxs, ExplorerValidatorUptime, + ExplorerCW20BalanceWithToken, + ContractTransactionWithMessages, +} from '../types/explorer' +import { + ExplorerApiResponse, ContractExplorerApiResponse, + WasmCodeExplorerApiResponse, BlockFromExplorerApiResponse, CW20BalanceExplorerApiResponse, ExplorerApiResponseWithPagination, TransactionFromExplorerApiResponse, + BankTransferFromExplorerApiResponse, ValidatorUptimeFromExplorerApiResponse, ContractTransactionExplorerApiResponse, - WasmCodeExplorerApiResponse, } from '../types/explorer-rest' -import { - Contract, - WasmCode, - BankTransfer, - ContractTransaction, - ExplorerCW20BalanceWithToken, - BankTransferFromExplorerApiResponse, - ContractTransactionWithMessages, -} from '../types/explorer' -import { - HttpRequestException, - UnspecifiedErrorCode, -} from '@injectivelabs/exceptions' -import { IndexerRestExplorerTransformer } from '../transformers' +import BaseRestConsumer from '../../base/BaseRestConsumer' import { Block, ExplorerValidator } from '../types/explorer' +import { IndexerRestExplorerTransformer } from '../transformers' import { IndexerModule } from '../types' const explorerEndpointSuffix = 'api/explorer/v1' diff --git a/packages/sdk-ts/src/client/indexer/transformers/IndexerRestExplorerTransformer.ts b/packages/sdk-ts/src/client/indexer/transformers/IndexerRestExplorerTransformer.ts index 99dee7af5..61c823c25 100644 --- a/packages/sdk-ts/src/client/indexer/transformers/IndexerRestExplorerTransformer.ts +++ b/packages/sdk-ts/src/client/indexer/transformers/IndexerRestExplorerTransformer.ts @@ -1,32 +1,32 @@ import { BigNumberInBase, BigNumberInWei } from '@injectivelabs/utils' +import { + Contract, + WasmCode, + Transaction, + CW20Message, + BankTransfer, + ExplorerTransaction, + ContractTransaction, + ExplorerBlockWithTxs, + ExplorerValidatorUptime, + ExplorerCW20BalanceWithToken, +} from '../types/explorer' import { Block, - ContractTransactionWithMessages, ExplorerValidator, + ContractTransactionWithMessages, } from '../types/explorer' -import { TokenType, TokenVerification } from '../../../types/token' import { - BaseTransaction, - BlockFromExplorerApiResponse, ContractExplorerApiResponse, - ContractTransactionExplorerApiResponse, + WasmCodeExplorerApiResponse, + BlockFromExplorerApiResponse, CW20BalanceExplorerApiResponse, - ExplorerBlockWithTxs, - ExplorerTransaction, - ExplorerValidatorUptime, TransactionFromExplorerApiResponse, + BankTransferFromExplorerApiResponse, + ContractTransactionExplorerApiResponse, ValidatorUptimeFromExplorerApiResponse, - WasmCodeExplorerApiResponse, } from '../types/explorer-rest' -import { - Contract, - WasmCode, - CW20Message, - BankTransfer, - ContractTransaction, - ExplorerCW20BalanceWithToken, - BankTransferFromExplorerApiResponse, -} from '../types/explorer' +import { TokenType, TokenVerification } from '../../../types/token' const ZERO_IN_BASE = new BigNumberInBase(0) @@ -149,7 +149,7 @@ export class IndexerRestExplorerTransformer { } static baseTransactionToTransaction( - transaction: BaseTransaction, + transaction: Transaction, ): ExplorerTransaction { return { ...transaction, @@ -227,6 +227,12 @@ export class IndexerRestExplorerTransformer { type: transaction.messages[0].type, logs: transaction.logs, signatures: transaction.signatures, + messages: (transaction.messages || []) + .filter((m) => m) + .map((message) => ({ + type: message.type, + message: message.value, + })), fee: transaction.gas_fee.amount ? new BigNumberInWei(transaction.gas_fee.amount[0].amount).toBase() : ZERO_IN_BASE, diff --git a/packages/sdk-ts/src/client/indexer/types/explorer-rest.ts b/packages/sdk-ts/src/client/indexer/types/explorer-rest.ts index 2076b7c16..b8df6cd38 100644 --- a/packages/sdk-ts/src/client/indexer/types/explorer-rest.ts +++ b/packages/sdk-ts/src/client/indexer/types/explorer-rest.ts @@ -1,33 +1,14 @@ /* eslint-disable camelcase */ +import { Coin } from '@injectivelabs/ts-types' import { + Paging, EventLog, Signature, - ValidatorUptime, - BlockWithTxs as BaseBlockWithTxs, - Transaction as BaseTransaction, + CosmWasmChecksum, + CosmWasmPermission, + ValidatorUptimeStatus, } from './explorer' -export enum AccessTypeCode { - AccessTypeUnspecified = 0, - AccessTypeNobody = 1, - AccessTypeOnlyAddress = 2, - AccessTypeEverybody = 3, - AccessTypeAnyOfAddresses = 4, -} - -export enum AccessType { - AccessTypeUnspecified = 'Unspecified', - AccessTypeNobody = 'Nobody', - AccessTypeOnlyAddress = 'Only Address', - AccessTypeEverybody = 'Everybody', - AccessTypeAnyOfAddresses = 'Any of Addresses', -} - -export interface Paging { - from: number - to: number - total: number -} export interface ExplorerApiResponse { data: T } @@ -92,50 +73,11 @@ export interface ExplorerBlockApiResponse { data: BlockFromExplorerApiResponse[] } -export interface Message { - type: string - message: any -} - -export interface ExplorerTransaction extends Omit { - memo: string - messages: Message[] - parsedMessages?: Message[] - errorLog?: string - logs?: EventLog[] - claimIds?: number[] -} - -export interface ExplorerBlockWithTxs extends Omit { - txs: ExplorerTransaction[] -} - -export enum ValidatorUptimeStatus { - Proposed = 'proposed', - Signed = 'signed', - Missed = 'missed', -} - export interface ValidatorUptimeFromExplorerApiResponse { block_number: number status: ValidatorUptimeStatus } -export interface ExplorerValidatorUptime - extends Omit { - status: ValidatorUptimeStatus -} - -export interface CosmWasmPermission { - access_type: AccessTypeCode - address: string -} - -export interface CosmWasmChecksum { - algorithm: string - hash: string -} - export interface WasmCodeExplorerApiResponse { checksum: CosmWasmChecksum code_id: number @@ -226,4 +168,10 @@ export interface CW20BalanceExplorerApiResponse { } } -export { BaseTransaction } +export interface BankTransferFromExplorerApiResponse { + sender: string + recipient: string + amounts: Coin[] + block_number: number + block_timestamp: string +} diff --git a/packages/sdk-ts/src/client/indexer/types/explorer.ts b/packages/sdk-ts/src/client/indexer/types/explorer.ts index 22b42abd2..450a68dcc 100644 --- a/packages/sdk-ts/src/client/indexer/types/explorer.ts +++ b/packages/sdk-ts/src/client/indexer/types/explorer.ts @@ -1,9 +1,36 @@ +import { Coin } from '@injectivelabs/ts-types' import { BigNumberInBase } from '@injectivelabs/utils' -import type { CosmWasmChecksum, CosmWasmPermission } from './explorer-rest' import { InjectiveExplorerRpc } from '@injectivelabs/indexer-proto-ts' -import { Coin } from '@injectivelabs/ts-types' import { TokenStatic } from '../../../types/token' +export enum AccessTypeCode { + AccessTypeUnspecified = 0, + AccessTypeNobody = 1, + AccessTypeOnlyAddress = 2, + AccessTypeEverybody = 3, + AccessTypeAnyOfAddresses = 4, +} + +export enum AccessType { + AccessTypeUnspecified = 'Unspecified', + AccessTypeNobody = 'Nobody', + AccessTypeOnlyAddress = 'Only Address', + AccessTypeEverybody = 'Everybody', + AccessTypeAnyOfAddresses = 'Any of Addresses', +} + +export enum ValidatorUptimeStatus { + Proposed = 'proposed', + Signed = 'signed', + Missed = 'missed', +} + +export interface Paging { + from: number + to: number + total: number +} + export interface EventLogEvent { type: string attributes: Array<{ @@ -23,6 +50,11 @@ export interface Signature { sequence: number } +export interface Message { + type: string + message: any +} + export interface IBCTransferTx { sender: string receiver: string @@ -269,23 +301,8 @@ export interface Contract { } } -export interface ContractTransaction { - txHash: string - type: string - code: number - amount: BigNumberInBase - fee: BigNumberInBase - height: number - time: number - data: string - memo: string - tx_number: number - error_log: string - logs: EventLog[] - signatures: Signature[] -} - -export interface ContractTransactionWithMessages extends ContractTransaction { +export interface ContractTransactionWithMessages + extends Omit { messages: Array<{ type: string value: { @@ -310,14 +327,6 @@ export interface WasmCode { proposalId?: number } -export interface BankTransferFromExplorerApiResponse { - sender: string - recipient: string - amounts: Coin[] - block_number: number - block_timestamp: string -} - export interface BankTransfer { sender: string recipient: string @@ -326,6 +335,50 @@ export interface BankTransfer { blockTimestamp: number } +export interface ContractTransaction { + txHash: string + type: string + code: number + messages: Message[] + amount: BigNumberInBase + fee: BigNumberInBase + height: number + time: number + data: string + memo: string + tx_number: number + error_log: string + logs: EventLog[] + signatures: Signature[] +} + +export interface ExplorerTransaction extends Omit { + memo: string + messages: Message[] + errorLog?: string + logs?: EventLog[] + claimIds?: number[] +} + +export interface ExplorerBlockWithTxs extends Omit { + txs: ExplorerTransaction[] +} + +export interface ExplorerValidatorUptime + extends Omit { + status: ValidatorUptimeStatus +} + +export interface CosmWasmPermission { + access_type: AccessTypeCode + address: string +} + +export interface CosmWasmChecksum { + algorithm: string + hash: string +} + export type GrpcIBCTransferTx = InjectiveExplorerRpc.IBCTransferTx export type GrpcPeggyDepositTx = InjectiveExplorerRpc.PeggyDepositTx export type GrpcPeggyWithdrawalTx = InjectiveExplorerRpc.PeggyWithdrawalTx