Skip to content

Commit

Permalink
Make SDK factory client patterns (#356)
Browse files Browse the repository at this point in the history
duplicate of #353 but with proper commit history (and rebasing off of a stacked diff)
  • Loading branch information
oveddan authored Nov 13, 2023
1 parent fc4ee0d commit dbe2444
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 185 deletions.
53 changes: 42 additions & 11 deletions packages/protocol-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ Protocol SDK allows users to manage zora mints and collects.
### Creating a mint from an on-chain contract:

```ts
import { MintAPI } from "@zoralabs/protocol-sdk";
import { createMintClient } from "@zoralabs/protocol-sdk";
import type { Address, WalletClient } from "viem";

async function mintNFT(
walletClient: WalletClient,
address: Address,
tokenId: bigint,
) {
const mintAPI = new MintAPI(walletClient.chain);
const mintAPI = createMintClient({ chain: walletClient.chain });
await mintAPI.mintNFT({
walletClient,
address,
Expand All @@ -33,18 +33,49 @@ async function mintNFT(
}
```

### Creating an 1155 contract:

If an object with {name, uri} is passed in to this helper, it uses the creatorAccount and those values to either 1) create or 2) mint to that existing contract.

If you wish to mint on an existing contract, pass that contract in the contract field. The return value is the prepared transaction that you can use viem or wagmi to send.

```ts
import type { PublicClient } from "viem";
import { create1155CreatorClient } from "@zoralabs/protocol-sdk";

export async function createContract({
publicClient,
walletClient,
}: {
publicClient: PublicClient;
walletClient: WalletClient;
}) {
const creatorClient = create1155CreatorClient({ publicClient });
const { request } = await creatorClient.createNew1155Token({
contract: {
name: "testContract",
uri: demoContractMetadataURI,
},
tokenMetadataURI: demoTokenMetadataURI,
account: creatorAccount,
mintToCreatorCount: 1,
});
const { request: simulateRequest } = publicClient.simulateContract(request);
const hash = await walletClient.writeContract(simulateRequest);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
return receipt;
}
```

### Creating a premint:

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

async function makePremint(walletClient: WalletClient) {
// Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently).
const premintAPI = new PremintAPI(walletClient.chain);

// Create premint
const premint = await premintAPI.createPremint({
const premint = await createPremintAPI(walletClient.chain).createPremint({
// Extra step to check the signature on-chain before attempting to sign
checkSignature: true,
// Collection information that this premint NFT will exist in once minted.
Expand Down Expand Up @@ -76,7 +107,7 @@ import type { Address, WalletClient } from "viem";

async function updatePremint(walletClient: WalletClient) {
// Create premint API object passing in the current wallet chain (only zora and zora testnet are supported currently).
const premintAPI = new PremintAPI(walletClient.chain);
const premintAPI = createPremintAPI(walletClient.chain);

// Create premint
const premint = await premintAPI.updatePremint({
Expand Down Expand Up @@ -105,7 +136,7 @@ import type { Address, 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 = new PremintAPI(walletClient.chain);
const premintAPI = createPremintClient({ chain: walletClient.chain });

// Create premint
const premint = await premintAPI.deletePremint({
Expand All @@ -132,7 +163,7 @@ async function executePremint(
premintAddress: Address,
premintUID: number,
) {
const premintAPI = new PremintAPI(walletClient.chain);
const premintAPI = createPremintClient({ chain: walletClient.chain });

return await premintAPI.executePremintWithWallet({
data: premintAPI.getPremintData(premintAddress, premintUID),
Expand All @@ -146,12 +177,12 @@ async function executePremint(

### Deleting a premint:

```js
```ts
import {PremintAPI} from '@zoralabs/premint-sdk';
import type {Address, WalletClient} from 'viem';

async function deletePremint(walletClient: WalletClient, collection: Address, uid: number) {
const premintAPI = new PremintAPI(walletClient.chain);
const premintAPI = createPremintClient({chain: walletClient.chain});

return await premintAPI.deletePremint({
walletClient,
Expand Down
66 changes: 40 additions & 26 deletions packages/protocol-sdk/src/create/1155-create-helper.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { parseEther } from "viem";
import { describe, expect } from "vitest";
import {
createNew1155Token,
create1155CreatorClient,
getTokenIdFromCreateReceipt,
} from "./1155-create-helper";
import { anvilTest } from "src/anvil";
Expand All @@ -19,8 +19,10 @@ describe("create-helper", () => {
address: creatorAddress,
value: parseEther("1"),
});
const new1155TokenRequest = await createNew1155Token({
publicClient,
const creatorClient = create1155CreatorClient({
publicClient: publicClient,
});
const { request } = await creatorClient.createNew1155Token({
contract: {
name: "testContract",
uri: demoContractMetadataURI,
Expand All @@ -29,7 +31,9 @@ describe("create-helper", () => {
account: creatorAddress,
mintToCreatorCount: 1,
});
const hash = await new1155TokenRequest.send(walletClient);
const { request: simulationResponse } =
await publicClient.simulateContract(request);
const hash = await walletClient.writeContract(simulationResponse);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
expect(receipt).not.toBeNull();
expect(receipt.to).to.equal("0x777777c338d93e2c7adf08d102d45ca7cc4ed021");
Expand All @@ -43,41 +47,51 @@ describe("create-helper", () => {
const addresses = await walletClient.getAddresses();
const creatorAccount = addresses[0]!;

const new1155TokenRequest = await createNew1155Token({
publicClient,
contract: {
name: "testContract2",
uri: demoContractMetadataURI,
},
tokenMetadataURI: demoTokenMetadataURI,
account: creatorAccount,
mintToCreatorCount: 1,
const creatorClient = create1155CreatorClient({
publicClient: publicClient,
});
expect(new1155TokenRequest.contractAddress).to.be.equal(

const { request, contractAddress, contractExists } =
await creatorClient.createNew1155Token({
contract: {
name: "testContract2",
uri: demoContractMetadataURI,
},
tokenMetadataURI: demoTokenMetadataURI,
account: creatorAccount,
mintToCreatorCount: 1,
});
expect(contractAddress).to.be.equal(
"0xb1A8928dF830C21eD682949Aa8A83C1C215194d3",
);
expect(new1155TokenRequest.contractExists).to.be.false;
const hash = await new1155TokenRequest.send(walletClient);
expect(contractExists).to.be.false;
const { request: simulateResponse } =
await publicClient.simulateContract(request);
const hash = await walletClient.writeContract(simulateResponse);
const receipt = await publicClient.waitForTransactionReceipt({ hash });
const firstTokenId = getTokenIdFromCreateReceipt(receipt);
expect(firstTokenId).to.be.equal(1n);
expect(receipt).not.toBeNull();

const newTokenOnExistingContract = await createNew1155Token({
publicClient,
contract: {
name: "testContract2",
uri: demoContractMetadataURI,
const newTokenOnExistingContract = await creatorClient.createNew1155Token(
{
contract: {
name: "testContract2",
uri: demoContractMetadataURI,
},
tokenMetadataURI: demoTokenMetadataURI,
account: creatorAccount,
mintToCreatorCount: 1,
},
tokenMetadataURI: demoTokenMetadataURI,
account: creatorAccount,
mintToCreatorCount: 1,
});
);
expect(newTokenOnExistingContract.contractAddress).to.be.equal(
"0xb1A8928dF830C21eD682949Aa8A83C1C215194d3",
);
expect(newTokenOnExistingContract.contractExists).to.be.true;
const newHash = await newTokenOnExistingContract.send(walletClient);
const { request: simulateRequest } = await publicClient.simulateContract(
newTokenOnExistingContract.request,
);
const newHash = await walletClient.writeContract(simulateRequest);
const newReceipt = await publicClient.waitForTransactionReceipt({
hash: newHash,
});
Expand Down
Loading

0 comments on commit dbe2444

Please sign in to comment.