Skip to content

Commit fc04264

Browse files
committed
feat: move connect wallet button to the calculations view
1 parent 75835e1 commit fc04264

File tree

3 files changed

+73
-60
lines changed

3 files changed

+73
-60
lines changed

apps/namadillo/src/App/Swap/SwapCalculations.tsx

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { ActionButton, Stack } from "@namada/components";
22
import { mapUndefined } from "@namada/utils";
3+
import { ConnectProviderButton } from "App/Common/ConnectProviderButton";
34
import { SelectAssetModal } from "App/Common/SelectAssetModal";
5+
import { SelectWalletModal } from "App/Common/SelectWalletModal";
46
import { TransactionFeeButton } from "App/Common/TransactionFeeButton";
57
import { SwapArrowsIcon } from "App/Icons/SwapArrowsIcon";
68
import { defaultShieldedAccountAtom } from "atoms/accounts";
@@ -12,7 +14,9 @@ import { tokenPricesFamily } from "atoms/prices/atoms";
1214
import BigNumber from "bignumber.js";
1315
import { TransactionFeeProps, useTransactionFee } from "hooks";
1416
import { useAvailableAmountMinusFees } from "hooks/useAvailableAmountMinusFee";
17+
import { useWalletManager } from "hooks/useWalletManager";
1518
import { wallets } from "integrations";
19+
import { KeplrWalletManager } from "integrations/Keplr";
1620
import { useAtom, useAtomValue, useSetAtom } from "jotai";
1721
import { useCallback, useEffect, useRef, useState } from "react";
1822
import { NamadaAsset } from "types";
@@ -27,12 +31,17 @@ import {
2731
swapStatusAtom,
2832
} from "./state/atoms";
2933

34+
const keplr = new KeplrWalletManager();
35+
3036
export 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 = ({
309356
const 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
};

apps/namadillo/src/App/Swap/SwapReview.tsx

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
import { ActionButton, Stack, Text } from "@namada/components";
2-
import { ConnectProviderButton } from "App/Common/ConnectProviderButton";
32
import { CurrentStatus } from "App/Common/CurrentStatus";
43
import { IconTooltip } from "App/Common/IconTooltip";
54
import { InlineError } from "App/Common/InlineError";
65
import { LedgerDeviceTooltip } from "App/Common/LedgerDeviceTooltip";
7-
import { SelectWalletModal } from "App/Common/SelectWalletModal";
86
import { WalletAddress } from "App/Common/WalletAddress";
97
import { SwapTradeIcon } from "App/Icons/SwapTradeIcon";
108
import { ledgerStatusDataAtom } from "atoms/ledger";
119
import { tokenPricesFamily } from "atoms/prices/atoms";
1210
import BigNumber from "bignumber.js";
1311
import clsx from "clsx";
1412
import { useWalletManager } from "hooks/useWalletManager";
15-
import { wallets } from "integrations";
1613
import { KeplrWalletManager } from "integrations/Keplr";
1714
import { getAssetImageUrl } from "integrations/utils";
1815
import invariant from "invariant";
1916
import { useAtom, useAtomValue } from "jotai";
20-
import { useCallback, useEffect, useState } from "react";
17+
import { useCallback } from "react";
2118
import { BsQuestionCircleFill } from "react-icons/bs";
2219
import { toDisplayAmount } from "utils";
2320
import { usePerformOsmosisSwapTx } from "./hooks/usePerformOsmosisSwapTx";
@@ -28,11 +25,6 @@ import { SLIPPAGE } from "./state/functions";
2825

2926
const keplr = new KeplrWalletManager();
3027
export const SwapReview = (): JSX.Element => {
31-
// Local state
32-
const [walletSelectorModalOpen, setWalletSelectorModalOpen] = useState(false);
33-
const [showConnectToWalletButton, setShowConnectToWalletButton] =
34-
useState(false);
35-
3628
// Feature state sanity checks
3729
const [status, setStatus] = useAtom(swapStatusAtom);
3830
const swapState = useAtomValue(swapStateAtom);
@@ -79,23 +71,7 @@ export const SwapReview = (): JSX.Element => {
7971
: fiatFee.lt(0.01) ? "<$0.01"
8072
: `~$${fiatFee.toString()}`;
8173

82-
const { walletAddress, connectToChainId, registry } = useWalletManager(keplr);
83-
useEffect(() => {
84-
// Because of the current bug with connectedWallets, this prevents button flash
85-
const handler = setTimeout(() => {
86-
setShowConnectToWalletButton(!walletAddress);
87-
}, 500);
88-
89-
return () => clearTimeout(handler);
90-
}, [walletAddress]);
91-
92-
const onChangeWallet = useCallback((): void => {
93-
if (registry) {
94-
connectToChainId(registry.chain.chain_id);
95-
return;
96-
}
97-
connectToChainId("osmosis-1");
98-
}, []);
74+
const { walletAddress } = useWalletManager(keplr);
9975

10076
const { error: _err, performSwap } = usePerformOsmosisSwapTx();
10177
const onSwap = useCallback(async (): Promise<void> => {
@@ -114,18 +90,7 @@ export const SwapReview = (): JSX.Element => {
11490
<>
11591
<Stack>
11692
<div className="relative bg-neutral-800 rounded-lg px-4 py-5 border border-yellow font-light">
117-
<Stack direction="horizontal" className="justify-between">
118-
<Text className="mt-0">Review Shielded Swap</Text>
119-
{showConnectToWalletButton && (
120-
<div className="h-[30px]">
121-
<ConnectProviderButton
122-
onClick={() => {
123-
setWalletSelectorModalOpen(true);
124-
}}
125-
/>
126-
</div>
127-
)}
128-
</Stack>
93+
<Text className="mt-0">Review Shielded Swap</Text>
12994
<Stack direction="horizontal">
13095
<span>
13196
<img
@@ -260,13 +225,6 @@ export const SwapReview = (): JSX.Element => {
260225
</ActionButton>
261226
)}
262227
</Stack>
263-
{walletSelectorModalOpen && (
264-
<SelectWalletModal
265-
availableWallets={[wallets.keplr]}
266-
onClose={() => setWalletSelectorModalOpen(false)}
267-
onConnect={onChangeWallet}
268-
/>
269-
)}
270228
</>
271229
);
272230
};

apps/namadillo/src/App/Swap/hooks/useSwapValidation.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,34 @@ export const useSwapValidation = ({
88
buyAsset,
99
sellAsset,
1010
availableAmountMinusFees,
11+
walletAddress,
1112
}: {
1213
swapState: SwapState;
1314
buyAsset?: NamadaAsset;
1415
sellAsset?: NamadaAsset;
1516
availableAmountMinusFees?: BigNumber;
17+
walletAddress?: string;
1618
}): string => {
1719
return useMemo(() => {
1820
if (!sellAsset) {
1921
return "NoSellAssetSelected";
2022
} else if (!buyAsset) {
2123
return "NoBuyAssetSelected";
24+
} else if (swapState.mode === "none") {
25+
return "SwapModeNone";
2226
} else if (!swapState.sellAmount || swapState.sellAmount.isZero()) {
2327
return "SellAmountIsZero";
2428
} else if (!swapState.buyAmount || swapState.buyAmount.isZero()) {
2529
return "BuyAmountIsZero";
26-
}
27-
if (
30+
} else if (
2831
!availableAmountMinusFees ||
2932
(swapState.sellAmount &&
3033
availableAmountMinusFees &&
3134
swapState.sellAmount.gt(availableAmountMinusFees))
3235
) {
3336
return "SellAmountExceedsBalance";
37+
} else if (!walletAddress) {
38+
return "NoWalletConnected";
3439
} else {
3540
return "Ok";
3641
}
@@ -39,5 +44,6 @@ export const useSwapValidation = ({
3944
buyAsset?.address,
4045
swapState.sellAmount,
4146
swapState.buyAmount,
47+
walletAddress,
4248
]);
4349
};

0 commit comments

Comments
 (0)