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

Alissa.crane/swap tokens #545

Closed
wants to merge 3 commits into from
Closed
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
26 changes: 17 additions & 9 deletions src/swap/components/Swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useCallback, useMemo, useState } from 'react';
import { cn } from '../../utils/cn';
import { SwapContext } from '../context';
import { getSwapQuote } from '../core/getSwapQuote';
import { isSwapError } from '../utils';
import { formatTokenAmount, isSwapError } from '../utils';
import type { SwapError, SwapReact } from '../types';
import type { Token } from '../../token';

Expand All @@ -12,9 +12,9 @@ export function Swap({ address, children, onError }: SwapReact) {
const [toAmount, setToAmount] = useState('');
const [toToken, setToToken] = useState<Token>();

const handleToAmountChange = useCallback(
const handleFromAmountChange = useCallback(
async (amount: string) => {
setToAmount(amount);
setFromAmount(amount);
const hasRequiredFields = fromToken && toToken && amount;
if (!hasRequiredFields) {
return;
Expand All @@ -24,23 +24,27 @@ export function Swap({ address, children, onError }: SwapReact) {
from: fromToken,
to: toToken,
amount,
amountReference: 'to',
amountReference: 'from',
});
if (isSwapError(response)) {
onError?.(response);
return;
}
setFromAmount(response?.fromAmount);
const formattedAmount = formatTokenAmount(
response?.toAmount,
response?.to?.decimals,
);
setToAmount(formattedAmount);
} catch (error) {
onError?.(error as SwapError);
}
},
[fromToken, toToken, setFromAmount, setToAmount],
);

const handleFromAmountChange = useCallback(
const handleToAmountChange = useCallback(
async (amount: string) => {
setFromAmount(amount);
setToAmount(amount);
const hasRequiredFields = fromToken && toToken && amount;
if (!hasRequiredFields) {
return;
Expand All @@ -50,13 +54,17 @@ export function Swap({ address, children, onError }: SwapReact) {
from: fromToken,
to: toToken,
amount,
amountReference: 'from',
amountReference: 'to',
});
if (isSwapError(response)) {
onError?.(response);
return;
}
setToAmount(response?.toAmount);
const formattedAmount = formatTokenAmount(
response.fromAmount,
response?.from?.decimals,
);
setFromAmount(formattedAmount);
} catch (error) {
onError?.(error as SwapError);
}
Expand Down
47 changes: 38 additions & 9 deletions src/swap/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { isValidAmount, isSwapError } from './utils'; // Adjust the import path as needed
import { formatTokenAmount, isSwapError, isValidAmount } from './utils'; // Adjust the import path as needed

describe('isValidAmount', () => {
it('should return true for an empty string', () => {
expect(isValidAmount('')).toBe(true);
});

it('should return true for a valid number string with less than 11 characters', () => {
expect(isValidAmount('12345')).toBe(true);
});

it('should return false for a number string with more than 11 characters', () => {
expect(isValidAmount('123456789012')).toBe(false);
});

it('should return true for a valid number string with a decimal point', () => {
expect(isValidAmount('123.45')).toBe(true);
});
Expand Down Expand Up @@ -69,3 +61,40 @@ describe('isSwapError function', () => {
expect(isSwapError(response)).toBe(false);
});
});

describe('formatTokenAmount', () => {
test('formats amount correctly with 18 decimals', () => {
const amount = '100000000000000000';
const decimals = 18;
const formattedAmount = formatTokenAmount(amount, decimals);
expect(formattedAmount).toBe('0.1');
});

test('formats amount correctly with different decimals', () => {
const amount = '1000000000';
const decimals = 9;
const formattedAmount = formatTokenAmount(amount, decimals);
expect(formattedAmount).toBe('1');
});

test('handles very small amounts correctly', () => {
const amount = '1';
const decimals = 18;
const formattedAmount = formatTokenAmount(amount, decimals);
expect(formattedAmount).toBe('1e-18');
});

test('handles zero amount correctly', () => {
const amount = '0';
const decimals = 18;
const formattedAmount = formatTokenAmount(amount, decimals);
expect(formattedAmount).toBe('0');
});

test('handles large amounts correctly', () => {
const amount = '1000000000000000000000000000';
const decimals = 18;
const formattedAmount = formatTokenAmount(amount, decimals);
expect(formattedAmount).toBe('1000000000');
});
});
9 changes: 6 additions & 3 deletions src/swap/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ import type { SwapError } from './types';

// checks that input is a number
export function isValidAmount(value: string) {
if (value.length > 11) {
return false;
}
if (value === '') {
return true;
}
Expand All @@ -17,3 +14,9 @@ export function isSwapError(response: unknown): response is SwapError {
response !== null && typeof response === 'object' && 'error' in response
);
}

export function formatTokenAmount(amount: string, decimals: number) {
// Convert the string amount to a number using decimals value
const numberAmount = Number(amount) / Math.pow(10, decimals);
return numberAmount.toString();
}
Loading