-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c915a9
commit e3b2dc8
Showing
2 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
160 changes: 160 additions & 0 deletions
160
packages/sdk-ts/src/client/chain/grpc/ChainGrpcAuthZApi.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
packages/sdk-ts/src/client/chain/transformers/ChainGrpcAuthZTransformer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
), | ||
} | ||
} | ||
} |