Skip to content

Commit

Permalink
Merge pull request #492 from WhisperNode/add_permission_functions_to_sdk
Browse files Browse the repository at this point in the history
feat: Add permission functions to ts-sdk
  • Loading branch information
ThomasRalee authored Sep 5, 2024
2 parents 9ad13b4 + be2ede6 commit a6386da
Show file tree
Hide file tree
Showing 23 changed files with 1,599 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/sdk-ts/src/client/chain/ChainGrpcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ChainGrpcInsuranceFundApi } from './grpc/ChainGrpcInsuranceFundApi'
import { ChainGrpcMintApi } from './grpc/ChainGrpcMintApi'
import { ChainGrpcOracleApi } from './grpc/ChainGrpcOracleApi'
import { ChainGrpcPeggyApi } from './grpc/ChainGrpcPeggyApi'
import { ChainGrpcPermissionsApi } from './grpc/ChainGrpcPermissionsApi'
import { ChainGrpcStakingApi } from './grpc/ChainGrpcStakingApi'
import { ChainGrpcTokenFactoryApi } from './grpc/ChainGrpcTokenFactoryApi'
import { ChainGrpcWasmApi } from './grpc/ChainGrpcWasmApi'
Expand Down Expand Up @@ -41,6 +42,8 @@ export class ChainGrpcClient {

peggy: ChainGrpcPeggyApi

permissions: ChainGrpcPermissionsApi

staking: ChainGrpcStakingApi

tokenfactory: ChainGrpcTokenFactoryApi
Expand All @@ -61,6 +64,7 @@ export class ChainGrpcClient {
this.mint = new ChainGrpcMintApi(endpoint)
this.oracle = new ChainGrpcOracleApi(endpoint)
this.peggy = new ChainGrpcPeggyApi(endpoint)
this.permissions = new ChainGrpcPermissionsApi(endpoint)
this.staking = new ChainGrpcStakingApi(endpoint)
this.tokenfactory = new ChainGrpcTokenFactoryApi(endpoint)
this.wasm = new ChainGrpcWasmApi(endpoint)
Expand Down
136 changes: 136 additions & 0 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcPermissionsApi.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { getNetworkEndpoints, Network } from '@injectivelabs/networks'
import { ChainGrpcPermissionsTransformer } from '../transformers'
import { mockFactory } from '@injectivelabs/test-utils'
import { ChainGrpcPermissionsApi } from './ChainGrpcPermissionsApi'
import { INJ_DENOM } from '@injectivelabs/utils'

const endpoints = getNetworkEndpoints(Network.MainnetSentry)
const chainGrpcPermissionsApi = new ChainGrpcPermissionsApi(endpoints.grpc)
const injectiveAddress = mockFactory.injectiveAddress


describe('ChainGrpcPermissionsApi', () => {
test('fetchModuleParams', async () => {
try {
const response = await chainGrpcPermissionsApi.fetchModuleParams()

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof ChainGrpcPermissionsTransformer.moduleParamsResponseToModuleParams
>
>(response),
)
} catch (e) {
console.error(
'chainGrpcPermissionsApi.fetchModuleParams => ' + (e as any).message,
)
}
})
test('fetchAddressRoles', async () => {
try {
const response = await chainGrpcPermissionsApi.fetchAddressRoles({
address: injectiveAddress,
denom: INJ_DENOM,
})

if (response.length === 0) {
console.warn('fetchAddressRoles.arrayIsEmpty')
}

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof ChainGrpcPermissionsTransformer.addressRolesResponseToAddressRoles
>
>(response),
)
} catch (e) {
console.error(
'chainGrpcPermissionsApi.fetchAddressRoles => ' + (e as any).message,
)
}
})
test('fetchAddressesByRoles', async () => {
try {
const response = await chainGrpcPermissionsApi.fetchAddressesByRole({
denom: INJ_DENOM,
role: "role",
})

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof ChainGrpcPermissionsTransformer.addressesByRolesResponseToAddressesByRoles
>
>(response),
)
} catch (e) {
console.error(
'chainGrpcPermissionsApi.fetchAddressesByRoles => ' + (e as any).message,
)
}
})
test('fetchAllNamespaces', async () => {
try {
const response = await chainGrpcPermissionsApi.fetchAllNamespaces()

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof ChainGrpcPermissionsTransformer.allNamespacesResponseToAllNamespaces
>
>(response),
)
} catch (e) {
console.error(
'chainGrpcPermissionsApi.fetchAllNamespaces => ' + (e as any).message,
)
}
})
test('fetchNamespaceByDenom', async () => {
try {
const response = await chainGrpcPermissionsApi.fetchNamespaceByDenom({
denom: INJ_DENOM,
include_roles: true,
})

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof ChainGrpcPermissionsTransformer.namespaceByDenomResponceToNamespaceByDenom
>
>(response),
)
} catch (e) {
console.error(
'chainGrpcPermissionsApi.fetchNamespaceByDenom=> ' + (e as any).message,
)
}
})
test('fetchVouchersForAddress', async () => {
try {
const response = await chainGrpcPermissionsApi.fetchVouchersForAddress({
address: injectiveAddress
})

expect(response).toBeDefined()
expect(response).toEqual(
expect.objectContaining<
ReturnType<
typeof ChainGrpcPermissionsTransformer.vouchersForAddressResponseToVouchersForAddress
>
>(response),
)
} catch (e) {
console.error(
'chainGrpcPermissionsApi.fetchVouchersForAddress => ' + (e as any).message,
)
}
})
})
219 changes: 219 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,219 @@
import {
GrpcUnaryRequestException,
UnspecifiedErrorCode,
} from '@injectivelabs/exceptions'
import { InjectivePermissionsV1Beta1Query } from '@injectivelabs/core-proto-ts'
import BaseGrpcConsumer from '../../base/BaseGrpcConsumer'
import { ChainGrpcPermissionsTransformer } from '../transformers'

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

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',
})
}
}

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,
)
} 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

0 comments on commit a6386da

Please sign in to comment.