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/permissions rework canary #513

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions packages/sdk-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@injectivelabs/sdk-ts",
"description": "SDK in TypeScript for building Injective applications in a browser, node, and react native environment.",
"version": "1.14.19",
"version": "1.14.20-alpha.0",
"sideEffects": false,
"license": "Apache-2.0",
"author": {
Expand Down Expand Up @@ -39,7 +39,7 @@
"@cosmjs/proto-signing": "^0.32.3",
"@cosmjs/stargate": "^0.32.3",
"@ethersproject/bytes": "^5.7.0",
"@injectivelabs/core-proto-ts": "1.13.3",
"@injectivelabs/core-proto-ts": "1.13.3-dev.0",
"@injectivelabs/exceptions": "^1.14.19",
"@injectivelabs/grpc-web": "^0.0.1",
"@injectivelabs/grpc-web-node-http-transport": "^0.0.2",
Expand Down
215 changes: 215 additions & 0 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcPermissionsApi._ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
import {
GrpcUnaryRequestException,
UnspecifiedErrorCode,
} from '@injectivelabs/exceptions'
import { InjectivePermissionsV1Beta1Query } from '@injectivelabs/core-proto-ts'
import BaseGrpcConsumer from '../../base/BaseGrpcConsumer.js'
import { ChainGrpcPermissionsTransformer } from '../transformers/index.js'
import { ChainModule } from '../types/index.js'

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

protected client: InjectivePermissionsV1Beta1Query.QueryClientImpl

constructor(endpoint: string) {
super(endpoint)

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

async fetchAddressesByRole({ denom, role }: { denom: string; role: string }) {
const request =
InjectivePermissionsV1Beta1Query.QueryAddressesByRoleRequest.create()

request.denom = denom
request.role = role

try {
const response =
await this.retry<InjectivePermissionsV1Beta1Query.QueryAddressesByRoleResponse>(
() => this.client.AddressesByRole(request, this.metadata),
)

return ChainGrpcPermissionsTransformer.addressesByRolesResponseToAddressesByRoles(
response,
)
} catch (e: unknown) {
if (e instanceof InjectivePermissionsV1Beta1Query.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'AddressesByRole',
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'AddressesByRole',
})
}
Comment on lines +43 to +54
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix incorrect usage of 'GrpcWebError' in error handling.

The check e instanceof InjectivePermissionsV1Beta1Query.GrpcWebError may be incorrect because GrpcWebError is likely not a property of InjectivePermissionsV1Beta1Query. Ensure that GrpcWebError is properly imported from the correct module and used appropriately.

Apply this diff to correct the error handling:

+ import { GrpcWebError } from '@injectivelabs/exceptions'

...

- if (e instanceof InjectivePermissionsV1Beta1Query.GrpcWebError) {
+ if (e instanceof GrpcWebError) {

Committable suggestion skipped: line range outside the PR's diff.

}

async fetchAddressRoles({
address,
denom,
}: {
address: string
denom: string
}) {
const request =
InjectivePermissionsV1Beta1Query.QueryAddressRolesRequest.create()

request.address = address
request.denom = denom

try {
const response =
await this.retry<InjectivePermissionsV1Beta1Query.QueryAddressRolesResponse>(
() => this.client.AddressRoles(request, this.metadata),
)

return ChainGrpcPermissionsTransformer.addressRolesResponseToAddressRoles(
response,
)
} catch (e: unknown) {
if (e instanceof InjectivePermissionsV1Beta1Query.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'AddressRoles',
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'AddressRoles',
})
}
}

async fetchAllNamespaces() {
const request =
InjectivePermissionsV1Beta1Query.QueryAllNamespacesRequest.create()

try {
const response =
await this.retry<InjectivePermissionsV1Beta1Query.QueryAllNamespacesResponse>(
() => this.client.AllNamespaces(request, this.metadata),
)

return ChainGrpcPermissionsTransformer.allNamespacesResponseToAllNamespaces(
response,
)
} catch (e: unknown) {
if (e instanceof InjectivePermissionsV1Beta1Query.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'AllNamespaces',
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'AllNamespaces',
})
}
}

async fetchModuleParams() {
const request = InjectivePermissionsV1Beta1Query.QueryParamsRequest.create()

try {
const response =
await this.retry<InjectivePermissionsV1Beta1Query.QueryParamsResponse>(
() => this.client.Params(request, this.metadata),
)

return ChainGrpcPermissionsTransformer.moduleParamsResponseToModuleParams(
response,
)
} catch (e: unknown) {
if (e instanceof InjectivePermissionsV1Beta1Query.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'Params',
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'Params',
})
}
}

async fetchNamespaceByDenom({
denom,
includeRoles,
}: {
denom: string
includeRoles: boolean
}) {
const request =
InjectivePermissionsV1Beta1Query.QueryNamespaceByDenomRequest.create()

request.denom = denom
request.includeRoles = includeRoles

try {
const response =
await this.retry<InjectivePermissionsV1Beta1Query.QueryNamespaceByDenomResponse>(
() => this.client.NamespaceByDenom(request, this.metadata),
)

return ChainGrpcPermissionsTransformer.namespaceByDenomResponceToNamespaceByDenom(
response,
)
Comment on lines +168 to +170
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Correct typo in transformer method name.

There's a typo in the method name namespaceByDenomResponceToNamespaceByDenom. The word "Responce" should be "Response".

Apply this diff to fix the typo:

- return ChainGrpcPermissionsTransformer.namespaceByDenomResponceToNamespaceByDenom(
+ return ChainGrpcPermissionsTransformer.namespaceByDenomResponseToNamespaceByDenom(
    response,
)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return ChainGrpcPermissionsTransformer.namespaceByDenomResponceToNamespaceByDenom(
response,
)
return ChainGrpcPermissionsTransformer.namespaceByDenomResponseToNamespaceByDenom(
response,
)

} catch (e: unknown) {
if (e instanceof InjectivePermissionsV1Beta1Query.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'NamespaceByDenom',
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'NamespaceByDenom',
})
}
}

async fetchVouchersForAddress({ address }: { address: string }) {
const request =
InjectivePermissionsV1Beta1Query.QueryVouchersForAddressRequest.create()

request.address = address

try {
const response =
await this.retry<InjectivePermissionsV1Beta1Query.QueryVouchersForAddressResponse>(
() => this.client.VouchersForAddress(request, this.metadata),
)

return ChainGrpcPermissionsTransformer.vouchersForAddressResponseToVouchersForAddress(
response,
)
} catch (e: unknown) {
if (e instanceof InjectivePermissionsV1Beta1Query.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'VouchersForAddress',
})
}

throw new GrpcUnaryRequestException(e as Error, {
code: UnspecifiedErrorCode,
context: 'VouchersForAddress',
})
}
}
}
Loading