Skip to content

Commit

Permalink
Merge pull request #4 from SweetmanTech/sweets/heno
Browse files Browse the repository at this point in the history
Sweets/heno
  • Loading branch information
sweetmantech authored Jan 21, 2024
2 parents cc5ba0f + 9769f01 commit bd0d0c0
Show file tree
Hide file tree
Showing 16 changed files with 10,944 additions and 5,028 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# onchain-magic

## 0.3.1

### Patch Changes

- bump

## 0.3.0

### Minor Changes

- I add useUniversalMinter & useCollection hooks.
- f08891b: I add getEncodedMinterArgs export.

## 0.2.6

### Patch Changes
Expand Down
6 changes: 2 additions & 4 deletions hooks/use1155Collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BigNumber, Contract, utils } from "ethers";
import { useEthersSigner } from "./useEthersSigner";
import abi from "../lib/abi/Zora1155Drop.json";
import { useZoraFixedPriceSaleStrategy } from "..";
import getEncodedMinterArgs from "../lib/zora/getEncodedMinterArgs";

const use1155Collect = (zora1155Drop: string, minterAddress: string) => {
const signer = useEthersSigner();
Expand All @@ -23,10 +24,7 @@ const use1155Collect = (zora1155Drop: string, minterAddress: string) => {
const value = BigNumber.from(response.pricePerToken.toString()).add(
zoraFee
);
const minterArguments = utils.defaultAbiCoder.encode(
["address", "string"],
[to, comment]
);
const minterArguments = getEncodedMinterArgs(to, comment);
const tx = await zora1155DropContract.mintWithRewards(
minterAddress,
tokenId,
Expand Down
75 changes: 75 additions & 0 deletions hooks/useCollection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { useEffect, useState } from "react";
import { BigNumber } from "ethers";
import { useAccount, useNetwork, useSwitchNetwork } from "wagmi";
import { zoraCreatorFixedPriceSaleStrategyAddress } from "@zoralabs/protocol-deployments";
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 "..";

const useCollection = (collectionAddress: string, chainId: number) => {
const [drops, setDrops] = useState([] as any);
const { mintBatchWithoutFees } = useUniversalMinter(chainId);
const { address } = useAccount();
const { chain } = useNetwork();
const defaultMinter =
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 collectAll = async (minter = defaultMinter) => {
if (chain?.id !== chainId) {
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;
};

useEffect(() => {
const init = async () => {
const response = await getNFTsForContract(collectionAddress, chainId);
const formattedDrops = getFormattedDrops(response.nfts, chainId);
setDrops(formattedDrops);
};

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

return { drops, collectAll };
};

export default useCollection;
45 changes: 45 additions & 0 deletions hooks/useUniversalMinter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Contract } from "ethers";
import { useMemo } from "react";
import { base } from "viem/chains";
import { zoraUniversalMinterAddress } from "@zoralabs/universal-minter";
import abi from "../lib/abi/ZoraUniversalMinter.json";
import { useEthersSigner } from "./useEthersSigner";

const useUniversalMinter = (chainId: number = base.id) => {
const universalMinter =
zoraUniversalMinterAddress[
chainId as keyof typeof zoraUniversalMinterAddress
];
const signer = useEthersSigner();

const universalMinterContract = useMemo(
() => new Contract(universalMinter, abi, signer),
[universalMinter, signer]
);

const mintBatchWithoutFees = async (
targets: any[],
calldatas: any[],
values: any[],
value: any
) => {
try {
const tx = await universalMinterContract.mintBatchWithoutFees(
targets,
calldatas,
values,
{
value,
}
);
const receipt = await tx.wait();
return receipt;
} catch (error) {
return { error };
}
};

return { mintBatchWithoutFees };
};

export default useUniversalMinter;
8 changes: 7 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@ import { uploadToIpfs, store } from "./lib/ipfs"
import type { Create1155ContractArgs } from "./lib/types/Create1155ContractArgs"
import use1155Collect from "./hooks/use1155Collect";
import useZoraFixedPriceSaleStrategy from "./hooks/useZoraFixedPriceSaleStrategy";
import getEncodedMinterArgs from "./lib/zora/getEncodedMinterArgs";
import useUniversalMinter from "./hooks/useUniversalMinter";
import useCollection from "./hooks/useCollection";

export {
// IPFS
store,
uploadToIpfs,

// ZORA
useCreate1155Contract,
type Create1155ContractArgs,
getEncodedMinterArgs,
use1155Collect,
useCollection,
useCreate1155Contract,
useZoraFixedPriceSaleStrategy,
useUniversalMinter,

// ETHERS
useEthersSigner
Expand Down
Loading

0 comments on commit bd0d0c0

Please sign in to comment.