Skip to content

Commit

Permalink
GPR-549 - set the fromToken in the swap widget (#1662)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrearampin authored Apr 15, 2024
1 parent 6ee40e1 commit ac54ec7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ describe('SwapForm', () => {
);

cySmartGet('toTokenInputs-select-form-select__target').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-native').click();
cySmartGet('fromTokenInputs-text-form-text__input').type('0.01').blur();
cySmartGet('swap-button').click();
cySmartGet('fromTokenInputs-select-form-select-control-error')
Expand Down Expand Up @@ -534,7 +534,7 @@ describe('SwapForm', () => {
cySmartGet('fromTokenInputs-select-form-coin-selector__option-eth-0xf57e7e7c23978c3caec3c3548e3d615c346e79ff')
.click();
cySmartGet('toTokenInputs-select-form-select__target').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-native').click();
cySmartGet('fromTokenInputs-text-form-text__input').type('0.01').trigger('change');

const params = [
Expand Down Expand Up @@ -580,7 +580,7 @@ describe('SwapForm', () => {
cySmartGet('fromTokenInputs-select-form-coin-selector__option-eth-0xf57e7e7c23978c3caec3c3548e3d615c346e79ff')
.click();
cySmartGet('toTokenInputs-select-form-select__target').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-native').click();
cySmartGet('fromTokenInputs-text-form-text__input').type('0.01').trigger('change');
cySmartGet('@fromAmountInStub').should('have.been.called');

Expand Down Expand Up @@ -629,7 +629,7 @@ describe('SwapForm', () => {
cySmartGet('fromTokenInputs-select-form-coin-selector__option-eth-0xf57e7e7c23978c3caec3c3548e3d615c346e79ff')
.click();
cySmartGet('toTokenInputs-select-form-select__target').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-native').click();
cySmartGet('fromTokenInputs-text-form-max-button').click();

const params = [
Expand Down Expand Up @@ -679,7 +679,7 @@ describe('SwapForm', () => {
cySmartGet('@fromAmountInStub').should('not.have.been.called');

cySmartGet('toTokenInputs-select-form-select__target').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-native').click();
cySmartGet('@fromAmountInStub').should('have.been.called');
});

Expand All @@ -698,7 +698,7 @@ describe('SwapForm', () => {
);

cySmartGet('toTokenInputs-select-form-select__target').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-native').click();
cySmartGet('fromTokenInputs-text-form-text__input').type('0.01').trigger('change');
cySmartGet('@fromAmountInStub').should('not.have.been.called');

Expand Down Expand Up @@ -726,7 +726,7 @@ describe('SwapForm', () => {
cySmartGet('fromTokenInputs-select-form-coin-selector__option-eth-0xf57e7e7c23978c3caec3c3548e3d615c346e79ff')
.click();
cySmartGet('toTokenInputs-select-form-select__target').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-native').click();
cySmartGet('fromTokenInputs-text-form-text__input').type('0').trigger('change');
cySmartGet('@fromAmountInStub').should('not.have.been.called');

Expand Down Expand Up @@ -807,7 +807,7 @@ describe('SwapForm', () => {
cySmartGet('fromTokenInputs-select-form-coin-selector__option-eth-0xf57e7e7c23978c3caec3c3548e3d615c346e79ff')
.click();
cySmartGet('toTokenInputs-select-form-select__target').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-native').click();
cySmartGet('fromTokenInputs-text-form-text__input').type('0.01').trigger('change');
cySmartGet('@fromAmountInStub').should('have.been.called');

Expand Down Expand Up @@ -972,7 +972,7 @@ describe('SwapForm', () => {
cySmartGet('fromTokenInputs-select-form-coin-selector__option-eth-0xf57e7e7c23978c3caec3c3548e3d615c346e79ff')
.click();
cySmartGet('toTokenInputs-select-form-select__target').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-native').click();

cySmartGet('fromTokenInputs-text-form-text__input').type('0.1').trigger('change');
cySmartGet('@fromAmountInStub').should('have.been.called');
Expand Down Expand Up @@ -1069,7 +1069,7 @@ describe('SwapForm', () => {
cySmartGet('toTokenInputs-select-form-coin-selector__option-eth-0xf57e7e7c23978c3caec3c3548e3d615c346e79ff')
.click();
cySmartGet('fromTokenInputs-select-form-select__target').click();
cySmartGet('fromTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('fromTokenInputs-select-form-coin-selector__option-native').click();

cySmartGet('toTokenInputs-text-form-text__input').type('0.00001').trigger('change');
cySmartGet('@fromAmountInStub').should('have.been.called');
Expand Down Expand Up @@ -1130,7 +1130,7 @@ describe('SwapForm', () => {
cySmartGet('fromTokenInputs-select-form-coin-selector__option-eth-0xf57e7e7c23978c3caec3c3548e3d615c346e79ff')
.click();
cySmartGet('toTokenInputs-select-form-select__target').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-native').click();

cySmartGet('fromTokenInputs-text-form-text__input').type('0.1').trigger('change');
cySmartGet('@fromAmountInStub').should('have.been.called');
Expand Down Expand Up @@ -1168,7 +1168,7 @@ describe('SwapForm', () => {
cySmartGet('fromTokenInputs-select-form-coin-selector__option-eth-0xf57e7e7c23978c3caec3c3548e3d615c346e79ff')
.click();
cySmartGet('toTokenInputs-select-form-select__target').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-imx-native').click();
cySmartGet('toTokenInputs-select-form-coin-selector__option-native').click();

cySmartGet('fromTokenInputs-text-form-text__input').type('0.00001').trigger('change');
cySmartGet('@fromAmountInStub').should('have.been.called');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
useCallback,
useContext, useEffect, useMemo, useRef, useState,
useContext, useEffect, useMemo, useState,
} from 'react';
import {
Body, Box, Heading, OptionKey,
Expand Down Expand Up @@ -116,7 +116,7 @@ export function SwapForm({ data, theme }: SwapFromProps) {
const defaultTokenImage = getDefaultTokenImage(checkout?.config.environment, theme);

const formatTokenOptionsId = useCallback((symbol: string, address?: string) => (isNativeToken(address)
? `${symbol.toLowerCase()}-${NATIVE}`
? NATIVE
: `${symbol.toLowerCase()}-${address!.toLowerCase()}`), []);

const { cryptoFiatState, cryptoFiatDispatch } = useContext(CryptoFiatContext);
Expand All @@ -126,7 +126,6 @@ export function SwapForm({ data, theme }: SwapFromProps) {
const [loading, setLoading] = useState(false);
const [conversion, setConversion] = useState(BigNumber.from(0));
const [swapFromToConversionText, setSwapFromToConversionText] = useState('');
const hasSetDefaultState = useRef(false);

const { track } = useAnalytics();

Expand Down Expand Up @@ -182,39 +181,34 @@ export function SwapForm({ data, theme }: SwapFromProps) {
setTokensOptionsForm(fromOptions);

// Set initial token options if provided
if (!hasSetDefaultState.current) {
hasSetDefaultState.current = true;

if (data?.fromTokenAddress) {
setFromToken(
allowedTokens.find((token) => (isNativeToken(token.address)
if (data?.fromTokenAddress && !fromToken) {
setFromToken(
allowedTokens.find((token) => (isNativeToken(token.address)
&& data?.fromTokenAddress?.toLowerCase() === NATIVE)
|| token.address?.toLowerCase()
=== data?.fromTokenAddress?.toLowerCase()),
);
setFromBalance(
tokenBalances.find(
(tokenBalance) => (
isNativeToken(tokenBalance.token.address)
);
setFromBalance(
tokenBalances.find(
(tokenBalance) => (
isNativeToken(tokenBalance.token.address)
&& data?.fromTokenAddress?.toLowerCase() === NATIVE)
|| (tokenBalance.token.address?.toLowerCase() === data?.fromTokenAddress?.toLowerCase()),
)?.formattedBalance ?? '',
);
}
)?.formattedBalance ?? '',
);
}

if (shouldSetToAddress(data?.toTokenAddress, data?.fromTokenAddress)) {
setToToken(allowedTokens.find((token) => (
isNativeToken(token.address) && data?.toTokenAddress?.toLowerCase() === NATIVE
) || (token.address?.toLowerCase() === data?.toTokenAddress?.toLowerCase())));
}
if (shouldSetToAddress(data?.toTokenAddress, data?.fromTokenAddress) && !toToken) {
setToToken(allowedTokens.find((token) => (
isNativeToken(token.address) && data?.toTokenAddress?.toLowerCase() === NATIVE
) || (token.address?.toLowerCase() === data?.toTokenAddress?.toLowerCase())));
}
}, [
tokenBalances,
allowedTokens,
cryptoFiatState.conversions,
data?.fromTokenAddress,
data?.toTokenAddress,
hasSetDefaultState.current,
setFromToken,
setFromBalance,
setToToken,
Expand Down

0 comments on commit ac54ec7

Please sign in to comment.