Skip to content

Commit

Permalink
Refactor burnNFT messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
ThibautBremand committed Oct 18, 2023
1 parent ee1e291 commit 90c9410
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
18 changes: 16 additions & 2 deletions packages/extension/cypress/e2e/NFT.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,22 @@ describe('Mint', () => {
});

it('Burn NFT', function () {
const url = `http://localhost:3000?NFTokenID=${this.NFTokenID}&fee=199&memos=%5B%7B%22memo%22%3A%7B%22memoType%22%3A%224465736372697074696f6e%22%2C%22memoData%22%3A%2254657374206d656d6f%22%7D%7D%5D&id=210325959&requestMessage=undefined&transaction=burnNFT`;
navigate(url, PASSWORD);
const url = `http://localhost:3000?burn-nft&storageKey=${STORAGE_KEY}&id=210402654&requestMessage=undefined&transaction=burnNFT`;

const params = {
NFTokenID: this.NFTokenID,
fee: '199',
memos: [
{
memo: {
memoType: '4465736372697074696f6e',
memoData: '54657374206d656d6f'
}
}
]
};

navigate(url, PASSWORD, STORAGE_KEY, params);

// Confirm
cy.get('h1[data-testid="page-title"]').should('have.text', 'Burn NFT');
Expand Down
19 changes: 9 additions & 10 deletions packages/extension/src/chromeServices/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,15 @@ chrome.runtime.onMessage.addListener(
});
} catch (e) {}
} else if (type === 'REQUEST_BURN_NFT/V3') {
handleTransactionRequest({
payload: message.payload,
sender,
parameter: PARAMETER_TRANSACTION_BURN_NFT,
receivingMessage: 'RECEIVE_BURN_NFT/V3',
errorPayload: {
type: ResponseType.Reject,
result: undefined
}
});
const { payload } = message;
try {
sendInMemoryMessage({
payload,
parameter: PARAMETER_TRANSACTION_BURN_NFT,
receivingMessage: 'RECEIVE_BURN_NFT/V3',
sender
});
} catch (e) {}
} else if (type === 'REQUEST_SET_ACCOUNT/V3') {
handleTransactionRequest({
payload: message.payload,
Expand Down
31 changes: 22 additions & 9 deletions packages/extension/src/components/pages/BurnNFT/BurnNFT.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { NFTokenBurn } from 'xrpl';

import {
API_ERROR_BAD_REQUEST,
BurnNFTRequest,
GEM_WALLET,
ReceiveBurnNFTBackgroundMessage,
ResponseType
} from '@gemwallet/constants';

import { STORAGE_MESSAGING_KEY } from '../../../constants';
import {
buildNFTokenBurn,
TransactionProgressStatus,
Expand All @@ -17,7 +19,7 @@ import {
useTransactionProgress,
useWallet
} from '../../../contexts';
import { useFees, useTransactionStatus } from '../../../hooks';
import { useFees, useFetchFromSessionStorage, useTransactionStatus } from '../../../hooks';
import { TransactionStatus } from '../../../types';
import { parseBaseParamsFromURLParamsNew } from '../../../utils/baseParams';
import { serializeError } from '../../../utils/errors';
Expand Down Expand Up @@ -51,6 +53,14 @@ export const BurnNFT: FC = () => {
params.transaction?.Fee
);

const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const { fetchedData } = useFetchFromSessionStorage(
urlParams.get(STORAGE_MESSAGING_KEY) ?? undefined
) as {
fetchedData: BurnNFTRequest | undefined;
};

const sendMessageToBackground = useCallback(
(message: ReceiveBurnNFTBackgroundMessage) => {
chrome.runtime.sendMessage(message);
Expand Down Expand Up @@ -107,21 +117,24 @@ export const BurnNFT: FC = () => {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const id = Number(urlParams.get('id')) || 0;

// BurnNFT fields
const NFTokenID = urlParams.get('NFTokenID');
const owner = urlParams.get('owner');
const wallet = getCurrentWallet();

if (!NFTokenID) {
if (!wallet) {
setIsParamsMissing(true);
return;
}

if (!wallet) {
setIsParamsMissing(true);
if (!fetchedData) {
return;
}

const NFTokenID = 'NFTokenID' in fetchedData ? fetchedData.NFTokenID : undefined;
const owner = 'owner' in fetchedData ? fetchedData.owner : undefined;

if (!NFTokenID) {
setIsParamsMissing(true);
}

const transaction = buildNFTokenBurn(
{
...parseBaseParamsFromURLParamsNew(urlParams),
Expand All @@ -135,7 +148,7 @@ export const BurnNFT: FC = () => {
id,
transaction
});
}, [getCurrentWallet]);
}, [fetchedData, getCurrentWallet]);

const handleReject = useCallback(() => {
setTransaction(TransactionStatus.Rejected);
Expand Down

0 comments on commit 90c9410

Please sign in to comment.