@zoralabs/protocol-sdk 0.7.6 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,15 @@
1
- import { Address, PublicClient } from "viem";
2
- import { ContractType } from "./types";
1
+ import { Address, Hex, PublicClient } from "viem";
3
2
  import {
4
3
  contracts1155,
5
4
  zoraCreator1155FactoryImplABI,
6
5
  zoraCreator1155FactoryImplAddress,
7
6
  zoraCreator1155ImplABI,
8
7
  } from "@zoralabs/protocol-deployments";
8
+ import { NewContractParams } from "./types";
9
9
 
10
10
  type contracts1155Address = keyof typeof contracts1155.addresses;
11
- function new1155ContractVersion(chainId: number): string {
11
+ export function new1155ContractVersion(chainId: number): string {
12
+ // todo: get from subgraph
12
13
  const address = contracts1155.addresses[chainId as contracts1155Address];
13
14
  if (!address) {
14
15
  throw new Error(`No contract address for chainId ${chainId}`);
@@ -17,72 +18,73 @@ function new1155ContractVersion(chainId: number): string {
17
18
  return address.CONTRACT_1155_IMPL_VERSION;
18
19
  }
19
20
 
20
- export async function getContractInfo({
21
+ export async function getContractInfoExistingContract({
21
22
  publicClient,
22
- chainId,
23
- contract,
24
- account,
23
+ contractAddress,
25
24
  }: {
26
25
  publicClient: Pick<PublicClient, "readContract">;
27
- chainId: number;
28
- contract: ContractType;
26
+ contractAddress: Address;
29
27
  // Account that is the creator of the contract
30
- account: Address;
31
28
  }): Promise<{
32
- contractExists: boolean;
33
- contractAddress: Address;
34
29
  contractVersion: string;
35
30
  nextTokenId: bigint;
36
31
  }> {
37
32
  // Check if contract exists either from metadata or the static address passed in.
38
33
  // If a static address is passed in, this fails if that contract does not exist.
39
- const contractAddress =
40
- typeof contract === "string"
41
- ? contract
42
- : await publicClient.readContract({
43
- abi: zoraCreator1155FactoryImplABI,
44
- // Since this address is deterministic we can hardcode a chain id safely here.
45
- address:
46
- zoraCreator1155FactoryImplAddress[
47
- chainId as keyof typeof zoraCreator1155FactoryImplAddress
48
- ],
49
- functionName: "deterministicContractAddress",
50
- args: [
51
- account,
52
- contract.uri,
53
- contract.name,
54
- contract.defaultAdmin || account,
55
- ],
56
- });
57
-
58
34
  let contractVersion: string;
59
- let contractExists: boolean;
60
35
  try {
61
36
  contractVersion = await publicClient.readContract({
62
37
  abi: zoraCreator1155ImplABI,
63
38
  address: contractAddress,
64
39
  functionName: "contractVersion",
65
40
  });
66
- contractExists = true;
67
41
  } catch (e: any) {
68
42
  // This logic branch is hit if the contract doesn't exist
69
43
  // falling back to contractExists to false.
70
- contractVersion = new1155ContractVersion(chainId);
71
- contractExists = false;
44
+ throw new Error(`Contract does not exist at ${contractAddress}`);
72
45
  }
73
46
 
74
- const nextTokenId = contractExists
75
- ? await publicClient.readContract({
76
- address: contractAddress,
77
- abi: zoraCreator1155ImplABI,
78
- functionName: "nextTokenId",
79
- })
80
- : 1n;
47
+ const nextTokenId = await publicClient.readContract({
48
+ address: contractAddress,
49
+ abi: zoraCreator1155ImplABI,
50
+ functionName: "nextTokenId",
51
+ });
81
52
 
82
53
  return {
83
- contractExists,
84
- contractAddress,
85
54
  contractVersion,
86
55
  nextTokenId,
87
56
  };
88
57
  }
58
+
59
+ export async function getDeterministicContractAddress({
60
+ publicClient,
61
+ account,
62
+ setupActions,
63
+ contract,
64
+ chainId,
65
+ }: {
66
+ account: Address;
67
+ publicClient: Pick<PublicClient, "readContract">;
68
+ setupActions: Hex[];
69
+ contract: NewContractParams;
70
+ chainId: number;
71
+ // Account that is the creator of the contract
72
+ }): Promise<Address> {
73
+ const contractAddress = await publicClient.readContract({
74
+ abi: zoraCreator1155FactoryImplABI,
75
+ address:
76
+ zoraCreator1155FactoryImplAddress[
77
+ chainId as keyof typeof zoraCreator1155FactoryImplAddress
78
+ ],
79
+ functionName: "deterministicContractAddressWithSetupActions",
80
+ args: [
81
+ account,
82
+ contract.uri,
83
+ contract.name,
84
+ contract.defaultAdmin || account,
85
+ setupActions,
86
+ ],
87
+ });
88
+
89
+ return contractAddress;
90
+ }
@@ -1,13 +1,11 @@
1
1
  import { Concrete } from "src/utils";
2
- import { Address, Hex } from "viem";
2
+ import { Account, Address, Hex, SimulateContractParameters } from "viem";
3
3
 
4
- export type ContractType =
5
- | {
6
- name: string;
7
- uri: string;
8
- defaultAdmin?: Address;
9
- }
10
- | Address;
4
+ export type NewContractParams = {
5
+ name: string;
6
+ uri: string;
7
+ defaultAdmin?: Address;
8
+ };
11
9
 
12
10
  export type SalesConfigParamsType = {
13
11
  // defaults to 0
@@ -22,16 +20,20 @@ export type SalesConfigParamsType = {
22
20
  currency?: Address;
23
21
  };
24
22
 
25
- export type CreateNew1155Params = {
23
+ export type CreateNew1155ParamsBase = {
26
24
  account: Address;
27
- contract: ContractType;
28
- getAdditionalSetupActions?: (args: {
29
- tokenId: bigint;
30
- contractAddress: Address;
31
- }) => Hex[];
25
+ getAdditionalSetupActions?: (args: { tokenId: bigint }) => Hex[];
32
26
  token: CreateNew1155TokenProps;
33
27
  };
34
28
 
29
+ export type CreateNew1155ContractParams = CreateNew1155ParamsBase & {
30
+ contract: NewContractParams;
31
+ };
32
+
33
+ export type CreateNew1155TokenParams = CreateNew1155ParamsBase & {
34
+ contractAddress: Address;
35
+ };
36
+
35
37
  export interface CreateNew1155TokenProps {
36
38
  maxSupply?: bigint | number;
37
39
  tokenMetadataURI: string;
@@ -55,3 +57,23 @@ export type New1155Token = {
55
57
  salesConfig: Concrete<SalesConfigParamsType>;
56
58
  tokenMetadataURI: string;
57
59
  };
60
+
61
+ export type CreateNew1155TokenReturn = {
62
+ parameters: SimulateContractParameters<
63
+ any,
64
+ any,
65
+ any,
66
+ any,
67
+ any,
68
+ Account | Address
69
+ >;
70
+ tokenSetupActions: Hex[];
71
+ newTokenId: bigint;
72
+ newToken: New1155Token;
73
+ minter: Address;
74
+ contractVersion: string;
75
+ };
76
+
77
+ export type CreateNew1155ContractAndTokenReturn = {
78
+ contractAddress: Address;
79
+ } & CreateNew1155TokenReturn;
package/src/sdk.ts CHANGED
@@ -13,7 +13,8 @@ export type CreatorClient = {
13
13
  createPremint: PremintClient["createPremint"];
14
14
  updatePremint: PremintClient["updatePremint"];
15
15
  deletePremint: PremintClient["deletePremint"];
16
- create1155: Create1155Client["createNew1155Token"];
16
+ create1155: Create1155Client["createNew1155"];
17
+ create1155OnExistingContract: Create1155Client["createNew1155OnExistingContract"];
17
18
  };
18
19
 
19
20
  export type CollectorClient = {
@@ -55,7 +56,9 @@ export function createCreatorClient(
55
56
  createPremint: (p) => premintClient.createPremint(p),
56
57
  updatePremint: (p) => premintClient.updatePremint(p),
57
58
  deletePremint: (p) => premintClient.deletePremint(p),
58
- create1155: (p) => create1155CreatorClient.createNew1155Token(p),
59
+ create1155: (p) => create1155CreatorClient.createNew1155(p),
60
+ create1155OnExistingContract: (p) =>
61
+ create1155CreatorClient.createNew1155OnExistingContract(p),
59
62
  };
60
63
  }
61
64