Skip to content

Commit

Permalink
undo changes to js sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
oveddan committed Aug 17, 2023
1 parent 17dbcd2 commit 4deabf5
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 109 deletions.
167 changes: 65 additions & 102 deletions package/preminter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ import {
createWalletClient,
createPublicClient,
} from "viem";
import { foundry, zoraTestnet } from "viem/chains";
import { foundry } from "viem/chains";
import { describe, it, beforeEach, expect } from "vitest";
import { parseEther } from "viem";
import {
zoraCreator1155PremintExecutorABI as preminterAbi,
zoraCreator1155PreminterABI as preminterAbi,
zoraCreator1155ImplABI,
zoraCreator1155PremintExecutorAddress,
zoraCreator1155FactoryImplAddress,
} from "./wagmiGenerated";
import ZoraCreator1155Attribution from "../out/ZoraCreator1155Attribution.sol/ZoraCreator1155Attribution.json";
import zoraCreator1155PremintExecutor from "../out/ZoraCreator1155PremintExecutor.sol/ZoraCreator1155PremintExecutor.json";
import preminter from "../out/ZoraCreator1155Preminter.sol/ZoraCreator1155Preminter.json";
import zoraCreator1155Impl from "../out/ZoraCreator1155Impl.sol/ZoraCreator1155Impl.json";
import zoraCreator1155FactoryImpl from "../out/ZoraCreator1155FactoryImpl.sol/ZoraCreator1155FactoryImpl.json";
import zoraCreatorFixedPriceSaleStrategy from "../out/ZoraCreatorFixedPriceSaleStrategy.sol/ZoraCreatorFixedPriceSaleStrategy.json";
Expand Down Expand Up @@ -61,10 +58,8 @@ const [

type TestContext = {
preminterAddress: `0x${string}`;
forkedChainId: keyof typeof zoraCreator1155FactoryImplAddress;
anvilChainId: number;
zoraMintFee: bigint;
fixedPriceMinterAddress: Address;
};

const deployContractAndGetAddress = async (
Expand All @@ -79,40 +74,34 @@ const deployContractAndGetAddress = async (
};

export const deployFactoryProxy = async () => {
console.log("deploying protocol rewards");
const protocolRewardsAddress = await deployContractAndGetAddress({
abi: protocolRewards.abi,
bytecode: protocolRewards.bytecode.object as `0x${string}`,
account: deployerAccount,
args: [],
});

console.log("deploying attribution lib");
const attributionAddress = await deployContractAndGetAddress({
abi: ZoraCreator1155Attribution.abi,
bytecode: ZoraCreator1155Attribution.bytecode.object as `0x${string}`,
account: deployerAccount,
});

console.log("attribution address is ", attributionAddress);
// const mockUpgradeGateAddress = await deployContractAndGetAddress({
// abi: mockUpgradeGate.abi,
// bytecode: mockUpgradeGate.bytecode.object as `0x${string}`,
// account: deployerAccount,
// args: []
// });

console.log("deploying 1155");
const zora1155Address = await deployContractAndGetAddress({
abi: zoraCreator1155Impl.abi,
bytecode: zoraCreator1155Impl.bytecode.object as `0x${string}`,
account: deployerAccount,
args: [0n, mintFeeRecipientAccount, zeroAddress, protocolRewardsAddress],
});

console.log("deploying fixed priced minter");
const fixedPriceMinterAddress = await deployContractAndGetAddress({
abi: zoraCreatorFixedPriceSaleStrategy.abi,
bytecode: zoraCreatorFixedPriceSaleStrategy.bytecode
.object as `0x${string}`,
account: deployerAccount,
});

console.log("deploying factory impl");
const factoryImplAddress = await deployContractAndGetAddress({
abi: zoraCreator1155FactoryImpl.abi,
bytecode: zoraCreator1155FactoryImpl.bytecode.object as `0x${string}`,
Expand All @@ -122,17 +111,16 @@ export const deployFactoryProxy = async () => {

const factoryProxyAddress = factoryImplAddress!;

return { factoryProxyAddress, zora1155Address, fixedPriceMinterAddress };
return { factoryProxyAddress, zora1155Address: zora1155Address! };
};

export const deployPreminterContract = async () => {
const factoryProxyAddress = (await deployFactoryProxy()).factoryProxyAddress;
const { factoryProxyAddress, zora1155Address } = await deployFactoryProxy();

const deployPreminterHash = await walletClient.deployContract({
abi: zoraCreator1155PremintExecutor.abi,
bytecode: zoraCreator1155PremintExecutor.bytecode.object as `0x${string}`,
abi: preminter.abi,
bytecode: preminter.bytecode.object as `0x${string}`,
account: deployerAccount,
args: [factoryProxyAddress],
});

const receipt = await publicClient.waitForTransactionReceipt({
Expand All @@ -141,7 +129,17 @@ export const deployPreminterContract = async () => {

const preminterAddress = receipt.contractAddress!;

return { preminterAddress, factoryProxyAddress };
const initializeHash = await walletClient.writeContract({
abi: preminterAbi,
address: preminterAddress,
functionName: "initialize",
account: deployerAccount,
args: [factoryProxyAddress],
});

await publicClient.waitForTransactionReceipt({ hash: initializeHash });

return { preminterAddress, factoryProxyAddress, zora1155Address };
};

// create token and contract creation config:
Expand All @@ -151,9 +149,7 @@ const defaultContractConfig = (): ContractCreationConfig => ({
contractName: "My fun NFT",
});

const defaultTokenConfig = (
fixedPriceMinterAddress: Address
): TokenCreationConfig => ({
const defaultTokenConfig = (): TokenCreationConfig => ({
tokenURI: "ipfs://tokenIpfsId0",
maxSupply: 100n,
maxTokensPerAddress: 10n,
Expand All @@ -163,17 +159,17 @@ const defaultTokenConfig = (
royaltyMintSchedule: 30,
royaltyBPS: 200,
royaltyRecipient: creatorAccount,
fixedPriceMinter: fixedPriceMinterAddress,
});

const defaultPremintConfig = (fixedPriceMinter: Address): PremintConfig => ({
tokenConfig: defaultTokenConfig(fixedPriceMinter),
const defaultPremintConfig = (): PremintConfig => ({
contractConfig: defaultContractConfig(),
tokenConfig: defaultTokenConfig(),
deleted: false,
uid: 105,
version: 0,
});

const useForkContract = true;
// const useForkContract = true;

describe("ZoraCreator1155Preminter", () => {
beforeEach<TestContext>(async (ctx) => {
Expand All @@ -183,40 +179,34 @@ describe("ZoraCreator1155Preminter", () => {
value: parseEther("10"),
});

ctx.forkedChainId = zoraTestnet.id;
// ctx.forkedChainId = zoraTestnet.id;
ctx.anvilChainId = foundry.id;

let preminterAddress: Address;
let zora1155Address: Address;

const deployed = await deployPreminterContract();

if (useForkContract) {
preminterAddress =
zoraCreator1155PremintExecutorAddress[ctx.forkedChainId];
} else {
const deployed = await deployPreminterContract();
preminterAddress = deployed.preminterAddress;
}
preminterAddress = deployed.preminterAddress;
zora1155Address = deployed.zora1155Address;

ctx.zoraMintFee = parseEther("0.000777");
ctx.zoraMintFee = await publicClient.readContract({
abi: zoraCreator1155ImplABI,
address: zora1155Address,
functionName: "mintFee",
});

ctx.preminterAddress = preminterAddress;
}, 20 * 1000);

it<TestContext>(
"can sign for another chain",
async ({ preminterAddress: preminterAddress, fixedPriceMinterAddress }) => {
const premintConfig = defaultPremintConfig(fixedPriceMinterAddress);
const contractConfig = defaultContractConfig();

const contractAddress = await publicClient.readContract({
abi: preminterAbi,
address: preminterAddress,
functionName: "getContractAddress",
args: [contractConfig],
});
async ({ preminterAddress: preminterAddress }) => {
const premintConfig = defaultPremintConfig();

const signedMessage = await walletClient.signTypedData({
...preminterTypedDataDefinition({
verifyingContract: contractAddress,
verifyingContract: preminterAddress,
chainId: 999,
premintConfig,
}),
Expand All @@ -239,25 +229,17 @@ describe("ZoraCreator1155Preminter", () => {
);
it<TestContext>(
"can sign and recover a signature",
async ({
preminterAddress: preminterAddress,
anvilChainId,
fixedPriceMinterAddress,
}) => {
const premintConfig = defaultPremintConfig(fixedPriceMinterAddress);
const contractConfig = defaultContractConfig();
async ({ preminterAddress: preminterAddress, anvilChainId }) => {
const premintConfig = defaultPremintConfig();

const contractAddress = await publicClient.readContract({
abi: preminterAbi,
address: preminterAddress,
functionName: "getContractAddress",
args: [contractConfig],
console.log({
defaultMind: defaultPremintConfig(),
});

// sign message containing contract and token creation config and uid
const signedMessage = await walletClient.signTypedData({
...preminterTypedDataDefinition({
verifyingContract: contractAddress,
verifyingContract: preminterAddress,
// we need to sign here for the anvil chain, cause thats where it is run on
chainId: anvilChainId,
premintConfig,
Expand All @@ -270,12 +252,7 @@ describe("ZoraCreator1155Preminter", () => {
abi: preminterAbi,
address: preminterAddress,
functionName: "recoverSigner",
args: [
premintConfig,
contractAddress,
signedMessage,
BigInt(anvilChainId),
],
args: [premintConfig, signedMessage],
});

expect(recoveredAddress).to.equal(creatorAccount);
Expand All @@ -290,11 +267,9 @@ describe("ZoraCreator1155Preminter", () => {
zoraMintFee,
anvilChainId,
preminterAddress: preminterAddress,
fixedPriceMinterAddress,
}) => {
// setup contract and token creation parameters
const premintConfig = defaultPremintConfig(fixedPriceMinterAddress);
const contractConfig = defaultContractConfig();
const premintConfig = defaultPremintConfig();

// lets make it a random number to not break the existing tests that expect fresh data
premintConfig.uid = Math.round(Math.random() * 1000000);
Expand All @@ -303,7 +278,7 @@ describe("ZoraCreator1155Preminter", () => {
abi: preminterAbi,
address: preminterAddress,
functionName: "getContractAddress",
args: [contractConfig],
args: [premintConfig.contractConfig],
});

// have creator sign the message to create the contract
Expand Down Expand Up @@ -333,10 +308,10 @@ describe("ZoraCreator1155Preminter", () => {

// get the premint status - it should not be minted
let tokenId = await publicClient.readContract({
abi: zoraCreator1155ImplABI,
address: contractAddress,
functionName: "delegatedTokenId",
args: [premintConfig.uid],
abi: preminterAbi,
address: preminterAddress,
functionName: "getPremintedTokenId",
args: [premintConfig.contractConfig, premintConfig.uid],
});

expect(tokenId).toBe(0n);
Expand All @@ -352,13 +327,7 @@ describe("ZoraCreator1155Preminter", () => {
functionName: "premint",
account: collectorAccount,
address: preminterAddress,
args: [
contractConfig,
premintConfig,
signedMessage,
quantityToMint,
comment,
],
args: [premintConfig, signedMessage, quantityToMint, comment],
value: valueToSend,
});

Expand All @@ -371,10 +340,10 @@ describe("ZoraCreator1155Preminter", () => {

// fetch the premint token id
let newTokenId = await publicClient.readContract({
abi: zoraCreator1155ImplABI,
address: contractAddress,
functionName: "delegatedTokenId",
args: [premintConfig.uid],
abi: preminterAbi,
address: preminterAddress,
functionName: "getPremintedTokenId",
args: [premintConfig.contractConfig, premintConfig.uid],
});

expect(newTokenId).not.toBe(0n);
Expand Down Expand Up @@ -423,13 +392,7 @@ describe("ZoraCreator1155Preminter", () => {
functionName: "premint",
account: collectorAccount,
address: preminterAddress,
args: [
contractConfig,
premintConfig2,
signedMessage2,
quantityToMint2,
comment,
],
args: [premintConfig2, signedMessage2, quantityToMint2, comment],
value: valueToSend2,
});

Expand All @@ -440,10 +403,10 @@ describe("ZoraCreator1155Preminter", () => {

// now premint status for the second mint, it should be minted
tokenId = await publicClient.readContract({
abi: zoraCreator1155ImplABI,
address: contractAddress,
functionName: "delegatedTokenId",
args: [premintConfig2.uid],
abi: preminterAbi,
address: preminterAddress,
functionName: "getPremintedTokenId",
args: [premintConfig2.contractConfig, premintConfig2.uid],
});

expect(tokenId).not.toBe(0n);
Expand Down
17 changes: 10 additions & 7 deletions package/preminter.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Address } from "abitype";
import { ExtractAbiFunction, AbiParametersToPrimitiveTypes } from "abitype";
import { zoraCreator1155PremintExecutorABI as preminterAbi } from "./wagmiGenerated";
import { zoraCreator1155PreminterABI as preminterAbi } from "./wagmiGenerated";
import { TypedDataDefinition } from "viem";

type PremintInputs = ExtractAbiFunction<
type PreminterHashInputs = ExtractAbiFunction<
typeof preminterAbi,
"premint"
"premintHashData"
>["inputs"];

type PreminterHashDataTypes = AbiParametersToPrimitiveTypes<PremintInputs>;
type PreminterHashDataTypes =
AbiParametersToPrimitiveTypes<PreminterHashInputs>;

export type ContractCreationConfig = PreminterHashDataTypes[0];
export type PremintConfig = PreminterHashDataTypes[1];
export type PremintConfig = PreminterHashDataTypes[0];
export type ContractCreationConfig = PremintConfig["contractConfig"];
export type TokenCreationConfig = PremintConfig["tokenConfig"];

// Convenience method to create the structured typed data
Expand All @@ -25,9 +26,10 @@ export const preminterTypedDataDefinition = ({
premintConfig: PremintConfig;
chainId: number;
}) => {
const { tokenConfig, uid, version, deleted } = premintConfig;
const { contractConfig, tokenConfig, uid, version, deleted } = premintConfig;
const types = {
Premint: [
{ name: "contractConfig", type: "ContractCreationConfig" },
{ name: "tokenConfig", type: "TokenCreationConfig" },
{ name: "uid", type: "uint32" },
{ name: "version", type: "uint32" },
Expand Down Expand Up @@ -60,6 +62,7 @@ export const preminterTypedDataDefinition = ({
},
types,
message: {
contractConfig,
tokenConfig,
uid,
version,
Expand Down

0 comments on commit 4deabf5

Please sign in to comment.