Skip to content

Commit

Permalink
fix: fingerprint estates issue (#2222)
Browse files Browse the repository at this point in the history
* fix: fingerprint estates issue

* fix: tests and move useFingerprint to main component
  • Loading branch information
juanmahidalgo committed Apr 17, 2024
1 parent e86503e commit f033cc9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -29,6 +30,8 @@ const BuyNftWithCryptoModalHOC = (props: Props) => {
metadata: { nft, order, slippage = 1 }
} = props

const [fingerprint] = useFingerprint(nft)

const onBuyNatively = useCallback(() => {
const contractNames = getContractNames()

Expand All @@ -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)
Expand All @@ -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 (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 7 additions & 3 deletions webapp/src/components/Modals/BuyWithCryptoModal/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 10 additions & 2 deletions webapp/src/components/Modals/BuyWithCryptoModal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BigNumber> => {
export const estimateBuyNftGas = async (
selectedChain: ChainId,
wallet: Wallet,
asset: NFT,
order: Order,
fingerprint?: string
): Promise<BigNumber> => {
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<BigNumber> => {
Expand Down

0 comments on commit f033cc9

Please sign in to comment.