diff --git a/package.json b/package.json index e7517ebd14d..8bdd81b73b9 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 47d0f4d9185..37ce989435c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -38,6 +38,9 @@ dependencies: '@leather-wallet/tokens': specifier: 0.4.0 version: 0.4.0 + '@leather-wallet/utils': + specifier: 0.6.5 + version: 0.6.5 '@ledgerhq/hw-transport-webusb': specifier: 6.27.19 version: 6.27.19 diff --git a/src/app/common/account-restoration/account-restore.ts b/src/app/common/account-restoration/account-restore.ts index d381448d0a5..5fa41c0dcd1 100644 --- a/src/app/common/account-restoration/account-restore.ts +++ b/src/app/common/account-restoration/account-restore.ts @@ -1,4 +1,5 @@ -import { fibonacciGenerator } from '../math/fibonacci'; +import { fibonacciGenerator } from '@leather-wallet/utils'; + import { createCounter } from '../utils/counter'; const numOfEmptyAccountsToCheck = 20; diff --git a/src/app/common/hooks/use-bitcoin-contracts.ts b/src/app/common/hooks/use-bitcoin-contracts.ts index 4cb0f50aaa4..78fcdb64486 100644 --- a/src/app/common/hooks/use-bitcoin-contracts.ts +++ b/src/app/common/hooks/use-bitcoin-contracts.ts @@ -7,6 +7,7 @@ import { useCalculateBitcoinFiatValue, useCryptoCurrencyMarketDataMeanAverage, } from '@leather-wallet/query'; +import { satToBtc } from '@leather-wallet/utils'; import { bytesToHex } from '@stacks/common'; import { @@ -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 { diff --git a/src/app/common/math/calculate-averages.spec.ts b/src/app/common/math/calculate-averages.spec.ts deleted file mode 100644 index b6aeacd66d5..00000000000 --- a/src/app/common/math/calculate-averages.spec.ts +++ /dev/null @@ -1,16 +0,0 @@ -import BigNumber from 'bignumber.js'; - -import { calculateMeanAverage } from './calculate-averages'; - -describe(calculateMeanAverage.name, () => { - test('it calculates average of 0', () => - expect(calculateMeanAverage([]).toString()).toEqual('0')); - - test('it calculates averages of 1', () => - expect(calculateMeanAverage([new BigNumber(1)]).toString()).toEqual('1')); - - test('it calculates average of many numbers', () => - expect( - calculateMeanAverage([new BigNumber(1), new BigNumber(2), new BigNumber(3)]).toString() - ).toEqual('2')); -}); diff --git a/src/app/common/math/calculate-averages.ts b/src/app/common/math/calculate-averages.ts deleted file mode 100644 index 7f8a15b73a9..00000000000 --- a/src/app/common/math/calculate-averages.ts +++ /dev/null @@ -1,11 +0,0 @@ -import BigNumber from 'bignumber.js'; - -import { initBigNumber } from './helpers'; - -export function calculateMeanAverage(numbers: BigNumber[] | number[]) { - if (numbers.length === 0) return new BigNumber(0); - return numbers - .map(initBigNumber) - .reduce((acc, price) => acc.plus(price), new BigNumber(0)) - .dividedBy(numbers.length); -} diff --git a/src/app/common/math/fibonacci.ts b/src/app/common/math/fibonacci.ts deleted file mode 100644 index e06d48cbc87..00000000000 --- a/src/app/common/math/fibonacci.ts +++ /dev/null @@ -1,11 +0,0 @@ -function fibonacci(n = 0): number { - if (n < 0) throw 'Cannot calculate from negative number'; - if (n < 2) return n; - return fibonacci(n - 1) + fibonacci(n - 2); -} - -export function* fibonacciGenerator(startIndex = 0): IterableIterator { - let index = startIndex; - while (index < Infinity) yield fibonacci(index++); - return Infinity; -} diff --git a/src/app/common/math/helpers.spec.ts b/src/app/common/math/helpers.spec.ts deleted file mode 100644 index 1761e4f3eda..00000000000 --- a/src/app/common/math/helpers.spec.ts +++ /dev/null @@ -1,26 +0,0 @@ -import BigNumber from 'bignumber.js'; - -import { sumNumbers } from './helpers'; - -const cases = [ - { - sums: [1, 2, 3], - expectedResult: new BigNumber(6), - }, - { - sums: [0.1, 0.2], - expectedResult: new BigNumber(0.3), - }, - { - sums: [Number.MAX_SAFE_INTEGER, 1], - expectedResult: new BigNumber('9007199254740992'), - }, -]; - -describe(sumNumbers.name, () => { - describe.each(cases)('Sum example', ({ sums, expectedResult }) => { - test('sum of ' + sums.toString(), () => { - expect(sumNumbers(sums).toString()).toEqual(expectedResult.toString()); - }); - }); -}); diff --git a/src/app/common/math/helpers.ts b/src/app/common/math/helpers.ts deleted file mode 100644 index 84746b97f80..00000000000 --- a/src/app/common/math/helpers.ts +++ /dev/null @@ -1,32 +0,0 @@ -import BigNumber from 'bignumber.js'; - -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[]) { - return nums.reduce((acc, num) => acc.plus(num), new BigNumber(0)); -} - -function isMultipleOf(multiple: number) { - return (num: number) => num % multiple === 0; -} - -export function isEven(num: number) { - return isMultipleOf(2)(num); -} - -export function countDecimals(num: string | number | BigNumber) { - const LARGE_NUMBER_OF_DECIMALS = 100; - BigNumber.config({ DECIMAL_PLACES: LARGE_NUMBER_OF_DECIMALS }); - const amount = initBigNumber(num); - const decimals = amount.toString(10).split('.')[1]; - return decimals ? decimals.length : 0; -} - -export function increaseValueByOneMicroStx(value: string | number | BigNumber) { - return new BigNumber(value).plus(0.000001).toNumber(); -} diff --git a/src/app/common/money/calculate-money.ts b/src/app/common/money/calculate-money.ts index fc44e6dee9a..8bde854e386 100644 --- a/src/app/common/money/calculate-money.ts +++ b/src/app/common/money/calculate-money.ts @@ -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'; diff --git a/src/app/common/money/unit-conversion.ts b/src/app/common/money/unit-conversion.ts deleted file mode 100644 index 268f919ea7d..00000000000 --- a/src/app/common/money/unit-conversion.ts +++ /dev/null @@ -1,30 +0,0 @@ -import BigNumber from 'bignumber.js'; - -import { BTC_DECIMALS, STX_DECIMALS } from '@shared/constants'; -import { Money } from '@shared/models/money.model'; - -import { initBigNumber } from '../math/helpers'; - -function fractionalUnitToUnit(decimals: number) { - return (unit: number | string | BigNumber) => { - const unitBigNumber = initBigNumber(unit); - return unitBigNumber.shiftedBy(-decimals); - }; -} - -export function unitToFractionalUnit(decimals: number) { - return (unit: number | string | BigNumber) => { - const unitBigNumber = initBigNumber(unit); - return unitBigNumber.shiftedBy(decimals); - }; -} - -export const satToBtc = fractionalUnitToUnit(BTC_DECIMALS); -export const btcToSat = unitToFractionalUnit(BTC_DECIMALS); - -export const microStxToStx = fractionalUnitToUnit(STX_DECIMALS); -export const stxToMicroStx = unitToFractionalUnit(STX_DECIMALS); - -export function moneyToBaseUnit(sum: Money) { - return fractionalUnitToUnit(sum.decimals)(sum.amount); -} diff --git a/src/app/common/stacks-utils.ts b/src/app/common/stacks-utils.ts index 071850d2ed4..943995ef3e4 100644 --- a/src/app/common/stacks-utils.ts +++ b/src/app/common/stacks-utils.ts @@ -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'; @@ -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 = ({ diff --git a/src/app/common/transactions/bitcoin/coinselect/local-coin-selection.spec.ts b/src/app/common/transactions/bitcoin/coinselect/local-coin-selection.spec.ts index 94218201b31..79af72a144f 100644 --- a/src/app/common/transactions/bitcoin/coinselect/local-coin-selection.spec.ts +++ b/src/app/common/transactions/bitcoin/coinselect/local-coin-selection.spec.ts @@ -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'; diff --git a/src/app/common/transactions/bitcoin/coinselect/local-coin-selection.ts b/src/app/common/transactions/bitcoin/coinselect/local-coin-selection.ts index 03bb507cca9..eb44eaa7e10 100644 --- a/src/app/common/transactions/bitcoin/coinselect/local-coin-selection.ts +++ b/src/app/common/transactions/bitcoin/coinselect/local-coin-selection.ts @@ -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'; diff --git a/src/app/common/transactions/bitcoin/fees/calculate-max-bitcoin-spend.ts b/src/app/common/transactions/bitcoin/fees/calculate-max-bitcoin-spend.ts index 713320d9f8f..ceb142bc269 100644 --- a/src/app/common/transactions/bitcoin/fees/calculate-max-bitcoin-spend.ts +++ b/src/app/common/transactions/bitcoin/fees/calculate-max-bitcoin-spend.ts @@ -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 { diff --git a/src/app/common/transactions/bitcoin/utils.ts b/src/app/common/transactions/bitcoin/utils.ts index 85278ffa110..6caaea86723 100644 --- a/src/app/common/transactions/bitcoin/utils.ts +++ b/src/app/common/transactions/bitcoin/utils.ts @@ -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, @@ -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'; diff --git a/src/app/common/utils.spec.ts b/src/app/common/utils.spec.ts index 91c033e7105..9dd7b8bf253 100644 --- a/src/app/common/utils.spec.ts +++ b/src/app/common/utils.spec.ts @@ -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'; diff --git a/src/app/common/validation/forms/amount-validators.ts b/src/app/common/validation/forms/amount-validators.ts index 1b50085c6c3..b48b1e23d2f 100644 --- a/src/app/common/validation/forms/amount-validators.ts +++ b/src/app/common/validation/forms/amount-validators.ts @@ -1,4 +1,11 @@ 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'; @@ -6,14 +13,7 @@ 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'; diff --git a/src/app/common/validation/forms/currency-validators.ts b/src/app/common/validation/forms/currency-validators.ts index e07aab364b1..57e7c4ee90a 100644 --- a/src/app/common/validation/forms/currency-validators.ts +++ b/src/app/common/validation/forms/currency-validators.ts @@ -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() diff --git a/src/app/common/validation/forms/fee-validators.ts b/src/app/common/validation/forms/fee-validators.ts index f24933a6e25..8043fbf7361 100644 --- a/src/app/common/validation/forms/fee-validators.ts +++ b/src/app/common/validation/forms/fee-validators.ts @@ -1,3 +1,4 @@ +import { btcToSat, moneyToBaseUnit, stxToMicroStx } from '@leather-wallet/utils'; import BigNumber from 'bignumber.js'; import { AnyObject, NumberSchema } from 'yup'; @@ -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; diff --git a/src/app/components/bitcoin-custom-fee/bitcoin-custom-fee-input.tsx b/src/app/components/bitcoin-custom-fee/bitcoin-custom-fee-input.tsx index 67646173069..7b6fd2fbe4f 100644 --- a/src/app/components/bitcoin-custom-fee/bitcoin-custom-fee-input.tsx +++ b/src/app/components/bitcoin-custom-fee/bitcoin-custom-fee-input.tsx @@ -1,5 +1,6 @@ import { useState } from 'react'; +import { satToBtc } from '@leather-wallet/utils'; import { useField } from 'formik'; import { Stack } from 'leather-styles/jsx'; @@ -7,7 +8,6 @@ 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'; diff --git a/src/app/components/fees-row/components/custom-fee-field.tsx b/src/app/components/fees-row/components/custom-fee-field.tsx index 0a4044c5c77..59f588e23f7 100644 --- a/src/app/components/fees-row/components/custom-fee-field.tsx +++ b/src/app/components/fees-row/components/custom-fee-field.tsx @@ -1,5 +1,6 @@ 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'; @@ -7,7 +8,6 @@ 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 { diff --git a/src/app/features/collectibles/hooks/use-is-fetching-collectibles.ts b/src/app/features/collectibles/hooks/use-is-fetching-collectibles.ts index e1f341f1315..199f2e56677 100644 --- a/src/app/features/collectibles/hooks/use-is-fetching-collectibles.ts +++ b/src/app/features/collectibles/hooks/use-is-fetching-collectibles.ts @@ -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[]) { diff --git a/src/app/features/dialogs/increase-fee-dialog/components/increase-fee-field.tsx b/src/app/features/dialogs/increase-fee-dialog/components/increase-fee-field.tsx index 5cc073b37c1..bf39a68d0ff 100644 --- a/src/app/features/dialogs/increase-fee-dialog/components/increase-fee-field.tsx +++ b/src/app/features/dialogs/increase-fee-dialog/components/increase-fee-field.tsx @@ -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'; diff --git a/src/app/features/dialogs/increase-fee-dialog/hooks/use-btc-increase-fee.ts b/src/app/features/dialogs/increase-fee-dialog/hooks/use-btc-increase-fee.ts index 15de7e9d3ed..7b50b6f66fc 100644 --- a/src/app/features/dialogs/increase-fee-dialog/hooks/use-btc-increase-fee.ts +++ b/src/app/features/dialogs/increase-fee-dialog/hooks/use-btc-increase-fee.ts @@ -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'; @@ -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, diff --git a/src/app/features/dialogs/increase-fee-dialog/increase-btc-fee-dialog.tsx b/src/app/features/dialogs/increase-fee-dialog/increase-btc-fee-dialog.tsx index 01fd1678cec..51c14be6bf4 100644 --- a/src/app/features/dialogs/increase-fee-dialog/increase-btc-fee-dialog.tsx +++ b/src/app/features/dialogs/increase-fee-dialog/increase-btc-fee-dialog.tsx @@ -1,6 +1,7 @@ import { Suspense } from 'react'; import { Outlet, useLocation, useNavigate } from 'react-router-dom'; +import { btcToSat } from '@leather-wallet/utils'; import { Formik } from 'formik'; import { Flex, Stack } from 'leather-styles/jsx'; @@ -10,7 +11,6 @@ import { RouteUrls } from '@shared/route-urls'; import { useLocationStateWithCache } from '@app/common/hooks/use-location-state'; import { formatMoney } from '@app/common/money/format-money'; -import { btcToSat } from '@app/common/money/unit-conversion'; import { getBitcoinTxValue } from '@app/common/transactions/bitcoin/utils'; import { BitcoinCustomFeeInput } from '@app/components/bitcoin-custom-fee/bitcoin-custom-fee-input'; import { BitcoinTransactionItem } from '@app/components/bitcoin-transaction-item/bitcoin-transaction-item'; diff --git a/src/app/features/dialogs/increase-fee-dialog/increase-stx-fee-dialog.tsx b/src/app/features/dialogs/increase-fee-dialog/increase-stx-fee-dialog.tsx index d82b0de1742..79294610985 100644 --- a/src/app/features/dialogs/increase-fee-dialog/increase-stx-fee-dialog.tsx +++ b/src/app/features/dialogs/increase-fee-dialog/increase-stx-fee-dialog.tsx @@ -1,6 +1,7 @@ import { Suspense, useCallback, useEffect } from 'react'; import { Outlet, useLocation, useNavigate, useSearchParams } from 'react-router-dom'; +import { microStxToStx, stxToMicroStx } from '@leather-wallet/utils'; import BigNumber from 'bignumber.js'; import { Formik } from 'formik'; import { Flex, Stack } from 'leather-styles/jsx'; @@ -10,7 +11,6 @@ import { RouteUrls } from '@shared/route-urls'; import { useRefreshAllAccountData } from '@app/common/hooks/account/use-refresh-all-account-data'; import { LoadingKeys, useLoading } from '@app/common/hooks/use-loading'; -import { microStxToStx, stxToMicroStx } from '@app/common/money/unit-conversion'; import { stacksValue } from '@app/common/stacks-utils'; import { safelyFormatHexTxid } from '@app/common/utils/safe-handle-txid'; import { stxFeeValidator } from '@app/common/validation/forms/fee-validators'; diff --git a/src/app/features/ledger/flows/stacks-tx-signing/steps/approve-sign-stacks-ledger-tx.tsx b/src/app/features/ledger/flows/stacks-tx-signing/steps/approve-sign-stacks-ledger-tx.tsx index 43408489337..b2cfd8e6cb4 100644 --- a/src/app/features/ledger/flows/stacks-tx-signing/steps/approve-sign-stacks-ledger-tx.tsx +++ b/src/app/features/ledger/flows/stacks-tx-signing/steps/approve-sign-stacks-ledger-tx.tsx @@ -1,9 +1,9 @@ import { useMemo } from 'react'; +import { microStxToStx } from '@leather-wallet/utils'; import { PayloadType, cvToString } from '@stacks/transactions'; import { BigNumber } from 'bignumber.js'; -import { microStxToStx } from '@app/common/money/unit-conversion'; import { isSip10Transfer } from '@app/common/transactions/stacks/is-sip-10-transfer'; import { useLedgerTxSigningContext } from '@app/features/ledger/generic-flows/tx-signing/ledger-sign-tx.context'; import { ApproveLedgerOperationLayout } from '@app/features/ledger/generic-steps/approve-ledger-operation/approve-ledger-operation.layout'; diff --git a/src/app/features/psbt-signer/hooks/use-psbt-totals.tsx b/src/app/features/psbt-signer/hooks/use-psbt-totals.tsx index e460ac00a31..eacbb422c79 100644 --- a/src/app/features/psbt-signer/hooks/use-psbt-totals.tsx +++ b/src/app/features/psbt-signer/hooks/use-psbt-totals.tsx @@ -1,8 +1,8 @@ import { useMemo } from 'react'; -import { createMoney } from '@shared/models/money.model'; +import { sumNumbers } from '@leather-wallet/utils'; -import { sumNumbers } from '@app/common/math/helpers'; +import { createMoney } from '@shared/models/money.model'; import { PsbtInput } from './use-parsed-inputs'; import { PsbtOutput } from './use-parsed-outputs'; diff --git a/src/app/features/retrieve-taproot-to-native-segwit/use-generate-retrieve-taproot-funds-tx.tsx b/src/app/features/retrieve-taproot-to-native-segwit/use-generate-retrieve-taproot-funds-tx.tsx index 4ba4084779d..24340bf13e5 100644 --- a/src/app/features/retrieve-taproot-to-native-segwit/use-generate-retrieve-taproot-funds-tx.tsx +++ b/src/app/features/retrieve-taproot-to-native-segwit/use-generate-retrieve-taproot-funds-tx.tsx @@ -5,12 +5,12 @@ import { useCurrentTaprootAccountUninscribedUtxos, useNumberOfInscriptionsOnUtxo, } from '@leather-wallet/query'; +import { sumNumbers } from '@leather-wallet/utils'; import * as btc from '@scure/btc-signer'; import { extractAddressIndexFromPath } from '@shared/crypto/bitcoin/bitcoin.utils'; import { Money, createMoney } from '@shared/models/money.model'; -import { sumNumbers } from '@app/common/math/helpers'; import { BtcSizeFeeEstimator } from '@app/common/transactions/bitcoin/fees/btc-size-fee-estimator'; import { useCurrentAccountIndex } from '@app/store/accounts/account'; import { useBitcoinScureLibNetworkConfig } from '@app/store/accounts/blockchain/bitcoin/bitcoin-keychain'; diff --git a/src/app/features/stacks-transaction-request/hooks/use-stacks-transaction-summary.ts b/src/app/features/stacks-transaction-request/hooks/use-stacks-transaction-summary.ts index a4b4b7a3fdc..b6fa5f9fc1c 100644 --- a/src/app/features/stacks-transaction-request/hooks/use-stacks-transaction-summary.ts +++ b/src/app/features/stacks-transaction-request/hooks/use-stacks-transaction-summary.ts @@ -1,4 +1,5 @@ import { useCryptoCurrencyMarketDataMeanAverage } from '@leather-wallet/query'; +import { microStxToStx } from '@leather-wallet/utils'; import { bytesToUtf8 } from '@stacks/common'; import { ClarityType, @@ -25,7 +26,6 @@ import { getEstimatedConfirmationTime } from '@app/common/transactions/stacks/tr import { removeTrailingNullCharacters } from '@app/common/utils'; import { useStacksBlockTime } from '@app/query/stacks/info/info.hooks'; import { useCurrentNetworkState } from '@app/store/networks/networks.hooks'; -import { microStxToStx } from '@app/ui/utils/micro-stx-to-stx'; export function useStacksTransactionSummary(token: CryptoCurrencies) { // TODO: unsafe type assumption diff --git a/src/app/features/stacks-transaction-request/hooks/use-transaction-error.ts b/src/app/features/stacks-transaction-request/hooks/use-transaction-error.ts index cc0ce3116bb..4a273f7835f 100644 --- a/src/app/features/stacks-transaction-request/hooks/use-transaction-error.ts +++ b/src/app/features/stacks-transaction-request/hooks/use-transaction-error.ts @@ -1,5 +1,6 @@ import { useMemo } from 'react'; +import { stxToMicroStx } from '@leather-wallet/utils'; import { ContractCallPayload, TransactionTypes } from '@stacks/connect'; import BigNumber from 'bignumber.js'; import { useFormikContext } from 'formik'; @@ -8,7 +9,6 @@ import { StacksTransactionFormValues } from '@shared/models/form.model'; import { useDefaultRequestParams } from '@app/common/hooks/use-default-request-search-params'; import { initialSearchParams } from '@app/common/initial-search-params'; -import { stxToMicroStx } from '@app/common/money/unit-conversion'; import { validateStacksAddress } from '@app/common/stacks-utils'; import { TransactionErrorReason } from '@app/features/stacks-transaction-request/transaction-error/transaction-error'; import { useCurrentStxAvailableUnlockedBalance } from '@app/query/stacks/balance/account-balance.hooks'; diff --git a/src/app/features/stacks-transaction-request/stacks-transaction-signer.tsx b/src/app/features/stacks-transaction-request/stacks-transaction-signer.tsx index bb0fad7f8fa..a2a38f5b159 100644 --- a/src/app/features/stacks-transaction-request/stacks-transaction-signer.tsx +++ b/src/app/features/stacks-transaction-request/stacks-transaction-signer.tsx @@ -1,6 +1,7 @@ import { useState } from 'react'; import { Outlet, useLocation, useNavigate } from 'react-router-dom'; +import { stxToMicroStx } from '@leather-wallet/utils'; import { StacksTransaction } from '@stacks/transactions'; import { Formik, FormikHelpers } from 'formik'; import { Flex } from 'leather-styles/jsx'; @@ -13,7 +14,6 @@ import { RouteUrls } from '@shared/route-urls'; import { useAnalytics } from '@app/common/hooks/analytics/use-analytics'; import { useOnMount } from '@app/common/hooks/use-on-mount'; -import { stxToMicroStx } from '@app/common/money/unit-conversion'; import { stxFeeValidator } from '@app/common/validation/forms/fee-validators'; import { nonceValidator } from '@app/common/validation/nonce-validators'; import { NonceSetter } from '@app/components/nonce-setter'; diff --git a/src/app/pages/bitcoin-contract-list/components/bitcoin-contract-list-item-layout.tsx b/src/app/pages/bitcoin-contract-list/components/bitcoin-contract-list-item-layout.tsx index 554040eb53a..39527b3b189 100644 --- a/src/app/pages/bitcoin-contract-list/components/bitcoin-contract-list-item-layout.tsx +++ b/src/app/pages/bitcoin-contract-list/components/bitcoin-contract-list-item-layout.tsx @@ -1,6 +1,7 @@ import { useCallback } from 'react'; import { useCryptoCurrencyMarketDataMeanAverage } from '@leather-wallet/query'; +import { satToBtc } from '@leather-wallet/utils'; import { Flex, HStack, styled } from 'leather-styles/jsx'; import { createMoneyFromDecimal } from '@shared/models/money.model'; @@ -8,7 +9,6 @@ import { createMoneyFromDecimal } from '@shared/models/money.model'; import { useBitcoinExplorerLink } from '@app/common/hooks/use-bitcoin-explorer-link'; import { baseCurrencyAmountInQuote } from '@app/common/money/calculate-money'; import { i18nFormatCurrency } from '@app/common/money/format-money'; -import { satToBtc } from '@app/common/money/unit-conversion'; import { Flag } from '@app/ui/components/flag/flag'; import { Caption } from '@app/ui/components/typography/caption'; import { BitcoinContractIcon } from '@app/ui/icons/bitcoin-contract-icon'; diff --git a/src/app/pages/bitcoin-contract-request/components/bitcoin-contract-offer/bitcoin-contract-offer-input.tsx b/src/app/pages/bitcoin-contract-request/components/bitcoin-contract-offer/bitcoin-contract-offer-input.tsx index b1c25519891..9819395d8f7 100644 --- a/src/app/pages/bitcoin-contract-request/components/bitcoin-contract-offer/bitcoin-contract-offer-input.tsx +++ b/src/app/pages/bitcoin-contract-request/components/bitcoin-contract-offer/bitcoin-contract-offer-input.tsx @@ -1,11 +1,11 @@ import { useCalculateBitcoinFiatValue } from '@leather-wallet/query'; +import { satToBtc } from '@leather-wallet/utils'; import { Stack, styled } from 'leather-styles/jsx'; import { createMoneyFromDecimal } from '@shared/models/money.model'; import { SimplifiedBitcoinContract } from '@app/common/hooks/use-bitcoin-contracts'; import { formatMoney, i18nFormatCurrency } from '@app/common/money/format-money'; -import { satToBtc } from '@app/common/money/unit-conversion'; import { truncateMiddle } from '@app/ui/utils/truncate-middle'; import { BitcoinContractLockAmount } from './bitcoin-contract-lock-amount'; diff --git a/src/app/pages/rpc-send-transfer/use-rpc-send-transfer.ts b/src/app/pages/rpc-send-transfer/use-rpc-send-transfer.ts index 019baca3d04..f24b6e38b6b 100644 --- a/src/app/pages/rpc-send-transfer/use-rpc-send-transfer.ts +++ b/src/app/pages/rpc-send-transfer/use-rpc-send-transfer.ts @@ -1,6 +1,7 @@ import { useMemo } from 'react'; import { useNavigate } from 'react-router-dom'; +import { sumNumbers } from '@leather-wallet/utils'; import BigNumber from 'bignumber.js'; import { createMoney } from '@shared/models/money.model'; @@ -9,7 +10,6 @@ import { RouteUrls } from '@shared/route-urls'; import { useDefaultRequestParams } from '@app/common/hooks/use-default-request-search-params'; import { useOnMount } from '@app/common/hooks/use-on-mount'; import { initialSearchParams } from '@app/common/initial-search-params'; -import { sumNumbers } from '@app/common/math/helpers'; import { useCurrentNativeSegwitUtxos } from '@app/query/bitcoin/address/utxos-by-address.hooks'; export function useRpcSendTransferRequestParams() { diff --git a/src/app/pages/send/locked-bitcoin-summary/locked-bitcoin-summary.tsx b/src/app/pages/send/locked-bitcoin-summary/locked-bitcoin-summary.tsx index 80204f8ce5a..63471edf4fa 100644 --- a/src/app/pages/send/locked-bitcoin-summary/locked-bitcoin-summary.tsx +++ b/src/app/pages/send/locked-bitcoin-summary/locked-bitcoin-summary.tsx @@ -1,11 +1,11 @@ import { useLocation } from 'react-router-dom'; +import { satToBtc } from '@leather-wallet/utils'; import { HStack, styled } from 'leather-styles/jsx'; import { useAnalytics } from '@app/common/hooks/analytics/use-analytics'; import { useBitcoinExplorerLink } from '@app/common/hooks/use-bitcoin-explorer-link'; import { useClipboard } from '@app/common/hooks/use-copy-to-clipboard'; -import { satToBtc } from '@app/common/money/unit-conversion'; import { InfoCardAssetValue, InfoCardBtn } from '@app/components/info-card/info-card'; import { useToast } from '@app/features/toasts/use-toast'; import { Footer } from '@app/ui/components/containers/footers/footer'; diff --git a/src/app/pages/send/ordinal-inscription/coinselect/select-inscription-coins.spec.ts b/src/app/pages/send/ordinal-inscription/coinselect/select-inscription-coins.spec.ts index 0a66c13d460..8dc2cc74544 100644 --- a/src/app/pages/send/ordinal-inscription/coinselect/select-inscription-coins.spec.ts +++ b/src/app/pages/send/ordinal-inscription/coinselect/select-inscription-coins.spec.ts @@ -1,4 +1,4 @@ -import { sumNumbers } from '@app/common/math/helpers'; +import { sumNumbers } from '@leather-wallet/utils'; import { selectTaprootInscriptionTransferCoins } from './select-inscription-coins'; diff --git a/src/app/pages/send/ordinal-inscription/coinselect/select-inscription-coins.ts b/src/app/pages/send/ordinal-inscription/coinselect/select-inscription-coins.ts index d62cf44f6dd..f9f9e2fa913 100644 --- a/src/app/pages/send/ordinal-inscription/coinselect/select-inscription-coins.ts +++ b/src/app/pages/send/ordinal-inscription/coinselect/select-inscription-coins.ts @@ -1,9 +1,9 @@ import type { UtxoResponseItem, UtxoWithDerivationPath } from '@leather-wallet/query'; +import { sumNumbers } from '@leather-wallet/utils'; import { BTC_P2WPKH_DUST_AMOUNT } from '@shared/constants'; import { isDefined } from '@shared/utils'; -import { sumNumbers } from '@app/common/math/helpers'; import { BtcSizeFeeEstimator } from '@app/common/transactions/bitcoin/fees/btc-size-fee-estimator'; import { createCounter } from '@app/common/utils/counter'; diff --git a/src/app/pages/send/send-crypto-asset-form/form/btc/btc-send-form-confirmation.tsx b/src/app/pages/send/send-crypto-asset-form/form/btc/btc-send-form-confirmation.tsx index a8d75dc1683..a85a5340425 100644 --- a/src/app/pages/send/send-crypto-asset-form/form/btc/btc-send-form-confirmation.tsx +++ b/src/app/pages/send/send-crypto-asset-form/form/btc/btc-send-form-confirmation.tsx @@ -5,6 +5,7 @@ import { useBitcoinBroadcastTransaction, useCryptoCurrencyMarketDataMeanAverage, } from '@leather-wallet/query'; +import { satToBtc } from '@leather-wallet/utils'; import { hexToBytes } from '@noble/hashes/utils'; import * as btc from '@scure/btc-signer'; import { SendCryptoAssetSelectors } from '@tests/selectors/send.selectors'; @@ -20,7 +21,6 @@ import { RouteUrls } from '@shared/route-urls'; import { useAnalytics } from '@app/common/hooks/analytics/use-analytics'; import { baseCurrencyAmountInQuote } from '@app/common/money/calculate-money'; import { formatMoneyPadded, i18nFormatCurrency } from '@app/common/money/format-money'; -import { satToBtc } from '@app/common/money/unit-conversion'; import { queryClient } from '@app/common/persistence'; import { FormAddressDisplayer } from '@app/components/address-displayer/form-address-displayer'; import { diff --git a/src/app/pages/send/send-crypto-asset-form/form/btc/use-btc-choose-fee.tsx b/src/app/pages/send/send-crypto-asset-form/form/btc/use-btc-choose-fee.tsx index a961b627a97..8d49c23d9bd 100644 --- a/src/app/pages/send/send-crypto-asset-form/form/btc/use-btc-choose-fee.tsx +++ b/src/app/pages/send/send-crypto-asset-form/form/btc/use-btc-choose-fee.tsx @@ -1,7 +1,8 @@ +import { btcToSat } from '@leather-wallet/utils'; + import { logger } from '@shared/logger'; import { createMoney } from '@shared/models/money.model'; -import { btcToSat } from '@app/common/money/unit-conversion'; import { formFeeRowValue } from '@app/common/send/utils'; import { useGenerateUnsignedNativeSegwitTx } from '@app/common/transactions/bitcoin/use-generate-bitcoin-tx'; import { OnChooseFeeArgs } from '@app/components/bitcoin-fees-list/bitcoin-fees-list'; diff --git a/src/app/pages/send/send-crypto-asset-form/form/stacks/stacks-send-form-confirmation.tsx b/src/app/pages/send/send-crypto-asset-form/form/stacks/stacks-send-form-confirmation.tsx index 682d3bd2102..e0beedc4864 100644 --- a/src/app/pages/send/send-crypto-asset-form/form/stacks/stacks-send-form-confirmation.tsx +++ b/src/app/pages/send/send-crypto-asset-form/form/stacks/stacks-send-form-confirmation.tsx @@ -1,6 +1,7 @@ import { Outlet, useParams } from 'react-router-dom'; import { deserializeTransaction } from '@stacks/transactions'; +import BigNumber from 'bignumber.js'; import { Box, Stack } from 'leather-styles/jsx'; import { CryptoCurrencies } from '@shared/models/currencies.model'; @@ -67,7 +68,7 @@ export function StacksSendFormConfirmation() { <> state.ledger; diff --git a/src/app/store/software-keys/software-key.selectors.ts b/src/app/store/software-keys/software-key.selectors.ts index 47d43ba6141..a812cd7c3e1 100644 --- a/src/app/store/software-keys/software-key.selectors.ts +++ b/src/app/store/software-keys/software-key.selectors.ts @@ -1,11 +1,11 @@ import { useSelector } from 'react-redux'; +import { initBigNumber } from '@leather-wallet/utils'; import { createSelector } from '@reduxjs/toolkit'; import { defaultWalletKeyId } from '@shared/utils'; import { initialSearchParams } from '@app/common/initial-search-params'; -import { initBigNumber } from '@app/common/math/helpers'; import { RootState } from '@app/store'; import { selectStacksChain } from '../chains/stx-chain.selectors'; diff --git a/src/app/store/transactions/token-transfer.hooks.ts b/src/app/store/transactions/token-transfer.hooks.ts index e33ea18ca07..412c3a2ec7c 100644 --- a/src/app/store/transactions/token-transfer.hooks.ts +++ b/src/app/store/transactions/token-transfer.hooks.ts @@ -2,6 +2,7 @@ import { useCallback } from 'react'; import { useAsync } from 'react-async-hook'; import type { Sip10CryptoAssetInfo } from '@leather-wallet/models'; +import { stxToMicroStx } from '@leather-wallet/utils'; import { bytesToHex } from '@stacks/common'; import { TransactionTypes } from '@stacks/connect'; import { @@ -20,7 +21,6 @@ import { import { logger } from '@shared/logger'; import type { StacksSendFormValues, StacksTransactionFormValues } from '@shared/models/form.model'; -import { stxToMicroStx } from '@app/common/money/unit-conversion'; import { ftUnshiftDecimals, getStacksContractIdStringParts } from '@app/common/stacks-utils'; import { GenerateUnsignedTransactionOptions, diff --git a/src/app/store/transactions/transaction.hooks.ts b/src/app/store/transactions/transaction.hooks.ts index a2eaa416684..c51d93b06ec 100644 --- a/src/app/store/transactions/transaction.hooks.ts +++ b/src/app/store/transactions/transaction.hooks.ts @@ -1,6 +1,7 @@ import { useCallback, useMemo } from 'react'; import { useAsync } from 'react-async-hook'; +import { stxToMicroStx } from '@leather-wallet/utils'; import { bytesToHex } from '@noble/hashes/utils'; import { TransactionTypes } from '@stacks/connect'; import { @@ -18,7 +19,6 @@ import { logger } from '@shared/logger'; import { StacksTransactionFormValues } from '@shared/models/form.model'; import { isUndefined } from '@shared/utils'; -import { stxToMicroStx } from '@app/common/money/unit-conversion'; import { validateStacksAddress } from '@app/common/stacks-utils'; import { GenerateUnsignedTransactionOptions, diff --git a/src/app/ui/components/address-displayer/address-displayer.tsx b/src/app/ui/components/address-displayer/address-displayer.tsx index 160c66b0b12..89d1cfb378b 100644 --- a/src/app/ui/components/address-displayer/address-displayer.tsx +++ b/src/app/ui/components/address-displayer/address-displayer.tsx @@ -1,7 +1,6 @@ +import { isEven } from '@leather-wallet/utils'; import type { HTMLStyledProps } from 'leather-styles/types'; -import { isEven } from '@app/common/math/helpers'; - import { AddressDisplayerLayout } from './address-displayer.layout'; import { groupByFour } from './address-displayer.utils'; diff --git a/src/app/ui/utils/micro-stx-to-stx.ts b/src/app/ui/utils/micro-stx-to-stx.ts deleted file mode 100644 index f6738e401ab..00000000000 --- a/src/app/ui/utils/micro-stx-to-stx.ts +++ /dev/null @@ -1,3 +0,0 @@ -export function microStxToStx(microStx: string | number): number | string { - return Number(Number(microStx) / Math.pow(10, 6)); -} diff --git a/tests/specs/transactions/transactions.spec.ts b/tests/specs/transactions/transactions.spec.ts index f8a373aff41..a67cbd24002 100644 --- a/tests/specs/transactions/transactions.spec.ts +++ b/tests/specs/transactions/transactions.spec.ts @@ -1,9 +1,8 @@ +import { stxToMicroStx } from '@leather-wallet/utils'; import { TokenTransferPayload, deserializeTransaction } from '@stacks/transactions'; import { TestAppPage } from '@tests/page-object-models/test-app.page'; import { TransactionRequestPage } from '@tests/page-object-models/transaction-request.page'; -import { stxToMicroStx } from '@app/common/money/unit-conversion'; - import { test } from '../../fixtures/fixtures'; test.describe('Transaction signing', () => {