Skip to content

Commit

Permalink
chore: useValue cleanup (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizzamia authored Aug 10, 2024
1 parent cd6c0cf commit 5168def
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
5 changes: 1 addition & 4 deletions src/swap/components/SwapAmountInput.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -8,10 +9,6 @@ import type { Token } from '../../token';
import type { SwapAmountInputReact } from '../types';
import { useSwapContext } from './SwapProvider';

function useValue<T>(object: T): T {
return useMemo(() => object, [object]);
}

// istanbul ignore next
export function SwapAmountInput({
className,
Expand Down
31 changes: 10 additions & 21 deletions src/swap/components/SwapProvider.tsx
Original file line number Diff line number Diff line change
@@ -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<T>(object: T): T {
return useMemo(() => object, [object]);
}

const emptyContext = {} as SwapContextType;

export const SwapContext = createContext<SwapContextType>(emptyContext);
Expand All @@ -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;

Expand Down
6 changes: 1 addition & 5 deletions src/swap/hooks/useSwapBalances.tsx
Original file line number Diff line number Diff line change
@@ -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<T>(object: T): T {
return useMemo(() => object, [object]);
}

export function useSwapBalances({
address,
fromToken,
Expand Down
9 changes: 9 additions & 0 deletions src/swap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
4 changes: 2 additions & 2 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
},
Expand Down

0 comments on commit 5168def

Please sign in to comment.