From 5168def254545ae98154de872b25d60a19b3255e Mon Sep 17 00:00:00 2001 From: Leonardo Zizzamia Date: Fri, 9 Aug 2024 23:21:47 -0700 Subject: [PATCH] chore: useValue cleanup (#1028) --- src/swap/components/SwapAmountInput.tsx | 5 +--- src/swap/components/SwapProvider.tsx | 31 ++++++++----------------- src/swap/hooks/useSwapBalances.tsx | 6 +---- src/swap/types.ts | 9 +++++++ vitest.config.ts | 4 ++-- 5 files changed, 23 insertions(+), 32 deletions(-) diff --git a/src/swap/components/SwapAmountInput.tsx b/src/swap/components/SwapAmountInput.tsx index 50a09c5a22..24ee106e1d 100644 --- a/src/swap/components/SwapAmountInput.tsx +++ b/src/swap/components/SwapAmountInput.tsx @@ -1,5 +1,6 @@ import { useCallback, useEffect, useMemo } from 'react'; import { TextInput } from '../../internal/components/TextInput'; +import { useValue } from '../../internal/hooks/useValue'; import { getRoundedAmount } from '../../internal/utils/getRoundedAmount'; import { isValidAmount } from '../../internal/utils/isValidAmount'; import { background, cn, color, pressable, text } from '../../styles/theme'; @@ -8,10 +9,6 @@ import type { Token } from '../../token'; import type { SwapAmountInputReact } from '../types'; import { useSwapContext } from './SwapProvider'; -function useValue(object: T): T { - return useMemo(() => object, [object]); -} - // istanbul ignore next export function SwapAmountInput({ className, diff --git a/src/swap/components/SwapProvider.tsx b/src/swap/components/SwapProvider.tsx index 66ee6d4d5b..a067d6fae9 100644 --- a/src/swap/components/SwapProvider.tsx +++ b/src/swap/components/SwapProvider.tsx @@ -1,26 +1,22 @@ -import { - createContext, - useCallback, - useContext, - useMemo, - useState, -} from 'react'; -import type { Address, TransactionReceipt } from 'viem'; +import { createContext, useCallback, useContext, useState } from 'react'; +import type { TransactionReceipt } from 'viem'; import { type BaseError, useConfig, useSendTransaction } from 'wagmi'; +import { useValue } from '../../internal/hooks/useValue'; import { formatTokenAmount } from '../../internal/utils/formatTokenAmount'; import type { Token } from '../../token'; import { USER_REJECTED_ERROR_CODE } from '../constants'; import { useFromTo } from '../hooks/useFromTo'; -import type { SwapContextType, SwapError, SwapErrorState } from '../types'; +import type { + SwapContextType, + SwapError, + SwapErrorState, + SwapProviderReact, +} from '../types'; import { buildSwapTransaction } from '../utils/buildSwapTransaction'; import { getSwapQuote } from '../utils/getSwapQuote'; import { isSwapError } from '../utils/isSwapError'; import { processSwapTransaction } from '../utils/processSwapTransaction'; -function useValue(object: T): T { - return useMemo(() => object, [object]); -} - const emptyContext = {} as SwapContextType; export const SwapContext = createContext(emptyContext); @@ -37,14 +33,7 @@ export function SwapProvider({ address, children, experimental, -}: { - address: Address; - children: React.ReactNode; - experimental: { - useAggregator: boolean; // Whether to use a DEX aggregator. (default: true) - maxSlippage?: number; // Maximum acceptable slippage for a swap. (default: 10) This is as a percent, not basis points - }; -}) { +}: SwapProviderReact) { // Feature flags const { useAggregator } = experimental; diff --git a/src/swap/hooks/useSwapBalances.tsx b/src/swap/hooks/useSwapBalances.tsx index c26be321e1..4f572a417a 100644 --- a/src/swap/hooks/useSwapBalances.tsx +++ b/src/swap/hooks/useSwapBalances.tsx @@ -1,13 +1,9 @@ -import { useMemo } from 'react'; import type { Address } from 'viem'; +import { useValue } from '../../internal/hooks/useValue'; import type { Token } from '../../token'; import { useGetETHBalance } from '../../wallet/hooks/useGetETHBalance'; import { useGetTokenBalance } from '../../wallet/hooks/useGetTokenBalance'; -function useValue(object: T): T { - return useMemo(() => object, [object]); -} - export function useSwapBalances({ address, fromToken, diff --git a/src/swap/types.ts b/src/swap/types.ts index 6ecafa8204..d8d94da87b 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -196,6 +196,15 @@ export type SwapParams = { to: Token; }; +export type SwapProviderReact = { + address: Address; + children: React.ReactNode; + experimental: { + useAggregator: boolean; // Whether to use a DEX aggregator. (default: true) + maxSlippage?: number; // Maximum acceptable slippage for a swap. (default: 10) This is as a percent, not basis points + }; +}; + /** * Note: exported as public Type */ diff --git a/vitest.config.ts b/vitest.config.ts index 7c6a50ee1c..b737f6e4a5 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -23,8 +23,8 @@ export default defineConfig({ reportOnFailure: true, thresholds: { statements: 99.24, - branches: 98.03, - functions: 93.42, + branches: 98.02, + functions: 93.33, lines: 99.24, }, },