Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename some params for better understanding what they mean #335

Merged
merged 10 commits into from
Nov 8, 2023
101 changes: 101 additions & 0 deletions packages/protocol-sdk/src/apis/chain-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import {
base,
baseGoerli,
foundry,
goerli,
mainnet,
optimism,
optimismGoerli,
zora,
zoraTestnet,
} from "viem/chains";
import type { components } from "./generated/premint-api-types";
import { parseEther } from "viem";
import { getSubgraph } from "../constants";

export type NetworkConfig = {
chainId: number;
zoraPathChainName: string;
zoraBackendChainName: components["schemas"]["ChainName"];
isTestnet: boolean;
subgraphUrl: string;
};

export const REWARD_PER_TOKEN = parseEther("0.000777");

export const BackendChainNamesLookup = {
ZORA_MAINNET: "ZORA-MAINNET",
ZORA_GOERLI: "ZORA-GOERLI",
OPTIMISM_MAINNET: "OPTIMISM-MAINNET",
OPTIMISM_GOERLI: "OPTIMISM-GOERLI",
ETHEREUM_MAINNET: "ETHEREUM-MAINNET",
ETHEREUM_GOERLI: "ETHEREUM-GOERLI",
BASE_MAINNET: "BASE-MAINNET",
BASE_GOERLI: "BASE-GOERLI",
} as const;

export const networkConfigByChain: Record<number, NetworkConfig> = {
[mainnet.id]: {
chainId: mainnet.id,
isTestnet: false,
zoraPathChainName: "eth",
zoraBackendChainName: BackendChainNamesLookup.ETHEREUM_MAINNET,
subgraphUrl: getSubgraph("zora-create-mainnet", "stable"),
},
[goerli.id]: {
chainId: goerli.id,
isTestnet: true,
zoraPathChainName: "gor",
zoraBackendChainName: BackendChainNamesLookup.ETHEREUM_GOERLI,
subgraphUrl: getSubgraph("zora-create-goerli", "stable"),
},
[zora.id]: {
chainId: zora.id,
isTestnet: false,
zoraPathChainName: "zora",
zoraBackendChainName: BackendChainNamesLookup.ZORA_MAINNET,
subgraphUrl: getSubgraph("zora-create-zora-mainnet", "stable"),
},
[zoraTestnet.id]: {
chainId: zora.id,
isTestnet: true,
zoraPathChainName: "zgor",
zoraBackendChainName: BackendChainNamesLookup.ZORA_GOERLI,
subgraphUrl: getSubgraph("zora-create-zora-testnet", "stable"),
},
[optimism.id]: {
chainId: optimism.id,
isTestnet: false,
zoraPathChainName: "opt",
zoraBackendChainName: BackendChainNamesLookup.OPTIMISM_MAINNET,
subgraphUrl: getSubgraph("zora-create-optimism", "stable"),
},
[optimismGoerli.id]: {
chainId: optimismGoerli.id,
isTestnet: true,
zoraPathChainName: "ogor",
zoraBackendChainName: BackendChainNamesLookup.OPTIMISM_GOERLI,
subgraphUrl: getSubgraph("zora-create-optimism-goerli", "stable"),
},
[base.id]: {
chainId: base.id,
isTestnet: false,
zoraPathChainName: "base",
zoraBackendChainName: BackendChainNamesLookup.BASE_MAINNET,
subgraphUrl: getSubgraph("zora-create-base-mainnet", "stable"),
},
[baseGoerli.id]: {
chainId: baseGoerli.id,
isTestnet: true,
zoraPathChainName: "bgor",
zoraBackendChainName: BackendChainNamesLookup.BASE_GOERLI,
subgraphUrl: getSubgraph("zora-create-base-goerli", "stable"),
},
[foundry.id]: {
chainId: foundry.id,
isTestnet: true,
zoraPathChainName: "zgor",
zoraBackendChainName: BackendChainNamesLookup.ZORA_GOERLI,
subgraphUrl: getSubgraph("zora-create-zora-testnet", "stable"),
},
};
46 changes: 2 additions & 44 deletions packages/protocol-sdk/src/apis/client-base.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,5 @@
import { foundry, zora, zoraTestnet } from "viem/chains";
import type { BackendChainNames as BackendChainNamesType } from "../premint/premint-api-client";
import {
Chain,
PublicClient,
createPublicClient,
http,
parseEther,
} from "viem";

export type NetworkConfig = {
chainId: number;
zoraPathChainName: string;
zoraBackendChainName: BackendChainNamesType;
isTestnet: boolean;
};

export const REWARD_PER_TOKEN = parseEther("0.000777");

export const BackendChainNamesLookup = {
ZORA_MAINNET: "ZORA-MAINNET",
ZORA_GOERLI: "ZORA-GOERLI",
} as const;

export const networkConfigByChain: Record<number, NetworkConfig> = {
[zora.id]: {
chainId: zora.id,
isTestnet: false,
zoraPathChainName: "zora",
zoraBackendChainName: BackendChainNamesLookup.ZORA_MAINNET,
},
[zoraTestnet.id]: {
chainId: zora.id,
isTestnet: true,
zoraPathChainName: "zgor",
zoraBackendChainName: BackendChainNamesLookup.ZORA_GOERLI,
},
[foundry.id]: {
chainId: foundry.id,
isTestnet: true,
zoraPathChainName: "zgor",
zoraBackendChainName: BackendChainNamesLookup.ZORA_GOERLI,
},
};
import { Chain, PublicClient, createPublicClient, http } from "viem";
import { NetworkConfig, networkConfigByChain } from "./chain-constants";

export abstract class ClientBase {
network: NetworkConfig;
Expand Down
18 changes: 3 additions & 15 deletions packages/protocol-sdk/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,9 @@ export const ZORA_API_BASE = "https://api.zora.co/";
export const OPEN_EDITION_MINT_SIZE = BigInt("18446744073709551615");

// Subgraph base settings
const CONFIG_BASE =
const SUBGRAPH_CONFIG_BASE =
"https://api.goldsky.com/api/public/project_clhk16b61ay9t49vm6ntn4mkz/subgraphs";

function getSubgraph(name: string, version: string): string {
return `${CONFIG_BASE}/${name}/${version}/gn`;
export function getSubgraph(name: string, version: string): string {
return `${SUBGRAPH_CONFIG_BASE}/${name}/${version}/gn`;
}

export const ZORA_SUBGRAPH_URLS: Record<number, string> = {
[1]: getSubgraph("zora-create-mainnet", "stable"),
[5]: getSubgraph("zora-create-goerli", "stable"),
[10]: getSubgraph("zora-create-optimism", "stable"),
[420]: getSubgraph("zora-create-optimism-goerli", "stable"),
[424]: getSubgraph("zora-create-publicgoods", "stable"),
[999]: getSubgraph("zora-create-zora-testnet", "stable"),
[7777777]: getSubgraph("zora-create-zora-mainnet", "stable"),
[84531]: getSubgraph("zora-create-base-goerli", "stable"),
[8453]: getSubgraph("zora-create-base-mainnet", "stable"),
};
43 changes: 33 additions & 10 deletions packages/protocol-sdk/src/create/1155-create-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ import type {
import { decodeEventLog, encodeFunctionData, zeroAddress } from "viem";
import { OPEN_EDITION_MINT_SIZE } from "../constants";

const saleEndForever = 18446744073709551615n;
const royaltyBPSDefault = 1000;
// Sales end forever amount (uint64 max)
const SALE_END_FOREVER = 18446744073709551615n;

// Default royalty bps
const ROYALTY_BPS_DEFAULT = 1000;

type SalesConfigParamsType = {
// defaults to 0
Expand All @@ -24,17 +27,24 @@ type SalesConfigParamsType = {
saleStart?: bigint;
// defaults to forever, in seconds
saleEnd?: bigint;
maxTokensPerAddress?: number;
// max tokens that can be minted per address
maxTokensPerAddress?: bigint;
fundsRecipient?: Address;
};

export const DEFAULT_SALE_SETTINGS = {
fundsRecipient: zeroAddress,
// Free Mint
pricePerToken: 0n,
// Sale start time – defaults to beginning of unix time
saleStart: 0n,
saleEnd: saleEndForever,
// This is the end of uint64, plenty of time
saleEnd: SALE_END_FOREVER,
// 0 Here means no limit
maxTokensPerAddress: 0n,
};

// Hardcode the permission bit for the minter
const PERMISSION_BIT_MINTER = 2n ** 2n;

type ContractType =
Expand All @@ -52,11 +62,16 @@ type RoyaltySettingsType = {

export function create1155TokenSetupArgs({
nextTokenId,
// How many NFTs upon initialization to mint to the creator
mintToCreatorCount,
tokenMetadataURI,
// Fixed price minter address – required minter
fixedPriceMinterAddress,
// Address to use as the create referral, optional.
createReferral,
// Optional max supply of the token. Default unlimited
maxSupply,
// wallet sending the transaction
account,
salesConfig,
royaltySettings,
Expand All @@ -65,6 +80,7 @@ export function create1155TokenSetupArgs({
createReferral?: Address;
nextTokenId: bigint;
mintToCreatorCount: bigint | number;
// wallet sending the transaction
account: Address;
tokenMetadataURI: string;
fixedPriceMinterAddress: Address;
Expand All @@ -77,12 +93,12 @@ export function create1155TokenSetupArgs({
maxSupply = BigInt(maxSupply);
mintToCreatorCount = BigInt(mintToCreatorCount);

const salesConfigWithDefaults = Object.assign(
const salesConfigWithDefaults = {
// Set static sales default.
DEFAULT_SALE_SETTINGS,
...DEFAULT_SALE_SETTINGS,
// Override with user settings.
salesConfig,
);
...salesConfig,
};

const setupActions = [
encodeFunctionData({
Expand Down Expand Up @@ -140,7 +156,7 @@ export function create1155TokenSetupArgs({
nextTokenId,
{
royaltyMintSchedule: 0,
royaltyBPS: royaltySettings?.royaltyBPS || royaltyBPSDefault,
royaltyBPS: royaltySettings?.royaltyBPS || ROYALTY_BPS_DEFAULT,
royaltyRecipient: royaltySettings?.royaltyRecipient || account,
},
],
Expand Down Expand Up @@ -171,6 +187,7 @@ export const getTokenIdFromCreateReceipt = (
async function getContractExists(
publicClient: PublicClient,
contract: ContractType,
// Account that is the creator of the contract
account: Address,
) {
let contractAddress;
Expand All @@ -188,6 +205,7 @@ async function getContractExists(
contract.defaultAdmin || account,
],
});

try {
await publicClient.readContract({
abi: zoraCreator1155ImplABI,
Expand All @@ -208,6 +226,7 @@ async function getContractExists(
};
}

// Create new 1155 token
export async function createNew1155Token({
publicClient,
contract,
Expand All @@ -233,12 +252,15 @@ export async function createNew1155Token({
contractAddress: Address;
}) => Hex[];
}) {
// Check if contract exists either from metadata or the static address passed in.
// If a static address is passed in, this fails if that contract does not exist.
const { contractExists, contractAddress } = await getContractExists(
publicClient,
contract,
account,
);

// Assume the next token id is the first token available for a new contract.
let nextTokenId = 1n;

if (contractExists) {
Expand All @@ -249,6 +271,7 @@ export async function createNew1155Token({
});
}

// Get the fixed price minter to use within the new token to set the sales configuration.
const fixedPriceMinterAddress = await publicClient.readContract({
abi: zoraCreator1155FactoryImplABI,
address: zoraCreator1155FactoryImplAddress[999],
Expand Down Expand Up @@ -287,7 +310,7 @@ export async function createNew1155Token({
{
// deprecated
royaltyMintSchedule: 0,
royaltyBPS: royaltySettings?.royaltyBPS || royaltyBPSDefault,
royaltyBPS: royaltySettings?.royaltyBPS || ROYALTY_BPS_DEFAULT,
royaltyRecipient: royaltySettings?.royaltyRecipient || account,
},
contract.defaultAdmin || account,
Expand Down
20 changes: 9 additions & 11 deletions packages/protocol-sdk/src/mint/mint-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { retries, get, post } from "../apis/http-api-base";
import { paths } from "../apis/generated/discover-api-types";
import { ZORA_API_BASE } from "../premint/premint-api-client";
import { ZORA_SUBGRAPH_URLS } from "../constants";
import { ZORA_API_BASE } from "../constants";

export type MintableGetToken =
paths["/mintables_v2/{chain_name}/{collection_address}"];
paths["/mintables/{chain_name}/{collection_address}"];
type MintableGetTokenPathParameters =
MintableGetToken["get"]["parameters"]["path"];
type MintableGetTokenGetQueryParameters =
Expand All @@ -22,24 +21,23 @@ const getMintable = async (
): Promise<MintableGetTokenResponse> =>
retries(() => {
return get<MintableGetTokenResponse>(
`${ZORA_API_BASE}discover/mintables_v2/${path.chain_name}/${path.collection_address}${
query?.token_id ? `?${encodeQueryParameters(query)}` : ""
}`,
`${ZORA_API_BASE}discover/mintables/${path.chain_name}/${
path.collection_address
}${query?.token_id ? `?${encodeQueryParameters(query)}` : ""}`,
);
}
);
});

export const getSalesConfigFixedPrice = async ({
contractAddress,
tokenId,
chainId,
subgraphUrl,
}: {
contractAddress: string;
tokenId: string;
chainId: number;
subgraphUrl: string;
}): Promise<undefined | string> =>
retries(async () => {
const response = await post<any>(ZORA_SUBGRAPH_URLS[chainId]!, {
const response = await post<any>(subgraphUrl, {
query:
"query($id: ID!) {\n zoraCreateToken(id: $id) {\n id\n salesStrategies{\n fixedPrice {\n address\n }\n }\n }\n}",
variables: { id: `${contractAddress.toLowerCase()}-${tokenId}` },
Expand Down
Loading
Loading