cosmos / cosmjs

The Swiss Army knife to power JavaScript based client solutions ranging from Web apps/explorers over browser extensions to server-side clients like faucets/scrapers.
https://cosmos.github.io/cosmjs/
Apache License 2.0
657 stars 344 forks source link

How do i query the governance module to get proposals etc ? #1205

Open Goochie opened 2 years ago

Goochie commented 2 years ago

The Rest end points has the following names space ...

/cosmos/gov/v1beta1/proposals

...... but i can find anything in cosmJS that enables querysing the governance module.

I searched through the issues and found this PR solving a certain issue related to a gov query but from looking at the PR, that directory structure is no longer found in the current stargate package in cosmJS...

https://github.com/cosmos/cosmjs/pull/881/commits/a4964147d344d461a5606cef65be0df61749ef85#diff-b3b7653bd101e087682788920f465563f471ed2c71d17940962392fa332630b1

In essence i want to be able to get open proposals per chain and vote on them via cosmJS.

AlexanderFSP commented 7 months ago

Hi there! A small code snippet that resolves active proposals using the governance module. I hope it helps someone)

import { QueryClient, setupGovExtension } from '@cosmjs/stargate';
import { connectComet } from '@cosmjs/tendermint-rpc';

// 1. Setup `CometClient` for `QueryClient`
const cometClient = await connectComet(`${ /** Your RPC URL */}`);

// 2. Setup `QueryClient` for `setupGovExtension` 
const queryClient = new QueryClient(cometClient);

// 2. Setup `GovExtension` abstraction, which provides some methods to retrieve governance staff
const govExtension = setupGovExtension(queryClient);

// 4. Retrieve proposals in voting phase
const votingProposals = await govExtension.gov.proposals(2, '', '');