Skip to content

Commit

Permalink
refactor: swap qa changes
Browse files Browse the repository at this point in the history
  • Loading branch information
fbwoolf committed Oct 12, 2023
1 parent 77bab7b commit 3379335
Show file tree
Hide file tree
Showing 58 changed files with 596 additions and 655 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
"@typescript-eslint/eslint-plugin": "6.7.4",
"@vkontakte/vk-qr": "2.0.13",
"@zondax/ledger-stacks": "1.0.4",
"alex-sdk": "^0.1.22",
"alex-sdk": "0.1.22",
"are-passive-events-supported": "1.1.1",
"argon2-browser": "1.18.0",
"assert": "2.0.0",
Expand Down
5 changes: 0 additions & 5 deletions src/app/common/date-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ export function displayDate(txDate: string): string {
return date.format('MMM Do, YYYY');
}

export function displayTime(txDate: string) {
const date = dayjs(txDate);
return date.format('h:mm A');
}

export function isoDateToLocalDateSafe(isoDate: string) {
try {
return isoDateToLocalDate(isoDate);
Expand Down
2 changes: 1 addition & 1 deletion src/app/common/hooks/use-bitcoin-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export function useBitcoinContracts() {
const txMoney = createMoneyFromDecimal(bitcoinValue, 'BTC');
const txFiatValue = i18nFormatCurrency(calculateFiatValue(txMoney)).toString();
const txFiatValueSymbol = bitcoinMarketData.price.symbol;
const txLink = { blockchain: 'bitcoin', txid: txId };
const txLink = { blockchain: 'bitcoin', txId };

return {
txId,
Expand Down
14 changes: 13 additions & 1 deletion src/app/common/hooks/use-convert-to-fiat-amount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { useCallback } from 'react';
import { CryptoCurrencies } from '@shared/models/currencies.model';
import type { Money } from '@shared/models/money.model';

import { useCryptoCurrencyMarketData } from '@app/query/common/market-data/market-data.hooks';
import {
useAlexMarketData,
useCryptoCurrencyMarketData,
} from '@app/query/common/market-data/market-data.hooks';

import { baseCurrencyAmountInQuote } from '../money/calculate-money';

Expand All @@ -15,3 +18,12 @@ export function useConvertCryptoCurrencyToFiatAmount(currency: CryptoCurrencies)
[cryptoCurrencyMarketData]
);
}

export function useConvertAlexSwapCurrencyToFiatAmount(currency: CryptoCurrencies, price: Money) {
const alexCurrencyMarketData = useAlexMarketData(currency, price);

return useCallback(
(value: Money) => baseCurrencyAmountInQuote(value, alexCurrencyMarketData),
[alexCurrencyMarketData]
);
}
7 changes: 5 additions & 2 deletions src/app/common/math/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import BigNumber from 'bignumber.js';

export function initBigNumber(num: string | number | BigNumber) {
return BigNumber.isBigNumber(num) ? num : new BigNumber(num);
import { isBigInt } from '@shared/utils';

export function initBigNumber(num: string | number | BigNumber | bigint) {
if (BigNumber.isBigNumber(num)) return num;
return isBigInt(num) ? new BigNumber(num.toString()) : new BigNumber(num);
}

export function sumNumbers(nums: number[]) {
Expand Down
10 changes: 3 additions & 7 deletions src/app/common/money/calculate-money.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { BigNumber } from 'bignumber.js';

import { MarketData, formatMarketPair } from '@shared/models/market.model';
import { Money, NumType, createMoney } from '@shared/models/money.model';
import { isBigInt, isNumber } from '@shared/utils';
import { isNumber } from '@shared/utils';

import { sumNumbers } from '../math/helpers';
import { initBigNumber, sumNumbers } from '../math/helpers';
import { formatMoney } from './format-money';
import { isMoney } from './is-money';

Expand Down Expand Up @@ -36,11 +36,7 @@ export function convertToMoneyTypeWithDefaultOfZero(
num?: NumType,
decimals?: number
) {
return createMoney(
isBigInt(num) ? new BigNumber(num.toString()) : new BigNumber(num ?? 0),
symbol.toUpperCase(),
decimals
);
return createMoney(initBigNumber(num ?? 0), symbol.toUpperCase(), decimals);
}

// ts-unused-exports:disable-next-line
Expand Down
15 changes: 0 additions & 15 deletions src/app/components/icons/dot-icon.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/components/icons/swap-icon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export function SwapIcon(props: React.SVGProps<SVGSVGElement>) {
<g clipPath="url(#clip0_5842_83269)">
<path
d="M1.94727 4.50229H13.2596M10.7086 7.59746L13.7987 4.50229L10.7086 1.41211M13.9934 11.4964H2.756M5.30702 8.4012L2.21185 11.4964L5.30702 14.5865"
stroke="#12100F"
stroke="currentColor"
strokeWidth="2"
strokeMiterlimit="10"
/>
Expand Down
6 changes: 4 additions & 2 deletions src/app/components/nonce-setter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect } from 'react';

import { useFormikContext } from 'formik';

import { logger } from '@shared/logger';
import { StacksSendFormValues, StacksTransactionFormValues } from '@shared/models/form.model';

import { useNextNonce } from '@app/query/stacks/nonce/account-nonces.hooks';
Expand All @@ -13,8 +14,9 @@ export function NonceSetter() {
const { data: nextNonce } = useNextNonce();

useEffect(() => {
if (nextNonce && !touched.nonce && values.nonce !== nextNonce.nonce)
setFieldValue('nonce', nextNonce.nonce);
const setAsyncFieldValue = async (nonce: number) => await setFieldValue('nonce', nonce);
if (nextNonce?.nonce && !touched.nonce && values.nonce !== nextNonce.nonce)
setAsyncFieldValue(nextNonce.nonce).catch(e => logger.error(e));
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [nextNonce?.nonce]);

Expand Down
2 changes: 1 addition & 1 deletion src/app/features/edit-nonce-drawer/edit-nonce-drawer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function EditNonceDrawer() {
const onBlur = useCallback(() => validateField('nonce'), [validateField]);

const onSubmit = useCallback(async () => {
validateField('nonce');
await validateField('nonce');
if (!errors.nonce) onGoBack();
}, [errors.nonce, onGoBack, validateField]);

Expand Down
2 changes: 1 addition & 1 deletion src/app/pages/rpc-sign-psbt/use-rpc-sign-psbt.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export function useRpcSignPsbt() {
txId: txid,
txLink: {
blockchain: 'bitcoin',
txid: txid || '',
txId: txid || '',
},
txValue: formatMoney(transferTotalAsMoney),
};
Expand Down
38 changes: 18 additions & 20 deletions src/app/pages/swap/components/selected-asset-field.tsx
Original file line number Diff line number Diff line change
@@ -1,46 +1,44 @@
import { Field } from 'formik';
import { Box, Flex, HStack, styled } from 'leather-styles/jsx';

import { Flag } from '@app/components/layout/flag';
import { Box, Flex, HStack } from 'leather-styles/jsx';

interface SelectedAssetFieldProps {
contentLeft: React.JSX.Element;
contentRight: React.JSX.Element;
icon?: string;
name: string;
showError?: boolean;
}
export function SelectedAssetField({
contentLeft,
contentRight,
icon,
name,
showError,
}: SelectedAssetFieldProps) {
return (
<Flex
alignItems="center"
border="1px solid"
borderColor="accent.border-default !important"
borderColor={showError ? 'error !important' : 'accent.border-default !important'}
borderRadius="10px"
css={{
'&:focus-within': {
border: '1px solid',
borderColor: `${
showError ? 'error !important' : 'accent.action-primary-default !important'
}`,
},
}}
height="76px"
mb="tight"
mb="space.02"
minHeight="64px"
px="base"
px="space.04"
width="100%"
>
<Box width="100%">
<Field as="div" name={name}>
<Flag
align="middle"
img={
icon ? <styled.img src={icon} width="24px" height="24px" alt="Swap asset" /> : null
}
spacing="tight"
>
<HStack alignItems="center" justifyContent="space-between">
{contentLeft}
{contentRight}
</HStack>
</Flag>
<HStack alignItems="center" justifyContent="space-between">
{contentLeft}
{contentRight}
</HStack>
</Field>
</Box>
</Flex>
Expand Down
67 changes: 39 additions & 28 deletions src/app/pages/swap/components/swap-amount-field.tsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,73 @@
import { ChangeEvent } from 'react';

import { Input, Stack, color } from '@stacks/ui';
import { useField, useFormikContext } from 'formik';
import { Stack, styled } from 'leather-styles/jsx';

import { isDefined, isUndefined } from '@shared/utils';

import { useShowFieldError } from '@app/common/form-utils';
import { Caption } from '@app/components/typography';

import { SwapFormValues } from '../hooks/use-swap';
import { SwapFormValues } from '../hooks/use-swap-form';
import { useSwapContext } from '../swap.context';

function getPlaceholderValue(name: string, values: SwapFormValues) {
if (name === 'swapAmountFrom' && isDefined(values.swapAssetFrom)) return '0';
if (name === 'swapAmountTo' && isDefined(values.swapAssetTo)) return '0';
return '-';
}

interface SwapAmountFieldProps {
amountAsFiat: string;
isDisabled?: boolean;
name: string;
}
export function SwapAmountField({ amountAsFiat, isDisabled, name }: SwapAmountFieldProps) {
const { fetchToAmount } = useSwapContext();
const { setFieldValue, values } = useFormikContext<SwapFormValues>();
const { fetchToAmount, onSetIsSendingMax } = useSwapContext();
const { setErrors, setFieldValue, values } = useFormikContext<SwapFormValues>();
const [field] = useField(name);
const showError = useShowFieldError(name);
const showError = useShowFieldError(name) && name === 'swapAmountFrom' && values.swapAssetTo;

async function onChange(event: ChangeEvent<HTMLInputElement>) {
field.onChange(event);
const value = event.currentTarget.value;
const { swapAssetFrom, swapAssetTo } = values;
if (swapAssetFrom != null && swapAssetTo && !isNaN(Number(value))) {
await setFieldValue('swapAmountTo', '');
const toAmount = await fetchToAmount(swapAssetFrom, swapAssetTo, value);
await setFieldValue('swapAmountTo', toAmount);
}
if (isUndefined(swapAssetFrom) || isUndefined(swapAssetTo)) return;
onSetIsSendingMax(false);
const value = event.currentTarget.value;
const toAmount = await fetchToAmount(swapAssetFrom, swapAssetTo, value);
await setFieldValue('swapAmountTo', Number(toAmount));
field.onChange(event);
setErrors({});
}

return (
<Stack alignItems="flex-end" spacing="extra-tight" width="50%">
<Caption as="label" hidden htmlFor={name}>
<Stack alignItems="flex-end" gap="space.01" width="65%">
<styled.label hidden htmlFor={name}>
{name}
</Caption>
<Input
_disabled={{ border: 'none', color: color('text-caption') }}
_focus={{ border: 'none' }}
</styled.label>
<styled.input
_disabled={{
bg: 'transparent',
border: 'none',
color: 'accent.text-subdued',
}}
autoComplete="off"
border="none"
color={showError ? color('feedback-error') : 'unset'}
color={showError ? 'error' : 'accent.text-primary'}
display="block"
fontSize="20px"
height="28px"
isDisabled={isDisabled}
disabled={isDisabled}
p="0px"
placeholder="0"
placeholder={getPlaceholderValue(name, values)}
ring="none"
textAlign="right"
type="number"
textStyle="heading.05"
width="100%"
{...field}
onChange={onChange}
/>
<Caption color={showError ? color('feedback-error') : color('text-caption')}>
{amountAsFiat}
</Caption>
{amountAsFiat ? (
<styled.span color={showError ? 'error' : 'accent.text-subdued'} textStyle="caption.02">
{amountAsFiat}
</styled.span>
) : null}
</Stack>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,22 @@ import { HStack, styled } from 'leather-styles/jsx';
import { Flag } from '@app/components/layout/flag';

interface SwapAssetItemLayoutProps {
caption: string;
icon: string;
symbol: string;
value: string;
}
export function SwapAssetItemLayout({ icon, symbol, value }: SwapAssetItemLayoutProps) {
export function SwapAssetItemLayout({ caption, icon, symbol, value }: SwapAssetItemLayoutProps) {
return (
<Flag
align="middle"
img={<styled.img src={icon} width="32px" height="32px" alt="Swap asset" />}
spacing="tight"
img={<styled.img src={icon} width="48px" height="48px" alt="Swap asset" />}
spacing="space.03"
width="100%"
>
<styled.span color="accent.text-subdued" textStyle="caption.02">
{caption}
</styled.span>
<HStack alignItems="center" justifyContent="space-between">
<styled.span textStyle="heading.05">{symbol}</styled.span>
<styled.span textStyle="heading.05">{value}</styled.span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export function SwapAssetsPairLayout({ swapAssetFrom, swapAssetTo }: SwapAssetsP
<Stack
border="1px solid"
borderColor="accent.border-default !important"
borderRadius="10px"
borderRadius="12px"
gap="space.01"
mb="space.05"
mt="space.04"
p="space.05"
p="space.04"
width="100%"
>
{swapAssetFrom}
<Box p="space.02">
<Box height="24px" px="space.04" py="space.01" width="48px">
<ArrowDownIcon />
</Box>
{swapAssetTo}
Expand Down
Loading

0 comments on commit 3379335

Please sign in to comment.