Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: swaps routes, closes #4317 #5177

Merged
merged 2 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ updates:
schedule:
interval: 'weekly'

- package-ecosystem: 'pnpm'
- package-ecosystem: 'npm'
directory: '/'
schedule:
interval: 'daily'
group:
groups:
leather:
applies-to: version-updates
patterns:
Expand Down
4 changes: 3 additions & 1 deletion src/app/features/container/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@ export function Container() {
const displayHeader = !isLandingPage(pathname) && !isNoHeaderPopup(pathname);
const isSessionLocked = getIsSessionLocked(pathname);

// TODO: Refactor? This is very hard to manage with dynamic routes. Temporarily
Copy link
Contributor

@pete-watters pete-watters Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, this isn't great. Before we were storing the whole header including callback in the state.

Maybe we could try using the location state to store where it should go back to?

For some of the other dynamic routes I was using match to get to more specific cases.

If we use includes here it also means if you reach the /swap/review page then you go all the way back to the homepage on back and not to the previous page where you are selecting what to swap.

In those cases I think it should go back navigate(-1)

Copy link
Collaborator

@kyranjamie kyranjamie Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than trying to put all the go back logic in the container—which think we all agree isn't a scalable solution—can't we instead find a way to define this in each component tree where there is a back button?

What if the header back button simply publishes a "go back" event, e.g. with PubSub pattern, and components, via a hook listen for this event?

function useHandleGoBack (handler) {
  // listen for go back logic, then fire handler()
}
export function Swap() {
  // nav passed as handler to avoid useNavigate if not needed
  useHandleGoBack(navigate => navigate(RouteUrls.Home))
  // would be nice to make a friendly API for this fn, perhaps could take a callback _or_ a RouteUrl for simple cases
  useHandleGoBack(RouteUrls.Home)

Perhaps not perfect. We'd need to be careful not to have double handlers, and ensure we define goback where necessary, but this demonstrates a way we can completely decouple the logic.

Any other ideas?

Copy link
Collaborator

@kyranjamie kyranjamie Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened issue for this here #5181

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another idea I have is to have route props that specify it. So we can pass a call back either when loading the component in the routes file, or have it as part of the location state. I'll have a think about it.

Getting the headers out of state was a good start but this logic isn't great so I am open to a full overhaul. If you check in route-helpers.ts, I've had to use routes to apply other logic so it could be good to also improve that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make an issue to refactor this based on your understanding for what you wrote here and your implementation? I will likely not tackle this here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm, my comments hadn't refreshed, sorry that last comment was outdated.

// added a fix to catch the swap route: '/swap/:base/:quote?'
function getOnGoBackLocation(pathname: RouteUrls) {
if (pathname.includes('/swap')) return navigate(RouteUrls.Home);
switch (pathname) {
case RouteUrls.Swap:
case RouteUrls.Fund.replace(':currency', 'STX'):
case RouteUrls.Fund.replace(':currency', 'BTC'):
case RouteUrls.SendCryptoAssetForm.replace(':symbol', 'stx'):
Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/home/components/account-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function AccountActions() {
data-testid={HomePageSelectors.SwapBtn}
icon={<SwapIcon />}
label="Swap"
onClick={() => navigate(RouteUrls.Swap)}
onClick={() => navigate(RouteUrls.Swap.replace(':base', 'STX').replace(':quote', ''))}
/>
),
[ChainID.Testnet]: null,
Expand Down
35 changes: 18 additions & 17 deletions src/app/pages/swap/alex-swap-container.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMemo, useState } from 'react';
import { Outlet, useNavigate } from 'react-router-dom';
import { Outlet } from 'react-router-dom';

import { bytesToHex } from '@stacks/common';
import { ContractCallPayload, TransactionTypes } from '@stacks/connect';
Expand Down Expand Up @@ -30,6 +30,7 @@ import { useAlexBroadcastSwap } from './hooks/use-alex-broadcast-swap';
import { oneHundredMillion, useAlexSwap } from './hooks/use-alex-swap';
import { useStacksBroadcastSwap } from './hooks/use-stacks-broadcast-swap';
import { SwapAsset, SwapFormValues } from './hooks/use-swap-form';
import { useSwapNavigate } from './hooks/use-swap-navigate';
import { SwapContext, SwapProvider } from './swap.context';
import {
defaultSwapFee,
Expand All @@ -41,7 +42,7 @@ export const alexSwapRoutes = generateSwapRoutes(<AlexSwapContainer />);

function AlexSwapContainer() {
const [isSendingMax, setIsSendingMax] = useState(false);
const navigate = useNavigate();
const navigate = useSwapNavigate();
const { setIsLoading } = useLoading(LoadingKeys.SUBMIT_SWAP_TRANSACTION);
const currentAccount = useCurrentStacksAccount();
const generateUnsignedTx = useGenerateStacksContractCallUnsignedTx();
Expand Down Expand Up @@ -78,14 +79,14 @@ function AlexSwapContainer() {
);

async function onSubmitSwapForReview(values: SwapFormValues) {
if (isUndefined(values.swapAssetFrom) || isUndefined(values.swapAssetTo)) {
if (isUndefined(values.swapAssetBase) || isUndefined(values.swapAssetQuote)) {
logger.error('Error submitting swap for review');
return;
}

const [router, lpFee] = await Promise.all([
alex.getRouter(values.swapAssetFrom.currency, values.swapAssetTo.currency),
alex.getFeeRate(values.swapAssetFrom.currency, values.swapAssetTo.currency),
alex.getRouter(values.swapAssetBase.currency, values.swapAssetQuote.currency),
alex.getFeeRate(values.swapAssetBase.currency, values.swapAssetQuote.currency),
]);

onSetSwapSubmissionData({
Expand All @@ -100,10 +101,10 @@ function AlexSwapContainer() {
.filter(isDefined),
slippage,
sponsored: isSponsoredByAlex,
swapAmountFrom: values.swapAmountFrom,
swapAmountTo: values.swapAmountTo,
swapAssetFrom: values.swapAssetFrom,
swapAssetTo: values.swapAssetTo,
swapAmountBase: values.swapAmountBase,
swapAmountQuote: values.swapAmountQuote,
swapAssetBase: values.swapAssetBase,
swapAssetQuote: values.swapAssetQuote,
timestamp: new Date().toISOString(),
});

Expand All @@ -117,8 +118,8 @@ function AlexSwapContainer() {
}

if (
isUndefined(swapSubmissionData.swapAssetFrom) ||
isUndefined(swapSubmissionData.swapAssetTo)
isUndefined(swapSubmissionData.swapAssetBase) ||
isUndefined(swapSubmissionData.swapAssetQuote)
) {
logger.error('No assets selected to perform swap');
return;
Expand All @@ -127,14 +128,14 @@ function AlexSwapContainer() {
setIsLoading();

const fromAmount = BigInt(
new BigNumber(swapSubmissionData.swapAmountFrom)
new BigNumber(swapSubmissionData.swapAmountBase)
.multipliedBy(oneHundredMillion)
.dp(0)
.toString()
);

const minToAmount = BigInt(
new BigNumber(swapSubmissionData.swapAmountTo)
new BigNumber(swapSubmissionData.swapAmountQuote)
.multipliedBy(oneHundredMillion)
.multipliedBy(new BigNumber(1).minus(slippage))
.dp(0)
Expand All @@ -143,8 +144,8 @@ function AlexSwapContainer() {

const tx = alex.runSwap(
currentAccount?.address,
swapSubmissionData.swapAssetFrom.currency,
swapSubmissionData.swapAssetTo.currency,
swapSubmissionData.swapAssetBase.currency,
swapSubmissionData.swapAssetQuote.currency,
fromAmount,
minToAmount,
swapSubmissionData.router.map(x => x.currency)
Expand Down Expand Up @@ -197,8 +198,8 @@ function AlexSwapContainer() {
onSetIsSendingMax: value => setIsSendingMax(value),
onSubmitSwapForReview,
onSubmitSwap,
swappableAssetsFrom: migratePositiveBalancesToTop(swappableAssets),
swappableAssetsTo: swappableAssets,
swappableAssetsBase: migratePositiveBalancesToTop(swappableAssets),
swappableAssetsQuote: swappableAssets,
swapSubmissionData,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function SwapAssetItem({ asset, onClick }: SwapAssetItemProps) {
const fallback = getAvatarFallback(asset.name);

return (
<Pressable data-testid={SwapSelectors.ChooseAssetListItem} onClick={onClick} my="space.02">
<Pressable data-testid={SwapSelectors.SwapAssetListItem} onClick={onClick} my="space.02">
<ItemLayout
flagImg={
<Avatar.Root>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,77 +1,78 @@
import { useNavigate } from 'react-router-dom';
import { useNavigate, useParams } from 'react-router-dom';

import { SwapSelectors } from '@tests/selectors/swap.selectors';
import BigNumber from 'bignumber.js';
import { useFormikContext } from 'formik';
import { Stack } from 'leather-styles/jsx';

import { createMoney } from '@shared/models/money.model';
import { RouteUrls } from '@shared/route-urls';
import { isUndefined } from '@shared/utils';

import { convertAmountToFractionalUnit } from '@app/common/money/calculate-money';
import { formatMoneyWithoutSymbol } from '@app/common/money/format-money';
import { useSwapContext } from '@app/pages/swap/swap.context';

import { SwapAsset, SwapFormValues } from '../../../hooks/use-swap-form';
import { useSwapChooseAssetState } from '../swap-choose-asset';
import { SwapAssetItem } from './swap-asset-item';

interface SwapAssetList {
assets: SwapAsset[];
type: string;
}
export function SwapAssetList({ assets }: SwapAssetList) {
export function SwapAssetList({ assets, type }: SwapAssetList) {
const { fetchToAmount } = useSwapContext();
pete-watters marked this conversation as resolved.
Show resolved Hide resolved
const { swapListType } = useSwapChooseAssetState();
const { setFieldError, setFieldValue, values } = useFormikContext<SwapFormValues>();
const navigate = useNavigate();

const isFromList = swapListType === 'from';
const isToList = swapListType === 'to';
const { base, quote } = useParams();
const isBaseList = type === 'base';
const isQuoteList = type === 'quote';

const selectableAssets = assets.filter(
asset =>
(isFromList && asset.name !== values.swapAssetTo?.name) ||
(isToList && asset.name !== values.swapAssetFrom?.name)
(isBaseList && asset.name !== values.swapAssetQuote?.name) ||
(isQuoteList && asset.name !== values.swapAssetBase?.name)
);

async function onChooseAsset(asset: SwapAsset) {
async function onSelectAsset(asset: SwapAsset) {
let from: SwapAsset | undefined;
let to: SwapAsset | undefined;
if (isFromList) {
if (isBaseList) {
from = asset;
to = values.swapAssetTo;
await setFieldValue('swapAssetFrom', asset);
} else if (isToList) {
from = values.swapAssetFrom;
to = values.swapAssetQuote;
await setFieldValue('swapAssetBase', asset);
navigate(RouteUrls.Swap.replace(':base', from.name).replace(':quote', quote ?? ''));
} else if (isQuoteList) {
from = values.swapAssetBase;
to = asset;
await setFieldValue('swapAssetTo', asset);
setFieldError('swapAssetTo', undefined);
await setFieldValue('swapAssetQuote', asset);
setFieldError('swapAssetQuote', undefined);
navigate(RouteUrls.Swap.replace(':base', base ?? '').replace(':quote', to.name));
}

navigate(-1);
if (from && to && values.swapAmountFrom) {
const toAmount = await fetchToAmount(from, to, values.swapAmountFrom);
if (from && to && values.swapAmountBase) {
const toAmount = await fetchToAmount(from, to, values.swapAmountBase);
if (isUndefined(toAmount)) {
await setFieldValue('swapAmountTo', '');
await setFieldValue('swapAmountQuote', '');
return;
}
const toAmountAsMoney = createMoney(
convertAmountToFractionalUnit(new BigNumber(toAmount), to?.balance.decimals),
to?.balance.symbol ?? '',
to?.balance.decimals
);
await setFieldValue('swapAmountTo', formatMoneyWithoutSymbol(toAmountAsMoney));
setFieldError('swapAmountTo', undefined);
await setFieldValue('swapAmountQuote', formatMoneyWithoutSymbol(toAmountAsMoney));
setFieldError('swapAmountQuote', undefined);
}
}

return (
<Stack mb="space.05" p="space.05" width="100%" data-testid={SwapSelectors.ChooseAssetList}>
<Stack mb="space.05" p="space.05" width="100%" data-testid={SwapSelectors.SwapAssetList}>
{selectableAssets.map(asset => (
<SwapAssetItem
asset={asset}
key={asset.balance.symbol}
onClick={() => onChooseAsset(asset)}
onClick={() => onSelectAsset(asset)}
/>
))}
</Stack>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { RouteUrls } from '@shared/route-urls';
pete-watters marked this conversation as resolved.
Show resolved Hide resolved

import { Dialog } from '@app/ui/components/containers/dialog/dialog';
import { Header } from '@app/ui/components/containers/headers/header';

import { useSwapNavigate } from '../../hooks/use-swap-navigate';
import { useSwapContext } from '../../swap.context';
import { SwapAssetList } from './components/swap-asset-list';

export function SwapAssetDialogBase() {
const { swappableAssetsBase } = useSwapContext();
const navigate = useSwapNavigate();

return (
<Dialog
isShowing
onClose={() => navigate(RouteUrls.Swap)}
header={
<Header
title={
<>
Choose asset <br /> to swap
</>
}
variant="bigTitle"
onGoBack={() => navigate(RouteUrls.Swap)}
/>
}
>
<SwapAssetList assets={swappableAssetsBase} type="base" />
</Dialog>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { RouteUrls } from '@shared/route-urls';

import { Dialog } from '@app/ui/components/containers/dialog/dialog';
import { Header } from '@app/ui/components/containers/headers/header';

import { useSwapNavigate } from '../../hooks/use-swap-navigate';
import { useSwapContext } from '../../swap.context';
import { SwapAssetList } from './components/swap-asset-list';

export function SwapAssetDialogQuote() {
const { swappableAssetsQuote } = useSwapContext();
const navigate = useSwapNavigate();

return (
<Dialog
isShowing
onClose={() => navigate(RouteUrls.Swap)}
header={
<Header
title={
<>
Choose asset <br /> to receive
</>
}
variant="bigTitle"
onGoBack={() => navigate(RouteUrls.Swap)}
/>
}
>
<SwapAssetList assets={swappableAssetsQuote} type="quote" />
</Dialog>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import { ChevronDownIcon } from '@app/ui/icons/chevron-down-icon';
interface SelectAssetTriggerButtonProps {
icon?: string;
name: string;
onChooseAsset(): void;
onSelectAsset(): void;
symbol: string;
}
export function SelectAssetTriggerButton({
icon,
name,
onChooseAsset,
onSelectAsset,
symbol,
}: SelectAssetTriggerButtonProps) {
const [field] = useField(name);
Expand All @@ -24,7 +24,7 @@ export function SelectAssetTriggerButton({
return (
<Button
data-testid={SwapSelectors.SelectAssetTriggerBtn}
onClick={onChooseAsset}
onClick={onSelectAsset}
p="space.02"
variant="ghost"
{...field}
Expand Down
Loading
Loading