Skip to content

Commit 7d1d6ff

Browse files
committed
feat: update voting
1 parent 18af9be commit 7d1d6ff

File tree

9 files changed

+118
-230
lines changed

9 files changed

+118
-230
lines changed

templates/chain-admin/components/voting/Proposal.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useEffect, useState } from 'react';
12
import {
23
Box,
34
Button,
@@ -13,18 +14,18 @@ import {
1314
import {
1415
Proposal as IProposal,
1516
ProposalStatus,
16-
} from 'interchain-query/cosmos/gov/v1/gov';
17+
} from '@interchainjs/react/cosmos/gov/v1/gov';
18+
import Markdown from 'react-markdown';
19+
import { useChain } from '@interchain-kit/react';
20+
21+
import { useVoting, Votes } from '@/hooks';
1722
import {
1823
exponentiate,
1924
formatDate,
2025
getExponentFromAsset,
2126
getNativeAsset,
2227
percent,
2328
} from '@/utils';
24-
import Markdown from 'react-markdown';
25-
import { useEffect, useState } from 'react';
26-
import { useVoting, Votes } from '@/hooks';
27-
import { useChain } from '@cosmos-kit/react';
2829

2930
// export declare enum VoteOption {
3031
// /** VOTE_OPTION_UNSPECIFIED - VOTE_OPTION_UNSPECIFIED defines a no-op vote option. */
@@ -64,8 +65,8 @@ export function Proposal({
6465
const [showMore, setShowMore] = useState(false);
6566
const [voteType, setVoteType] = useState<GovernanceVoteType>();
6667

67-
const { assets } = useChain(chainName);
68-
const coin = getNativeAsset(assets!);
68+
const { assetList } = useChain(chainName);
69+
const coin = getNativeAsset(assetList);
6970
const exponent = getExponentFromAsset(coin);
7071
const { isVoting, onVote } = useVoting({ chainName, proposal });
7172

templates/chain-admin/components/voting/Voting.tsx

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { useEffect, useState } from 'react';
2-
import { useChain } from '@cosmos-kit/react';
2+
import { useChain } from '@interchain-kit/react';
33
import {
44
Proposal as IProposal,
55
ProposalStatus,
66
TallyResult,
7-
} from 'interchain-query/cosmos/gov/v1/gov';
7+
} from '@interchainjs/react/cosmos/gov/v1/gov';
88
import {
99
BasicModal,
1010
Box,
@@ -13,10 +13,11 @@ import {
1313
Text,
1414
useColorModeValue,
1515
} from '@interchain-ui/react';
16+
import { chains } from 'chain-registry';
17+
1618
import { useModal, useVotingData } from '@/hooks';
1719
import { Proposal } from '@/components';
1820
import { formatDate } from '@/utils';
19-
import { chains } from 'chain-registry';
2021

2122
function status(s: ProposalStatus) {
2223
switch (s) {
@@ -65,7 +66,9 @@ export function Voting({ chainName }: VotingProps) {
6566
if (proposal.status === ProposalStatus.PROPOSAL_STATUS_VOTING_PERIOD) {
6667
(async () => {
6768
for (const { address } of chain?.apis?.rest || []) {
68-
const api = `${address}/cosmos/gov/v1/proposals/${Number(proposal.id)}/tally`;
69+
const api = `${address}/cosmos/gov/v1/proposals/${Number(
70+
proposal.id
71+
)}/tally`;
6972
try {
7073
const tally = (await (await fetch(api)).json()).tally;
7174
if (!tally) {
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
export * from './useModal';
22
export * from './useVoting';
33
export * from './useVotingData';
4-
export * from './useQueryHooks';
5-
export * from './useRpcQueryClient';

templates/chain-admin/hooks/voting/useQueryHooks.ts

Lines changed: 0 additions & 46 deletions
This file was deleted.

templates/chain-admin/hooks/voting/useRpcQueryClient.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

templates/chain-admin/hooks/voting/useVoting.ts

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { useState } from 'react';
2-
import { cosmos } from 'interchain-query';
3-
import { toast } from '@interchain-ui/react';
4-
import { useChain } from '@cosmos-kit/react';
5-
import { coins, StdFee } from '@cosmjs/stargate';
6-
import { Proposal } from 'interchain-query/cosmos/gov/v1/gov';
7-
import { getNativeAsset } from '@/utils';
8-
import { useVotingTx } from './useVotingTx';
1+
import { useChain } from '@interchain-kit/react';
2+
import { Proposal } from '@interchainjs/react/cosmos/gov/v1/gov';
3+
import { useVote } from '@interchainjs/react/cosmos/gov/v1beta1/tx.rpc.react';
4+
import { MsgVote } from '@interchainjs/react/cosmos/gov/v1beta1/tx';
5+
import { defaultContext } from '@tanstack/react-query';
6+
import { StdFee } from '@interchainjs/react/types';
97

10-
const MessageComposer = cosmos.gov.v1beta1.MessageComposer;
8+
import { getNativeAsset } from '@/utils';
9+
import { useSigningClient, useToastHandlers } from '../common';
1110

1211
export type useVotingOptions = {
1312
chainName: string;
@@ -21,11 +20,18 @@ export type onVoteOptions = {
2120
};
2221

2322
export function useVoting({ chainName, proposal }: useVotingOptions) {
24-
const { tx } = useVotingTx(chainName);
25-
const { address, assets } = useChain(chainName);
26-
const [isVoting, setIsVoting] = useState(false);
23+
const { address, assetList } = useChain(chainName);
24+
const toastHandlers = useToastHandlers();
25+
const { data: signingClient } = useSigningClient(chainName);
26+
const { mutate: vote, isLoading: isVoting } = useVote({
27+
clientResolver: signingClient,
28+
options: {
29+
context: defaultContext,
30+
...toastHandlers,
31+
},
32+
});
2733

28-
const coin = getNativeAsset(assets!);
34+
const coin = getNativeAsset(assetList);
2935

3036
async function onVote({
3137
option,
@@ -34,35 +40,39 @@ export function useVoting({ chainName, proposal }: useVotingOptions) {
3440
}: onVoteOptions) {
3541
if (!address || !option) return;
3642

37-
const msg = MessageComposer.fromPartial.vote({
43+
const msg = MsgVote.fromPartial({
3844
option,
3945
voter: address,
4046
proposalId: proposal.id,
4147
});
4248

4349
const fee: StdFee = {
44-
amount: coins('1000', coin.base),
50+
amount: [
51+
{
52+
denom: coin.base,
53+
amount: '0',
54+
},
55+
],
4556
gas: '100000',
4657
};
4758

48-
try {
49-
setIsVoting(true);
50-
const res = await tx([msg], { fee });
51-
if (res.error) {
52-
error();
53-
console.error(res.error);
54-
toast.error(res.errorMsg);
55-
} else {
56-
success();
57-
toast.success('Vote successful');
59+
vote(
60+
{
61+
signerAddress: address,
62+
message: msg,
63+
fee,
64+
memo: 'Vote',
65+
},
66+
{
67+
onSuccess: () => {
68+
success();
69+
},
70+
onError: (err) => {
71+
error();
72+
console.error(err);
73+
},
5874
}
59-
} catch (e) {
60-
error();
61-
console.error(e);
62-
toast.error('Vote failed');
63-
} finally {
64-
setIsVoting(false);
65-
}
75+
);
6676
}
6777

6878
return { isVoting, onVote };

0 commit comments

Comments
 (0)