From beb1eb4935e4fd9c156d761cb9487e805a585646 Mon Sep 17 00:00:00 2001 From: Bojan Angjelkoski Date: Tue, 7 Jun 2022 18:49:21 +0300 Subject: [PATCH] fix: gov modules --- .../src/client/chain/grpc/ChainGrpcGovApi.ts | 41 ++++++++++++++----- .../transformers/ChainGrpcGovTransformer.ts | 31 ++++++++++++++ 2 files changed, 61 insertions(+), 11 deletions(-) diff --git a/packages/sdk-ts/src/client/chain/grpc/ChainGrpcGovApi.ts b/packages/sdk-ts/src/client/chain/grpc/ChainGrpcGovApi.ts index 647c29ede..50f6a0bd5 100644 --- a/packages/sdk-ts/src/client/chain/grpc/ChainGrpcGovApi.ts +++ b/packages/sdk-ts/src/client/chain/grpc/ChainGrpcGovApi.ts @@ -20,21 +20,40 @@ import { paginationRequestFromPagination } from '../../../utils/pagination' import { ChainGrpcGovTransformer } from '../transformers/ChainGrpcGovTransformer' export class ChainGrpcGovApi extends BaseConsumer { - async fetchParamsByType(type: string) { - const request = new QueryGovernanceParamsRequest() + async fetchModuleParams() { + const paramTypes = ['voting', 'deposit', 'tallying'] + const requests = paramTypes.map((type) => { + const request = new QueryGovernanceParamsRequest() + request.setParamsType(type) - request.setParamsType(type) + return request + }) try { - const response = await this.request< - QueryGovernanceParamsRequest, - QueryGovernanceParamsResponse, - typeof GovernanceQuery.Params - >(request, GovernanceQuery.Params) - - return ChainGrpcGovTransformer.moduleParamsResponseToModuleParams( - response, + const responses = await Promise.all( + requests.map((request) => + this.request< + QueryGovernanceParamsRequest, + QueryGovernanceParamsResponse, + typeof GovernanceQuery.Params + >(request, GovernanceQuery.Params), + ), ) + const votingParams = responses.find((response) => + response.hasVotingParams(), + )! + const tallyParams = responses.find((response) => + response.hasTallyParams(), + )! + const depositParams = responses.find((response) => + response.hasDepositParams(), + )! + + return ChainGrpcGovTransformer.moduleParamsResponseToModuleParamsByType({ + votingParams: votingParams.getVotingParams()!, + tallyParams: tallyParams.getTallyParams()!, + depositParams: depositParams.getDepositParams()!, + }) } catch (e: any) { throw new Error(e.message) } diff --git a/packages/sdk-ts/src/client/chain/transformers/ChainGrpcGovTransformer.ts b/packages/sdk-ts/src/client/chain/transformers/ChainGrpcGovTransformer.ts index c418a1818..3668d428e 100644 --- a/packages/sdk-ts/src/client/chain/transformers/ChainGrpcGovTransformer.ts +++ b/packages/sdk-ts/src/client/chain/transformers/ChainGrpcGovTransformer.ts @@ -15,6 +15,9 @@ import { Vote, TallyResult, GrpcTallyResult, + GrpcGovernanceDepositParams, + GrpcGovernanceVotingParams, + GrpcGovernanceTallyParams, } from '../types/gov' import { Pagination } from '../../../types/index' import { grpcPaginationToPagination } from '../../../utils/pagination' @@ -47,6 +50,34 @@ export class ChainGrpcGovTransformer { } } + static moduleParamsResponseToModuleParamsByType({ + depositParams, + votingParams, + tallyParams, + }: { + depositParams: GrpcGovernanceDepositParams + votingParams: GrpcGovernanceVotingParams + tallyParams: GrpcGovernanceTallyParams + }): GovModuleStateParams { + return { + depositParams: { + minDepositList: depositParams + ?.getMinDepositList() + .map((m) => m.toObject()), + maxDepositPeriod: + depositParams?.getMaxDepositPeriod()?.getSeconds() || 0, + }, + votingParams: { + votingPeriod: votingParams.getVotingPeriod()?.getSeconds() || 0, + }, + tallyParams: { + quorum: uint8ArrayToString(tallyParams.getQuorum()) || '', + threshold: uint8ArrayToString(tallyParams.getThreshold()) || '', + vetoThreshold: uint8ArrayToString(tallyParams.getVetoThreshold()) || '', + }, + } + } + static proposalResponseToProposal(response: QueryProposalResponse): Proposal { const proposal = response.getProposal()!