Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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/exceptions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@injectivelabs/exceptions",
"version": "1.17.2",
"version": "1.17.3-alpha.0",
"description": "List of exceptions that can be reused throughout Injective's projects.",
"license": "Apache-2.0",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion packages/networks/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@injectivelabs/networks",
"version": "1.17.2",
"version": "1.17.3-alpha.0",
"description": "Endpoints, networks, etc. Can be reused throughout Injective's projects.",
"license": "Apache-2.0",
"author": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@injectivelabs/sdk-ts",
"version": "1.17.2",
"version": "1.17.3-alpha.0",
"description": "SDK in TypeScript for building Injective applications in a browser, node, and react native environment.",
"license": "Apache-2.0",
"author": {
Expand Down
6 changes: 2 additions & 4 deletions packages/sdk-ts/src/client/abacus/grpc/AbacusGrpcApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import { AbacusGrpcTransformer } from './transformers/index.js'

export class AbacusGrpcApi extends BaseGrpcConsumer {
protected module: string = IndexerErrorModule.Abacus
private client: PointsSvcClient

constructor(endpoint: string) {
super(endpoint)
this.client = new PointsSvcClient(this.transport)
private get client() {
return this.initClient(PointsSvcClient)
}

async fetchAccountLatestPoints(address: string) {
Expand Down
53 changes: 45 additions & 8 deletions packages/sdk-ts/src/client/base/BaseGrpcConsumer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,80 @@ import {
} from '@injectivelabs/exceptions'
import { GrpcWebRpcTransport } from './GrpcWebRpcTransport.js'
import type { UnaryCall, RpcOptions } from '@protobuf-ts/runtime-rpc'
import type { GrpcWebTransportAdditionalOptions } from '../../types'

/**
* BaseGrpcConsumer provides base functionality for all gRPC consumers.
* It uses the GrpcWebRpcTransport with GrpcWebFetchTransport from @protobuf-ts/grpcweb-transport.
*/
export default class BaseGrpcConsumer {
protected transport: GrpcWebRpcTransport
private _client: unknown
protected endpoint: string
protected module: string = ''
protected transport: GrpcWebRpcTransport
protected metadata?: Record<string, string>
protected endpoint: string
protected options?: GrpcWebTransportAdditionalOptions

constructor(endpoint: string) {
constructor(endpoint: string, options?: GrpcWebTransportAdditionalOptions) {
this.options = options
this.endpoint = endpoint
this.transport = new GrpcWebRpcTransport(endpoint, {
headers: {},
})
this.transport = new GrpcWebRpcTransport(endpoint, options)
}

/**
* @deprecated Pass options into the constructor instead
*/
public setMetadata(map: Record<string, string>) {
this.metadata = map
// Recreate transport with new metadata
// Recreate transport with new metadata, preserving existing options
this.transport = new GrpcWebRpcTransport(this.endpoint, {
headers: this.metadata,
...this.options,
meta: this.metadata,
})

// Invalidate cached client so initClient creates a new client with updated transport
this._client = undefined

return this
}

/**
* @deprecated Manage options within the constructor instead
*/
public clearMetadata() {
this.metadata = undefined
// Recreate transport without metadata, preserving existing options
this.transport = new GrpcWebRpcTransport(this.endpoint, this.options)
// Invalidate cached client so initClient creates a new client with updated transport
this._client = undefined
}

public getTransport(): GrpcWebRpcTransport {
return this.transport
}

/**
* Lazily initializes and returns the gRPC client.
* Call this from a getter in subclasses to avoid constructor boilerplate.
*
* @example
* private get client() {
* return this.initClient(MyGrpcClient)
* }
*/
protected initClient<TClient>(
ClientClass: new (transport: GrpcWebRpcTransport) => TClient,
): TClient {
if (!this._client) {
this._client = new ClientClass(this.transport)
}

return this._client as TClient
}

/**
* Builds RpcOptions with metadata
* @deprecated Options should be managed externally and passed into the constructor instead
*/
protected getRpcOptions(): RpcOptions {
const options: RpcOptions = {
Expand Down
11 changes: 2 additions & 9 deletions packages/sdk-ts/src/client/base/GrpcWebRpcTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
ClientStreamingCall,
DuplexStreamingCall,
} from '@protobuf-ts/runtime-rpc'
import type { GrpcWebTransportAdditionalOptions } from '../../types'

/**
* GrpcWebRpcTransport provides a simple wrapper around GrpcWebFetchTransport
Expand All @@ -18,15 +19,7 @@ import type {
export class GrpcWebRpcTransport implements RpcTransport {
private transport: RpcTransport

constructor(
baseUrl: string,
options?: {
fetch?: typeof fetch
headers?: Record<string, string>
timeout?: number
credentials?: RequestCredentials
},
) {
constructor(baseUrl: string, options?: GrpcWebTransportAdditionalOptions) {
this.transport = getGrpcWebTransport(baseUrl, options)
}

Expand Down
9 changes: 2 additions & 7 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcAuctionApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ import { QueryClient as InjectiveAuctionV1Beta1QueryClient } from '@injectivelab
import { ChainModule } from '../types/index.js'
import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
import { ChainGrpcAuctionTransformer } from '../transformers/index.js'

/**
* @category Chain Grpc API
*/
export class ChainGrpcAuctionApi extends BaseGrpcConsumer {
protected module: string = ChainModule.Auction

private client: InjectiveAuctionV1Beta1QueryClient

constructor(endpoint: string) {
super(endpoint)

this.client = new InjectiveAuctionV1Beta1QueryClient(this.transport)
private get client() {
return this.initClient(InjectiveAuctionV1Beta1QueryClient)
}

async fetchModuleParams() {
Expand Down
7 changes: 2 additions & 5 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcAuthApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
import { ChainGrpcAuthTransformer } from '../transformers/ChainGrpcAuthTransformer.js'
import { ChainGrpcCommonTransformer } from '../transformers/ChainGrpcCommonTransformer.js'
import type { PaginationOption } from '../../../types/pagination.js'

/**
* @category Chain Grpc API
*/
export class ChainGrpcAuthApi extends BaseGrpcConsumer {
protected module: string = ChainModule.Auth
private client: CosmosAuthV1BetaQueryClient

constructor(endpoint: string) {
super(endpoint)
this.client = new CosmosAuthV1BetaQueryClient(this.transport)
private get client() {
return this.initClient(CosmosAuthV1BetaQueryClient)
}

async fetchModuleParams() {
Expand Down
7 changes: 2 additions & 5 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcAuthZApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
import { ChainGrpcAuthZTransformer } from '../transformers/ChainGrpcAuthZTransformer.js'
import { ChainGrpcCommonTransformer } from '../transformers/ChainGrpcCommonTransformer.js'
import type { PaginationOption } from '../../../types/pagination.js'

/**
* @category Chain Grpc API
*/
export class ChainGrpcAuthZApi extends BaseGrpcConsumer {
protected module: string = ChainModule.Authz
private client: CosmosAuthzV1BetaQueryClient

constructor(endpoint: string) {
super(endpoint)
this.client = new CosmosAuthzV1BetaQueryClient(this.transport)
private get client() {
return this.initClient(CosmosAuthzV1BetaQueryClient)
}

async fetchGrants({
Expand Down
7 changes: 2 additions & 5 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcBankApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@ import { ChainGrpcBankTransformer } from '../transformers/index.js'
import { fetchAllWithPagination } from '../../../utils/pagination.js'
import { ChainGrpcCommonTransformer } from '../transformers/ChainGrpcCommonTransformer.js'
import type { PaginationOption } from '../../../types/pagination.js'

const MAX_LIMIT_FOR_SUPPLY = 10000

/**
* @category Chain Grpc API
*/
export class ChainGrpcBankApi extends BaseGrpcConsumer {
protected module: string = ChainModule.Bank
private client: CosmosBankV1BetaQueryClient

constructor(endpoint: string) {
super(endpoint)
this.client = new CosmosBankV1BetaQueryClient(this.transport)
private get client() {
return this.initClient(CosmosBankV1BetaQueryClient)
}

async fetchModuleParams() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
import { ChainGrpcDistributionTransformer } from '../transformers/index.js'
import type { Coin } from '@injectivelabs/ts-types'
import type { ValidatorRewards } from '../types/index.js'

/**
* @category Chain Grpc API
*/
export class ChainGrpcDistributionApi extends BaseGrpcConsumer {
protected module: string = ChainModule.Distribution
private client: CosmosDistributionV1Beta1QueryClient

constructor(endpoint: string) {
super(endpoint)
this.client = new CosmosDistributionV1Beta1QueryClient(this.transport)
private get client() {
return this.initClient(CosmosDistributionV1Beta1QueryClient)
}

async fetchModuleParams() {
Expand Down
9 changes: 2 additions & 7 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcErc20Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ChainGrpcCommonTransformer,
} from '../transformers/index.js'
import type { PaginationOption } from '../../../types/pagination.js'

const MAX_LIMIT_FOR_SUPPLY = 10000

/**
Expand All @@ -17,12 +16,8 @@ const MAX_LIMIT_FOR_SUPPLY = 10000
export class ChainGrpcErc20Api extends BaseGrpcConsumer {
protected module: string = ChainModule.Erc20

private client: InjectiveErc20V1Beta1QueryClient

constructor(endpoint: string) {
super(endpoint)

this.client = new InjectiveErc20V1Beta1QueryClient(this.transport)
private get client() {
return this.initClient(InjectiveErc20V1Beta1QueryClient)
}

async fetchModuleParams() {
Expand Down
9 changes: 2 additions & 7 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcEvmApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ import { QueryClient as InjectiveEvmV1QueryClient } from '@injectivelabs/core-pr
import { ChainModule } from '../types/index.js'
import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
import { ChainGrpcEvmTransformer } from '../transformers/index.js'

/**
* @category Chain Grpc API
*/
export class ChainGrpcEvmApi extends BaseGrpcConsumer {
protected module: string = ChainModule.Evm

private client: InjectiveEvmV1QueryClient

constructor(endpoint: string) {
super(endpoint)

this.client = new InjectiveEvmV1QueryClient(this.transport)
private get client() {
return this.initClient(InjectiveEvmV1QueryClient)
}

async fetchAccount(ethAddress: string) {
Expand Down
7 changes: 2 additions & 5 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcExchangeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
import { ChainGrpcExchangeTransformer } from '../transformers/index.js'
import type * as InjectiveExchangeV1Beta1GenesisPb from '@injectivelabs/core-proto-ts-v2/generated/injective/exchange/v1beta1/genesis_pb'
import type * as InjectiveExchangeV1Beta1ExchangePb from '@injectivelabs/core-proto-ts-v2/generated/injective/exchange/v1beta1/exchange_pb'

/**
* @category Chain Grpc API
*/
export class ChainGrpcExchangeApi extends BaseGrpcConsumer {
protected module: string = ChainModule.Exchange
private client: InjectiveExchangeV1Beta1QueryClient

constructor(endpoint: string) {
super(endpoint)
this.client = new InjectiveExchangeV1Beta1QueryClient(this.transport)
private get client() {
return this.initClient(InjectiveExchangeV1Beta1QueryClient)
}

async fetchModuleParams() {
Expand Down
7 changes: 2 additions & 5 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcGovApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ import { ChainGrpcGovTransformer } from '../transformers/ChainGrpcGovTransformer
import { ChainGrpcCommonTransformer } from '../transformers/ChainGrpcCommonTransformer.js'
import type * as CosmosGovV1GovPb from '@injectivelabs/core-proto-ts-v2/generated/cosmos/gov/v1/gov_pb'
import type { PaginationOption } from '../../../types/pagination.js'

/**
* @category Chain Grpc API
*/
export class ChainGrpcGovApi extends BaseGrpcConsumer {
protected module: string = ChainModule.Gov
private client: CosmosGovV1QueryClient

constructor(endpoint: string) {
super(endpoint)
this.client = new CosmosGovV1QueryClient(this.transport)
private get client() {
return this.initClient(CosmosGovV1QueryClient)
}

async fetchModuleParams() {
Expand Down
7 changes: 2 additions & 5 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcIbcApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import { ChainModule } from '../types/index.js'
import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
import { ChainGrpcCommonTransformer } from '../transformers/ChainGrpcCommonTransformer.js'
import type { PaginationOption } from '../../../types/pagination.js'

/**
* @category Chain Grpc API
*/
export class ChainGrpcIbcApi extends BaseGrpcConsumer {
protected module: string = ChainModule.Ibc
private client: IbcApplicationsTransferV1QueryClient

constructor(endpoint: string) {
super(endpoint)
this.client = new IbcApplicationsTransferV1QueryClient(this.transport)
private get client() {
return this.initClient(IbcApplicationsTransferV1QueryClient)
}

async fetchDenomTrace(hash: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ import { QueryClient as InjectiveInsuranceV1Beta1QueryClient } from '@injectivel
import { ChainModule } from '../types/index.js'
import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
import { ChainGrpcInsuranceFundTransformer } from '../transformers/ChainGrpcInsuranceFundTransformer.js'

/**
* @category Chain Grpc API
*/
export class ChainGrpcInsuranceFundApi extends BaseGrpcConsumer {
protected module: string = ChainModule.InsuranceFund
private client: InjectiveInsuranceV1Beta1QueryClient

constructor(endpoint: string) {
super(endpoint)
this.client = new InjectiveInsuranceV1Beta1QueryClient(this.transport)
private get client() {
return this.initClient(InjectiveInsuranceV1Beta1QueryClient)
}

async fetchModuleParams() {
Expand Down
Loading