Skip to content

Commit

Permalink
fix: swapping AVAX/wAVAX (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w authored Feb 19, 2025
1 parent 76a75c5 commit 3a2087f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
56 changes: 55 additions & 1 deletion src/contexts/SwapProvider/SwapProvider.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SwapSide } from 'paraswap';
import { ETHER_ADDRESS, SwapSide } from 'paraswap';
import { createRef, forwardRef, useImperativeHandle } from 'react';

import { matchingPayload, render } from '@src/tests/test-utils';
Expand Down Expand Up @@ -274,6 +274,60 @@ describe('contexts/SwapProvider', () => {
});
});

it('maps srcToken to 0xEeEe... when its the native token', async () => {
jest.spyOn(global, 'fetch').mockResolvedValueOnce({
json: async () => ({
priceRoute: {
address: ROUTE_ADDRESS,
destAmount: 1000,
},
}),
ok: true,
} as any);

const { getRate } = getSwapProvider();
const params = buildGetRateParams({
srcToken: networkContext.network.networkToken.symbol,
});

await getRate(params);

expect(global.fetch).toHaveBeenCalledWith(
getExpectedURL('prices', {
...params,
srcToken: ETHER_ADDRESS,
srcDecimals: 18,
}),
);
});

it('maps destToken to 0xEeEe... when its the native token', async () => {
jest.spyOn(global, 'fetch').mockResolvedValueOnce({
json: async () => ({
priceRoute: {
address: ROUTE_ADDRESS,
destAmount: 1000,
},
}),
ok: true,
} as any);

const { getRate } = getSwapProvider();
const params = buildGetRateParams({
destToken: networkContext.network.networkToken.symbol,
});

await getRate(params);

expect(global.fetch).toHaveBeenCalledWith(
getExpectedURL('prices', {
...params,
destToken: ETHER_ADDRESS,
destDecimals: 18,
}),
);
});

describe('when a server times out', () => {
beforeEach(() => {
jest
Expand Down
15 changes: 7 additions & 8 deletions src/contexts/SwapProvider/SwapProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,17 @@ export function SwapContextProvider({ children }: { children: any }) {
throw new Error(`Feature (SWAP) is currently unavailable`);
}

const isFromTokenNative = activeNetwork.networkToken.symbol === srcToken;
const isDestTokenNative = activeNetwork.networkToken.symbol === destToken;

const query = new URLSearchParams({
srcToken,
destToken,
srcToken: isFromTokenNative ? ETHER_ADDRESS : srcToken,
destToken: isDestTokenNative ? ETHER_ADDRESS : destToken,
amount: srcAmount,
side: swapSide || SwapSide.SELL,
network: ChainId.AVALANCHE_MAINNET_ID.toString(),
srcDecimals: `${
activeNetwork.networkToken.symbol === srcToken ? 18 : srcDecimals
}`,
destDecimals: `${
activeNetwork.networkToken.symbol === destToken ? 18 : destDecimals
}`,
srcDecimals: `${isFromTokenNative ? 18 : srcDecimals}`,
destDecimals: `${isDestTokenNative ? 18 : destDecimals}`,
userAddress: activeAccount.addressC,
});

Expand Down

0 comments on commit 3a2087f

Please sign in to comment.