Add gov extension

This commit is contained in:
Simon Warta 2021-07-27 15:37:36 +02:00
parent d4be3b0d79
commit 07b7bb7ec5
6 changed files with 121 additions and 0 deletions

View File

@ -25,6 +25,7 @@ and this project adheres to
- @cosmjs/faucet: Add new configuration variable `FAUCET_PATH_PATTERN` to
configure the HD path of the faucet accounts ([#832]).
- @cosmjs/cosmwasm-stargate: Add field `ibcPortId` to `Contract` ([#836]).
- @cosmjs/stargate: Add `GovExtension` for query client.
[#832]: https://github.com/cosmos/cosmjs/issues/832
[#836]: https://github.com/cosmos/cosmjs/issues/836

View File

@ -60,12 +60,16 @@ export {
createPagination,
createProtobufRpcClient,
DistributionExtension,
GovExtension,
GovParamsType,
GovProposalId,
IbcExtension,
ProtobufRpcClient,
QueryClient,
setupAuthExtension,
setupBankExtension,
setupDistributionExtension,
setupGovExtension,
setupIbcExtension,
setupStakingExtension,
StakingExtension,

View File

@ -0,0 +1,105 @@
import { Uint64 } from "@cosmjs/math";
import { ProposalStatus } from "cosmjs-types/cosmos/gov/v1beta1/gov";
import {
QueryClientImpl,
QueryDepositResponse,
QueryDepositsResponse,
QueryParamsResponse,
QueryProposalResponse,
QueryProposalsResponse,
QueryTallyResultResponse,
QueryVoteResponse,
QueryVotesResponse,
} from "cosmjs-types/cosmos/gov/v1beta1/query";
import Long from "long";
import { QueryClient } from "./queryclient";
import { createProtobufRpcClient, longify } from "./utils";
export type GovParamsType = "deposit" | "tallying" | "voting";
export type GovProposalId = string | number | Long | Uint64;
export interface GovExtension {
readonly gov: {
readonly params: (parametersType: GovParamsType) => Promise<QueryParamsResponse>;
readonly proposals: (
proposalStatus: ProposalStatus,
depositor: string,
voter: string,
) => Promise<QueryProposalsResponse>;
readonly proposal: (proposalId: GovProposalId) => Promise<QueryProposalResponse>;
readonly deposits: (proposalId: GovProposalId) => Promise<QueryDepositsResponse>;
readonly deposit: (proposalId: GovProposalId, depositorAddress: string) => Promise<QueryDepositResponse>;
readonly tally: (proposalId: GovProposalId) => Promise<QueryTallyResultResponse>;
readonly votes: (proposalId: GovProposalId) => Promise<QueryVotesResponse>;
readonly vote: (proposalId: GovProposalId, voterAddress: string) => Promise<QueryVoteResponse>;
};
}
export function setupGovExtension(base: QueryClient): GovExtension {
const rpc = createProtobufRpcClient(base);
// Use this service to get easy typed access to query methods
// This cannot be used for proof verification
const queryService = new QueryClientImpl(rpc);
return {
gov: {
params: async (parametersType: GovParamsType) => {
const response = await queryService.Params({ paramsType: parametersType });
return response;
},
proposals: async (proposalStatus: ProposalStatus, depositorAddress: string, voterAddress: string) => {
// TODO: pagination
const response = await queryService.Proposals({
proposalStatus,
depositor: depositorAddress,
voter: voterAddress,
pagination: undefined,
});
return response;
},
proposal: async (proposalId: GovProposalId) => {
const response = await queryService.Proposal({ proposalId: longify(proposalId) });
return response;
},
deposits: async (proposalId: GovProposalId) => {
// TODO: pagination
const response = await queryService.Deposits({
proposalId: longify(proposalId),
pagination: undefined,
});
return response;
},
deposit: async (proposalId: GovProposalId, depositorAddress: string) => {
const response = await queryService.Deposit({
proposalId: longify(proposalId),
depositor: depositorAddress,
});
return response;
},
tally: async (proposalId: GovProposalId) => {
const response = await queryService.TallyResult({
proposalId: longify(proposalId),
});
return response;
},
votes: async (proposalId: GovProposalId) => {
// TODO: pagination
const response = await queryService.Votes({
proposalId: longify(proposalId),
pagination: undefined,
});
return response;
},
vote: async (proposalId: GovProposalId, voterAddress: string) => {
const response = await queryService.Vote({
proposalId: longify(proposalId),
voter: voterAddress,
});
return response;
},
},
};
}

View File

@ -7,6 +7,7 @@ export { QueryClient } from "./queryclient";
export { AuthExtension, setupAuthExtension } from "./auth";
export { BankExtension, setupBankExtension } from "./bank";
export { DistributionExtension, setupDistributionExtension } from "./distribution";
export { setupGovExtension, GovExtension, GovProposalId, GovParamsType } from "./gov";
export { IbcExtension, setupIbcExtension } from "./ibc";
export { setupStakingExtension, StakingExtension } from "./staking";
export { createPagination, createProtobufRpcClient, ProtobufRpcClient } from "./utils";

View File

@ -1,4 +1,5 @@
import { Bech32 } from "@cosmjs/encoding";
import { Uint64 } from "@cosmjs/math";
import { PageRequest } from "cosmjs-types/cosmos/base/query/v1beta1/pagination";
import Long from "long";
@ -36,3 +37,12 @@ export function createProtobufRpcClient(base: QueryClient): ProtobufRpcClient {
},
};
}
/**
* Takes a uint64 value as string, number, Long or Uint64 and returns an unsigned Long instance
* of it.
*/
export function longify(value: string | number | Long | Uint64): Long {
const checkedValue = Uint64.fromString(value.toString());
return Long.fromBytesBE([...checkedValue.toBytesBigEndian()], true);
}