create-collection.tsā¢3.57 kB
import { z } from "zod";
import { api } from "../api";
import { MCP_CONFIG } from "../config";
import { createSmartContractSchema, SMART_CONTRACTS_USER_LIMIT, SMART_CONTRACTS_ADMIN_LIMIT } from "../utils";
// Use the same schema as Raycast for consistency
export const createCollectionInputSchema = createSmartContractSchema;
export interface CreateCollectionResult {
success: boolean;
data?: {
collection: {
id: string;
name: string;
symbol: string;
type: string;
status: string;
ercType: string;
address?: string;
createdAt?: string;
};
message: string;
};
error?: string;
}
export async function createCollection(
params: z.infer<typeof createCollectionInputSchema>,
): Promise<CreateCollectionResult> {
try {
// First validate input using our schema
const validatedParams = createCollectionInputSchema.parse(params);
// Get user info to check limits
const accountResponse = await api.account.getMe({
deviceId: MCP_CONFIG.DEVICE_ID,
});
if (accountResponse.status !== "ok" || !accountResponse.ok) {
return {
success: false,
error: "Failed to get user account information",
};
}
const user = accountResponse.ok;
const isAdmin = user.role === "ADMIN";
const userId = user.userId;
const smartContractsLimit = isAdmin ? SMART_CONTRACTS_ADMIN_LIMIT : SMART_CONTRACTS_USER_LIMIT;
// Get existing contracts to check limits
const contractsResponse = await api.contracts.list(null);
if (contractsResponse.status !== "ok") {
return {
success: false,
error: "Failed to check existing collections",
};
}
const contracts = contractsResponse.data || [];
const personalContracts = contracts.filter(
(contract) => contract.type !== "EXTERNAL" && userId === contract.userId,
);
// Check if user can create more contracts
if (personalContracts.length >= smartContractsLimit) {
return {
success: false,
error: `You have reached the limit of ${smartContractsLimit} collections`,
};
}
// Create the contract
const createResponse = await api.contracts.create({
name: validatedParams.name,
symbol: validatedParams.symbol,
type: validatedParams.type,
});
if (createResponse.status !== "ok" || !createResponse.data) {
return {
success: false,
error: createResponse.errorCode || "Failed to create collection",
};
}
const createdContract = createResponse.data;
return {
success: true,
data: {
collection: {
id: createdContract.id,
name: createdContract.name,
symbol: createdContract.symbol,
type: createdContract.type,
status: createdContract.status,
ercType: createdContract.ercType,
address: createdContract.address || undefined,
createdAt: createdContract.createdAt
? new Date(createdContract.createdAt.seconds * 1000).toISOString()
: undefined,
},
message: `Collection "${createdContract.name}" created successfully!`,
},
};
} catch (error) {
if (error instanceof z.ZodError) {
const errorMessages = error.issues.map((err) => err.message).join(", ");
return {
success: false,
error: `Validation error: ${errorMessages}`,
};
}
return {
success: false,
error: error instanceof Error ? error.message : "Unknown error occurred",
};
}
}