Skip to content

Commit

Permalink
updated readme with better examples. renamed premint mint function
Browse files Browse the repository at this point in the history
  • Loading branch information
oveddan committed Nov 28, 2023
1 parent 71df626 commit 4edcd6a
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 72 deletions.
145 changes: 81 additions & 64 deletions packages/protocol-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,53 +214,71 @@ export async function createContract({
}
```

### Creating a premint:
### Creating a token for free (gasless creation):

```ts
import { PremintAPI } from "@zoralabs/protocol-sdk";
import type { Address, WalletClient } from "viem";
import {createPremintClient} from "@zoralabs/protocol-sdk";
import type {Address, PublicClient, WalletClient} from "viem";

async function makePremint(walletClient: WalletClient) {
// Create premint
const premint = await createPremintAPI(walletClient.chain).createPremint({
// Extra step to check the signature on-chain before attempting to sign
async function createForFree({
walletClient,
creatorAccount,
}: {
// wallet client that will submit the transaction
walletClient: WalletClient;
// address of the token contract
creatorAccount: Address;
}) {
const premintClient = createPremintClient({ chain: walletClient.chain! });

// create and sign a free token creation.
const createdPremint = await premintClient.createPremint({
walletClient,
account: creatorAccount,
// if true, will validate that the creator is authorized to create premints on the contract.
checkSignature: true,
// Collection information that this premint NFT will exist in once minted.
// collection info of collection to create
collection: {
contractAdmin: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
contractAdmin: creatorAccount,
contractName: "Testing Contract",
contractURI:
"ipfs://bafkreiainxen4b4wz4ubylvbhons6rembxdet4a262nf2lziclqvv7au3e",
},
// WalletClient doing the signature
walletClient,
// Token information, falls back to defaults set in DefaultMintArguments.
// token info of token to create
token: {
tokenURI:
"ipfs://bafkreice23maski3x52tsfqgxstx3kbiifnt5jotg3a5ynvve53c4soi2u",
},
});

console.log(`created ZORA premint, link: ${premint.url}`);
return premint;
}
const premintUid = createdPremint.uid;
const premintCollectionAddress = createdPremint.verifyingContract;

return {
// unique id of created premint, which can be used later to
// update or delete the premint
uid: premintUid,
tokenContract: premintCollectionAddress,
}
}
```

### Updating a premint:
### Updating a token that was created for free (before it was brought onchain):

Before a token that was created for free is brought onchain, it can be updated by the original creator of that token, by having that creator sign a message indicating the update. This is useful for updating the tokenURI, other metadata, or token sale configuration (price, duration, limits, etc.):

```ts
import { PremintAPI } from "@zoralabs/premint-sdk";
import type { Address, WalletClient } from "viem";
import {createPremintClient} from "@zoralabs/protocol-sdk";
import type {Address, PublicClient, WalletClient} from "viem";

async function updatePremint(walletClient: WalletClient) {
async function updateCreatedForFreeToken(walletClient: WalletClient, premintUid: number) {
// Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently).
const premintAPI = createPremintAPI(walletClient.chain);
const premintClient = createPremintClient({ chain: walletClient.chain! });

// Create premint
const premint = await premintAPI.updatePremint({
// Extra step to check the signature on-chain before attempting to sign
// sign a message to update the created for free token, and store the update
await premintClient.updatePremint({
collection: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
uid: 23,
uid: premintUid,
// WalletClient doing the signature
walletClient,
// Token information, falls back to defaults set in DefaultMintArguments.
Expand All @@ -269,73 +287,72 @@ async function updatePremint(walletClient: WalletClient) {
"ipfs://bafkreice23maski3x52tsfqgxstx3kbiifnt5jotg3a5ynvve53c4soi2u",
},
});

console.log(`updated ZORA premint, link: ${premint.url}`);
return premint;
}
```

### Deleting a premint:
### Deleting a token that was created for free (before it was brought onchain):

Before a token that was created for free is brought onchain, it can be deleted by the original creator of that token, by having that creator sign a message indicating the deletion:

```ts
import { PremintAPI } from "@zoralabs/premint-sdk";
import type { Address, WalletClient } from "viem";
import {createPremintClient} from "@zoralabs/protocol-sdk";
import type {Address, PublicClient, WalletClient} from "viem";

async function deletePremint(walletClient: WalletClient) {
// Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently).
const premintAPI = createPremintClient({ chain: walletClient.chain });
async function deleteCreatedForFreeToken(walletClient: WalletClient) {
const premintClient = createPremintClient({ chain: walletClient.chain! });

// Create premint
const premint = await premintAPI.deletePremint({
// sign a message to delete the premint, and store the deletion
await premintClient.deletePremint({
// Extra step to check the signature on-chain before attempting to sign
collection: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
uid: 23,
// WalletClient doing the signature
walletClient,
});

console.log(`updated ZORA premint, link: ${premint.url}`);
return premint;
}
```

### Executing a premint:
### Minting a token that was created for free (and bringing it onchain):

```ts
import { PremintAPI } from "@zoralabs/premint-sdk";
import type { Address, WalletClient } from "viem";
import {createPremintClient} from "@zoralabs/protocol-sdk";
import type {Address, PublicClient, WalletClient} from "viem";

async function executePremint(
async function mintCreatedForFreeToken(
walletClient: WalletClient,
premintAddress: Address,
premintUID: number,
publicClient: PublicClient,
minterAccount: Address,
) {
const premintAPI = createPremintClient({ chain: walletClient.chain });

return await premintAPI.executePremintWithWallet({
data: premintAPI.getPremintData(premintAddress, premintUID),
walletClient,
const premintClient = createPremintClient({ chain: walletClient.chain! });

const simulateContractParameters = await premintClient.makeMintParameters({
account: minterAccount,
data: await premintClient.getPremintData({
address: "0xf8dA7f53c283d898818af7FB9d98103F559bDac2",
uid: 3,
}),
mintArguments: {
quantityToMint: 1,
mintComment: "",
},
});
}
```

### Deleting a premint:
// simulate the transaction and get any validation errors
const { request } = await publicClient.simulateContract(simulateContractParameters);

```ts
import {PremintAPI} from '@zoralabs/premint-sdk';
import type {Address, WalletClient} from 'viem';
// submit the transaction to the network
const txHash = await walletClient.writeContract(request);

async function deletePremint(walletClient: WalletClient, collection: Address, uid: number) {
const premintAPI = createPremintClient({chain: walletClient.chain});
// wait for the transaction to be complete
const receipt = await publicClient.waitForTransactionReceipt({hash: txHash});

return await premintAPI.deletePremint({
walletClient,
uid,
collection
});
}
const { urls } = await premintClient.getDataFromPremintReceipt(receipt);

```
// block explorer url:
console.log(urls.explorer);
// collect url:
console.log(urls.zoraCollect);
// manage url:
console.log(urls.zoraManage);
}
```
4 changes: 2 additions & 2 deletions packages/protocol-sdk/src/premint/premint-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe("ZoraCreator1155Premint", () => {
});
premintClient.apiClient.postSignature = vi.fn();

const { request } = await premintClient.executePremint({
const simulateContractParameters = await premintClient.makeMintParameters({
account: deployerAccount!,
data: await premintClient.getPremintData({
address: "0xf8dA7f53c283d898818af7FB9d98103F559bDac2",
Expand All @@ -157,7 +157,7 @@ describe("ZoraCreator1155Premint", () => {
},
});
const { request: simulateRequest } =
await publicClient.simulateContract(request);
await publicClient.simulateContract(simulateContractParameters);
const hash = await walletClient.writeContract(simulateRequest);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
const { premintedLog, urls } =
Expand Down
10 changes: 4 additions & 6 deletions packages/protocol-sdk/src/premint/premint-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ class PremintClient {
* @param settings.publicClient Optional public client for preflight checks.
* @returns receipt, log, zoraURL
*/
async executePremint({
async makeMintParameters({
data,
account,
mintArguments,
Expand All @@ -553,7 +553,7 @@ class PremintClient {
quantityToMint: number;
mintComment?: string;
};
}): Promise<{ request: SimulateContractParameters }> {
}): Promise<SimulateContractParameters> {
if (mintArguments && mintArguments?.quantityToMint < 1) {
throw new Error("Quantity to mint cannot be below 1");
}
Expand All @@ -574,7 +574,7 @@ class PremintClient {

const value = numberToMint * REWARD_PER_TOKEN;

const request = {
const request: SimulateContractParameters = {
account,
abi: zoraCreator1155PremintExecutorImplABI,
functionName: "premint",
Expand All @@ -583,9 +583,7 @@ class PremintClient {
args,
};

return {
request,
};
return request;
}
}

Expand Down

0 comments on commit 4edcd6a

Please sign in to comment.