Skip to content

Commit

Permalink
fix: swap issues (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w authored Dec 19, 2024
1 parent e2cef67 commit 9c6b938
Show file tree
Hide file tree
Showing 12 changed files with 361 additions and 232 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@
"@commitlint/cli": "17.0.3",
"@commitlint/config-angular": "17.0.3",
"@commitlint/config-conventional": "17.0.3",
"@eslint/compat": "1.2.4",
"@eslint/eslintrc": "3.2.0",
"@eslint/js": "9.16.0",
"@eslint/compat": "1.2.4",
"@eslint/eslintrc": "3.2.0",
"@eslint/js": "9.16.0",
"@google/semantic-release-replace-plugin": "1.1.0",
"@lavamoat/allow-scripts": "2.0.0",
"@lavamoat/preinstall-always-fail": "1.0.0",
Expand Down Expand Up @@ -167,7 +167,7 @@
"eslint-plugin-react-hooks": "5.1.0",
"file-loader": "6.2.0",
"fs": "0.0.1-security",
"globals": "15.13.0",
"globals": "15.13.0",
"husky": "7.0.4",
"i18next-scanner": "4.1.1",
"i18next-scanner-typescript": "1.1.1",
Expand Down
7 changes: 5 additions & 2 deletions src/components/common/BNInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ export function BNInput({
return;
}

if (value === 0n && Number(valStr) === 0) {
if (value === 0n) {
if (Number(valStr) !== 0) {
setValStr('');
}
return;
}

Expand Down Expand Up @@ -119,7 +122,7 @@ export function BNInput({
<Stack sx={{ position: 'relative' }}>
<InputNumber
fullWidth={fullWidth}
value={valStr}
value={valStr.replaceAll(',', '')}
onChange={(e) => onValueChanged(e.target.value)}
type="number"
onKeyDown={(e) => {
Expand Down
6 changes: 4 additions & 2 deletions src/components/common/TokenSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ interface TokenSelectProps {
skipHandleMaxAmount?: boolean;
containerRef?: MutableRefObject<HTMLElement | null>;
withMaxButton?: boolean;
withOnlyTokenPreselect?: boolean;
}

export function TokenSelect({
Expand All @@ -107,6 +108,7 @@ export function TokenSelect({
setIsOpen,
containerRef,
withMaxButton = true,
withOnlyTokenPreselect = true,
}: TokenSelectProps) {
const { t } = useTranslation();
const { currencyFormatter, currency } = useSettingsContext();
Expand Down Expand Up @@ -184,7 +186,7 @@ export function TokenSelect({
const isOnlyTokenNotSelected =
theOnlyToken && theOnlyToken?.symbol !== selectedToken?.symbol;

if (isOnlyTokenNotSelected) {
if (withOnlyTokenPreselect && isOnlyTokenNotSelected) {
onTokenChange(theOnlyToken);
return;
}
Expand All @@ -198,7 +200,7 @@ export function TokenSelect({
) {
onTokenChange(tokensList[0]);
}
}, [tokensList, onTokenChange, selectedToken]);
}, [withOnlyTokenPreselect, tokensList, onTokenChange, selectedToken]);

const rowRenderer = useCallback(
({ key, index, style }) => {
Expand Down
118 changes: 78 additions & 40 deletions src/contexts/SwapProvider/SwapProvider.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ const getSwapProvider = (): SwapContextAPI => {
return ref.current ?? ({} as SwapContextAPI);
};

const TestConsumerComponent = forwardRef((_props, ref) => {
const waitForRetries = async (retries: number) => {
for (let i = 0; i < retries; i++) {
jest.runAllTimers();
await new Promise(jest.requireActual('timers').setImmediate);
}
};

const TestConsumerComponent = forwardRef((_props: unknown, ref) => {
const { getRate, swap } = useSwapContext();

useImperativeHandle(ref, () => ({
Expand Down Expand Up @@ -105,6 +112,7 @@ describe.only('contexts/SwapProvider', () => {

beforeEach(() => {
jest.resetAllMocks();
jest.useRealTimers();

jest.spyOn(global, 'fetch').mockResolvedValue({
json: async () => ({}),
Expand Down Expand Up @@ -643,19 +651,29 @@ describe.only('contexts/SwapProvider', () => {
slippage,
} = getSwapParams();

await expect(
swap({
srcToken,
srcDecimals,
srcAmount,
destToken,
destDecimals,
destAmount,
gasLimit,
priceRoute,
slippage,
}),
).rejects.toThrow('Data Error: Error: Invalid transaction params');
jest.useFakeTimers();

swap({
srcToken,
srcDecimals,
srcAmount,
destToken,
destDecimals,
destAmount,
gasLimit,
priceRoute,
slippage,
})
.then(() => {
fail('Expected to throw');
})
.catch((err) => {
expect(err.message).toEqual(
'Data Error: Error: Invalid transaction params',
);
});

await waitForRetries(10);
});

it('handles Paraswap API error responses', async () => {
Expand Down Expand Up @@ -691,19 +709,29 @@ describe.only('contexts/SwapProvider', () => {
slippage,
} = getSwapParams();

await expect(
swap({
srcToken,
srcDecimals,
srcAmount,
destToken,
destDecimals,
destAmount,
gasLimit,
priceRoute,
slippage,
}),
).rejects.toThrow('Data Error: Error: Some API error happened');
jest.useFakeTimers();

swap({
srcToken,
srcDecimals,
srcAmount,
destToken,
destDecimals,
destAmount,
gasLimit,
priceRoute,
slippage,
})
.then(() => {
fail('Expected to throw');
})
.catch((err) => {
expect(err.message).toEqual(
'Data Error: Error: Some API error happened',
);
});

await waitForRetries(10);
});

it('handles API HTTP errors', async () => {
Expand Down Expand Up @@ -743,19 +771,29 @@ describe.only('contexts/SwapProvider', () => {
slippage,
} = getSwapParams();

await expect(
swap({
srcToken,
srcDecimals,
srcAmount,
destToken,
destDecimals,
destAmount,
gasLimit,
priceRoute,
slippage,
}),
).rejects.toThrow('Data Error: Error: Invalid transaction params');
jest.useFakeTimers();

swap({
srcToken,
srcDecimals,
srcAmount,
destToken,
destDecimals,
destAmount,
gasLimit,
priceRoute,
slippage,
})
.then(() => {
fail('Expected to throw');
})
.catch((err) => {
expect(err.message).toEqual(
'Data Error: Error: Invalid transaction params',
);
});

await waitForRetries(10);
});

describe('when everything goes right', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/contexts/SwapProvider/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,8 @@ export function SwapContextProvider({ children }: { children: any }) {
(result as APIError).message === 'Server too busy' ||
// paraswap returns responses like this: {error: 'Not enough 0x4f60a160d8c2dddaafe16fcc57566db84d674…}
// when they are too slow to detect the approval
(result as any).error
(result as any).error ||
result instanceof Error
);
}

Expand Down
1 change: 1 addition & 0 deletions src/localization/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@
"Error:": "Error:",
"Estimated": "Estimated",
"Estimated gas units needed to complete the transaction. Includes a small buffer. Not editable for this transaction.": "Estimated gas units needed to complete the transaction. Includes a small buffer. Not editable for this transaction.",
"Estimated loss greater than impact. Try lowering the amount.": "Estimated loss greater than impact. Try lowering the amount.",
"Ethereum Address": "Ethereum Address",
"Euro": "Euro",
"Explore Ecosystem": "Explore Ecosystem",
Expand Down
Loading

0 comments on commit 9c6b938

Please sign in to comment.