Skip to content

Commit

Permalink
refactor(utils): use utils package, closes leather-wallet#73
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed May 24, 2024
1 parent 8b26fdd commit 8d85f98
Show file tree
Hide file tree
Showing 55 changed files with 61 additions and 203 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@
"@leather-wallet/models": "0.6.5",
"@leather-wallet/query": "0.6.6",
"@leather-wallet/tokens": "0.4.0",
"@leather-wallet/utils": "0.6.5",
"@ledgerhq/hw-transport-webusb": "6.27.19",
"@noble/hashes": "1.3.2",
"@noble/secp256k1": "2.0.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/app/common/account-restoration/account-restore.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { fibonacciGenerator } from '../math/fibonacci';
import { fibonacciGenerator } from '@leather-wallet/utils';

import { createCounter } from '../utils/counter';

const numOfEmptyAccountsToCheck = 20;
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 @@ -7,6 +7,7 @@ import {
useCalculateBitcoinFiatValue,
useCryptoCurrencyMarketDataMeanAverage,
} from '@leather-wallet/query';
import { satToBtc } from '@leather-wallet/utils';
import { bytesToHex } from '@stacks/common';

import {
Expand All @@ -27,7 +28,6 @@ import { useCurrentNetwork } from '@app/store/networks/networks.selectors';

import { initialSearchParams } from '../initial-search-params';
import { i18nFormatCurrency } from '../money/format-money';
import { satToBtc } from '../money/unit-conversion';
import { useDefaultRequestParams } from './use-default-request-search-params';

export interface SimplifiedBitcoinContract {
Expand Down
16 changes: 0 additions & 16 deletions src/app/common/math/calculate-averages.spec.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/app/common/math/calculate-averages.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/app/common/math/fibonacci.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/app/common/math/helpers.spec.ts

This file was deleted.

32 changes: 0 additions & 32 deletions src/app/common/math/helpers.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/common/money/calculate-money.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { initBigNumber, 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 { isNumber } from '@shared/utils';

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

Expand Down
30 changes: 0 additions & 30 deletions src/app/common/money/unit-conversion.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/app/common/stacks-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { initBigNumber, microStxToStx } from '@leather-wallet/utils';
import { ChainID } from '@stacks/transactions';
import BigNumber from 'bignumber.js';
import { c32addressDecode } from 'c32check';
Expand All @@ -8,8 +9,6 @@ import { isValidUrl } from '@shared/utils/validate-url';

import { abbreviateNumber } from '@app/common/utils';

import { initBigNumber } from './math/helpers';
import { microStxToStx } from './money/unit-conversion';
import { convertUnicodeToAscii } from './string-utils';

export const stacksValue = ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { sumNumbers } from '@leather-wallet/utils';

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

import { sumNumbers } from '@app/common/math/helpers';
import { createNullArrayOfLength } from '@app/common/utils';

import { determineUtxosForSpend } from './local-coin-selection';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import type { UtxoResponseItem } from '@leather-wallet/query';
import { sumNumbers } from '@leather-wallet/utils';
import BigNumber from 'bignumber.js';
import { validate } from 'bitcoin-address-validation';

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

import { sumNumbers } from '@app/common/math/helpers';
import { sumMoney } from '@app/common/money/calculate-money';

import { filterUneconomicalUtxos, getSizeInfo } from '../utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import type { UtxoResponseItem } from '@leather-wallet/query';
import { 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 { satToBtc } from '@app/common/money/unit-conversion';

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

interface CalculateMaxBitcoinSpend {
Expand Down
3 changes: 1 addition & 2 deletions src/app/common/transactions/bitcoin/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { UtxoResponseItem } from '@leather-wallet/query';
import { satToBtc, sumNumbers } from '@leather-wallet/utils';
import BigNumber from 'bignumber.js';
import {
type AddressInfo,
Expand All @@ -14,8 +15,6 @@ import {
BitcoinTx,
} from '@shared/models/transactions/bitcoin-transaction.model';

import { sumNumbers } from '@app/common/math/helpers';
import { satToBtc } from '@app/common/money/unit-conversion';
import { truncateMiddle } from '@app/ui/utils/truncate-middle';

import { BtcSizeFeeEstimator } from './fees/btc-size-fee-estimator';
Expand Down
12 changes: 0 additions & 12 deletions src/app/common/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,5 @@
import { extractPhraseFromString, getTicker } from '@app/common/utils';

import { countDecimals } from './math/helpers';

describe(countDecimals.name, () => {
test('that it returns 0 when given an integer', () => expect(countDecimals(100)).toEqual(0));

test('that it returns accurate decimal numbers', () => {
expect(countDecimals(0.999)).toEqual(3);
expect(countDecimals('0.000000000000000000000000000001')).toEqual(30);
expect(countDecimals(0.1)).toEqual(1);
});
});

const nothing = 'micro-nothing';
const novel_token_19 = 'novel-token-19';
const stella = 'stella';
Expand Down
14 changes: 7 additions & 7 deletions src/app/common/validation/forms/amount-validators.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { UtxoResponseItem } from '@leather-wallet/query';
import {
btcToSat,
countDecimals,
microStxToStx,
satToBtc,
stxToMicroStx,
} from '@leather-wallet/utils';
import BigNumber from 'bignumber.js';
import * as yup from 'yup';

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

import { countDecimals } from '@app/common/math/helpers';
import { convertAmountToBaseUnit } from '@app/common/money/calculate-money';
import {
btcToSat,
microStxToStx,
satToBtc,
stxToMicroStx,
} from '@app/common/money/unit-conversion';

import { FormErrorMessages } from '../../../../shared/error-messages';
import { formatInsufficientBalanceError, formatPrecisionError } from '../../error-formatters';
Expand Down
3 changes: 1 addition & 2 deletions src/app/common/validation/forms/currency-validators.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { countDecimals } from '@leather-wallet/utils';
import * as yup from 'yup';

import { BTC_DECIMALS, STX_DECIMALS } from '@shared/constants';
import { FormErrorMessages } from '@shared/error-messages';
import { isNumber } from '@shared/utils';

import { countDecimals } from '@app/common/math/helpers';

export function currencyAmountValidator() {
return yup
.number()
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,3 +1,4 @@
import { btcToSat, moneyToBaseUnit, stxToMicroStx } from '@leather-wallet/utils';
import BigNumber from 'bignumber.js';
import { AnyObject, NumberSchema } from 'yup';

Expand All @@ -10,8 +11,6 @@ import {
stxAmountPrecisionValidator,
} from '@app/common/validation/forms/currency-validators';

import { btcToSat, moneyToBaseUnit, stxToMicroStx } from '../../money/unit-conversion';

interface FeeValidatorFactoryArgs {
availableBalance?: Money;
unitConverter(unit: string | number | BigNumber): BigNumber;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useState } from 'react';

import { 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 { satToBtc } from '@app/common/money/unit-conversion';
import { InsufficientFundsError } from '@app/common/transactions/bitcoin/coinselect/local-coin-selection';
import { Input } from '@app/ui/components/input/input';

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

import { stxToMicroStx } from '@leather-wallet/utils';
import { SharedComponentsSelectors } from '@tests/selectors/shared-component.selectors';
import { useField } from 'formik';
import { Flex, Stack, styled } from 'leather-styles/jsx';

import { CryptoCurrencies } from '@shared/models/currencies.model';
import { StacksFeeEstimate } from '@shared/models/fees/stacks-fees.model';

import { stxToMicroStx } from '@app/common/money/unit-conversion';
import { SendFormWarningMessages } from '@app/common/warning-messages';

interface CustomFeeFieldProps {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sumNumbers } from '@leather-wallet/utils';
import { useIsFetching } from '@tanstack/react-query';

import { sumNumbers } from '@app/common/math/helpers';
import { QueryPrefixes } from '@app/query/query-prefixes';

function areAnyQueriesFetching(...args: number[]) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useCallback, useMemo, useState } from 'react';

import { microStxToStx, stxToMicroStx } from '@leather-wallet/utils';
import { useField } from 'formik';
import { Stack, styled } from 'leather-styles/jsx';

import { microStxToStx, stxToMicroStx } from '@app/common/money/unit-conversion';
import { ErrorLabel } from '@app/components/error-label';

import { FeeMultiplier } from './fee-multiplier';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMemo } from 'react';
import { useNavigate } from 'react-router-dom';

import { useBitcoinBroadcastTransaction } from '@leather-wallet/query';
import { btcToSat } from '@leather-wallet/utils';
import * as btc from '@scure/btc-signer';
import BigNumber from 'bignumber.js';
import * as yup from 'yup';
Expand All @@ -12,7 +13,6 @@ import { RouteUrls } from '@shared/route-urls';
import { isError } from '@shared/utils';

import { useAnalytics } from '@app/common/hooks/analytics/use-analytics';
import { btcToSat } from '@app/common/money/unit-conversion';
import { queryClient } from '@app/common/persistence';
import {
getBitcoinTxSizeEstimation,
Expand Down
Loading

0 comments on commit 8d85f98

Please sign in to comment.