Skip to content

Commit

Permalink
Merge pull request #6 from SweetmanTech/sweets/matter
Browse files Browse the repository at this point in the history
Sweets/matter
  • Loading branch information
sweetmantech authored Feb 6, 2024
2 parents bd0d0c0 + 20e222b commit a043a70
Show file tree
Hide file tree
Showing 16 changed files with 868 additions and 102 deletions.
84 changes: 84 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,89 @@
# onchain-magic

## 0.5.7

### Patch Changes

- I push missing exports.

## 0.5.6

### Patch Changes

- I init getIpfsLink and add export of getAlchemyBaseUrl.

## 0.5.5

### Patch Changes

- I update types to be more inclusive for BigNumbers in getCallSaleData function."

## 0.5.4

### Patch Changes

- I init getCallSaleData lib.

## 0.5.3

### Patch Changes

- I add useErc20FixedPriceSaleStrategy and getDefaultProvider.

## 0.5.2

### Patch Changes

- I update useUniversalMinter to prevent crashing while loading contract.

## 0.5.1

### Patch Changes

- I update useZoraFixedPriceSaleStrategy to prevent crashing from missing contract address.

## 0.5.0

### Minor Changes

- daf98c3: I add useCallSale and usePermission hooks.

## 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
46 changes: 0 additions & 46 deletions hooks/use1155Collect.ts

This file was deleted.

15 changes: 15 additions & 0 deletions hooks/useCallSale.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const useCallSale = (collectionContract: any) => {
const callSale = async (
tokenId: number,
salesConfig: string,
data: string
) => {
const tx = await collectionContract.callSale(tokenId, salesConfig, data);
const receipt = await tx.wait();
return receipt;
};

return { callSale };
};

export default useCallSale;
97 changes: 72 additions & 25 deletions hooks/useCollection.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
import { useEffect, useState } from "react";
import { BigNumber } from "ethers";
import { useEffect, useMemo, useState } from "react";
import { BigNumber, Contract } 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 "..";
import useZoraFixedPriceSaleStrategy from "./useZoraFixedPriceSaleStrategy";
import { ZORA_FEE, getEncodedMinterArgs, useEthersSigner } from "..";
import usePermission from "./usePermission";
import useCallSale from "./useCallSale";
import abi from "../lib/abi/Zora1155Drop.json";

const useCollection = (collectionAddress: string, chainId: number) => {
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 defaultMinter =
const minter =
minterOverride ||
zoraCreatorFixedPriceSaleStrategyAddress[
chainId as keyof typeof zoraCreatorFixedPriceSaleStrategyAddress
];
const { sale } = useZoraFixedPriceSaleStrategy(defaultMinter);
const { priceValues, sale } = useZoraFixedPriceSaleStrategy({
saleConfig: minter,
drops,
});
const { switchNetwork } = useSwitchNetwork();
const signer = useEthersSigner();
const collectionContract = useMemo(
() => collectionAddress && new Contract(collectionAddress, abi, signer),
[collectionAddress, signer]
);
const { addPermission, isAdminOrRole } = usePermission(collectionContract);
const { callSale } = useCallSale(collectionContract);

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) => {
const collectAll = async () => {
if (chain?.id !== chainId) {
switchNetwork?.(chainId);
return false;
Expand All @@ -45,22 +57,49 @@ const useCollection = (collectionAddress: string, chainId: number) => {
address as string,
address as string
);
const values = await getValues();
const totalValue = values.reduce(
(total, value) => total.add(BigNumber.from(value)),
const totalValue = priceValues.reduce(
(total: any, value: any) => total.add(BigNumber.from(value || "0")),
BigNumber.from(0)
);
const response = await mintBatchWithoutFees(
targets,
calldatas,
values,
priceValues,
totalValue
);
return response;
};

const collectWithRewards = async (
tokenId: string,
to: string,
referral: string,
comment = "🪄🪄🪄"
) => {
const response = await sale(collectionAddress, tokenId);
const value = BigNumber.from(response.pricePerToken.toString()).add(
ZORA_FEE
);
const minterArguments = getEncodedMinterArgs(to, comment);
if (!collectionContract) return;
const tx = await collectionContract.mintWithRewards(
minter,
tokenId,
1,
minterArguments,
referral,
{
value,
}
);

const receipt = await tx.wait();
return receipt;
};

useEffect(() => {
const init = async () => {
if (!collectionAddress) return;
const response = await getNFTsForContract(collectionAddress, chainId);
const formattedDrops = getFormattedDrops(response.nfts, chainId);
setDrops(formattedDrops);
Expand All @@ -69,7 +108,15 @@ const useCollection = (collectionAddress: string, chainId: number) => {
init();
}, [collectionAddress, chainId]);

return { drops, collectAll };
return {
addPermission,
callSale,
drops,
collectAll,
priceValues,
collectWithRewards,
isAdminOrRole,
};
};

export default useCollection;
54 changes: 54 additions & 0 deletions hooks/useErc20FixedPriceSaleStrategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Contract } from "ethers";
import { useCallback, useEffect, useMemo, useState } from "react";
import abi from "@/lib/abi/ERC20FixedPriceSaleStrategy.json";
import getDefaultProvider from "@/lib/getDefaultProvider";

type UseErc20FixedPriceSaleStrategyParams = {
saleConfig: string;
drops: any[];
chainId: number;
};

const useErc20FixedPriceSaleStrategy = ({
saleConfig,
drops,
chainId,
}: UseErc20FixedPriceSaleStrategyParams) => {
const [priceValues, setPriceValues] = useState([] as string[]);
const saleConfigContract = useMemo(
() =>
saleConfig && new Contract(saleConfig, abi, getDefaultProvider(chainId)),
[saleConfig, chainId]
);

const sale = useCallback(
async (tokenContract: string, tokenId: string) => {
try {
if (!saleConfigContract) return;
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.toString());
setPriceValues(values);
};

getValues();
}, [drops, sale]);

return { sale, priceValues };
};

export default useErc20FixedPriceSaleStrategy;
22 changes: 22 additions & 0 deletions hooks/usePermission.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const usePermission = (collectionContract: any) => {
const isAdminOrRole = async (user: string, tokenId: number, role: number) => {
if (!collectionContract) return false;
const response = await collectionContract.isAdminOrRole(
user,
tokenId,
role
);
return response;
};

const addPermission = async (tokenId: number, user: string, role: number) => {
if (!collectionContract) return false;
const tx = await collectionContract.addPermission(tokenId, user, role);
const receipt = await tx.wait();
return receipt;
};

return { addPermission, isAdminOrRole };
};

export default usePermission;
4 changes: 2 additions & 2 deletions hooks/useUniversalMinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const useUniversalMinter = (chainId: number = base.id) => {
const signer = useEthersSigner();

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

Expand All @@ -39,7 +39,7 @@ const useUniversalMinter = (chainId: number = base.id) => {
}
};

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

export default useUniversalMinter;
Loading

0 comments on commit a043a70

Please sign in to comment.