UNPKG

four-flap-meme-sdk

Version:

SDK for Flap bonding curve and four.meme TokenManager

39 lines (38 loc) 1.9 kB
import { FLAP_IPFS_API_URL } from './constants.js'; /** * 上传代币元数据到 IPFS * @param file 图片文件(File 对象) * @param meta 元数据信息 * @param apiUrl IPFS GraphQL 端点(可选,默认使用 Flap 官方端点) * @returns IPFS CID * * ⚠️ 注意: * - 默认端点 (FLAP_IPFS_API_URL) 是占位符,可能不可用 * - 建议联系 Flap 团队获取真实的 IPFS GraphQL 端点 * - 或使用自己的 IPFS 服务(Pinata、Infura、Web3.Storage 等) * - 或在 newTokenV2/newTokenV3 中直接传入 imageFile,让 SDK 自动处理 */ export async function uploadTokenMeta(file, meta, apiUrl) { const endpoint = apiUrl || FLAP_IPFS_API_URL; // 如果使用默认端点,给出警告 if (!apiUrl && endpoint === FLAP_IPFS_API_URL) { console.warn('⚠️ Warning: Using default IPFS endpoint which may not be available.\n' + 'Please either:\n' + '1. Pass a valid IPFS GraphQL endpoint as the 3rd parameter\n' + '2. Contact Flap team for the real IPFS endpoint\n' + '3. Use your own IPFS service (Pinata, Infura, Web3.Storage, etc.)'); } const form = new FormData(); const MUTATION_CREATE = `mutation Create($file: Upload!, $meta: MetadataInput!) { create(file: $file, meta: $meta) }`; form.append('operations', JSON.stringify({ query: MUTATION_CREATE, variables: { file: null, meta } })); form.append('map', JSON.stringify({ '0': ['variables.file'] })); form.append('0', file); const res = await fetch(endpoint, { method: 'POST', body: form }); if (!res.ok) { throw new Error(`IPFS upload failed: ${res.statusText} (${res.status})\n` + `Endpoint: ${endpoint}\n` + `This endpoint may not be valid. Please provide a working IPFS GraphQL endpoint.`); } const j = await res.json(); return j?.data?.create; }