Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: rename SwapLite utils to Buy utils #1742

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { act, renderHook } from '@testing-library/react';
import { base } from 'viem/chains';
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest';
import { useValue } from '../../core-react/internal/hooks/useValue';
import { useSwapBalances } from '../../swap/hooks/useSwapBalances';
import type { Token } from '../../token';
import { usdcToken } from '../../token/constants';
import { useSwapBalances } from './useSwapBalances';
import { useSwapLiteToken } from './useSwapLiteToken';
import { useBuyToken } from './useBuyToken';

vi.mock('./useSwapBalances', () => ({
vi.mock('../../swap/hooks/useSwapBalances', () => ({
useSwapBalances: vi.fn(),
}));

Expand All @@ -25,7 +25,7 @@ const toToken: Token = {
chainId: base.id,
};

describe('useSwapLiteToken', () => {
describe('useBuyToken', () => {
beforeEach(() => {
vi.clearAllMocks();
});
Expand All @@ -43,7 +43,7 @@ describe('useSwapLiteToken', () => {
...props,
}));
const { result } = renderHook(() =>
useSwapLiteToken(toToken, usdcToken, '0x123'),
useBuyToken(toToken, usdcToken, '0x123'),
);
expect(result.current).toEqual({
amount: '',
Expand Down Expand Up @@ -71,7 +71,7 @@ describe('useSwapLiteToken', () => {
response: props.response,
}));
const { result } = renderHook(() =>
useSwapLiteToken(toToken, usdcToken, '0x123'),
useBuyToken(toToken, usdcToken, '0x123'),
);
await act(async () => {
await result.current.balanceResponse?.refetch();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useState } from 'react';
import type { Address } from 'viem';
import { useValue } from '../../core-react/internal/hooks/useValue';
import { useSwapBalances } from '../../swap/hooks/useSwapBalances';
import type { Token } from '../../token';
import { useSwapBalances } from './useSwapBalances';

export const useSwapLiteToken = (
export const useBuyToken = (
toToken: Token,
token: Token | undefined,
address: Address | undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import { act, renderHook } from '@testing-library/react';
import { base } from 'viem/chains';
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest';
import { useValue } from '../../core-react/internal/hooks/useValue';
import { useSwapBalances } from '../../swap/hooks/useSwapBalances';
import type { Token } from '../../token';
import {
daiToken,
degenToken,
ethToken,
usdcToken,
} from '../../token/constants';
import { useSwapBalances } from './useSwapBalances';
import { useSwapLiteToken } from './useSwapLiteToken';
import { useSwapLiteTokens } from './useSwapLiteTokens';
import { useBuyToken } from './useBuyToken';
import { useBuyTokens } from './useBuyTokens';

vi.mock('./useSwapLiteToken');
vi.mock('./useSwapBalances');
vi.mock('./useBuyToken');
vi.mock('../../swap/hooks/useSwapBalances');
vi.mock('../../core-react/internal/hooks/useValue');

const toToken: Token = {
Expand Down Expand Up @@ -73,10 +73,10 @@ const mockTo = {

const address = '0x123';

describe('useSwapLiteTokens', () => {
describe('useBuyTokens', () => {
beforeEach(() => {
vi.clearAllMocks();
(useSwapLiteToken as Mock).mockImplementation((_toToken, fromToken) => {
(useBuyToken as Mock).mockImplementation((_toToken, fromToken) => {
if (fromToken === ethToken) {
return mockFromETH;
}
Expand All @@ -97,12 +97,12 @@ describe('useSwapLiteTokens', () => {

it('should return expected swap tokens', () => {
const { result } = renderHook(() =>
useSwapLiteTokens(toToken, daiToken, address),
useBuyTokens(toToken, daiToken, address),
);

expect(useSwapLiteToken).toHaveBeenCalledWith(toToken, ethToken, address);
expect(useSwapLiteToken).toHaveBeenCalledWith(toToken, usdcToken, address);
expect(useSwapLiteToken).toHaveBeenCalledWith(toToken, daiToken, address);
expect(useBuyToken).toHaveBeenCalledWith(toToken, ethToken, address);
expect(useBuyToken).toHaveBeenCalledWith(toToken, usdcToken, address);
expect(useBuyToken).toHaveBeenCalledWith(toToken, daiToken, address);
expect(useSwapBalances).toHaveBeenCalledWith({
address,
fromToken: ethToken,
Expand Down Expand Up @@ -130,7 +130,7 @@ describe('useSwapLiteTokens', () => {
});

it('should handle toToken.symbol === ETH', () => {
renderHook(() => useSwapLiteTokens(ethToken, degenToken, address));
renderHook(() => useBuyTokens(ethToken, degenToken, address));

expect(useSwapBalances).toHaveBeenCalledWith({
address,
Expand All @@ -151,7 +151,7 @@ describe('useSwapLiteTokens', () => {
response: props.response,
}));
const { result } = renderHook(() =>
useSwapLiteTokens(toToken, undefined, '0x123'),
useBuyTokens(toToken, undefined, '0x123'),
);
await act(async () => {
await result.current.to.balanceResponse?.refetch();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { useState } from 'react';
import type { Address } from 'viem';
import { useValue } from '../../core-react/internal/hooks/useValue';
import { useSwapBalances } from '../../swap/hooks/useSwapBalances';
import type { Token } from '../../token';
import { ethToken, usdcToken } from '../../token/constants';
import type { SwapLiteTokens } from '../types';
import { useSwapBalances } from './useSwapBalances';
import { useSwapLiteToken } from './useSwapLiteToken';
import type { BuyTokens } from '../types';
import { useBuyToken } from './useBuyToken';

export const useSwapLiteTokens = (
export const useBuyTokens = (
toToken: Token,
fromToken?: Token,
address?: Address,
): SwapLiteTokens => {
const fromETH = useSwapLiteToken(toToken, ethToken, address);
const fromUSDC = useSwapLiteToken(toToken, usdcToken, address);
const from = useSwapLiteToken(toToken, fromToken, address);
): BuyTokens => {
const fromETH = useBuyToken(toToken, ethToken, address);
const fromUSDC = useBuyToken(toToken, usdcToken, address);
const from = useBuyToken(toToken, fromToken, address);

const [toAmount, setToAmount] = useState('');
const [toAmountUSD, setToAmountUSD] = useState('');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { act, renderHook } from '@testing-library/react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import type { SwapUnit } from '../types';
import { useResetSwapLiteInputs } from './useResetSwapLiteInputs';
import type { SwapUnit } from '../../swap/types';
import { useResetBuyInputs } from './useResetBuyInputs';

describe('useResetSwapLiteInputs', () => {
describe('useResetBuyInputs', () => {
const mockQueryResponse = {
data: undefined,
error: null,
Expand Down Expand Up @@ -80,7 +80,7 @@ describe('useResetSwapLiteInputs', () => {

it('should return a function', () => {
const { result } = renderHook(() =>
useResetSwapLiteInputs({
useResetBuyInputs({
fromETH: mockFromETH,
fromUSDC: mockFromUSDC,
from: mockFrom,
Expand All @@ -92,7 +92,7 @@ describe('useResetSwapLiteInputs', () => {

it('should call refetch on responses and set amounts to empty strings when executed', async () => {
const { result } = renderHook(() =>
useResetSwapLiteInputs({
useResetBuyInputs({
fromETH: mockFromETH,
fromUSDC: mockFromUSDC,
from: mockFrom,
Expand All @@ -112,7 +112,7 @@ describe('useResetSwapLiteInputs', () => {

it("should not create a new function reference if from and to haven't changed", () => {
const { result, rerender } = renderHook(() =>
useResetSwapLiteInputs({
useResetBuyInputs({
fromETH: mockFromETH,
fromUSDC: mockFromUSDC,
to: mockTo,
Expand All @@ -126,7 +126,7 @@ describe('useResetSwapLiteInputs', () => {
it('should create a new function reference if from or to change', () => {
const { result, rerender } = renderHook(
({ fromETH, fromUSDC, to }) =>
useResetSwapLiteInputs({
useResetBuyInputs({
fromETH,
fromUSDC,
to,
Expand Down Expand Up @@ -170,7 +170,7 @@ describe('useResetSwapLiteInputs', () => {
balanceResponse: null,
} as unknown as SwapUnit;
const { result } = renderHook(() =>
useResetSwapLiteInputs({
useResetBuyInputs({
fromETH: mockFromWithNullResponse,
fromUSDC: mockFromUSDCWithNullResponse,
to: mockToWithNullResponse,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useCallback } from 'react';
import type { SwapLiteTokens } from '../types';
import type { BuyTokens } from '../types';

// Refreshes balances and inputs post-swap
export const useResetSwapLiteInputs = ({
export const useResetBuyInputs = ({
fromETH,
fromUSDC,
from,
to,
}: SwapLiteTokens) => {
}: BuyTokens) => {
return useCallback(async () => {
await Promise.all([
from?.balanceResponse?.refetch(),
Expand Down
8 changes: 8 additions & 0 deletions src/buy/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { SwapUnit } from '@/swap/types';

export type BuyTokens = {
fromETH: SwapUnit;
fromUSDC: SwapUnit;
to: SwapUnit;
from?: SwapUnit;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Token } from '@/token/types';
import { base } from 'viem/chains';
import { type Mock, beforeEach, describe, expect, it, vi } from 'vitest';
import { isSwapError } from '../../swap/utils/isSwapError';
import { getSwapLiteQuote } from './getSwapLiteQuote';
import { getBuyQuote } from './getBuyQuote';

vi.mock('@/core/api/getSwapQuote');
vi.mock('@/core/utils/formatTokenAmount');
Expand Down Expand Up @@ -66,13 +66,13 @@ const mockFromSwapUnit = {
token: fromToken,
};

describe('getSwapLiteQuote', () => {
describe('getBuyQuote', () => {
beforeEach(() => {
vi.clearAllMocks();
});

it('should return default values if `from` token is not provided', async () => {
const result = await getSwapLiteQuote({
const result = await getBuyQuote({
amount: '1',
amountReference: 'exactIn',
maxSlippage: '0.5',
Expand All @@ -93,7 +93,7 @@ describe('getSwapLiteQuote', () => {
(isSwapError as unknown as Mock).mockReturnValue(false);
(formatTokenAmount as Mock).mockReturnValue('1.0');

const result = await getSwapLiteQuote({
const result = await getBuyQuote({
amount: '1',
amountReference: 'exactIn',
from: fromToken,
Expand Down Expand Up @@ -128,7 +128,7 @@ describe('getSwapLiteQuote', () => {
(isSwapError as unknown as Mock).mockReturnValue(false);
(formatTokenAmount as Mock).mockReturnValue('1.0');

const result = await getSwapLiteQuote({
const result = await getBuyQuote({
amount: '1',
amountReference: 'exactIn',
from: fromToken,
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('getSwapLiteQuote', () => {
(getSwapQuote as Mock).mockResolvedValue(mockError);
(isSwapError as unknown as Mock).mockReturnValue(true);

const result = await getSwapLiteQuote({
const result = await getBuyQuote({
amount: '1',
amountReference: 'exactIn',
from: fromToken,
Expand All @@ -187,7 +187,7 @@ describe('getSwapLiteQuote', () => {
});

it('should not call `getSwapQuote` if `from` and `to` tokens are the same', async () => {
const result = await getSwapLiteQuote({
const result = await getBuyQuote({
amount: '1',
amountReference: 'exactIn',
from: fromToken,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,30 @@ import type { SwapError, SwapUnit } from '../../swap/types';
import { isSwapError } from '../../swap/utils/isSwapError';
import type { Token } from '../../token';

type GetSwapLiteQuoteResponse = {
type GetBuyQuoteResponse = {
response?: GetSwapQuoteResponse;
error?: APIError;
formattedFromAmount?: string;
};

type GetSwapLiteQuoteParams = Omit<GetSwapQuoteParams, 'from'> & {
fromSwapUnit: SwapUnit;
type GetBuyQuoteParams = Omit<GetSwapQuoteParams, 'from'> & {
fromSwapUnit?: SwapUnit;
from?: Token;
};

/**
* Fetches a quote for a swap, but only if the from and to tokens are different.
*/

export async function getSwapLiteQuote({
export async function getBuyQuote({
amount,
amountReference,
from,
maxSlippage,
to,
useAggregator,
fromSwapUnit,
}: GetSwapLiteQuoteParams): Promise<GetSwapLiteQuoteResponse> {
}: GetBuyQuoteParams): Promise<GetBuyQuoteResponse> {
// only fetch quote if the from token is provided
if (!from) {
return { response: undefined, formattedFromAmount: '', error: undefined };
Expand All @@ -57,8 +57,8 @@ export async function getSwapLiteQuote({
? formatTokenAmount(response.fromAmount, response.from.decimals)
: '';

fromSwapUnit.setAmountUSD(response?.fromAmountUSD || '');
fromSwapUnit.setAmount(formattedFromAmount || '');
fromSwapUnit?.setAmountUSD(response?.fromAmountUSD || '');
fromSwapUnit?.setAmount(formattedFromAmount || '');
}

let error: SwapError | undefined;
Expand Down
7 changes: 0 additions & 7 deletions src/swap/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,6 @@ export type ProcessSwapTransactionParams = {
walletCapabilities: WalletCapabilities; // EIP-5792 wallet capabilities
};

export type SwapLiteTokens = {
fromETH: SwapUnit;
fromUSDC: SwapUnit;
to: SwapUnit;
from?: SwapUnit;
};

/**
* Note: exported as public Type
*/
Expand Down
Loading