Skip to content

Commit

Permalink
refactor(utils): migrate money utils out of extension
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed May 29, 2024
1 parent 36e5238 commit 43ad127
Show file tree
Hide file tree
Showing 83 changed files with 102 additions and 214 deletions.
3 changes: 2 additions & 1 deletion src/app/common/asset-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Money } from '@leather-wallet/models';

import type { MarketData } from '@shared/models/market.model';
import { type Money } from '@shared/models/money.model';

import { baseCurrencyAmountInQuote } from './money/calculate-money';
import { i18nFormatCurrency } from './money/format-money';
Expand Down
2 changes: 1 addition & 1 deletion src/app/common/error-formatters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Money } from '@leather-wallet/models';
import { isFunction } from '@leather-wallet/utils';

import { FormErrorMessages } from '@shared/error-messages';
import { Money } from '@shared/models/money.model';

export function formatPrecisionError(num?: Money) {
if (!num) return FormErrorMessages.CannotDeterminePrecision;
Expand Down
3 changes: 1 addition & 2 deletions src/app/common/hooks/balance/use-total-balance.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useMemo } from 'react';

import { useCryptoCurrencyMarketDataMeanAverage } from '@leather-wallet/query';

import { createMoney } from '@shared/models/money.model';
import { createMoney } from '@leather-wallet/utils';

import { baseCurrencyAmountInQuote } from '@app/common/money/calculate-money';
import { i18nFormatCurrency } from '@app/common/money/format-money';
Expand Down
4 changes: 2 additions & 2 deletions src/app/common/hooks/use-bitcoin-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { useNavigate } from 'react-router-dom';

import { RpcErrorCode } from '@btckit/types';
import { JsDLCInterface } from '@dlc-link/dlc-tools';
import type { Money } from '@leather-wallet/models';
import {
sendAcceptedBitcoinContractOfferToProtocolWallet,
useCalculateBitcoinFiatValue,
useCryptoCurrencyMarketDataMeanAverage,
} from '@leather-wallet/query';
import { satToBtc } from '@leather-wallet/utils';
import { createMoneyFromDecimal, satToBtc } from '@leather-wallet/utils';
import { bytesToHex } from '@stacks/common';

import {
deriveAddressIndexKeychainFromAccount,
extractAddressIndexFromPath,
} from '@shared/crypto/bitcoin/bitcoin.utils';
import { Money, createMoneyFromDecimal } from '@shared/models/money.model';
import { RouteUrls } from '@shared/route-urls';
import { BitcoinContractResponseStatus } from '@shared/rpc/methods/accept-bitcoin-contract';
import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-methods';
Expand Down
2 changes: 1 addition & 1 deletion src/app/common/hooks/use-convert-to-fiat-amount.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useCallback } from 'react';

import type { Money } from '@leather-wallet/models';
import { useCryptoCurrencyMarketDataMeanAverage } from '@leather-wallet/query';

import { CryptoCurrencies } from '@shared/models/currencies.model';
import { type Money } from '@shared/models/money.model';

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

Expand Down
2 changes: 1 addition & 1 deletion src/app/common/money/calculate-money.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createMoney, createMoneyFromDecimal } from '@leather-wallet/utils';
import BigNumber from 'bignumber.js';

import { MarketData, createMarketData, createMarketPair } from '@shared/models/market.model';
import { createMoney, createMoneyFromDecimal } from '@shared/models/money.model';

import {
baseCurrencyAmountInQuote,
Expand Down
4 changes: 2 additions & 2 deletions src/app/common/money/calculate-money.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { initBigNumber, isNumber, sumNumbers } from '@leather-wallet/utils';
import type { Money, NumType } from '@leather-wallet/models';
import { createMoney, initBigNumber, isNumber, sumNumbers } from '@leather-wallet/utils';
import { BigNumber } from 'bignumber.js';

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

import { formatMoney } from './format-money';
import { isMoney } from './money.utils';
Expand Down
2 changes: 1 addition & 1 deletion src/app/common/money/format-money.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Money } from '@shared/models/money.model';
import type { Money } from '@leather-wallet/models';

const thinSpace = ' ';

Expand Down
3 changes: 1 addition & 2 deletions src/app/common/money/money.utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Money } from '@leather-wallet/models';
import { isObject } from '@leather-wallet/utils';
import BigNumber from 'bignumber.js';

import { Money } from '@shared/models/money.model';

export function isMoney(val: unknown): val is Money {
if (!isObject(val)) return false;
return 'amount' in val && 'symbol' in val && 'decimals' in val;
Expand Down
3 changes: 1 addition & 2 deletions src/app/common/psbt/requests.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import type { Money } from '@leather-wallet/models';
import { isString } from '@leather-wallet/utils';
import * as btc from '@scure/btc-signer';
import { PsbtPayload } from '@stacks/connect';
import { decodeToken } from 'jsontokens';

import { Money } from '@shared/models/money.model';

export interface SignPsbtArgs {
addressNativeSegwitTotal?: Money;
addressTaprootTotal?: Money;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createNullArrayOfLength, sumNumbers } from '@leather-wallet/utils';
import { createMoney, createNullArrayOfLength, sumNumbers } from '@leather-wallet/utils';

import { BTC_P2WPKH_DUST_AMOUNT } from '@shared/constants';
import { createMoney } from '@shared/models/money.model';

import { determineUtxosForSpend } from './local-coin-selection';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { UtxoResponseItem } from '@leather-wallet/query';
import { createMoney } from '@leather-wallet/utils';
import BigNumber from 'bignumber.js';
import { sha256 } from 'bitcoinjs-lib/src/crypto';

import { createMoney } from '@shared/models/money.model';

import { filterUneconomicalUtxos } from '../utils';
import { calculateMaxBitcoinSpend } from './calculate-max-bitcoin-spend';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { UtxoResponseItem } from '@leather-wallet/query';
import { satToBtc } from '@leather-wallet/utils';
import { createMoney, satToBtc } from '@leather-wallet/utils';
import BigNumber from 'bignumber.js';

import { AverageBitcoinFeeRates } from '@shared/models/fees/bitcoin-fees.model';
import { createMoney } from '@shared/models/money.model';

import { filterUneconomicalUtxos, getSpendableAmount } from '../utils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useCallback } from 'react';

import type { Money } from '@leather-wallet/models';
import type { UtxoResponseItem } from '@leather-wallet/query';
import * as btc from '@scure/btc-signer';

import { logger } from '@shared/logger';
import type { TransferRecipient } from '@shared/models/form.model';
import { Money } from '@shared/models/money.model';

import {
determineUtxosForSpend,
Expand Down
2 changes: 1 addition & 1 deletion src/app/common/validation/forms/amount-validators.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Money } from '@leather-wallet/models';
import type { UtxoResponseItem } from '@leather-wallet/query';
import {
btcToSat,
Expand All @@ -10,7 +11,6 @@ import {
import BigNumber from 'bignumber.js';
import * as yup from 'yup';

import { Money } from '@shared/models/money.model';
import { analytics } from '@shared/utils/analytics';

import { convertAmountToBaseUnit } from '@app/common/money/calculate-money';
Expand Down
3 changes: 1 addition & 2 deletions src/app/common/validation/forms/fee-validators.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { Money } from '@leather-wallet/models';
import { btcToSat, isNumber, moneyToBaseUnit, stxToMicroStx } from '@leather-wallet/utils';
import BigNumber from 'bignumber.js';
import { AnyObject, NumberSchema } from 'yup';

import { Money } from '@shared/models/money.model';

import { formatInsufficientBalanceError, formatPrecisionError } from '@app/common/error-formatters';
import {
btcAmountPrecisionValidator,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { Money } from '@leather-wallet/models';
import { Flex, HStack, styled } from 'leather-styles/jsx';

import { Money } from '@shared/models/money.model';

import { formatBalance } from '@app/common/format-balance';
import { ftDecimals } from '@app/common/stacks-utils';
import { Flag } from '@app/ui/components/flag/flag';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { useState } from 'react';

import { satToBtc } from '@leather-wallet/utils';
import { createMoney, satToBtc } from '@leather-wallet/utils';
import { useField } from 'formik';
import { Stack } from 'leather-styles/jsx';

import type { TransferRecipient } from '@shared/models/form.model';
import { createMoney } from '@shared/models/money.model';

import { useOnMount } from '@app/common/hooks/use-on-mount';
import { InsufficientFundsError } from '@app/common/transactions/bitcoin/coinselect/local-coin-selection';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Dispatch, SetStateAction, useCallback, useRef } from 'react';

import { createMoney } from '@leather-wallet/utils';
import { SendCryptoAssetSelectors } from '@tests/selectors/send.selectors';
import { Form, Formik } from 'formik';
import { Stack, styled } from 'leather-styles/jsx';
import * as yup from 'yup';

import { BtcFeeType } from '@shared/models/fees/bitcoin-fees.model';
import type { TransferRecipient } from '@shared/models/form.model';
import { createMoney } from '@shared/models/money.model';

import { openInNewTab } from '@app/common/utils/open-in-new-tab';
import { Button } from '@app/ui/components/button/button';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useCallback } from 'react';

import type { Money } from '@leather-wallet/models';
import { useCryptoCurrencyMarketDataMeanAverage } from '@leather-wallet/query';
import { createMoney } from '@leather-wallet/utils';

import type { TransferRecipient } from '@shared/models/form.model';
import { Money, createMoney } from '@shared/models/money.model';

import { baseCurrencyAmountInQuote } from '@app/common/money/calculate-money';
import { i18nFormatCurrency } from '@app/common/money/format-money';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useMemo } from 'react';

import type { Money } from '@leather-wallet/models';
import {
type UtxoResponseItem,
useAverageBitcoinFeeRates,
useCryptoCurrencyMarketDataMeanAverage,
} from '@leather-wallet/query';
import { createMoney } from '@leather-wallet/utils';

import { BtcFeeType, btcTxTimeMap } from '@shared/models/fees/bitcoin-fees.model';
import type { TransferRecipient } from '@shared/models/form.model';
import { Money, createMoney } from '@shared/models/money.model';

import { baseCurrencyAmountInQuote } from '@app/common/money/calculate-money';
import { formatMoneyPadded, i18nFormatCurrency } from '@app/common/money/format-money';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useMemo } from 'react';

import type { Money } from '@leather-wallet/models';
import {
type UtxoResponseItem,
useAverageBitcoinFeeRates,
useCryptoCurrencyMarketDataMeanAverage,
} from '@leather-wallet/query';
import { createMoney } from '@leather-wallet/utils';

import { BtcFeeType, btcTxTimeMap } from '@shared/models/fees/bitcoin-fees.model';
import { Money, createMoney } from '@shared/models/money.model';

import { baseCurrencyAmountInQuote } from '@app/common/money/calculate-money';
import { formatMoneyPadded, i18nFormatCurrency } from '@app/common/money/format-money';
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/fees-row/components/transaction-fee.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { Money } from '@leather-wallet/models';
import { SharedComponentsSelectors } from '@tests/selectors/shared-component.selectors';
import { styled } from 'leather-styles/jsx';

import { CryptoCurrencies } from '@shared/models/currencies.model';
import { Money } from '@shared/models/money.model';

import { formatDustUsdAmounts, i18nFormatCurrency } from '@app/common/money/format-money';
import { BasicTooltip } from '@app/ui/components/tooltip/basic-tooltip';
Expand Down
3 changes: 1 addition & 2 deletions src/app/components/fees-row/fees-row.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { useCallback, useEffect, useMemo, useState } from 'react';

import { isNumber, isString } from '@leather-wallet/utils';
import { createMoney, isNumber, isString } from '@leather-wallet/utils';
import { SharedComponentsSelectors } from '@tests/selectors/shared-component.selectors';
import BigNumber from 'bignumber.js';
import { useField } from 'formik';
import { Box } from 'leather-styles/jsx';

import { STX_DECIMALS } from '@shared/constants';
import { FeeTypes, Fees } from '@shared/models/fees/fees.model';
import { createMoney } from '@shared/models/money.model';

import { useConvertCryptoCurrencyToFiatAmount } from '@app/common/hooks/use-convert-to-fiat-amount';
import { convertAmountToBaseUnit } from '@app/common/money/calculate-money';
Expand Down
2 changes: 1 addition & 1 deletion src/app/features/bitcoin-choose-fee/bitcoin-choose-fee.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useState } from 'react';

import type { Money } from '@leather-wallet/models';
import { Box, FlexProps, Stack, styled } from 'leather-styles/jsx';

import { BtcFeeType } from '@shared/models/fees/bitcoin-fees.model';
import type { TransferRecipient } from '@shared/models/form.model';
import { Money } from '@shared/models/money.model';

import { formatMoney } from '@app/common/money/format-money';
import { BitcoinCustomFee } from '@app/components/bitcoin-custom-fee/bitcoin-custom-fee';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useState } from 'react';

import { Money, createMoney } from '@shared/models/money.model';
import type { Money } from '@leather-wallet/models';
import { createMoney } from '@leather-wallet/utils';

import { subtractMoney, sumMoney } from '@app/common/money/calculate-money';
import { useCurrentBtcCryptoAssetBalanceNativeSegwit } from '@app/query/bitcoin/balance/btc-balance-native-segwit.hooks';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import { useMemo } from 'react';
import { useNavigate } from 'react-router-dom';

import { useBitcoinBroadcastTransaction } from '@leather-wallet/query';
import { btcToSat, isError } from '@leather-wallet/utils';
import { btcToSat, createMoney, isError } from '@leather-wallet/utils';
import * as btc from '@scure/btc-signer';
import BigNumber from 'bignumber.js';
import * as yup from 'yup';

import { createMoney } from '@shared/models/money.model';
import { BitcoinTx } from '@shared/models/transactions/bitcoin-transaction.model';
import { RouteUrls } from '@shared/route-urls';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Suspense } from 'react';
import { Outlet, useLocation, useNavigate } from 'react-router-dom';

import { btcToSat } from '@leather-wallet/utils';
import { btcToSat, createMoney } from '@leather-wallet/utils';
import { Formik } from 'formik';
import { Flex, Stack } from 'leather-styles/jsx';

import { createMoney } from '@shared/models/money.model';
import { BitcoinTx } from '@shared/models/transactions/bitcoin-transaction.model';
import { RouteUrls } from '@shared/route-urls';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { truncateMiddle } from '@leather-wallet/utils';

import { createMoney } from '@shared/models/money.model';
import { createMoney, truncateMiddle } from '@leather-wallet/utils';

import { formatMoney } from '@app/common/money/format-money';
import { PsbtInput } from '@app/features/psbt-signer/hooks/use-parsed-inputs';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { truncateMiddle } from '@leather-wallet/utils';

import { createMoney } from '@shared/models/money.model';
import { createMoney, truncateMiddle } from '@leather-wallet/utils';

import { formatMoney } from '@app/common/money/format-money';
import { PsbtOutput } from '@app/features/psbt-signer/hooks/use-parsed-outputs';
Expand Down
3 changes: 1 addition & 2 deletions src/app/features/psbt-signer/components/psbt-request-fee.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { Money } from '@leather-wallet/models';
import { useCalculateBitcoinFiatValue } from '@leather-wallet/query';
import { HStack, Stack, styled } from 'leather-styles/jsx';

import { Money } from '@shared/models/money.model';

import { formatMoney, i18nFormatCurrency } from '@app/common/money/format-money';

import { PsbtRequestDetailsSectionLayout } from './psbt-request-details-section.layout';
Expand Down
3 changes: 1 addition & 2 deletions src/app/features/psbt-signer/hooks/use-psbt-details.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { useCallback, useMemo } from 'react';

import { createMoney } from '@leather-wallet/utils';
import * as btc from '@scure/btc-signer';

import { createMoney } from '@shared/models/money.model';

import { subtractMoney } from '@app/common/money/calculate-money';
import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks';
import { useCurrentAccountTaprootIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/taproot-account.hooks';
Expand Down
4 changes: 1 addition & 3 deletions src/app/features/psbt-signer/hooks/use-psbt-totals.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { useMemo } from 'react';

import { sumNumbers } from '@leather-wallet/utils';

import { createMoney } from '@shared/models/money.model';
import { createMoney, sumNumbers } from '@leather-wallet/utils';

import { PsbtInput } from './use-parsed-inputs';
import { PsbtOutput } from './use-parsed-outputs';
Expand Down
4 changes: 1 addition & 3 deletions src/app/features/psbt-signer/psbt-signer.context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { createContext, useContext } from 'react';

import type { Inscription } from '@leather-wallet/models';

import { Money } from '@shared/models/money.model';
import type { Inscription, Money } from '@leather-wallet/models';

import { PsbtInput } from './hooks/use-parsed-inputs';
import { PsbtOutput } from './hooks/use-parsed-outputs';
Expand Down
Loading

0 comments on commit 43ad127

Please sign in to comment.