-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #492 from WhisperNode/add_permission_functions_to_sdk
feat: Add permission functions to ts-sdk
- Loading branch information
Showing
23 changed files
with
1,599 additions
and
0 deletions.
There are no files selected for viewing
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
136 changes: 136 additions & 0 deletions
136
packages/sdk-ts/src/client/chain/grpc/ChainGrpcPermissionsApi.spec.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,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
219
packages/sdk-ts/src/client/chain/grpc/ChainGrpcPermissionsApi.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,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', | ||
}) | ||
} | ||
} | ||
} |
Oops, something went wrong.