Skip to content

Commit

Permalink
Fixes for converting an ERC-20 token to an NFT (#20747)
Browse files Browse the repository at this point in the history
Fixes 2 issues when converting an ERC-20 token to an NFT:
- The contract address was not pre-populated in the modal
- The NFT was not removed from the tokens tab
  • Loading branch information
bergeron authored Sep 18, 2023
1 parent 8157dc9 commit bec1ab0
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 27 deletions.
1 change: 1 addition & 0 deletions test/data/mock-send-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"appState": {
"networkDropdownOpen": false,
"importNftsModal": { "open": false },
"gasIsLoading": false,
"isLoading": false,
"modal": {
Expand Down
1 change: 1 addition & 0 deletions test/data/mock-state.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
},
"appState": {
"networkDropdownOpen": false,
"importNftsModal": { "open": false },
"gasIsLoading": false,
"isLoading": false,
"modal": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ const ConvertTokenToNFTModal = ({ hideModal, tokenAddress }) => {
pathname: `${ASSET_ROUTE}/${tokenAddress}/${tokenId}`,
});
} else {
dispatch(showImportNftsModal());
dispatch(
showImportNftsModal({ tokenAddress, ignoreErc20Token: true }),
);
}
hideModal();
}}
Expand Down
21 changes: 9 additions & 12 deletions ui/components/multichain/import-nfts-modal/import-nfts-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,14 @@ export const ImportNftsModal = ({ onClose }) => {
const nftsDropdownState = useSelector(getNftsDropdownState);
const selectedAddress = useSelector(getSelectedAddress);
const chainId = useSelector(getCurrentChainId);
const addressEnteredOnImportTokensPage =
history?.location?.state?.addressEnteredOnImportTokensPage;
const contractAddressToConvertFromTokenToNft =
history?.location?.state?.tokenAddress;
const {
tokenAddress: initialTokenAddress,
tokenId: initialTokenId,
ignoreErc20Token,
} = useSelector((state) => state.appState.importNftsModal);

const [nftAddress, setNftAddress] = useState(
addressEnteredOnImportTokensPage ??
contractAddressToConvertFromTokenToNft ??
'',
);
const [tokenId, setTokenId] = useState('');
const [nftAddress, setNftAddress] = useState(initialTokenAddress ?? '');
const [tokenId, setTokenId] = useState(initialTokenId ?? '');
const [disabled, setDisabled] = useState(true);
const [nftAddFailed, setNftAddFailed] = useState(false);
const trackEvent = useContext(MetaMetricsContext);
Expand All @@ -99,10 +96,10 @@ export const ImportNftsModal = ({ onClose }) => {
setNftAddFailed(true);
return;
}
if (contractAddressToConvertFromTokenToNft) {
if (ignoreErc20Token && nftAddress) {
await dispatch(
ignoreTokens({
tokensToIgnore: contractAddressToConvertFromTokenToNft,
tokensToIgnore: nftAddress,
dontShowLoadingIndicator: true,
}),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ jest.mock('react-router-dom', () => ({
useHistory: jest.fn(
jest.fn().mockReturnValue({
push: jest.fn(),
location: {
state: {
tokenAddress: '0xTokenAddress',
},
},
}),
),
}));
Expand All @@ -48,7 +43,7 @@ jest.mock('../../../store/actions.ts', () => ({
}));

describe('ImportNftsModal', () => {
const store = configureMockStore([thunk])(mockState);
let store = configureMockStore([thunk])(mockState);

beforeEach(() => {
jest.restoreAllMocks();
Expand Down Expand Up @@ -97,6 +92,11 @@ describe('ImportNftsModal', () => {
});

it('should call addNftVerifyOwnership, updateNftDropDownState, setNewNftAddedMessage, and ignoreTokens action with correct values (tokenId should not be in scientific notation)', async () => {
store = configureMockStore([thunk])({
...mockState,
appState: { importNftsModal: { ignoreErc20Token: true } },
});

const onClose = jest.fn();
const { getByPlaceholderText, getByText } = renderWithProvider(
<ImportNftsModal onClose={onClose} />,
Expand Down Expand Up @@ -133,7 +133,7 @@ describe('ImportNftsModal', () => {

expect(ignoreTokens).toHaveBeenCalledWith({
dontShowLoadingIndicator: true,
tokensToIgnore: '0xTokenAddress',
tokensToIgnore: VALID_ADDRESS,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export const ImportTokensModal = ({ onClose }) => {
<ButtonLink
className="import-tokens-modal__nft-address-error-link"
onClick={() => {
dispatch(showImportNftsModal());
dispatch(showImportNftsModal({ tokenAddress: address }));
onClose();
}}
color={TextColor.primaryDefault}
Expand Down
18 changes: 14 additions & 4 deletions ui/ducks/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ interface AppState {
values?: { address?: string | null };
} | null;
networkDropdownOpen: boolean;
importNftsModalOpen: boolean;
importNftsModal: {
open: boolean;
tokenAddress?: string;
tokenId?: string;
ignoreErc20Token?: boolean;
};
showIpfsModalOpen: boolean;
importTokensModalOpen: boolean;
showSelectActionModal: boolean;
Expand Down Expand Up @@ -98,7 +103,7 @@ const initialState: AppState = {
alertMessage: null,
qrCodeData: null,
networkDropdownOpen: false,
importNftsModalOpen: false,
importNftsModal: { open: false },
showIpfsModalOpen: false,
importTokensModalOpen: false,
showSelectActionModal: false,
Expand Down Expand Up @@ -174,13 +179,18 @@ export default function reduceApp(
case actionConstants.IMPORT_NFTS_MODAL_OPEN:
return {
...appState,
importNftsModalOpen: true,
importNftsModal: {
open: true,
...action.payload,
},
};

case actionConstants.IMPORT_NFTS_MODAL_CLOSE:
return {
...appState,
importNftsModalOpen: false,
importNftsModal: {
open: false,
},
};

case actionConstants.SHOW_IPFS_MODAL_OPEN:
Expand Down
2 changes: 1 addition & 1 deletion ui/pages/routes/routes.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function mapStateToProps(state) {
isNetworkMenuOpen: state.metamask.isNetworkMenuOpen,
isImportTokensModalOpen: state.appState.importTokensModalOpen,
accountDetailsAddress: state.appState.accountDetailsAddress,
isImportNftsModalOpen: state.appState.importNftsModalOpen,
isImportNftsModalOpen: state.appState.importNftsModal.open,
isIpfsModalOpen: state.appState.showIpfsModalOpen,
isSelectActionModalOpen: state.appState.showSelectActionModal,
};
Expand Down
7 changes: 6 additions & 1 deletion ui/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2413,9 +2413,14 @@ export function hideModal(): Action {
};
}

export function showImportNftsModal(): Action {
export function showImportNftsModal(payload: {
tokenAddress?: string;
tokenId?: string;
ignoreErc20Token?: boolean;
}) {
return {
type: actionConstants.IMPORT_NFTS_MODAL_OPEN,
payload,
};
}

Expand Down

0 comments on commit bec1ab0

Please sign in to comment.