Skip to content

Commit

Permalink
Add gov query extension (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahadoodle authored Mar 22, 2023
1 parent 2477969 commit 5e91b24
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 3 deletions.
4 changes: 4 additions & 0 deletions packages/nibijs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline

* .

## v0.19.4

* feat: Add gov query extension

## v0.19.2

* chore: Raise minimum gas limit for transactions with low values for the simulated "gas used".
Expand Down
4 changes: 2 additions & 2 deletions packages/nibijs/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@nibiruchain/nibijs",
"description": "The TypeScript SDK for the Nibiru blockchain.",
"version": "0.19.3",
"version": "0.19.4",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -41,4 +41,4 @@
"typedoc-plugin-markdown": "^3.14.0"
},
"gitHead": "4da45aaaf65a44e6b4edc0b5d11ea1083229f977"
}
}
129 changes: 129 additions & 0 deletions packages/nibijs/src/query/gov.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import {
createPagination,
createProtobufRpcClient,
GovParamsType,
QueryClient,
} from "@cosmjs/stargate"
import * as cosmosGovQuery from "@nibiruchain/protojs/dist/cosmos/gov/v1beta1/query"
import { ProposalStatus } from "@nibiruchain/protojs/src/cosmos/gov/v1beta1/gov"
import Long from "long"

export interface GovExtension {
gov: Readonly<{
params: (paramsType: GovParamsType) => Promise<cosmosGovQuery.QueryParamsResponse>
proposal: (proposalId: number) => Promise<cosmosGovQuery.QueryProposalResponse>
proposals: (
status: ProposalStatus,
voter: string,
depositor: string,
pagination?: Uint8Array,
) => Promise<cosmosGovQuery.QueryProposalsResponse>
vote: (
proposalId: number,
voter: string,
) => Promise<cosmosGovQuery.QueryVoteResponse>
votes: (
proposalId: number,
pagination?: Uint8Array,
) => Promise<cosmosGovQuery.QueryVotesResponse>
deposit: (
proposalId: number,
depositor: string,
) => Promise<cosmosGovQuery.QueryDepositResponse>
deposits: (
proposalId: number,
pagination?: Uint8Array,
) => Promise<cosmosGovQuery.QueryDepositsResponse>
tallyResult: (
proposalId: number,
) => Promise<cosmosGovQuery.QueryTallyResultResponse>
}>
}

export function setupGovExtension(base: QueryClient): GovExtension {
const rpcClient = createProtobufRpcClient(base)
const queryService = new cosmosGovQuery.QueryClientImpl(rpcClient)

return {
gov: {
proposal: async (
proposalId: number,
): Promise<cosmosGovQuery.QueryProposalResponse> => {
const response = await queryService.Proposal({
proposalId: Long.fromValue(proposalId),
})
return response
},
proposals: async (
status: ProposalStatus,
voter: string,
depositor: string,
pagination?: Uint8Array,
): Promise<cosmosGovQuery.QueryProposalsResponse> => {
const response = await queryService.Proposals({
proposalStatus: status,
voter,
depositor,
pagination: createPagination(pagination),
})
return response
},
vote: async (
proposalId: number,
voter: string,
): Promise<cosmosGovQuery.QueryVoteResponse> => {
const response = await queryService.Vote({
proposalId: Long.fromValue(proposalId),
voter,
})
return response
},
votes: async (
proposalId: number,
pagination?: Uint8Array,
): Promise<cosmosGovQuery.QueryVotesResponse> => {
const response = await queryService.Votes({
proposalId: Long.fromValue(proposalId),
pagination: createPagination(pagination),
})
return response
},
params: async (
paramsType: GovParamsType,
): Promise<cosmosGovQuery.QueryParamsResponse> => {
const response = await queryService.Params({
paramsType,
})
return response
},
deposit: async (
proposalId: number,
depositor: string,
): Promise<cosmosGovQuery.QueryDepositResponse> => {
const response = await queryService.Deposit({
proposalId: Long.fromValue(proposalId),
depositor,
})
return response
},
deposits: async (
proposalId: number,
pagination?: Uint8Array,
): Promise<cosmosGovQuery.QueryDepositsResponse> => {
const response = await queryService.Deposits({
proposalId: Long.fromValue(proposalId),
pagination: createPagination(pagination),
})
return response
},
tallyResult: async (
proposalId: number,
): Promise<cosmosGovQuery.QueryTallyResultResponse> => {
const response = await queryService.TallyResult({
proposalId: Long.fromValue(proposalId),
})
return response
},
},
}
}
5 changes: 4 additions & 1 deletion packages/nibijs/src/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { OracleExtension, setupOracleExtension } from "./oracle"
import { setupStakingExtension, StakingExtension } from "./staking"
import { setupVpoolExtension, VpoolExtension } from "./vpool"
import { DistributionExtension, setupDistributionExtension } from "./distribution"
import { GovExtension, setupGovExtension } from "./gov"

export type ExtendedQueryClient = BankExtension &
QueryClient &
Expand All @@ -24,7 +25,8 @@ export type ExtendedQueryClient = BankExtension &
OracleExtension &
EpochsExtension &
StakingExtension &
DistributionExtension
DistributionExtension &
GovExtension

export interface IQueryCmd {
/**
Expand Down Expand Up @@ -90,6 +92,7 @@ export class QueryCmd implements IQueryCmd {
setupEpochsExtension,
setupStakingExtension,
setupDistributionExtension,
setupGovExtension,
)
}

Expand Down
13 changes: 13 additions & 0 deletions packages/nibijs/src/test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,16 @@ describe("distribution module queries", () => {
})
})
})

describe("gov module queries", () => {
test("gov params", async () => {
const { client: query } = await newQueryCmd(TEST_CHAIN)
const resp = await query.gov.params("voting")
const { votingParams } = resp
expect(votingParams).toBeDefined()
const properties: string[] = ["votingPeriod"]
properties.forEach((prop) => {
expect(votingParams).toHaveProperty(prop)
})
})
})

0 comments on commit 5e91b24

Please sign in to comment.