Skip to content

Commit

Permalink
chore(suite): update network symbol naming & fix passing symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
adderpositive authored and tomasklim committed Dec 3, 2024
1 parent f732917 commit 0eee069
Show file tree
Hide file tree
Showing 21 changed files with 335 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useTranslation } from 'src/hooks/suite';
import { useStakeEthFormContext } from 'src/hooks/wallet/useStakeEthForm';
import {
validateDecimals,
validateLimitsBigNum,
validateCryptoLimits,
validateMin,
validateReserveOrBalance,
} from 'src/utils/suite/validation';
Expand Down Expand Up @@ -61,7 +61,7 @@ export const Inputs = () => {
reserveOrBalance: validateReserveOrBalance(translationString, {
account,
}),
limits: validateLimitsBigNum(translationString, {
limits: validateCryptoLimits(translationString, {
amountLimits,
formatter: CryptoAmountFormatter,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NumberInput } from 'src/components/suite';
import { CRYPTO_INPUT, FIAT_INPUT } from 'src/types/wallet/stakeForms';
import { useSelector, useTranslation } from 'src/hooks/suite';
import { selectSelectedAccount } from 'src/reducers/wallet/selectedAccountReducer';
import { validateDecimals, validateLimitsBigNum, validateMin } from 'src/utils/suite/validation';
import { validateDecimals, validateCryptoLimits, validateMin } from 'src/utils/suite/validation';
import { useUnstakeEthFormContext } from 'src/hooks/wallet/useUnstakeEthForm';

export const Inputs = () => {
Expand Down Expand Up @@ -42,7 +42,7 @@ export const Inputs = () => {
validate: {
min: validateMin(translationString),
decimals: validateDecimals(translationString, { decimals: network.decimals }),
limits: validateLimitsBigNum(translationString, {
limits: validateCryptoLimits(translationString, {
amountLimits,
formatter: CryptoAmountFormatter,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
getAmountLimits,
} from 'src/utils/wallet/coinmarket/buyUtils';
import { useFormDraft } from 'src/hooks/wallet/useFormDraft';
import { AmountLimits } from 'src/types/wallet/coinmarketCommonTypes';
import { CoinmarketTradeBuyType, UseCoinmarketFormProps } from 'src/types/coinmarket/coinmarket';
import {
addIdsToQuotes,
Expand Down Expand Up @@ -50,6 +49,7 @@ import { useCoinmarketCurrencySwitcher } from 'src/hooks/wallet/coinmarket/form/
import { useCoinmarketModalCrypto } from 'src/hooks/wallet/coinmarket/form/common/useCoinmarketModalCrypto';
import { useCoinmarketInfo } from 'src/hooks/wallet/coinmarket/useCoinmarketInfo';
import { useCoinmarketBuyFormDefaultValues } from 'src/hooks/wallet/coinmarket/form/useCoinmarketBuyFormDefaultValues';
import type { AmountLimitProps } from 'src/utils/suite/validation';

import { useCoinmarketInitializer } from './common/useCoinmarketInitializer';

Expand All @@ -71,7 +71,7 @@ export const useCoinmarketBuyForm = ({
useCoinmarketNavigation(account);

// states
const [amountLimits, setAmountLimits] = useState<AmountLimits | undefined>(undefined);
const [amountLimits, setAmountLimits] = useState<AmountLimitProps | undefined>(undefined);
const [innerQuotes, setInnerQuotes] = useState<BuyTrade[] | undefined>(
isNotFormPage ? quotes : undefined,
);
Expand Down Expand Up @@ -233,11 +233,13 @@ export const useCoinmarketBuyForm = ({
const isSelectedPaymentMethodAvailable =
paymentMethodsFromQuotes.find(item => item.value === paymentMethodSelected) !==
undefined;
const limits = getAmountLimits(quoteRequest, quotesDefault); // from all quotes except alternative
if (limits && quoteRequest.wantCrypto) {
limits.currency =
cryptoIdToCoinSymbol(quoteRequest.receiveCurrency) ?? limits.currency;
}
const symbol =
cryptoIdToCoinSymbol(quoteRequest.receiveCurrency) ?? quoteRequest.receiveCurrency;
const limits = getAmountLimits({
request: quoteRequest,
quotes: quotesDefault,
currency: symbol,
}); // from all quotes except alternative

setInnerQuotes(quotesSuccess);
dispatch(coinmarketBuyActions.saveQuotes(quotesSuccess));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {
import { useFormDraft } from 'src/hooks/wallet/useFormDraft';
import { useCoinmarketNavigation } from 'src/hooks/wallet/useCoinmarketNavigation';
import { useBitcoinAmountUnit } from 'src/hooks/wallet/useBitcoinAmountUnit';
import { CryptoAmountLimits, TradeExchange } from 'src/types/wallet/coinmarketCommonTypes';
import { TradeExchange } from 'src/types/wallet/coinmarketCommonTypes';
import {
CoinmarketTradeExchangeType,
UseCoinmarketFormProps,
Expand Down Expand Up @@ -64,6 +64,7 @@ import { useCoinmarketModalCrypto } from 'src/hooks/wallet/coinmarket/form/commo
import { useCoinmarketAccount } from 'src/hooks/wallet/coinmarket/form/common/useCoinmarketAccount';
import { useCoinmarketInfo } from 'src/hooks/wallet/coinmarket/useCoinmarketInfo';
import { useCoinmarketFiatValues } from 'src/hooks/wallet/coinmarket/form/common/useCoinmarketFiatValues';
import type { CryptoAmountLimitProps } from 'src/utils/suite/validation';

import { useCoinmarketInitializer } from './common/useCoinmarketInitializer';

Expand Down Expand Up @@ -102,7 +103,7 @@ export const useCoinmarketExchangeForm = ({
recomposeAndSign,
} = useCoinmarketRecomposeAndSign();

const [amountLimits, setAmountLimits] = useState<CryptoAmountLimits | undefined>(undefined);
const [amountLimits, setAmountLimits] = useState<CryptoAmountLimitProps | undefined>(undefined);

const [innerQuotes, setInnerQuotes] = useState<ExchangeTrade[] | undefined>(
coinmarketGetSuccessQuotes<CoinmarketTradeExchangeType>(quotes),
Expand Down Expand Up @@ -274,11 +275,8 @@ export const useCoinmarketExchangeForm = ({
const allQuotes = await getQuotesRequest(quotesRequest);

if (Array.isArray(allQuotes)) {
const limits = getAmountLimits(allQuotes);
if (limits) {
limits.currency =
cryptoIdToCoinSymbol(limits.currency as CryptoId) ?? limits.currency;
}
const currency = cryptoIdToCoinSymbol(quotesRequest.send) ?? quotesRequest.send;
const limits = getAmountLimits({ quotes: allQuotes, currency });

const successQuotes = addIdsToQuotes<CoinmarketTradeExchangeType>(
getSuccessQuotesOrdered(allQuotes),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { createQuoteLink, getAmountLimits } from 'src/utils/wallet/coinmarket/se
import { useFormDraft } from 'src/hooks/wallet/useFormDraft';
import { useCoinmarketNavigation } from 'src/hooks/wallet/useCoinmarketNavigation';
import { useBitcoinAmountUnit } from 'src/hooks/wallet/useBitcoinAmountUnit';
import { AmountLimits, TradeSell } from 'src/types/wallet/coinmarketCommonTypes';
import { TradeSell } from 'src/types/wallet/coinmarketCommonTypes';
import { selectLocalCurrency } from 'src/reducers/wallet/settingsReducer';
import {
CoinmarketAccountOptionsGroupOptionProps,
Expand Down Expand Up @@ -54,6 +54,7 @@ import { useCoinmarketComposeTransaction } from 'src/hooks/wallet/coinmarket/for
import { useCoinmarketCurrencySwitcher } from 'src/hooks/wallet/coinmarket/form/common/useCoinmarketCurrencySwitcher';
import { useCoinmarketAccount } from 'src/hooks/wallet/coinmarket/form/common/useCoinmarketAccount';
import { useCoinmarketInfo } from 'src/hooks/wallet/coinmarket/useCoinmarketInfo';
import type { AmountLimitProps } from 'src/utils/suite/validation';

import { useCoinmarketInitializer } from './common/useCoinmarketInitializer';

Expand Down Expand Up @@ -102,7 +103,7 @@ export const useCoinmarketSellForm = ({
trade => trade.tradeType === 'sell' && trade.key === transactionId,
) as TradeSell | undefined;

const [amountLimits, setAmountLimits] = useState<AmountLimits | undefined>(undefined);
const [amountLimits, setAmountLimits] = useState<AmountLimitProps | undefined>(undefined);
const [sellStep, setSellStep] = useState<CoinmarketSellStepType>('BANK_ACCOUNT');
const [innerQuotes, setInnerQuotes] = useState<SellFiatTrade[] | undefined>(
coinmarketGetSuccessQuotes<CoinmarketTradeSellType>(quotes),
Expand Down Expand Up @@ -254,11 +255,14 @@ export const useCoinmarketSellForm = ({
const allQuotes = await getQuotesRequest(quoteRequest);

if (Array.isArray(allQuotes)) {
const limits = getAmountLimits(quoteRequest, allQuotes);
if (limits && quoteRequest.amountInCrypto) {
limits.currency =
cryptoIdToCoinSymbol(quoteRequest.cryptoCurrency) ?? limits.currency;
}
const currency =
cryptoIdToCoinSymbol(quoteRequest.cryptoCurrency) ??
quoteRequest.cryptoCurrency;
const limits = getAmountLimits({
request: quoteRequest,
quotes: allQuotes,
currency,
});

const quotesDefault = filterQuotesAccordingTags<CoinmarketTradeSellType>(
addIdsToQuotes<CoinmarketTradeSellType>(allQuotes, 'sell'),
Expand Down
9 changes: 3 additions & 6 deletions packages/suite/src/hooks/wallet/useStakeEthForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import {
} from '@suite-common/wallet-utils';
import { isChanged } from '@suite-common/suite-utils';
import { PrecomposedTransactionFinal, StakeFormState } from '@suite-common/wallet-types';
import {
AmountLimitsString,
StakeContextValues,
selectFiatRatesByFiatRateKey,
} from '@suite-common/wallet-core';
import { StakeContextValues, selectFiatRatesByFiatRateKey } from '@suite-common/wallet-core';
import {
MIN_ETH_AMOUNT_FOR_STAKING,
MIN_ETH_BALANCE_FOR_STAKING,
Expand All @@ -36,6 +32,7 @@ import {
import { selectLocalCurrency } from 'src/reducers/wallet/settingsReducer';
import { signTransaction } from 'src/actions/wallet/stakeActions';
import { getEthNetworkForWalletSdk, getStakeFormsDefaultValues } from 'src/utils/suite/stake';
import type { CryptoAmountLimitProps } from 'src/utils/suite/validation';

import { useStakeCompose } from './form/useStakeCompose';
import { useFormDraft } from './useFormDraft';
Expand All @@ -57,7 +54,7 @@ export const useStakeEthForm = ({ selectedAccount }: UseStakeFormsProps): StakeC
selectFiatRatesByFiatRateKey(state, getFiatRateKey(symbol, localCurrency), 'current'),
);

const amountLimits: AmountLimitsString = {
const amountLimits: CryptoAmountLimitProps = {
currency: symbol,
minCrypto: MIN_ETH_AMOUNT_FOR_STAKING.toString(),
maxCrypto: account.formattedBalance,
Expand Down
6 changes: 3 additions & 3 deletions packages/suite/src/hooks/wallet/useUnstakeEthForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import { PrecomposedTransactionFinal } from '@suite-common/wallet-types';
import { isChanged } from '@suite-common/suite-utils';
import {
AmountLimitsString,
selectFiatRatesByFiatRateKey,
UnstakeContextValues as UnstakeContextValuesBase,
UnstakeFormState,
Expand All @@ -35,6 +34,7 @@ import {
getStakeFormsDefaultValues,
simulateUnstake,
} from 'src/utils/suite/stake';
import type { AmountLimitProps } from 'src/utils/suite/validation';

import { useStakeCompose } from './form/useStakeCompose';
import { useFormDraft } from './useFormDraft';
Expand All @@ -43,7 +43,7 @@ import { useFees } from './form/useFees';
type UnstakeOptions = 'all' | 'rewards' | 'other';

type UnstakeContextValues = UnstakeContextValuesBase & {
amountLimits: AmountLimitsString;
amountLimits: AmountLimitProps;
approximatedInstantEthAmount?: string | null;
unstakeOption: UnstakeOptions;
setUnstakeOption: (option: UnstakeOptions) => void;
Expand Down Expand Up @@ -72,7 +72,7 @@ export const useUnstakeEthForm = ({
);

const autocompoundBalance = getAccountAutocompoundBalance(account);
const amountLimits: AmountLimitsString = {
const amountLimits: AmountLimitProps = {
currency: symbol,
maxCrypto: autocompoundBalance,
};
Expand Down
21 changes: 8 additions & 13 deletions packages/suite/src/types/coinmarket/coinmarketForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,7 @@ import type { Account } from 'src/types/wallet';
import type { BuyInfo } from 'src/actions/wallet/coinmarketBuyActions';
import { AppState } from 'src/reducers/store';
import { Dispatch, GetState } from 'src/types/suite';
import {
AmountLimits,
CryptoAmountLimits,
Option,
TradeExchange,
TradeSell,
} from 'src/types/wallet/coinmarketCommonTypes';
import { Option, TradeExchange, TradeSell } from 'src/types/wallet/coinmarketCommonTypes';
import { SendContextValues } from 'src/types/wallet/sendForm';
import { SellInfo } from 'src/actions/wallet/coinmarketSellActions';
import { ExchangeInfo } from 'src/actions/wallet/coinmarketExchangeActions';
Expand All @@ -69,6 +63,7 @@ import {
FORM_RATE_TYPE,
} from 'src/constants/wallet/coinmarket/form';
import type { TranslationKey } from 'src/components/suite/Translation';
import { AmountLimitProps, CryptoAmountLimitProps } from 'src/utils/suite/validation';

export interface CoinmarketBuyFormProps {
fiatInput?: string;
Expand Down Expand Up @@ -165,7 +160,7 @@ interface CoinmarketCommonFormBuySellProps {
defaultCurrency: Option;
defaultPaymentMethod: CoinmarketPaymentMethodListProps;
paymentMethods: CoinmarketPaymentMethodListProps[];
amountLimits?: AmountLimits;
amountLimits?: AmountLimitProps;
}

type CoinmarketVerifyAccountProps = (
Expand Down Expand Up @@ -194,7 +189,7 @@ export interface CoinmarketBuyFormContextProps
confirmTrade: (address: string) => void;
verifyAddress: CoinmarketVerifyAccountProps;
removeDraft: (key: string) => void;
setAmountLimits: (limits?: AmountLimits) => void;
setAmountLimits: (limits?: AmountLimitProps) => void;
}

export interface CoinmarketSellFormContextProps
Expand All @@ -221,7 +216,7 @@ export interface CoinmarketSellFormContextProps
shouldSendInSats: boolean | undefined;
changeFeeLevel: (level: FeeLevel['label']) => void;
composeRequest: SendContextValues<CoinmarketSellExchangeFormProps>['composeTransaction'];
setAmountLimits: (limits?: AmountLimits) => void;
setAmountLimits: (limits?: AmountLimitProps) => void;

setSellStep: (step: CoinmarketSellStepType) => void;
addBankAccount: () => void;
Expand Down Expand Up @@ -249,7 +244,7 @@ export interface CoinmarketExchangeFormContextProps

exchangeInfo?: ExchangeInfo;
defaultCurrency: Option;
amountLimits?: CryptoAmountLimits;
amountLimits?: CryptoAmountLimitProps;
composedLevels?: PrecomposedLevels | PrecomposedLevelsCardano;
allQuotes: ExchangeTrade[] | undefined;
quotes: ExchangeTrade[] | undefined;
Expand All @@ -259,7 +254,7 @@ export interface CoinmarketExchangeFormContextProps
addressVerified: string | undefined;
shouldSendInSats: boolean | undefined;
setReceiveAccount: (account?: Account) => void;
setAmountLimits: (limits?: CryptoAmountLimits) => void;
setAmountLimits: (limits?: CryptoAmountLimitProps) => void;
composeRequest: SendContextValues<CoinmarketSellExchangeFormProps>['composeTransaction'];
changeFeeLevel: (level: FeeLevel['label']) => void;
removeDraft: (key: string) => void;
Expand Down Expand Up @@ -339,7 +334,7 @@ export interface CoinmarketUseFormActionsProps<T extends CoinmarketSellExchangeF
draftUpdated: CoinmarketSellExchangeFormProps | null;
type: CoinmarketTradeSellExchangeType;
handleChange: (offLoading?: boolean) => Promise<void>;
setAmountLimits: (limits?: AmountLimits) => void;
setAmountLimits: (limits?: AmountLimitProps) => void;
changeFeeLevel: (level: FeeLevel['label']) => void;
composeRequest: SendContextValues<CoinmarketSellExchangeFormProps>['composeTransaction'];
setAccountOnChange: (account: Account) => void;
Expand Down
11 changes: 0 additions & 11 deletions packages/suite/src/types/wallet/coinmarketCommonTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,3 @@ export type FiatCurrencyOption = {
value: FiatCurrencyCode;
label: string;
};

export interface CryptoAmountLimits {
currency: string;
minCrypto?: number;
maxCrypto?: number;
}

export interface AmountLimits extends CryptoAmountLimits {
minFiat?: number;
maxFiat?: number;
}
5 changes: 3 additions & 2 deletions packages/suite/src/utils/suite/stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ import {
MAX_ETH_AMOUNT_FOR_STAKING,
UNSTAKE_INTERCHANGES,
} from '@suite-common/wallet-constants';
import { NetworkSymbol } from '@suite-common/wallet-config';
import type { NetworkSymbol } from '@suite-common/wallet-config';
import { getEthereumEstimateFeeParams, isPending, sanitizeHex } from '@suite-common/wallet-utils';
import TrezorConnect, { EthereumTransaction, Success, InternalTransfer } from '@trezor/connect';
import { BigNumber } from '@trezor/utils/src/bigNumber';
import { ValidatorsQueue } from '@suite-common/wallet-core';
import { BlockchainEstimatedFee } from '@trezor/connect/src/types/api/blockchainEstimateFee';
import { PartialRecord } from '@trezor/type-utils';

import { TranslationFunction } from 'src/hooks/suite/useTranslation';

Expand All @@ -37,7 +38,7 @@ const secondsToDays = (seconds: number) => Math.round(seconds / 60 / 60 / 24);
type EthNetwork = 'holesky' | 'mainnet';

export const getEthNetworkForWalletSdk = (symbol: NetworkSymbol): EthNetwork => {
const ethNetworks: { [key in NetworkSymbol]?: EthNetwork } = {
const ethNetworks: PartialRecord<NetworkSymbol, EthNetwork> = {
thol: 'holesky',
eth: 'mainnet',
};
Expand Down
Loading

0 comments on commit 0eee069

Please sign in to comment.