Skip to content

Create Onchain 1155 Contracts & Tokens

The Protocol SDK can be used to prepare transactions to create 1155 contracts and tokens, using the Creator Client.

Setup/Create the Creator Client

Initialize a Creator Client with a chain id and PublicClient. The chain is used to determine the network to interact with.

import { createCreatorClient } from "@zoralabs/protocol-sdk";
import { createPublicClient, http } from 'viem';
import { zora } from "viem/chains";
 
const publicClient = createPublicClient({ 
  chain: zora, 
  transport: http() 
});
 
const creatorClient = createCreatorClient({ chainId: zora.id, publicClient });

Creating a new 1155 contract and token

The function create1155 is used to create a new 1155 contract and/or token. The contract argument can be either contract creation parameters or a contract uri.

If contract is set to be contract creation parameters, an 1155 contract is either created or retrieved at a deterministic address based on those parameters, and the token is created on that contract.

example.ts
import {
  useAccount,
  useChainId,
  usePublicClient,
  useWriteContract,
} from "wagmi";
import { createCreatorClient } from "@zoralabs/protocol-sdk";
 
// use wagmi hooks to get the chainId, publicClient, and account
 
const chainId = useChainId();
const publicClient = usePublicClient()!;
const { address } = useAccount();
 
const creatorClient = createCreatorClient({ chainId, publicClient });
 
const { parameters } = await creatorClient.create1155({
  // by providing a contract creation config, the contract will be created
  // if it does not exist at a deterministic address
  contract: {
    // contract name
    name: "testContract",
    // contract metadata uri
    uri: "ipfs://DUMMY/contract.json",
  },
  token: {
    tokenMetadataURI: "ipfs://DUMMY/token.json",
  },
  // account to execute the transaction (the creator)
  account: address!,
});
 
const { writeContract } = useWriteContract();
 
writeContract(parameters);

Creating a token on an existing 1155 contract

If contract is set to be a contract uri, the token is created on that existing contract.

example.ts
import { createCreatorClient } from "@zoralabs/protocol-sdk";
import { publicClient, walletClient, chainId, creatorAccount } from "./config";
import { collectionAddress } from "./createNewContract";
 
const creatorClient = createCreatorClient({ chainId, publicClient });
 
const { parameters } = await creatorClient.create1155({
  // by providing a contract address, the token will be created on an existing contract
  // at that address
  contract: collectionAddress, 
  token: {
    // token metadata uri
    tokenMetadataURI: "ipfs://DUMMY/token.json",
  },
  // account to execute the transaction (the creator)
  account: creatorAccount,
});
 
// simulate the transaction
const { request } = await publicClient.simulateContract(parameters);
 
// execute the transaction
const hash = await walletClient.writeContract(request);
// wait for the response
await publicClient.waitForTransactionReceipt({ hash });