From 0f7baadae2e1a887c2a59964494aa71474a0a9ec Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Sun, 21 Jan 2024 11:37:38 -0300 Subject: [PATCH 1/8] I add package exports for getNFTsForContract, getFormattedDrops, getCalldatas & ZORA_FEE. --- .changeset/unlucky-dolls-hear.md | 5 +++++ hooks/useCollection.ts | 7 ++++++- index.ts | 23 +++++++++++++++++------ 3 files changed, 28 insertions(+), 7 deletions(-) create mode 100644 .changeset/unlucky-dolls-hear.md diff --git a/.changeset/unlucky-dolls-hear.md b/.changeset/unlucky-dolls-hear.md new file mode 100644 index 0000000..1d5aa64 --- /dev/null +++ b/.changeset/unlucky-dolls-hear.md @@ -0,0 +1,5 @@ +--- +"onchain-magic": minor +--- + +I add package exports for getNFTsForContract, getFormattedDrops, getCalldatas & ZORA_FEE. diff --git a/hooks/useCollection.ts b/hooks/useCollection.ts index 9f87a91..8ecfc18 100644 --- a/hooks/useCollection.ts +++ b/hooks/useCollection.ts @@ -33,23 +33,28 @@ const useCollection = (collectionAddress: string, chainId: number) => { return values; }; - const collectAll = async (minter = defaultMinter) => { + const collectAll = async (minter: string = defaultMinter) => { if (chain?.id !== chainId) { switchNetwork?.(chainId); return false; } const targets = Array(drops.length).fill(collectionAddress); + console.log("SWEETS TARGETS", targets) const calldatas = getCalldatas( drops.length, minter, address as string, address as string ); + console.log("SWEETS calldatas", calldatas) const values = await getValues(); + console.log("SWEETS values", values) const totalValue = values.reduce( (total, value) => total.add(BigNumber.from(value)), BigNumber.from(0) ); + console.log("SWEETS totalValue", totalValue) + const response = await mintBatchWithoutFees( targets, calldatas, diff --git a/index.ts b/index.ts index 59f0b19..bd82443 100644 --- a/index.ts +++ b/index.ts @@ -7,12 +7,11 @@ import useZoraFixedPriceSaleStrategy from "./hooks/useZoraFixedPriceSaleStrategy import getEncodedMinterArgs from "./lib/zora/getEncodedMinterArgs"; import useUniversalMinter from "./hooks/useUniversalMinter"; import useCollection from "./hooks/useCollection"; - +import getNFTsForContract from "./lib/alchemy/getNFTsForContract"; +import getFormattedDrops from "./lib/getFormattedDrops"; +import getCalldatas from "./lib/getCalldatas"; +import { ZORA_FEE } from "./lib/consts"; export { - // IPFS - store, - uploadToIpfs, - // ZORA type Create1155ContractArgs, getEncodedMinterArgs, @@ -21,8 +20,20 @@ export { useCreate1155Contract, useZoraFixedPriceSaleStrategy, useUniversalMinter, + ZORA_FEE, // ETHERS - useEthersSigner + useEthersSigner, + + // ALCHEMY + getNFTsForContract, + + // IPFS + store, + uploadToIpfs, + + // Misc. + getCalldatas, + getFormattedDrops }; From b3b532e61e400fce10c3286347f413297d9850cf Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Sun, 21 Jan 2024 12:03:25 -0300 Subject: [PATCH 2/8] I add priceValues export from a useEffect to speed up call to collectAll by gathering price info in the background. --- .changeset/unlucky-dolls-hear.md | 5 -- CHANGELOG.md | 18 ++++++ hooks/useCollection.ts | 108 +++++++++++++++---------------- hooks/useUniversalMinter.ts | 2 +- package.json | 2 +- 5 files changed, 71 insertions(+), 64 deletions(-) delete mode 100644 .changeset/unlucky-dolls-hear.md diff --git a/.changeset/unlucky-dolls-hear.md b/.changeset/unlucky-dolls-hear.md deleted file mode 100644 index 1d5aa64..0000000 --- a/.changeset/unlucky-dolls-hear.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -"onchain-magic": minor ---- - -I add package exports for getNFTsForContract, getFormattedDrops, getCalldatas & ZORA_FEE. diff --git a/CHANGELOG.md b/CHANGELOG.md index df0fe78..b78a9e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,23 @@ # onchain-magic +## 0.4.2 + +### Patch Changes + +- I add universalMinter to useUniversalMinter export." + +## 0.4.1 + +### Patch Changes + +- I build. + +## 0.4.0 + +### Minor Changes + +- 0f7baad: I add package exports for getNFTsForContract, getFormattedDrops, getCalldatas & ZORA_FEE. + ## 0.3.1 ### Patch Changes diff --git a/hooks/useCollection.ts b/hooks/useCollection.ts index 8ecfc18..c998c7b 100644 --- a/hooks/useCollection.ts +++ b/hooks/useCollection.ts @@ -9,72 +9,66 @@ import getCalldatas from "../lib/getCalldatas"; import { ZORA_FEE } from "../lib/consts"; import { useZoraFixedPriceSaleStrategy } from ".."; -const useCollection = (collectionAddress: string, chainId: number) => { - const [drops, setDrops] = useState([] as any); - const { mintBatchWithoutFees } = useUniversalMinter(chainId); - const { address } = useAccount(); - const { chain } = useNetwork(); - const defaultMinter = +type UseCollectionParams = { + collectionAddress: string + chainId: number + minterOverride?: string +} + +const useCollection = ({ collectionAddress, chainId, minterOverride }: UseCollectionParams) => { + const [drops, setDrops] = useState([] as any) + const [priceValues, setPriceValues] = useState([] as string[]) + const { mintBatchWithoutFees, universalMinter } = useUniversalMinter(chainId) + const { address } = useAccount() + const { chain } = useNetwork() + const minter = + minterOverride || zoraCreatorFixedPriceSaleStrategyAddress[ chainId as keyof typeof zoraCreatorFixedPriceSaleStrategyAddress - ]; - const { sale } = useZoraFixedPriceSaleStrategy(defaultMinter); - const { switchNetwork } = useSwitchNetwork(); - - const getValues = async () => { - const pricesPromises = drops.map((_: any, index: number) => { - const tokenId = BigNumber.from(index + 1); - return sale(collectionAddress, tokenId.toString()); - }); - const prices = await Promise.all(pricesPromises); - const values = prices.map((price) => - price.pricePerToken.add(ZORA_FEE).toString() - ); - return values; - }; + ] + const { sale } = useZoraFixedPriceSaleStrategy(minter) + const { switchNetwork } = useSwitchNetwork() - const collectAll = async (minter: string = defaultMinter) => { + const collectAll = async () => { if (chain?.id !== chainId) { - switchNetwork?.(chainId); - return false; + switchNetwork?.(chainId) + return false } - const targets = Array(drops.length).fill(collectionAddress); - console.log("SWEETS TARGETS", targets) - const calldatas = getCalldatas( - drops.length, - minter, - address as string, - address as string - ); - console.log("SWEETS calldatas", calldatas) - const values = await getValues(); - console.log("SWEETS values", values) - const totalValue = values.reduce( + const targets = Array(drops.length).fill(collectionAddress) + const calldatas = getCalldatas(drops.length, minter, address as string, address as string) + const totalValue = priceValues.reduce( (total, value) => total.add(BigNumber.from(value)), - BigNumber.from(0) - ); - console.log("SWEETS totalValue", totalValue) - - const response = await mintBatchWithoutFees( - targets, - calldatas, - values, - totalValue - ); - return response; - }; + BigNumber.from(0), + ) + const response = await mintBatchWithoutFees(targets, calldatas, priceValues, totalValue) + return response + } useEffect(() => { const init = async () => { - const response = await getNFTsForContract(collectionAddress, chainId); - const formattedDrops = getFormattedDrops(response.nfts, chainId); - setDrops(formattedDrops); - }; + const response = await getNFTsForContract(collectionAddress, chainId) + const formattedDrops = getFormattedDrops(response.nfts, chainId) + setDrops(formattedDrops) + } + + init() + }, [collectionAddress, chainId]) + + useEffect(() => { + const getValues = async () => { + const pricesPromises = drops.map((_: any, index: number) => { + const tokenId = BigNumber.from(index + 1) + return sale(collectionAddress, tokenId.toString()) + }) + const prices = await Promise.all(pricesPromises) + const values = prices.map((price) => price.pricePerToken.add(ZORA_FEE).toString()) + setPriceValues(values) + } + getValues() + }, [collectionAddress, sale, drops]) - init(); - }, [collectionAddress, chainId]); + return { drops, collectAll, priceValues } +} - return { drops, collectAll }; -}; +export default useCollection -export default useCollection; diff --git a/hooks/useUniversalMinter.ts b/hooks/useUniversalMinter.ts index b5b4f91..14c3adf 100644 --- a/hooks/useUniversalMinter.ts +++ b/hooks/useUniversalMinter.ts @@ -39,7 +39,7 @@ const useUniversalMinter = (chainId: number = base.id) => { } }; - return { mintBatchWithoutFees }; + return { mintBatchWithoutFees, universalMinter }; }; export default useUniversalMinter; diff --git a/package.json b/package.json index abe5424..c0c09da 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "onchain-magic", - "version": "0.3.1", + "version": "0.4.2", "private": false, "type": "module", "peerDependencies": { From 87c1b00cfa2d1b64f3d14ca3061ae2378a6c8d6f Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Sun, 21 Jan 2024 12:04:58 -0300 Subject: [PATCH 3/8] built. --- CHANGELOG.md | 6 +++ hooks/useCollection.ts | 97 ++++++++++++++++++++++++------------------ package.json | 2 +- 3 files changed, 63 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b78a9e6..1938e63 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # onchain-magic +## 0.4.3 + +### Patch Changes + +- I add priceValues export from a useEffect to speed up call to collectAll by gathering price info in the background. + ## 0.4.2 ### Patch Changes diff --git a/hooks/useCollection.ts b/hooks/useCollection.ts index c998c7b..2baf839 100644 --- a/hooks/useCollection.ts +++ b/hooks/useCollection.ts @@ -10,65 +10,80 @@ import { ZORA_FEE } from "../lib/consts"; import { useZoraFixedPriceSaleStrategy } from ".."; type UseCollectionParams = { - collectionAddress: string - chainId: number - minterOverride?: string -} + collectionAddress: string; + chainId: number; + minterOverride?: string; +}; -const useCollection = ({ collectionAddress, chainId, minterOverride }: UseCollectionParams) => { - const [drops, setDrops] = useState([] as any) - const [priceValues, setPriceValues] = useState([] as string[]) - const { mintBatchWithoutFees, universalMinter } = useUniversalMinter(chainId) - const { address } = useAccount() - const { chain } = useNetwork() +const useCollection = ({ + collectionAddress, + chainId, + minterOverride, +}: UseCollectionParams) => { + const [drops, setDrops] = useState([] as any); + const [priceValues, setPriceValues] = useState([] as string[]); + const { mintBatchWithoutFees, universalMinter } = useUniversalMinter(chainId); + const { address } = useAccount(); + const { chain } = useNetwork(); const minter = minterOverride || zoraCreatorFixedPriceSaleStrategyAddress[ chainId as keyof typeof zoraCreatorFixedPriceSaleStrategyAddress - ] - const { sale } = useZoraFixedPriceSaleStrategy(minter) - const { switchNetwork } = useSwitchNetwork() + ]; + const { sale } = useZoraFixedPriceSaleStrategy(minter); + const { switchNetwork } = useSwitchNetwork(); const collectAll = async () => { if (chain?.id !== chainId) { - switchNetwork?.(chainId) - return false + switchNetwork?.(chainId); + return false; } - const targets = Array(drops.length).fill(collectionAddress) - const calldatas = getCalldatas(drops.length, minter, address as string, address as string) + const targets = Array(drops.length).fill(collectionAddress); + const calldatas = getCalldatas( + drops.length, + minter, + address as string, + address as string + ); const totalValue = priceValues.reduce( (total, value) => total.add(BigNumber.from(value)), - BigNumber.from(0), - ) - const response = await mintBatchWithoutFees(targets, calldatas, priceValues, totalValue) - return response - } + BigNumber.from(0) + ); + const response = await mintBatchWithoutFees( + targets, + calldatas, + priceValues, + totalValue + ); + return response; + }; useEffect(() => { const init = async () => { - const response = await getNFTsForContract(collectionAddress, chainId) - const formattedDrops = getFormattedDrops(response.nfts, chainId) - setDrops(formattedDrops) - } + const response = await getNFTsForContract(collectionAddress, chainId); + const formattedDrops = getFormattedDrops(response.nfts, chainId); + setDrops(formattedDrops); + }; - init() - }, [collectionAddress, chainId]) + init(); + }, [collectionAddress, chainId]); useEffect(() => { const getValues = async () => { const pricesPromises = drops.map((_: any, index: number) => { - const tokenId = BigNumber.from(index + 1) - return sale(collectionAddress, tokenId.toString()) - }) - const prices = await Promise.all(pricesPromises) - const values = prices.map((price) => price.pricePerToken.add(ZORA_FEE).toString()) - setPriceValues(values) - } - getValues() - }, [collectionAddress, sale, drops]) - - return { drops, collectAll, priceValues } -} + const tokenId = BigNumber.from(index + 1); + return sale(collectionAddress, tokenId.toString()); + }); + const prices = await Promise.all(pricesPromises); + const values = prices.map((price) => + price.pricePerToken.add(ZORA_FEE).toString() + ); + setPriceValues(values); + }; + getValues(); + }, [collectionAddress, sale, drops]); -export default useCollection + return { drops, collectAll, priceValues }; +}; +export default useCollection; diff --git a/package.json b/package.json index c0c09da..4039865 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "onchain-magic", - "version": "0.4.2", + "version": "0.4.3", "private": false, "type": "module", "peerDependencies": { From e437fcb2aa32264c152d55f2d1df29e447d2f704 Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Sun, 21 Jan 2024 12:20:28 -0300 Subject: [PATCH 4/8] I update useEffect dependency array to be more efficient. remove infinite loops. --- hooks/useCollection.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/hooks/useCollection.ts b/hooks/useCollection.ts index 2baf839..d2c1ec6 100644 --- a/hooks/useCollection.ts +++ b/hooks/useCollection.ts @@ -22,7 +22,7 @@ const useCollection = ({ }: UseCollectionParams) => { const [drops, setDrops] = useState([] as any); const [priceValues, setPriceValues] = useState([] as string[]); - const { mintBatchWithoutFees, universalMinter } = useUniversalMinter(chainId); + const { mintBatchWithoutFees } = useUniversalMinter(chainId); const { address } = useAccount(); const { chain } = useNetwork(); const minter = @@ -70,6 +70,7 @@ const useCollection = ({ useEffect(() => { const getValues = async () => { + if (drops.length === 0) return; const pricesPromises = drops.map((_: any, index: number) => { const tokenId = BigNumber.from(index + 1); return sale(collectionAddress, tokenId.toString()); @@ -80,8 +81,10 @@ const useCollection = ({ ); setPriceValues(values); }; + getValues(); - }, [collectionAddress, sale, drops]); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [minter, drops]); return { drops, collectAll, priceValues }; }; From 684d7c05fd6febc5d9b7819d0e6e14e7a82b3be0 Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Sun, 21 Jan 2024 12:27:03 -0300 Subject: [PATCH 5/8] v0.4.4 --- CHANGELOG.md | 6 ++++++ package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1938e63..f7742ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # onchain-magic +## 0.4.4 + +### Patch Changes + +- I update useEffect dependency array to be more efficient. remove infinite loops. + ## 0.4.3 ### Patch Changes diff --git a/package.json b/package.json index 4039865..0834f3d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "onchain-magic", - "version": "0.4.3", + "version": "0.4.4", "private": false, "type": "module", "peerDependencies": { From 210ad85d76010dd305c2f4a1fb0e71bd74a471da Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Sun, 21 Jan 2024 16:45:24 -0300 Subject: [PATCH 6/8] I migrate retrieval of price information from usecollection => useZoraFixedPriceSaleStrategy for more efficient useEffect without infinite loops. --- hooks/useCollection.ts | 90 ++++++++------------------ hooks/useZoraFixedPriceSaleStrategy.ts | 55 +++++++++++----- 2 files changed, 68 insertions(+), 77 deletions(-) diff --git a/hooks/useCollection.ts b/hooks/useCollection.ts index d2c1ec6..fcfb56d 100644 --- a/hooks/useCollection.ts +++ b/hooks/useCollection.ts @@ -6,8 +6,7 @@ import getNFTsForContract from "../lib/alchemy/getNFTsForContract"; import getFormattedDrops from "../lib/getFormattedDrops"; import useUniversalMinter from "./useUniversalMinter"; import getCalldatas from "../lib/getCalldatas"; -import { ZORA_FEE } from "../lib/consts"; -import { useZoraFixedPriceSaleStrategy } from ".."; +import useZoraFixedPriceSaleStrategy from "./useZoraFixedPriceSaleStrategy"; type UseCollectionParams = { collectionAddress: string; @@ -15,78 +14,45 @@ type UseCollectionParams = { minterOverride?: string; }; -const useCollection = ({ - collectionAddress, - chainId, - minterOverride, -}: UseCollectionParams) => { - const [drops, setDrops] = useState([] as any); - const [priceValues, setPriceValues] = useState([] as string[]); - const { mintBatchWithoutFees } = useUniversalMinter(chainId); - const { address } = useAccount(); - const { chain } = useNetwork(); +const useCollection = ({ collectionAddress, chainId, minterOverride }: UseCollectionParams) => { + const [drops, setDrops] = useState([] as any) + const { mintBatchWithoutFees } = useUniversalMinter(chainId) + const { address } = useAccount() + const { chain } = useNetwork() const minter = minterOverride || zoraCreatorFixedPriceSaleStrategyAddress[ chainId as keyof typeof zoraCreatorFixedPriceSaleStrategyAddress - ]; - const { sale } = useZoraFixedPriceSaleStrategy(minter); - const { switchNetwork } = useSwitchNetwork(); + ] + const { priceValues } = useZoraFixedPriceSaleStrategy({ saleConfig: minter, drops }) + const { switchNetwork } = useSwitchNetwork() const collectAll = async () => { if (chain?.id !== chainId) { - switchNetwork?.(chainId); - return false; + switchNetwork?.(chainId) + return false } - const targets = Array(drops.length).fill(collectionAddress); - const calldatas = getCalldatas( - drops.length, - minter, - address as string, - address as string - ); + const targets = Array(drops.length).fill(collectionAddress) + const calldatas = getCalldatas(drops.length, minter, address as string, address as string) const totalValue = priceValues.reduce( - (total, value) => total.add(BigNumber.from(value)), - BigNumber.from(0) - ); - const response = await mintBatchWithoutFees( - targets, - calldatas, - priceValues, - totalValue - ); - return response; - }; + (total: any, value: any) => total.add(BigNumber.from(value)), + BigNumber.from(0), + ) + const response = await mintBatchWithoutFees(targets, calldatas, priceValues, totalValue) + return response + } useEffect(() => { const init = async () => { - const response = await getNFTsForContract(collectionAddress, chainId); - const formattedDrops = getFormattedDrops(response.nfts, chainId); - setDrops(formattedDrops); - }; - - init(); - }, [collectionAddress, chainId]); - - useEffect(() => { - const getValues = async () => { - if (drops.length === 0) return; - const pricesPromises = drops.map((_: any, index: number) => { - const tokenId = BigNumber.from(index + 1); - return sale(collectionAddress, tokenId.toString()); - }); - const prices = await Promise.all(pricesPromises); - const values = prices.map((price) => - price.pricePerToken.add(ZORA_FEE).toString() - ); - setPriceValues(values); - }; + const response = await getNFTsForContract(collectionAddress, chainId) + const formattedDrops = getFormattedDrops(response.nfts, chainId) + setDrops(formattedDrops) + } - getValues(); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [minter, drops]); + init() + }, [collectionAddress, chainId]) - return { drops, collectAll, priceValues }; -}; + return { drops, collectAll, priceValues } +} -export default useCollection; +export default useCollection diff --git a/hooks/useZoraFixedPriceSaleStrategy.ts b/hooks/useZoraFixedPriceSaleStrategy.ts index 92c124e..3bdac51 100644 --- a/hooks/useZoraFixedPriceSaleStrategy.ts +++ b/hooks/useZoraFixedPriceSaleStrategy.ts @@ -1,25 +1,50 @@ import { Contract } from "ethers"; -import { useMemo } from "react"; +import { useCallback, useEffect, useMemo, useState } from "react"; import { useEthersSigner } from "./useEthersSigner"; import abi from "../lib/abi/ZoraCreatorFixedPriceSaleStrategy.json"; +import { ZORA_FEE } from "../lib/consts"; -const useZoraFixedPriceSaleStrategy = (saleConfig: string) => { - const signer = useEthersSigner(); +type UseZoraFixedPriceSaleStrategyParams = { + saleConfig: string + drops: any[] +} + +const useZoraFixedPriceSaleStrategy = ({ + saleConfig, + drops, +}: UseZoraFixedPriceSaleStrategyParams) => { + const [priceValues, setPriceValues] = useState([] as string[]) + const signer = useEthersSigner() const saleConfigContract = useMemo( () => new Contract(saleConfig, abi, signer), - [saleConfig, signer] - ); + [saleConfig, signer], + ) + + const sale = useCallback( + async (tokenContract: string, tokenId: string) => { + try { + const response = await saleConfigContract.sale(tokenContract, tokenId) + return response + } catch (error) { + return error + } + }, + [saleConfigContract], + ) - const sale = async (tokenContract: string, tokenId: string) => { - try { - const response = await saleConfigContract.sale(tokenContract, tokenId); - return response; - } catch (error) { - return error; + useEffect(() => { + const getValues = async () => { + if (drops.length === 0) return + const pricesPromises = drops.map((drop: any) => sale(drop.contractAddress, drop.tokenId)) + const prices = await Promise.all(pricesPromises) + const values = prices.map((price) => price.pricePerToken.add(ZORA_FEE).toString()) + setPriceValues(values) } - }; - return { sale }; -}; + getValues() + }, [drops, sale]) + + return { sale, priceValues } +} -export default useZoraFixedPriceSaleStrategy; +export default useZoraFixedPriceSaleStrategy \ No newline at end of file From dd90271d793a13c104b2f2f2772dd2ebf368f6bc Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Sun, 21 Jan 2024 16:45:24 -0300 Subject: [PATCH 7/8] I migrate retrieval of price information from usecollection => useZoraFixedPriceSaleStrategy for more efficient useEffect without infinite loops. --- hooks/use1155Collect.ts | 7 ++-- hooks/useCollection.ts | 29 +++------------ hooks/useZoraFixedPriceSaleStrategy.ts | 51 ++++++++++++++++++++------ 3 files changed, 49 insertions(+), 38 deletions(-) diff --git a/hooks/use1155Collect.ts b/hooks/use1155Collect.ts index e42d122..93e71ed 100644 --- a/hooks/use1155Collect.ts +++ b/hooks/use1155Collect.ts @@ -2,7 +2,7 @@ import { useMemo } from "react"; import { BigNumber, Contract, utils } from "ethers"; import { useEthersSigner } from "./useEthersSigner"; import abi from "../lib/abi/Zora1155Drop.json"; -import { useZoraFixedPriceSaleStrategy } from ".."; +import { ZORA_FEE, useZoraFixedPriceSaleStrategy } from ".."; import getEncodedMinterArgs from "../lib/zora/getEncodedMinterArgs"; const use1155Collect = (zora1155Drop: string, minterAddress: string) => { @@ -11,7 +11,7 @@ const use1155Collect = (zora1155Drop: string, minterAddress: string) => { () => new Contract(zora1155Drop, abi, signer), [zora1155Drop, signer] ); - const { sale } = useZoraFixedPriceSaleStrategy(minterAddress); + const { sale } = useZoraFixedPriceSaleStrategy({ saleConfig: minterAddress }); const mintWithRewards = async ( tokenId: string, @@ -20,9 +20,8 @@ const use1155Collect = (zora1155Drop: string, minterAddress: string) => { comment = "🪄🪄🪄" ) => { const response = await sale(zora1155Drop, "1"); - const zoraFee = BigNumber.from("777000000000000"); const value = BigNumber.from(response.pricePerToken.toString()).add( - zoraFee + ZORA_FEE ); const minterArguments = getEncodedMinterArgs(to, comment); const tx = await zora1155DropContract.mintWithRewards( diff --git a/hooks/useCollection.ts b/hooks/useCollection.ts index d2c1ec6..d2e7c7d 100644 --- a/hooks/useCollection.ts +++ b/hooks/useCollection.ts @@ -6,8 +6,7 @@ import getNFTsForContract from "../lib/alchemy/getNFTsForContract"; import getFormattedDrops from "../lib/getFormattedDrops"; import useUniversalMinter from "./useUniversalMinter"; import getCalldatas from "../lib/getCalldatas"; -import { ZORA_FEE } from "../lib/consts"; -import { useZoraFixedPriceSaleStrategy } from ".."; +import useZoraFixedPriceSaleStrategy from "./useZoraFixedPriceSaleStrategy"; type UseCollectionParams = { collectionAddress: string; @@ -21,7 +20,6 @@ const useCollection = ({ minterOverride, }: UseCollectionParams) => { const [drops, setDrops] = useState([] as any); - const [priceValues, setPriceValues] = useState([] as string[]); const { mintBatchWithoutFees } = useUniversalMinter(chainId); const { address } = useAccount(); const { chain } = useNetwork(); @@ -30,7 +28,10 @@ const useCollection = ({ zoraCreatorFixedPriceSaleStrategyAddress[ chainId as keyof typeof zoraCreatorFixedPriceSaleStrategyAddress ]; - const { sale } = useZoraFixedPriceSaleStrategy(minter); + const { priceValues } = useZoraFixedPriceSaleStrategy({ + saleConfig: minter, + drops, + }); const { switchNetwork } = useSwitchNetwork(); const collectAll = async () => { @@ -46,7 +47,7 @@ const useCollection = ({ address as string ); const totalValue = priceValues.reduce( - (total, value) => total.add(BigNumber.from(value)), + (total: any, value: any) => total.add(BigNumber.from(value)), BigNumber.from(0) ); const response = await mintBatchWithoutFees( @@ -68,24 +69,6 @@ const useCollection = ({ init(); }, [collectionAddress, chainId]); - useEffect(() => { - const getValues = async () => { - if (drops.length === 0) return; - const pricesPromises = drops.map((_: any, index: number) => { - const tokenId = BigNumber.from(index + 1); - return sale(collectionAddress, tokenId.toString()); - }); - const prices = await Promise.all(pricesPromises); - const values = prices.map((price) => - price.pricePerToken.add(ZORA_FEE).toString() - ); - setPriceValues(values); - }; - - getValues(); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, [minter, drops]); - return { drops, collectAll, priceValues }; }; diff --git a/hooks/useZoraFixedPriceSaleStrategy.ts b/hooks/useZoraFixedPriceSaleStrategy.ts index 92c124e..e5daf3f 100644 --- a/hooks/useZoraFixedPriceSaleStrategy.ts +++ b/hooks/useZoraFixedPriceSaleStrategy.ts @@ -1,25 +1,54 @@ import { Contract } from "ethers"; -import { useMemo } from "react"; +import { useCallback, useEffect, useMemo, useState } from "react"; import { useEthersSigner } from "./useEthersSigner"; import abi from "../lib/abi/ZoraCreatorFixedPriceSaleStrategy.json"; +import { ZORA_FEE } from "../lib/consts"; -const useZoraFixedPriceSaleStrategy = (saleConfig: string) => { +type UseZoraFixedPriceSaleStrategyParams = { + saleConfig: string; + drops?: any[]; +}; + +const useZoraFixedPriceSaleStrategy = ({ + saleConfig, + drops = [], +}: UseZoraFixedPriceSaleStrategyParams) => { + const [priceValues, setPriceValues] = useState([] as string[]); const signer = useEthersSigner(); const saleConfigContract = useMemo( () => new Contract(saleConfig, abi, signer), [saleConfig, signer] ); - const sale = async (tokenContract: string, tokenId: string) => { - try { - const response = await saleConfigContract.sale(tokenContract, tokenId); - return response; - } catch (error) { - return error; - } - }; + const sale = useCallback( + async (tokenContract: string, tokenId: string) => { + try { + const response = await saleConfigContract.sale(tokenContract, tokenId); + return response; + } catch (error) { + return error; + } + }, + [saleConfigContract] + ); + + useEffect(() => { + const getValues = async () => { + if (drops.length === 0) return; + const pricesPromises = drops.map((drop: any) => + sale(drop.contractAddress, drop.tokenId) + ); + const prices = await Promise.all(pricesPromises); + const values = prices.map((price) => + price.pricePerToken.add(ZORA_FEE).toString() + ); + setPriceValues(values); + }; + + getValues(); + }, [drops, sale]); - return { sale }; + return { sale, priceValues }; }; export default useZoraFixedPriceSaleStrategy; From 2321d2b73c599d44d99f3b1060c48bb746e67d80 Mon Sep 17 00:00:00 2001 From: Sweets Sweetman Date: Sun, 21 Jan 2024 16:50:01 -0300 Subject: [PATCH 8/8] v0.4.5 --- CHANGELOG.md | 6 ++++++ package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f7742ad..ac0efb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # onchain-magic +## 0.4.5 + +### Patch Changes + +- I migrate retrieval of price information from usecollection => useZoraFixedPriceSaleStrategy for more efficient useEffect without infinite loops. + ## 0.4.4 ### Patch Changes diff --git a/package.json b/package.json index 0834f3d..1a82313 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "onchain-magic", - "version": "0.4.4", + "version": "0.4.5", "private": false, "type": "module", "peerDependencies": {