1
- import { Asset } from "@chain-registry/types" ;
1
+ import { Asset , IBCTrace } from "@chain-registry/types" ;
2
2
import { Stack } from "@namada/components" ;
3
3
import { AccountType , BparamsMsgValue } from "@namada/types" ;
4
4
import { allDefaultAccountsAtom } from "atoms/accounts" ;
5
5
import { namadaShieldedAssetsAtom } from "atoms/balance" ;
6
6
import { chainParametersAtom } from "atoms/chain" ;
7
- import { findAssetsByChainById , ibcChannelsFamily } from "atoms/integrations" ;
7
+ import { findAssetsByChainId , ibcChannelsFamily } from "atoms/integrations" ;
8
8
import { SwapResponse , SwapResponseError , SwapResponseOk } from "atoms/swaps" ;
9
9
import { createOsmosisSwapTxAtom } from "atoms/transfer/atoms" ;
10
10
import BigNumber from "bignumber.js" ;
@@ -31,14 +31,16 @@ export const OsmosisSwap: React.FC = () => {
31
31
const chainParameters = useAtomValue ( chainParametersAtom ) ;
32
32
const namadaAssets =
33
33
chainParameters . data ?
34
- findAssetsByChainById ( chainParameters . data . chainId )
34
+ findAssetsByChainId ( chainParameters . data . chainId )
35
35
: [ ] ;
36
+ const osmosisAssets =
37
+ chainParameters . data ? findAssetsByChainId ( "osmosis-1" ) : [ ] ;
36
38
37
39
const [ from , setFrom ] = useState < AddressWithAssetAndAmount | undefined > ( ) ;
38
40
const [ to , setTo ] = useState < Asset | undefined > ( ) ;
39
41
const [ amount , setAmount ] = useState < string > ( "" ) ;
40
42
const [ recipient , setRecipient ] = useState < string > (
41
- "znam17k7jw0wmvzdzmfm46m8600t9cah5mjl6se75cu9jvwxywk75k3kmxehmxk7wha62l35puzl6srd "
43
+ "znam17drxewzvge966gzcl0u6tr4j90traepujm2vd8ptwwkgrftnhs2hdtnyzgl5freyjsdnchn4ddy "
42
44
) ;
43
45
const [ localRecoveryAddr , setLocalRecoveryAddress ] = useState < string > (
44
46
"osmo18st0wqx84av8y6xdlss9d6m2nepyqwj6n3q7js"
@@ -53,11 +55,24 @@ export const OsmosisSwap: React.FC = () => {
53
55
54
56
useEffect ( ( ) => {
55
57
const call = async ( ) : Promise < void > => {
58
+ invariant ( from , "No from asset selected" ) ;
59
+ invariant ( to , "No to asset selected" ) ;
60
+ // We have to map namada assets to osmosis assets to get correct base
61
+ const fromOsmosis = osmosisAssets . find (
62
+ ( assets ) => assets . symbol === from . asset . symbol
63
+ ) ;
64
+ const toOsmosis = osmosisAssets . find (
65
+ ( assets ) => assets . symbol === to . symbol
66
+ ) ;
67
+
68
+ invariant ( fromOsmosis , "From asset is not found in Osmosis assets" ) ;
69
+ invariant ( toOsmosis , "To asset is not found in Osmosis assets" ) ;
70
+
56
71
const quote = await fetch (
57
72
"https://sqs.osmosis.zone/router/quote?" +
58
73
new URLSearchParams ( {
59
- tokenIn : `${ amount } ${ from ! . asset . base } ` ,
60
- tokenOutDenom : to ! . base ,
74
+ tokenIn : `${ amount } ${ fromOsmosis . base } ` ,
75
+ tokenOutDenom : toOsmosis . base ,
61
76
humanDenoms : "false" ,
62
77
} ) . toString ( )
63
78
) ;
@@ -90,11 +105,21 @@ export const OsmosisSwap: React.FC = () => {
90
105
invariant ( transparentAccount , "No transparent account is found" ) ;
91
106
invariant ( shieldedAccount , "No shielded account is found" ) ;
92
107
invariant ( from , "No from asset" ) ;
108
+ invariant ( to , "No to asset" ) ;
93
109
invariant ( ibcChannels , "No ibc channels" ) ;
94
110
invariant ( quote , "No quote" ) ;
95
111
invariant ( localRecoveryAddr , "No local recovery address" ) ;
96
112
invariant ( recipient , "No recipient" ) ;
97
113
114
+ const toTrace = to . traces ?. find ( ( t ) : t is IBCTrace => t . type === "ibc" )
115
+ ?. chain . path ;
116
+ invariant ( toTrace , "No IBC trace found for the to asset" ) ;
117
+ invariant ( quote . route [ 0 ] , "No route found in the quote" ) ;
118
+ const route = quote . route [ 0 ] . pools . map ( ( p ) => ( {
119
+ poolId : String ( p . id ) ,
120
+ tokenOutDenom : p . token_out_denom ,
121
+ } ) ) ;
122
+
98
123
let bparams : BparamsMsgValue [ ] | undefined ;
99
124
if ( transparentAccount . type === AccountType . Ledger ) {
100
125
const sdk = await getSdkInstance ( ) ;
@@ -106,7 +131,7 @@ export const OsmosisSwap: React.FC = () => {
106
131
const transfer = {
107
132
amountInBaseDenom : BigNumber ( amount ) ,
108
133
// osmosis channel
109
- channelId : "channel-13 " ,
134
+ channelId : "channel-7 " ,
110
135
portId : "transfer" ,
111
136
token : from . originalAddress ,
112
137
source : shieldedAccount . pseudoExtendedKey ! ,
@@ -118,7 +143,7 @@ export const OsmosisSwap: React.FC = () => {
118
143
} ;
119
144
const params = {
120
145
transfer,
121
- outputDenom : "TODO" ,
146
+ outputDenom : toTrace ,
122
147
recipient,
123
148
// TODO: this should also be disposable address most likely
124
149
overflow : transparentAccount . address ,
@@ -128,6 +153,7 @@ export const OsmosisSwap: React.FC = () => {
128
153
. toString ( ) ,
129
154
} ,
130
155
localRecoveryAddr,
156
+ route,
131
157
// TODO: not sure if hardcoding is ok, maybe we should connect keplr wallet
132
158
osmosisRestRpc : "https://osmosis-rest.publicnode.com" ,
133
159
} ;
@@ -149,7 +175,8 @@ export const OsmosisSwap: React.FC = () => {
149
175
encodedTxData ,
150
176
transparentAccount . address !
151
177
) ;
152
- await broadcastTransaction ( encodedTxData , signedTxs ) ;
178
+ const wwww = await broadcastTransaction ( encodedTxData , signedTxs ) ;
179
+ console . log ( "Transaction broadcasted:" , wwww ) ;
153
180
alert ( "Transaction sent 🚀" ) ;
154
181
} catch ( error ) {
155
182
console . error ( "Error performing Osmosis swap:" , error ) ;
@@ -166,8 +193,8 @@ export const OsmosisSwap: React.FC = () => {
166
193
onChange = { ( e ) => setFrom ( availableAssets ?. [ e . target . value ] ) }
167
194
>
168
195
< option value = "" > </ option >
169
- { Object . values ( availableAssets || { } ) . map ( ( al ) => (
170
- < option key = { al . asset . base } value = { al . originalAddress } >
196
+ { Object . values ( availableAssets || { } ) . map ( ( al , idx ) => (
197
+ < option key = { ` ${ al . asset . base } _ ${ idx } ` } value = { al . originalAddress } >
171
198
{ al . asset . symbol }
172
199
</ option >
173
200
) ) }
0 commit comments