11import { ActionButton , Stack } from "@namada/components" ;
22import { mapUndefined } from "@namada/utils" ;
3+ import { ConnectProviderButton } from "App/Common/ConnectProviderButton" ;
34import { SelectAssetModal } from "App/Common/SelectAssetModal" ;
5+ import { SelectWalletModal } from "App/Common/SelectWalletModal" ;
46import { TransactionFeeButton } from "App/Common/TransactionFeeButton" ;
57import { SwapArrowsIcon } from "App/Icons/SwapArrowsIcon" ;
68import { defaultShieldedAccountAtom } from "atoms/accounts" ;
@@ -12,7 +14,9 @@ import { tokenPricesFamily } from "atoms/prices/atoms";
1214import BigNumber from "bignumber.js" ;
1315import { TransactionFeeProps , useTransactionFee } from "hooks" ;
1416import { useAvailableAmountMinusFees } from "hooks/useAvailableAmountMinusFee" ;
17+ import { useWalletManager } from "hooks/useWalletManager" ;
1518import { wallets } from "integrations" ;
19+ import { KeplrWalletManager } from "integrations/Keplr" ;
1620import { useAtom , useAtomValue , useSetAtom } from "jotai" ;
1721import { useCallback , useEffect , useRef , useState } from "react" ;
1822import { NamadaAsset } from "types" ;
@@ -27,12 +31,17 @@ import {
2731 swapStatusAtom ,
2832} from "./state/atoms" ;
2933
34+ const keplr = new KeplrWalletManager ( ) ;
35+
3036export const SwapCalculations = ( ) : JSX . Element => {
3137 // Local state
3238 const [ sellAssetSelectorModalOpen , setSellAssetSelectorModalOpen ] =
3339 useState ( false ) ;
3440 const [ buyAssetSelectorModalOpen , setBuyAssetSelectorModalOpen ] =
3541 useState ( false ) ;
42+ const [ showConnectToWalletButton , setShowConnectToWalletButton ] =
43+ useState ( false ) ;
44+ const [ walletSelectorModalOpen , setWalletSelectorModalOpen ] = useState ( false ) ;
3645
3746 // Feature state
3847 const [ swapState , setSwapState ] = useAtom ( swapStateAtom ) ;
@@ -57,6 +66,8 @@ export const SwapCalculations = (): JSX.Element => {
5766 quote,
5867 } ) ;
5968
69+ const { walletAddress, connectToChainId, registry } = useWalletManager ( keplr ) ;
70+
6071 // Derived state
6172 const { sellAsset, buyAsset } = swapState ;
6273 const availableAmount = mapUndefined (
@@ -73,6 +84,7 @@ export const SwapCalculations = (): JSX.Element => {
7384 sellAsset,
7485 buyAsset,
7586 availableAmountMinusFees,
87+ walletAddress,
7688 } ) ;
7789
7890 const balances = Object . entries ( assetsWithBalance || { } ) . reduce (
@@ -170,20 +182,48 @@ export const SwapCalculations = (): JSX.Element => {
170182 [ sortedAssets . length , buyAsset ?. symbol , sellAsset ?. symbol ]
171183 ) ;
172184
185+ useEffect ( ( ) => {
186+ // Because of the current bug with connectedWallets, this prevents button flash
187+ const handler = setTimeout ( ( ) => {
188+ setShowConnectToWalletButton ( ! walletAddress ) ;
189+ } , 500 ) ;
190+
191+ return ( ) => clearTimeout ( handler ) ;
192+ } , [ walletAddress ] ) ;
193+
194+ const onChangeWallet = useCallback ( ( ) : void => {
195+ if ( registry ) {
196+ connectToChainId ( registry . chain . chain_id ) ;
197+ return ;
198+ }
199+ connectToChainId ( "osmosis-1" ) ;
200+ } , [ ] ) ;
201+
173202 return (
174203 < >
175204 < Stack >
176- < SwapSource
177- asset = { sellAsset }
178- isLoadingAssets = { false }
179- openAssetSelector = { ( ) => setSellAssetSelectorModalOpen ( true ) }
180- availableAmount = { availableAmount }
181- availableAmountMinusFees = { availableAmountMinusFees }
182- amount = { swapState . sellAmount }
183- onChangeAmount = { onChangeSellAmount }
184- isSubmitting = { false }
185- label = "Sell"
186- />
205+ < div className = "relative" >
206+ < SwapSource
207+ asset = { sellAsset }
208+ isLoadingAssets = { false }
209+ openAssetSelector = { ( ) => setSellAssetSelectorModalOpen ( true ) }
210+ availableAmount = { availableAmount }
211+ availableAmountMinusFees = { availableAmountMinusFees }
212+ amount = { swapState . sellAmount }
213+ onChangeAmount = { onChangeSellAmount }
214+ isSubmitting = { false }
215+ label = "Sell"
216+ />
217+ { showConnectToWalletButton && (
218+ < div className = "absolute top-4 right-4 h-[30px]" >
219+ < ConnectProviderButton
220+ onClick = { ( ) => {
221+ setWalletSelectorModalOpen ( true ) ;
222+ } }
223+ />
224+ </ div >
225+ ) }
226+ </ div >
187227 < i
188228 className = "flex items-center justify-center w-13 mx-auto relative z-10 -my-8 cursor-pointer duration-300 hover:rotate-180 transition-transform ease-in-out"
189229 onClick = { onSwapArrowsClick }
@@ -248,6 +288,13 @@ export const SwapCalculations = (): JSX.Element => {
248288 wallet = { wallets . namada }
249289 />
250290 ) }
291+ { walletSelectorModalOpen && (
292+ < SelectWalletModal
293+ availableWallets = { [ wallets . keplr ] }
294+ onClose = { ( ) => setWalletSelectorModalOpen ( false ) }
295+ onConnect = { onChangeWallet }
296+ />
297+ ) }
251298 </ >
252299 ) ;
253300} ;
@@ -309,8 +356,10 @@ const SwapCalculationsFooter = ({
309356const ValidationMessages : Record < string , string > = {
310357 NoSellAssetSelected : "Select a token to sell" ,
311358 NoBuyAssetSelected : "Select a token to buy" ,
312- SellAmountIsZero : "Enter an amount to sell" ,
313- BuyAmountIsZero : "Enter an amount to buy" ,
359+ SwapModeNone : "Enter an amount to swap" ,
360+ SellAmountIsZero : "Calculating amount to sell" ,
361+ BuyAmountIsZero : "Calculating amount to buy" ,
314362 SellAmountExceedsBalance : "Insufficient balance" ,
363+ NoWalletConnected : "Connect Keplr Wallet" ,
315364 Ok : "Review" ,
316365} ;
0 commit comments