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
1211export type useVotingOptions = {
1312 chainName : string ;
@@ -21,11 +20,18 @@ export type onVoteOptions = {
2120} ;
2221
2322export 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