1
- import { Box , Text , Button , Link } from "@interchain-ui/react" ;
2
- import { useState } from "react" ;
3
- import { useChain } from "@interchain-kit/react" ;
4
- import { defaultAssetList , defaultChain , defaultChainName } from "@/config" ;
5
- import useBalance from "@/hooks/useBalance" ;
6
- import { toEncoders , toConverters } from '@interchainjs/cosmos/utils' ;
7
- import { MsgSend } from '../src/codegen/cosmos/bank/v1beta1/tx' ;
8
- import { cosmos } from '../src/codegen' ;
1
+ import { Box , Text , Button , Link } from '@interchain-ui/react' ;
2
+ import { useState } from 'react' ;
3
+ import { useChain } from '@interchain-kit/react' ;
4
+ import { defaultAssetList , defaultChain , defaultChainName } from '@/config' ;
5
+ import useBalance from '@/hooks/useBalance' ;
6
+ import { MsgSend } from 'injective-react/cosmos/bank/v1beta1/tx' ;
7
+ import { useSend } from 'injective-react/cosmos/bank/v1beta1/tx.rpc.react' ;
8
+ import { useSigningClient } from '../hooks/useTx' ;
9
+ import { WalletState } from '@interchain-kit/core' ;
10
+ import { defaultContext } from '@tanstack/react-query' ;
9
11
10
12
export default function SendMsg ( ) {
11
13
const coin = defaultAssetList ?. assets [ 0 ] ;
12
14
13
- const denom = coin ! . base !
15
+ const denom = coin ! . base ! ;
14
16
15
17
const COIN_DISPLAY_EXPONENT = coin ! . denomUnits . find (
16
18
( unit ) => unit . denom === coin ! . display
17
19
) ?. exponent as number ;
18
20
19
- const chain = defaultChain
20
- const txPage = chain ?. explorers ?. [ 0 ] . txPage
21
+ const chain = defaultChain ;
22
+ const txPage = chain ?. explorers ?. [ 0 ] . txPage ;
21
23
22
- const { address, signingClient, isLoading } = useChain ( defaultChainName ) ;
23
- signingClient ?. addEncoders ( toEncoders ( MsgSend ) ) ;
24
- signingClient ?. addConverters ( toConverters ( MsgSend ) ) ;
24
+ const { address } = useChain ( defaultChainName ) ;
25
+ const { data : signingClient , isLoading } = useSigningClient (
26
+ defaultChainName ,
27
+ {
28
+ walletStatus : WalletState . Connected ,
29
+ }
30
+ ) ;
31
+
32
+ const { mutate : send } = useSend ( {
33
+ options : {
34
+ context : defaultContext ,
35
+ } ,
36
+ clientResolver : signingClient ,
37
+ } ) ;
25
38
26
39
const [ sending , setSending ] = useState ( false ) ;
27
40
const [ txHash , setTxHash ] = useState < string | null > ( null ) ;
28
41
const [ error , setError ] = useState < string | null > ( null ) ;
29
42
30
- const {
31
- balance,
32
- isBalanceLoaded,
33
- isFetchingBalance,
34
- refetchBalance,
35
- } = useBalance ( {
36
- address,
37
- } )
43
+ const { balance, isBalanceLoaded, isFetchingBalance, refetchBalance } =
44
+ useBalance ( {
45
+ address,
46
+ } ) ;
38
47
39
48
const handleSend = async ( ) => {
40
49
if ( sending || isLoading ) return ;
@@ -44,62 +53,76 @@ export default function SendMsg() {
44
53
setSending ( true ) ;
45
54
46
55
const fee = {
47
- amount : [ {
48
- denom,
49
- amount : '2500' ,
50
- } ] ,
51
- gas : "1000000" ,
56
+ amount : [
57
+ {
58
+ denom,
59
+ amount : '2500' ,
60
+ } ,
61
+ ] ,
62
+ gas : '1000000' ,
52
63
} ;
53
- const { send } = cosmos . bank . v1beta1 . MessageComposer . withTypeUrl
54
- const msgs = [ send ( {
64
+
65
+ const msg = MsgSend . fromPartial ( {
55
66
fromAddress : address ,
56
67
toAddress : address ,
57
- amount : [ { denom, amount : '1' } ]
58
- } ) ]
59
- try {
60
- const data = await signingClient . signAndBroadcast (
61
- address , msgs , fee , 'using interchainjs'
62
- ) as any
63
- console . log ( 'onSuccess' , data )
64
- if ( data . code === 0 ) {
65
- setTimeout ( ( ) => {
66
- refetchBalance ( )
67
- setTxHash ( data . hash ) ;
68
+ amount : [ { denom, amount : '1' } ] ,
69
+ } ) ;
70
+
71
+ send (
72
+ {
73
+ signerAddress : address ,
74
+ message : msg ,
75
+ fee,
76
+ memo : 'using interchainjs' ,
77
+ } ,
78
+ {
79
+ onSuccess : ( data ) => {
80
+ refetchBalance ( ) ;
81
+ setTxHash ( data . transactionHash ) ;
82
+ setSending ( false ) ;
83
+ } ,
84
+ onError : ( error ) => {
85
+ setError ( error ?. message || 'Unknown error' ) ;
68
86
setSending ( false ) ;
69
- } , 4000 )
70
- } else {
71
- setError ( data . rawLog || JSON . stringify ( data || { } ) ) ;
72
- setSending ( false ) ;
87
+ } ,
73
88
}
74
- } catch ( error : any ) {
75
- console . log ( 'onError' , error )
76
- setError ( error ?. message || 'Unknown error' ) ;
77
- setSending ( false ) ;
78
- }
79
- }
89
+ ) ;
90
+ } ;
80
91
81
92
return (
82
93
< Box display = "flex" flexDirection = "column" alignItems = "center" >
83
- < Box mb = '$4' >
84
- < Text fontSize = '$2xl' > Balance: { isFetchingBalance ? '--' : ( balance ?. toFixed ( COIN_DISPLAY_EXPONENT ) ) } { coin ?. symbol } </ Text >
94
+ < Box mb = "$4" >
95
+ < Text fontSize = "$2xl" >
96
+ Balance:{ ' ' }
97
+ { isFetchingBalance ? '--' : balance ?. toFixed ( COIN_DISPLAY_EXPONENT ) } { ' ' }
98
+ { coin ?. symbol }
99
+ </ Text >
85
100
</ Box >
86
101
< Box >
87
102
< Button
88
103
disabled = { sending || isLoading }
89
104
isLoading = { sending }
90
105
onClick = { handleSend }
91
- > { isLoading ? 'Initializing...' : 'Send Token' } </ Button >
106
+ >
107
+ { isLoading ? 'Initializing...' : 'Send Token' }
108
+ </ Button >
92
109
</ Box >
93
- { txHash && < Box mt = '$4' display = 'flex' flexDirection = 'row' alignItems = 'center' >
94
- < Text attributes = { { mr : '$1' } } > Details:</ Text >
95
- < Link href = { txPage ?. replace ( '${txHash}' , txHash ) ! } target = "_blank" >
96
- { txPage ?. replace ( '${txHash}' , txHash ) ! }
97
- </ Link >
98
- </ Box > }
99
- { error && < Box mt = '$4' display = 'flex' flexDirection = 'row' alignItems = 'center' >
100
- < Text attributes = { { mr : '$1' } } fontSize = '$2xl' > Error:</ Text >
101
- < Text fontSize = '$2xl' > { error } </ Text >
102
- </ Box > }
110
+ { txHash && (
111
+ < Box mt = "$4" display = "flex" flexDirection = "row" alignItems = "center" >
112
+ < Text attributes = { { mr : '$1' } } > Details:</ Text >
113
+ < Link href = { txPage ?. replace ( '${txHash}' , txHash ) ! } target = "_blank" >
114
+ { txPage ?. replace ( '${txHash}' , txHash ) ! }
115
+ </ Link >
116
+ </ Box >
117
+ ) }
118
+ { error && (
119
+ < Box mt = "$4" display = "flex" flexDirection = "row" alignItems = "center" >
120
+ < Text attributes = { { mr : '$1' } } fontSize = "$2xl" >
121
+ Error:
122
+ </ Text >
123
+ < Text fontSize = "$2xl" > { error } </ Text >
124
+ </ Box >
125
+ ) }
103
126
</ Box >
104
127
) ;
105
- }
128
+ }
0 commit comments