Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sweets/heno #5

Merged
merged 11 commits into from
Feb 6, 2024
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,41 @@
# 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

- I update useEffect dependency array to be more efficient. remove infinite loops.

## 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

- 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
Expand Down
7 changes: 3 additions & 4 deletions hooks/use1155Collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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) => {
Expand All @@ -11,7 +11,7 @@
() => new Contract(zora1155Drop, abi, signer),
[zora1155Drop, signer]
);
const { sale } = useZoraFixedPriceSaleStrategy(minterAddress);
const { sale } = useZoraFixedPriceSaleStrategy({ saleConfig: minterAddress });

Check failure on line 14 in hooks/use1155Collect.ts

View workflow job for this annotation

GitHub Actions / Run linters

Argument of type '{ saleConfig: string; }' is not assignable to parameter of type 'UseZoraFixedPriceSaleStrategyParams'.

const mintWithRewards = async (
tokenId: string,
Expand All @@ -20,9 +20,8 @@
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(
Expand Down
93 changes: 38 additions & 55 deletions hooks/useCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,70 +6,53 @@ 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";

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 { mintBatchWithoutFees } = 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 { priceValues } = useZoraFixedPriceSaleStrategy({ saleConfig: minter, drops })
const { switchNetwork } = useSwitchNetwork()

const collectAll = async (minter = defaultMinter) => {
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 values = await getValues();
const totalValue = values.reduce(
(total, value) => total.add(BigNumber.from(value)),
BigNumber.from(0)
);
const response = await mintBatchWithoutFees(
targets,
calldatas,
values,
totalValue
);
return response;
};
const targets = Array(drops.length).fill(collectionAddress)
const calldatas = getCalldatas(drops.length, minter, address as string, address as string)
const totalValue = priceValues.reduce(
(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);
};
const response = await getNFTsForContract(collectionAddress, chainId)
const formattedDrops = getFormattedDrops(response.nfts, chainId)
setDrops(formattedDrops)
}

init();
}, [collectionAddress, chainId]);
init()
}, [collectionAddress, chainId])

return { drops, collectAll };
};
return { drops, collectAll, priceValues }
}

export default useCollection;
export default useCollection
2 changes: 1 addition & 1 deletion hooks/useUniversalMinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const useUniversalMinter = (chainId: number = base.id) => {
}
};

return { mintBatchWithoutFees };
return { mintBatchWithoutFees, universalMinter };
};

export default useUniversalMinter;
55 changes: 40 additions & 15 deletions hooks/useZoraFixedPriceSaleStrategy.ts
Original file line number Diff line number Diff line change
@@ -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
23 changes: 17 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -21,8 +20,20 @@ export {
useCreate1155Contract,
useZoraFixedPriceSaleStrategy,
useUniversalMinter,
ZORA_FEE,

// ETHERS
useEthersSigner
useEthersSigner,

// ALCHEMY
getNFTsForContract,

// IPFS
store,
uploadToIpfs,

// Misc.
getCalldatas,
getFormattedDrops
};

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "onchain-magic",
"version": "0.3.1",
"version": "0.4.5",
"private": false,
"type": "module",
"peerDependencies": {
Expand Down
Loading