Skip to content

Commit

Permalink
Merge pull request #217 from InjectiveLabs/feat/sgt-create-strategy
Browse files Browse the repository at this point in the history
feat/sgt-create-strategy-authz
  • Loading branch information
bangjelkoski authored Aug 10, 2023
2 parents 60358c4 + 58d1310 commit 379f574
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 122 deletions.
106 changes: 103 additions & 3 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcAuthZApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
GrpcUnaryRequestException,
UnspecifiedErrorCode,
GrpcUnaryRequestException,
} from '@injectivelabs/exceptions'
import { CosmosAuthzV1Beta1Query } from '@injectivelabs/core-proto-ts'
import BaseGrpcConsumer from '../../BaseGrpcConsumer'
Expand All @@ -25,9 +25,31 @@ export class ChainGrpcAuthZApi extends BaseGrpcConsumer {
)
}

async fetchGrants(pagination?: PaginationOption) {
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) {
Expand All @@ -45,7 +67,7 @@ export class ChainGrpcAuthZApi extends BaseGrpcConsumer {
if (e instanceof CosmosAuthzV1Beta1Query.GrpcWebError) {
throw new GrpcUnaryRequestException(new Error(e.toString()), {
code: e.code,
context: 'Params',
context: 'Grants',
contextModule: this.module,
})
}
Expand All @@ -57,4 +79,82 @@ export class ChainGrpcAuthZApi extends BaseGrpcConsumer {
})
}
}

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,
})
}
}
}
60 changes: 0 additions & 60 deletions packages/sdk-ts/src/client/chain/grpc/ChainGrpcAuthzApi.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { grpcPaginationToPagination } from '../../../utils/pagination'
import {
GoogleProtobufAny,
CosmosAuthzV1Beta1Authz,
CosmosAuthzV1Beta1Query,
} from '@injectivelabs/core-proto-ts'
Expand All @@ -10,9 +11,18 @@ import {
export class ChainGrpcAuthZTransformer {
static grpcGrantToGrant(grant: CosmosAuthzV1Beta1Authz.Grant) {
return {
authorization: grant.authorization
? Buffer.from(grant.authorization.value).toString('utf-8')
: '',
authorization: decodeAuthorization(grant.authorization),
expiration: grant.expiration,
}
}

static grpcGrantAuthorizationToGrantAuthorization(
grant: CosmosAuthzV1Beta1Authz.GrantAuthorization,
) {
return {
granter: grant.granter,
grantee: grant.grantee,
authorization: decodeAuthorization(grant.authorization),
expiration: grant.expiration,
}
}
Expand All @@ -25,4 +35,39 @@ export class ChainGrpcAuthZTransformer {
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,
),
}
}
}

const decodeAuthorization = (authorization?: GoogleProtobufAny.Any) => {
if (!authorization) {
return ''
}

switch (authorization.typeUrl) {
case '/cosmos.authz.v1beta1.GenericAuthorization':
return Buffer.from(authorization.value).toString('utf-8')
default:
return Buffer.from(authorization.value).toString('utf-8')
}
}

This file was deleted.

29 changes: 29 additions & 0 deletions packages/sdk-ts/src/client/chain/types/authZ.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
CosmosAuthzV1Beta1Authz,
InjectiveExchangeV1Beta1Authz,
} from '@injectivelabs/core-proto-ts'

export type Grant = CosmosAuthzV1Beta1Authz.Grant
export type GrantAuthorization = CosmosAuthzV1Beta1Authz.GrantAuthorization

export type CreateSpotLimitOrderAuthz =
InjectiveExchangeV1Beta1Authz.CreateSpotLimitOrderAuthz
export type CreateSpotMarketOrderAuthz =
InjectiveExchangeV1Beta1Authz.CreateSpotMarketOrderAuthz
export type BatchCreateSpotLimitOrdersAuthz =
InjectiveExchangeV1Beta1Authz.BatchCreateSpotLimitOrdersAuthz
export type CancelSpotOrderAuthz =
InjectiveExchangeV1Beta1Authz.CancelSpotOrderAuthz
export type BatchCancelSpotOrdersAuthz =
InjectiveExchangeV1Beta1Authz.BatchCancelSpotOrdersAuthz

export type CreateDerivativeLimitOrderAuthz =
InjectiveExchangeV1Beta1Authz.CreateDerivativeLimitOrderAuthz
export type CreateDerivativeMarketOrderAuthz =
InjectiveExchangeV1Beta1Authz.CreateDerivativeMarketOrderAuthz
export type BatchCreateDerivativeLimitOrdersAuthz =
InjectiveExchangeV1Beta1Authz.BatchCreateDerivativeLimitOrdersAuthz
export type CancelDerivativeOrderAuthz =
InjectiveExchangeV1Beta1Authz.CancelDerivativeOrderAuthz
export type BatchCancelDerivativeOrdersAuthz =
InjectiveExchangeV1Beta1Authz.BatchCancelDerivativeOrdersAuthz
2 changes: 2 additions & 0 deletions packages/sdk-ts/src/client/chain/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ChainErrorModule } from '@injectivelabs/exceptions'

export * from './auth'
export * from './authZ'
export * from './auction'
export * from './auth-rest'
export * from './bank-rest'
Expand Down
28 changes: 27 additions & 1 deletion packages/sdk-ts/src/client/indexer/grpc/IndexerGrpcTradingApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,35 @@ export class IndexerGrpcTradingApi extends BaseGrpcConsumer {
)
}

async fetchGridStrategies() {
async fetchGridStrategies({
accountAddress,
subaccountId,
state,
marketId,
}: {
accountAddress?: string
subaccountId?: string
state?: string
marketId?: string
}) {
const request = InjectiveTradingRpc.ListTradingStrategiesRequest.create()

if (accountAddress) {
request.accountAddress = accountAddress
}

if (subaccountId) {
request.subaccountId = subaccountId
}

if (state) {
request.state = state
}

if (marketId) {
request.marketId = marketId
}

try {
const response =
await this.retry<InjectiveTradingRpc.ListTradingStrategiesResponse>(
Expand Down
1 change: 1 addition & 0 deletions packages/sdk-ts/src/client/indexer/grpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { IndexerGrpcInsuranceFundApi } from './IndexerGrpcInsuranceFundApi'
export { IndexerGrpcDerivativesApi } from './IndexerGrpcDerivativesApi'
export { IndexerGrpcSpotApi } from './IndexerGrpcSpotApi'
export { IndexerGrpcTransactionApi } from './IndexerGrpcTransactionApi'
export { IndexerGrpcTradingApi } from './IndexerGrpcTradingApi'
13 changes: 12 additions & 1 deletion packages/sdk-ts/src/core/modules/authz/msgs/MsgGrant.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ const protoParamsAmino = snakecaseKeys({
expiration: new Date(params.expiration! * 1000),
},
})
const protoParamsWeb3 = {
grantee: params.grantee,
granter: params.granter,
grant: {
authorization: {
'@type': '/cosmos.authz.v1beta1.GenericAuthorization',
msg: params.messageType,
},
expiration: new Date(params.expiration! * 1000),
},
}
const message = MsgGrant.fromJSON(params)

describe('MsgGrant', () => {
Expand Down Expand Up @@ -111,7 +122,7 @@ describe('MsgGrant', () => {

expect(web3).toStrictEqual({
'@type': protoType,
...protoParamsAmino,
...protoParamsWeb3,
})
})
})
Loading

0 comments on commit 379f574

Please sign in to comment.