Skip to content

Commit

Permalink
ERC721 utility WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
SamueleA committed Dec 5, 2024
1 parent 6868a12 commit da9730b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
23 changes: 17 additions & 6 deletions packages/checkout/src/hooks/useSaleContractCheckout.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CheckoutOptionsSalesContractArgs } from '@0xsequence/marketplace'
import { useContractInfo } from '@0xsequence/kit'
import { findSupportedNetwork } from '@0xsequence/network'

import { useSelectPaymentModal } from './useSelectPaymentModal'
import { useCheckoutOptionsSalesContract } from "./useCheckoutOptionsSalesContract"
import { ERC_1155_SALE_CONTRACT, ERC_721_SALE_CONTRACT } from '../constants/abi'

Expand Down Expand Up @@ -44,6 +44,14 @@ export const useSaleContractCheckout = ({
const isLoading = isLoadingCheckoutOptions || isLoadingSaleConfig
const error = isErrorCheckoutOtions || isErrorSaleConfig


const openCheckoutModal = () => {
if (isLoading || error) return

// return openCheckoutModal({
// })
}

return ({
data: undefined,
isLoading,
Expand Down Expand Up @@ -135,8 +143,6 @@ export const useSaleContractConfig = ({ chainId, tokenType, contractAddress, tok
const isErrorERC1155 = isErrorPaymentTokenERC1155 || isErrorGlobalSaleDetailsERC1155 || isErrorTokenSaleDetailsERC1155
const isErrorERC721 = isErrorSaleDetailsERC721

let saleInfos: SaleConfig[] = []

if (isLoadingERC1155 || isLoadingERC721 || isErrorERC1155 || isErrorERC721) {
return ({
data: undefined,
Expand All @@ -145,8 +151,11 @@ export const useSaleContractConfig = ({ chainId, tokenType, contractAddress, tok
})
}

const getPrice = () => {
if (isLoadingERC1155 || isLoadingERC721 || isErrorERC1155 || isErrorERC721) return
const getSaleConfigs = (): SaleConfig[] => {
let saleInfos: SaleConfig[] = []

if (isLoadingERC1155 || isLoadingERC721 || isErrorERC1155 || isErrorERC721) return saleInfos

if (tokenType === 'ERC1155') {
// In the sale contract, the global sale has priority over the token sale
// So we need to check if the global sale is set, and if it is, use that
Expand All @@ -169,12 +178,14 @@ export const useSaleContractConfig = ({ chainId, tokenType, contractAddress, tok
})
})
}

return saleInfos
}

return ({
data: {
currencyAddress: tokenType === 'ERC1155' ? (paymentTokenERC1155 as string) : (saleDetailsERC721 as SaleDetailsERC721)[2],
saleConfigs: saleInfos
saleConfigs: getSaleConfigs()
},
isLoading: tokenType === 'ERC1155' ? isLoadingERC1155 : isLoadingERC721,
isError: tokenType === 'ERC1155' ? isErrorERC1155 : isErrorERC721
Expand Down
67 changes: 64 additions & 3 deletions packages/checkout/src/hooks/useSelectPaymentModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const useSelectPaymentModal = () => {
return { openSelectPaymentModal, closeSelectPaymentModal, selectPaymentSettings }
}

type ERC1155SaleContractSettings = Omit<SelectPaymentSettings, 'txData'>
type SaleContractSettings = Omit<SelectPaymentSettings, 'txData'>

export const getERC1155SaleContractConfig = ({
chain,
Expand All @@ -21,7 +21,7 @@ export const getERC1155SaleContractConfig = ({
collectionAddress,
isDev = false,
...restProps
}: ERC1155SaleContractSettings): SelectPaymentSettings => {
}: SaleContractSettings): SelectPaymentSettings => {
const erc1155SalesContractAbi = [
{
type: 'function',
Expand Down Expand Up @@ -61,7 +61,7 @@ export const getERC1155SaleContractConfig = ({

export const useERC1155SaleContractPaymentModal = () => {
const { openSelectPaymentModal, closeSelectPaymentModal, selectPaymentSettings } = useSelectPaymentModal()
const openERC1155SaleContractPaymentModal = (saleContractSettings: ERC1155SaleContractSettings) => {
const openERC1155SaleContractPaymentModal = (saleContractSettings: SaleContractSettings) => {
openSelectPaymentModal(getERC1155SaleContractConfig(saleContractSettings))
}

Expand All @@ -71,3 +71,64 @@ export const useERC1155SaleContractPaymentModal = () => {
selectPaymentSettings
}
}

// TODO: ERC721 utility
// export const getERC1155SaleContractConfig = ({
// chain,
// price,
// currencyAddress = ethers.ZeroAddress,
// recipientAddress,
// collectibles,
// collectionAddress,
// isDev = false,
// ...restProps
// }: SaleContractSettings): SelectPaymentSettings => {
// const erc1155SalesContractAbi = [
// {
// type: 'function',
// name: 'mint',
// inputs: [
// { name: 'to', type: 'address', internalType: 'address' },
// { name: 'tokenIds', type: 'uint256[]', internalType: 'uint256[]' },
// { name: 'amounts', type: 'uint256[]', internalType: 'uint256[]' },
// { name: 'data', type: 'bytes', internalType: 'bytes' },
// { name: 'expectedPaymentToken', type: 'address', internalType: 'address' },
// { name: 'maxTotal', type: 'uint256', internalType: 'uint256' },
// { name: 'proof', type: 'bytes32[]', internalType: 'bytes32[]' }
// ],
// outputs: [],
// stateMutability: 'payable'
// }
// ]

// const purchaseTransactionData = encodeFunctionData({
// abi: erc1155SalesContractAbi,
// functionName: 'mint',
// args: [recipientAddress, collectibles.map(c => BigInt(c.tokenId)), collectibles.map(c => BigInt(c.quantity)), toHex(0), currencyAddress, price, [toHex(0, { size: 32 })]]
// })

// return {
// chain,
// price,
// currencyAddress,
// recipientAddress,
// collectibles,
// collectionAddress,
// isDev,
// txData: purchaseTransactionData,
// ...restProps
// }
// }

export const useERC721SaleContractPaymentModal = () => {
const { openSelectPaymentModal, closeSelectPaymentModal, selectPaymentSettings } = useSelectPaymentModal()
const openERC721SaleContractPaymentModal = (saleContractSettings: SaleContractSettings) => {
openSelectPaymentModal(getERC721SaleContractConfig(saleContractSettings))
}

return {
openERC721SaleContractPaymentModal,
closeERC721SaleContractPaymentModal: closeSelectPaymentModal,
selectPaymentSettings
}
}

0 comments on commit da9730b

Please sign in to comment.