From f033cc93a5ffb4398e881a1375a1e4d26a562eb1 Mon Sep 17 00:00:00 2001 From: Juanma Hidalgo Date: Wed, 17 Apr 2024 15:38:40 +0200 Subject: [PATCH] fix: fingerprint estates issue (#2222) * fix: fingerprint estates issue * fix: tests and move useFingerprint to main component --- .../BuyNftWithCryptoModal/BuyNftWithCryptoModal.tsx | 11 +++++++---- .../Modals/BuyWithCryptoModal/hooks.spec.ts | 3 ++- .../components/Modals/BuyWithCryptoModal/hooks.ts | 10 +++++++--- .../components/Modals/BuyWithCryptoModal/utils.ts | 12 ++++++++++-- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/webapp/src/components/Modals/BuyWithCryptoModal/BuyNftWithCryptoModal/BuyNftWithCryptoModal.tsx b/webapp/src/components/Modals/BuyWithCryptoModal/BuyNftWithCryptoModal/BuyNftWithCryptoModal.tsx index d14553ac8..8f3cc5a85 100644 --- a/webapp/src/components/Modals/BuyWithCryptoModal/BuyNftWithCryptoModal/BuyNftWithCryptoModal.tsx +++ b/webapp/src/components/Modals/BuyWithCryptoModal/BuyNftWithCryptoModal/BuyNftWithCryptoModal.tsx @@ -5,6 +5,7 @@ import { AuthorizedAction } from 'decentraland-dapps/dist/containers/withAuthori import { getAnalytics } from 'decentraland-dapps/dist/modules/analytics' import { AuthorizationType } from 'decentraland-dapps/dist/modules/authorization' import { ContractName } from 'decentraland-transactions' +import { useFingerprint } from '../../../../modules/nft/hooks' import { getBuyItemStatus, getError } from '../../../../modules/order/selectors' import { getContractNames } from '../../../../modules/vendor' import { Contract as DCLContract } from '../../../../modules/vendor/services' @@ -29,6 +30,8 @@ const BuyNftWithCryptoModalHOC = (props: Props) => { metadata: { nft, order, slippage = 1 } } = props + const [fingerprint] = useFingerprint(nft) + const onBuyNatively = useCallback(() => { const contractNames = getContractNames() @@ -49,9 +52,9 @@ const BuyNftWithCryptoModalHOC = (props: Props) => { targetContract: mana as Contract, authorizedContractLabel: marketplace.label || marketplace.name, requiredAllowanceInWei: order.price, - onAuthorized: (alreadyAuthorized: boolean) => onExecuteOrder(order, nft, undefined, !alreadyAuthorized) // undefined as fingerprint + onAuthorized: (alreadyAuthorized: boolean) => onExecuteOrder(order, nft, fingerprint, !alreadyAuthorized) // undefined as fingerprint }) - }, [nft, order, getContract, onAuthorizedAction, onExecuteOrder]) + }, [nft, order, fingerprint, getContract, onAuthorizedAction, onExecuteOrder]) const onBuyWithCard = useCallback(() => { getAnalytics().track(events.CLICK_BUY_NFT_WITH_CARD) @@ -64,8 +67,8 @@ const BuyNftWithCryptoModalHOC = (props: Props) => { [order] ) const onGetGasCost: OnGetGasCost = useCallback( - (selectedToken, chainNativeToken, wallet) => useBuyNftGasCost(nft, order, selectedToken, chainNativeToken, wallet), - [nft, order] + (selectedToken, chainNativeToken, wallet) => useBuyNftGasCost(nft, order, selectedToken, chainNativeToken, wallet, fingerprint), + [nft, order, fingerprint] ) return ( diff --git a/webapp/src/components/Modals/BuyWithCryptoModal/hooks.spec.ts b/webapp/src/components/Modals/BuyWithCryptoModal/hooks.spec.ts index 02c025f33..96c653f52 100644 --- a/webapp/src/components/Modals/BuyWithCryptoModal/hooks.spec.ts +++ b/webapp/src/components/Modals/BuyWithCryptoModal/hooks.spec.ts @@ -301,7 +301,8 @@ describe('when using the buy nft gas cost hook', () => { beforeEach(() => { nft = { - id: 'aNftId' + id: 'aNftId', + data: {} } as NFT order = { id: 'anOrderId', diff --git a/webapp/src/components/Modals/BuyWithCryptoModal/hooks.ts b/webapp/src/components/Modals/BuyWithCryptoModal/hooks.ts index 68a4946f4..05358a1ab 100644 --- a/webapp/src/components/Modals/BuyWithCryptoModal/hooks.ts +++ b/webapp/src/components/Modals/BuyWithCryptoModal/hooks.ts @@ -168,13 +168,17 @@ export const useBuyNftGasCost = ( order: Order, selectedToken: Token, chainNativeToken: Token | undefined, - wallet: Wallet | null + wallet: Wallet | null, + fingerprint?: string ): GasCost => { const chainId = parseInt(selectedToken.chainId) as ChainId const estimateGas = useCallback( - () => (wallet ? estimateBuyNftGas(chainId, wallet, nft, order) : Promise.resolve(undefined)), - [chainId, wallet, order] + () => + wallet && (!nft.data.estate || (!!nft.data.estate && !!fingerprint)) + ? estimateBuyNftGas(chainId, wallet, nft, order, fingerprint) + : Promise.resolve(undefined), + [chainId, wallet, order, nft, fingerprint] ) const shouldUseCrossChainProvider = useShouldUseCrossChainProvider(selectedToken, order.network) diff --git a/webapp/src/components/Modals/BuyWithCryptoModal/utils.ts b/webapp/src/components/Modals/BuyWithCryptoModal/utils.ts index 6ec655fc8..5bf0f986c 100644 --- a/webapp/src/components/Modals/BuyWithCryptoModal/utils.ts +++ b/webapp/src/components/Modals/BuyWithCryptoModal/utils.ts @@ -193,14 +193,22 @@ export const estimateMintNftGas = async (selectedChain: ChainId, wallet: Wallet, return c.estimateGas.buy([[asset.contractAddress, [asset.itemId], [asset.price], [wallet.address]]], { from: wallet.address }) } -export const estimateBuyNftGas = async (selectedChain: ChainId, wallet: Wallet, asset: NFT, order: Order): Promise => { +export const estimateBuyNftGas = async ( + selectedChain: ChainId, + wallet: Wallet, + asset: NFT, + order: Order, + fingerprint?: string +): Promise => { const networkProvider = await getNetworkProvider(selectedChain) const provider = new ethers.providers.Web3Provider(networkProvider) const contractName = getContractName(order.marketplaceAddress) const contract = getContract(contractName, order.chainId) const c = new ethers.Contract(contract.address, contract.abi, provider) - return c.estimateGas.executeOrder(asset.contractAddress, asset.tokenId, order.price, { from: wallet.address }) + return fingerprint + ? c.estimateGas.safeExecuteOrder(asset.contractAddress, asset.tokenId, order.price, fingerprint, { from: wallet.address }) + : c.estimateGas.executeOrder(asset.contractAddress, asset.tokenId, order.price, { from: wallet.address }) } export const estimateNameMintingGas = async (name: string, selectedChain: ChainId, ownerAddress: string): Promise => {