Skip to content

Commit

Permalink
chore: minor
Browse files Browse the repository at this point in the history
  • Loading branch information
bangjelkoski committed Aug 10, 2023
1 parent 8c915a9 commit e3b2dc8
Show file tree
Hide file tree
Showing 2 changed files with 221 additions and 0 deletions.
160 changes: 160 additions & 0 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcAuthZApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import {
UnspecifiedErrorCode,
GrpcUnaryRequestException,
} from '@injectivelabs/exceptions'
import { CosmosAuthzV1Beta1Query } from '@injectivelabs/core-proto-ts'
import BaseGrpcConsumer from '../../BaseGrpcConsumer'
import { ChainModule } from '../types'
import { PaginationOption } from '../../../types/pagination'
import { paginationRequestFromPagination } from '../../../utils/pagination'
import { ChainGrpcAuthZTransformer } from '../transformers/ChainGrpcAuthZTransformer'

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

protected client: CosmosAuthzV1Beta1Query.QueryClientImpl

constructor(endpoint: string) {
super(endpoint)

this.client = new CosmosAuthzV1Beta1Query.QueryClientImpl(
this.getGrpcWebImpl(endpoint),
)
}

async fetchGrants({
pagination,
granter,
grantee,
msgTypeUrl,
}: {
pagination?: PaginationOption
granter: string
grantee: string
msgTypeUrl?: string
}) {
const request = CosmosAuthzV1Beta1Query.QueryGrantsRequest.create()

if (granter) {
request.granter = granter
}

if (grantee) {
request.grantee = grantee
}

if (msgTypeUrl) {
request.msgTypeUrl = msgTypeUrl
}

const paginationForRequest = paginationRequestFromPagination(pagination)

if (paginationForRequest) {
request.pagination = paginationForRequest
}

try {
const response =
await this.retry<CosmosAuthzV1Beta1Query.QueryGrantsResponse>(() =>
this.client.Grants(request),
)

return ChainGrpcAuthZTransformer.grpcGrantsToGrants(response)
} catch (e: unknown) {
if (e instanceof CosmosAuthzV1Beta1Query.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'Grants',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'Grants',
contextModule: this.module,
})
}
}

async fetchGranterGrants(granter: string, pagination?: PaginationOption) {
const request = CosmosAuthzV1Beta1Query.QueryGranterGrantsRequest.create()

if (granter) {
request.granter = granter
}

const paginationForRequest = paginationRequestFromPagination(pagination)

if (paginationForRequest) {
request.pagination = paginationForRequest
}

try {
const response =
await this.retry<CosmosAuthzV1Beta1Query.QueryGranterGrantsResponse>(
() => this.client.GranterGrants(request),
)

return ChainGrpcAuthZTransformer.grpcGranterGrantsToGranterGrants(
response,
)
} catch (e: unknown) {
if (e instanceof CosmosAuthzV1Beta1Query.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'GranterGrants',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'GranterGrants',
contextModule: this.module,
})
}
}

async fetchGranteeGrants(grantee: string, pagination?: PaginationOption) {
const request = CosmosAuthzV1Beta1Query.QueryGranteeGrantsRequest.create()

if (grantee) {
request.grantee = grantee
}

const paginationForRequest = paginationRequestFromPagination(pagination)

if (paginationForRequest) {
request.pagination = paginationForRequest
}

try {
const response =
await this.retry<CosmosAuthzV1Beta1Query.QueryGranteeGrantsResponse>(
() => this.client.GranteeGrants(request),
)

return ChainGrpcAuthZTransformer.grpcGranteeGrantsToGranteeGrants(
response,
)
} catch (e: unknown) {
if (e instanceof CosmosAuthzV1Beta1Query.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'GranteeGrants',
contextModule: this.module,
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'GranteeGrants',
contextModule: this.module,
})
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { grpcPaginationToPagination } from '../../../utils/pagination'
import {
CosmosAuthzV1Beta1Authz,
CosmosAuthzV1Beta1Query,
} from '@injectivelabs/core-proto-ts'

/**
* @category Chain Grpc Transformer
*/
export class ChainGrpcAuthZTransformer {
static grpcGrantToGrant(grant: CosmosAuthzV1Beta1Authz.Grant) {
return {
authorization: grant.authorization
? Buffer.from(grant.authorization.value).toString('utf-8')
: '',
expiration: grant.expiration,
}
}

static grpcGrantAuthorizationToGrantAuthorization(
grant: CosmosAuthzV1Beta1Authz.Grant,
) {
return {
authorization: grant.authorization
? Buffer.from(grant.authorization.value).toString('utf-8')
: '',
expiration: grant.expiration,
}
}

static grpcGrantsToGrants(
response: CosmosAuthzV1Beta1Query.QueryGrantsResponse,
) {
return {
pagination: grpcPaginationToPagination(response.pagination!),
grants: response.grants.map(ChainGrpcAuthZTransformer.grpcGrantToGrant),
}
}

static grpcGranteeGrantsToGranteeGrants(
response: CosmosAuthzV1Beta1Query.QueryGranteeGrantsResponse,
) {
return {
pagination: grpcPaginationToPagination(response.pagination!),
grants: response.grants.map(
ChainGrpcAuthZTransformer.grpcGrantAuthorizationToGrantAuthorization,
),
}
}

static grpcGranterGrantsToGranterGrants(
response: CosmosAuthzV1Beta1Query.QueryGranterGrantsResponse,
) {
return {
pagination: grpcPaginationToPagination(response.pagination!),
grants: response.grants.map(
ChainGrpcAuthZTransformer.grpcGrantAuthorizationToGrantAuthorization,
),
}
}
}

0 comments on commit e3b2dc8

Please sign in to comment.