Skip to content

Commit

Permalink
premint client now follows standard mint client functinoality
Browse files Browse the repository at this point in the history
  • Loading branch information
oveddan committed Nov 28, 2023
1 parent 97f58b3 commit a8b7ba4
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 66 deletions.
90 changes: 72 additions & 18 deletions packages/protocol-sdk/src/premint/premint-api-client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { post, retries, get } from "../apis/http-api-base";
import {
IHttpClient,
httpClient as defaultHttpClient,
} from "../apis/http-api-base";
import { components, paths } from "../apis/generated/premint-api-types";
import { ZORA_API_BASE } from "../constants";
import { NetworkConfig } from "src/apis/chain-constants";
import { getApiNetworkConfigForChain } from "src/mint/mint-api-client";

type SignaturePostType = paths["/signature"]["post"];
type PremintSignatureRequestBody =
Expand All @@ -22,36 +27,85 @@ type PremintSignatureGetPathParameters =
export type PremintSignatureGetResponse =
SignaturePremintGetType["responses"][200]["content"]["application/json"];

export type PremintCollection = PremintSignatureGetResponse['collection'];

export type BackendChainNames = components["schemas"]["ChainName"];

const postSignature = async (
data: PremintSignatureRequestBody,
): Promise<PremintSignatureResponse> =>
const postSignature = async ({
httpClient: { post, retries } = defaultHttpClient,
...data
}: PremintSignatureRequestBody & {
httpClient?: Pick<IHttpClient, "retries" | "post">;
}): Promise<PremintSignatureResponse> =>
retries(() =>
post<PremintSignatureResponse>(`${ZORA_API_BASE}premint/signature`, data),
);

const getNextUID = async (
path: PremintNextUIDGetPathParameters,
): Promise<PremintNextUIDGetResponse> =>
const getNextUID = async ({
chain_name,
collection_address,
httpClient: { retries, get } = defaultHttpClient,
}: PremintNextUIDGetPathParameters & {
httpClient?: Pick<IHttpClient, "retries" | "get">;
}): Promise<PremintNextUIDGetResponse> =>
retries(() =>
get<PremintNextUIDGetResponse>(
`${ZORA_API_BASE}premint/signature/${path.chain_name}/${path.collection_address}/next_uid`,
`${ZORA_API_BASE}premint/signature/${chain_name}/${collection_address}/next_uid`,
),
);

const getSignature = async (
path: PremintSignatureGetPathParameters,
): Promise<PremintSignatureGetResponse> =>
const getSignature = async ({
collection_address,
uid,
chain_name,
httpClient: { retries, get } = defaultHttpClient,
}: PremintSignatureGetPathParameters & {
httpClient?: Pick<IHttpClient, "retries" | "get">;
}): Promise<PremintSignatureGetResponse> =>
retries(() =>
get<PremintSignatureGetResponse>(
`${ZORA_API_BASE}premint/signature/${path.chain_name}/${path.collection_address}/${path.uid}`,
`${ZORA_API_BASE}premint/signature/${chain_name}/${collection_address}/${uid}`,
),
);

export const PremintAPIClient = {
postSignature,
getSignature,
getNextUID,
};
export { ZORA_API_BASE };
type OmitChainName<T> = Omit<T, "chain_name">;

class PremintAPIClient {
httpClient: IHttpClient;
networkConfig: NetworkConfig;

constructor(chainId: number, httpClient?: IHttpClient) {
this.httpClient = httpClient || defaultHttpClient;
this.networkConfig = getApiNetworkConfigForChain(chainId);
}
postSignature = async (
data: OmitChainName<PremintSignatureRequestBody>,
): Promise<PremintSignatureResponse> =>
postSignature({
...data,
chain_name: this.networkConfig.zoraBackendChainName,
httpClient: this.httpClient,
});

getNextUID = async (
path: OmitChainName<PremintNextUIDGetPathParameters>,
): Promise<PremintNextUIDGetResponse> =>
getNextUID({
...path,
chain_name: this.networkConfig.zoraBackendChainName,
httpClient: this.httpClient,
});

getSignature = async ({
collection_address,
uid,
}: OmitChainName<PremintSignatureGetPathParameters>): Promise<PremintSignatureGetResponse> =>
getSignature({
collection_address,
uid,
chain_name: this.networkConfig.zoraBackendChainName,
httpClient: this.httpClient,
});
}

export { ZORA_API_BASE, PremintAPIClient };
7 changes: 2 additions & 5 deletions packages/protocol-sdk/src/premint/premint-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("ZoraCreator1155Premint", () => {
"can sign on the forked premint contract",
async ({ viemClients: { walletClient, publicClient } }) => {
const [deployerAccount] = await walletClient.getAddresses();
const premintClient = createPremintClient({ chain: foundry });
const premintClient = createPremintClient({ chain: foundry, publicClient });

premintClient.apiClient.getNextUID = vi
.fn()
Expand All @@ -20,7 +20,6 @@ describe("ZoraCreator1155Premint", () => {

await premintClient.createPremint({
walletClient,
publicClient,
account: deployerAccount!,
checkSignature: true,
collection: {
Expand All @@ -36,7 +35,6 @@ describe("ZoraCreator1155Premint", () => {
});

expect(premintClient.apiClient.postSignature).toHaveBeenCalledWith({
chain_name: BackendChainNamesLookup.ZORA_GOERLI,
collection: {
contractAdmin: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
contractName: "Testing Contract",
Expand Down Expand Up @@ -71,7 +69,7 @@ describe("ZoraCreator1155Premint", () => {
anvilTest(
"can validate premint on network",
async ({ viemClients: { publicClient } }) => {
const premintClient = createPremintClient({ chain: foundry });
const premintClient = createPremintClient({ chain: foundry, publicClient });

const premintData = {
collection: {
Expand Down Expand Up @@ -106,7 +104,6 @@ describe("ZoraCreator1155Premint", () => {
const signatureValid = await premintClient.isValidSignature({
// @ts-ignore: Fix enum type
data: premintData,
publicClient,
});
expect(signatureValid.isValid).toBe(true);
},
Expand Down
69 changes: 26 additions & 43 deletions packages/protocol-sdk/src/premint/premint-client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { decodeEventLog } from "viem";
import { createPublicClient, decodeEventLog, http } from "viem";
import type {
Account,
Address,
Expand All @@ -21,9 +21,10 @@ import type {
} from "./premint-api-client";
import { PremintAPIClient } from "./premint-api-client";
import type { DecodeEventLogReturnType } from "viem";
import { ClientBase } from "../apis/client-base";
import { OPEN_EDITION_MINT_SIZE } from "../constants";
import { REWARD_PER_TOKEN } from "src/apis/chain-constants";
import { IHttpClient } from "src/apis/http-api-base";
import { getApiNetworkConfigForChain } from "src/mint/mint-api-client";

type MintArgumentsSettings = {
tokenURI: string;
Expand Down Expand Up @@ -143,16 +144,16 @@ export const encodePremintForAPI = ({
* Preminter API to access ZORA Premint functionality.
* Currently only supports V1 premints.
*/
class PremintClient extends ClientBase {
apiClient: typeof PremintAPIClient;

constructor(chain: Chain, apiClient?: typeof PremintAPIClient) {
super(chain);

if (!apiClient) {
apiClient = PremintAPIClient;
}
this.apiClient = apiClient;
class PremintClient {
readonly apiClient: PremintAPIClient;
readonly publicClient: PublicClient;
readonly chain: Chain;

constructor(chain: Chain, publicClient?: PublicClient, httpClient?: IHttpClient) {
this.chain = chain;
this.apiClient = new PremintAPIClient(chain.id, httpClient);
this.publicClient =
publicClient || createPublicClient({ chain, transport: http() });
}

/**
Expand Down Expand Up @@ -219,7 +220,6 @@ class PremintClient extends ClientBase {
collection: Address;
}): Promise<SignedPremintResponse> {
const signatureResponse = await this.apiClient.getSignature({
chain_name: this.network.zoraBackendChainName,
collection_address: collection.toLowerCase(),
uid: uid,
});
Expand All @@ -241,7 +241,6 @@ class PremintClient extends ClientBase {
account,
checkSignature: false,
verifyingContract: collection,
publicClient: this.getPublicClient(),
uid: uid,
collection: {
...signerData.collection,
Expand Down Expand Up @@ -270,16 +269,13 @@ class PremintClient extends ClientBase {
uid,
account,
collection,
publicClient,
}: {
walletClient: WalletClient;
publicClient: PublicClient;
uid: number;
account?: Account | Address;
collection: Address;
}) {
const signatureResponse = await this.apiClient.getSignature({
chain_name: this.network.zoraBackendChainName,
collection_address: collection.toLowerCase(),
uid: uid,
});
Expand All @@ -298,7 +294,6 @@ class PremintClient extends ClientBase {
account,
checkSignature: false,
verifyingContract: collection,
publicClient: this.getPublicClient(publicClient),
uid: uid,
collection: signerData.collection,
premintConfig: signerData.premint,
Expand All @@ -313,15 +308,13 @@ class PremintClient extends ClientBase {
*/
private async signAndSubmitPremint({
walletClient,
publicClient,
verifyingContract,
premintConfig,
uid,
account,
checkSignature,
collection,
}: {
publicClient: PublicClient;
uid: number;
walletClient: WalletClient;
verifyingContract: Address;
Expand All @@ -347,7 +340,7 @@ class PremintClient extends ClientBase {
});

if (checkSignature) {
const [isValidSignature] = await publicClient.readContract({
const [isValidSignature] = await this.publicClient.readContract({
abi: zoraCreator1155PremintExecutorImplABI,
address: this.getExecutorAddress(),
functionName: "isValidSignature",
Expand All @@ -361,7 +354,6 @@ class PremintClient extends ClientBase {
const apiData = {
collection,
premint: encodePremintForAPI(premintConfig),
chain_name: this.network.zoraBackendChainName,
signature: signature,
};

Expand Down Expand Up @@ -394,7 +386,6 @@ class PremintClient extends ClientBase {
account,
collection,
token,
publicClient,
walletClient,
executionSettings,
checkSignature = false,
Expand All @@ -404,15 +395,12 @@ class PremintClient extends ClientBase {
walletClient: WalletClient;
collection: PremintSignatureGetResponse["collection"];
token: MintArgumentsSettings;
publicClient?: PublicClient;
executionSettings?: {
deleted?: boolean;
uid?: number;
};
}) {
publicClient = this.getPublicClient(publicClient);

const newContractAddress = await publicClient.readContract({
const newContractAddress = await this.publicClient.readContract({
address: this.getExecutorAddress(),
abi: zoraCreator1155PremintExecutorImplABI,
functionName: "getContractAddress",
Expand All @@ -429,7 +417,6 @@ class PremintClient extends ClientBase {
let uid = executionSettings?.uid;
if (!uid) {
const uidResponse = await this.apiClient.getNextUID({
chain_name: this.network.zoraBackendChainName,
collection_address: newContractAddress.toLowerCase(),
});
uid = uidResponse.next_uid;
Expand All @@ -454,7 +441,6 @@ class PremintClient extends ClientBase {
premintConfig,
checkSignature,
account,
publicClient,
walletClient,
collection,
});
Expand All @@ -475,7 +461,6 @@ class PremintClient extends ClientBase {
uid: number;
}): Promise<PremintSignatureGetResponse> {
return await this.apiClient.getSignature({
chain_name: this.network.zoraBackendChainName,
collection_address: address,
uid,
});
Expand All @@ -489,19 +474,15 @@ class PremintClient extends ClientBase {
*/
async isValidSignature({
data,
publicClient,
}: {
data: PremintSignatureGetResponse;
publicClient?: PublicClient;
}): Promise<{
isValid: boolean;
contractAddress: Address;
recoveredSigner: Address;
}> {
publicClient = this.getPublicClient(publicClient);

const [isValid, contractAddress, recoveredSigner] =
await publicClient.readContract({
await this.publicClient.readContract({
abi: zoraCreator1155PremintExecutorImplABI,
address: this.getExecutorAddress(),
functionName: "isValidSignature",
Expand Down Expand Up @@ -530,19 +511,21 @@ class PremintClient extends ClientBase {

const zoraTokenPath = uid ? `premint-${uid}` : tokenId;

const network = getApiNetworkConfigForChain(this.chain.id);

return {
explorer: tokenId
? `https://${this.chain.blockExplorers?.default.url}/token/${address}/instance/${tokenId}`
: null,
zoraCollect: `https://${
this.network.isTestnet ? "testnet." : ""
network.isTestnet ? "testnet." : ""
}zora.co/collect/${
this.network.zoraPathChainName
network.zoraPathChainName
}:${address}/${zoraTokenPath}`,
zoraManage: `https://${
this.network.isTestnet ? "testnet." : ""
network.isTestnet ? "testnet." : ""
}zora.co/collect/${
this.network.zoraPathChainName
network.zoraPathChainName
}:${address}/${zoraTokenPath}`,
};
}
Expand Down Expand Up @@ -608,10 +591,10 @@ class PremintClient extends ClientBase {

export function createPremintClient({
chain,
premintAPIClient,
httpClient,
publicClient
}: {
chain: Chain;
premintAPIClient?: typeof PremintAPIClient;
chain: Chain, publicClient?: PublicClient, httpClient?: IHttpClient
}) {
return new PremintClient(chain, premintAPIClient);
return new PremintClient(chain, publicClient, httpClient);
}

0 comments on commit a8b7ba4

Please sign in to comment.