UNPKG

@leda-mint-io/candymachine-client-sdk

Version:

Metaplex Candy Machine Client SDK

142 lines (141 loc) 6.87 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.arweaveUpload = void 0; const arweave_cost_1 = require("@j0nnyboi/arweave-cost"); const anchor = __importStar(require("@j0nnyboi/anchor")); const constants_1 = require("../constants"); const helpers_1 = require("./helpers"); const transactions_1 = require("./transactions"); /** * @param fileSizes - array of file sizes * @returns {Promise<number>} - estimated cost to store files in lamports */ function fetchAssetCostToStore(fileSizes) { return __awaiter(this, void 0, void 0, function* () { const result = yield (0, arweave_cost_1.calculate)(fileSizes); console.log('Arweave cost estimates:', result); return result.safecoin * anchor.web3.LAMPORTS_PER_SAFE; }); } /** * After doing a tx to the metaplex arweave wallet to store the NFTs and their metadata, this function calls a serverless function from metaplex * in which the files to upload are attached to the http form. * @param data - FormData object * @param manifest json manifest containing metadata * @param index index of the NFTs to upload * @returns http response */ function upload(data, manifest, index) { return __awaiter(this, void 0, void 0, function* () { console.log(`trying to upload image ${index}: ${manifest.name}`); const res = yield (yield fetch(constants_1.ARWEAVE_UPLOAD_ENDPOINT, { method: 'POST', body: data, })).json(); return res; }); } function estimateManifestSize(filenames) { const paths = {}; for (const name of filenames) { console.log('name', name); paths[name] = { id: 'artestaC_testsEaEmAGFtestEGtestmMGmgMGAV438', ext: (0, helpers_1.getFileExtension)(name), }; } const manifest = { manifest: 'arweave/paths', version: '0.1.0', paths, index: { path: 'metadata.json', }, }; const data = Buffer.from(JSON.stringify(manifest), 'utf8'); console.log('Estimated manifest size:', data.length); return data.length; } /** * Upload the NFTs and their metadata to the Arweave network. * @param walletKeyPair - keypair of the wallet to use for the mint transaction * @param anchorProgram - anchor program to use for the mint transaction * @param env - environment to use for the mint transaction (mainnet-beta, devnet, testnet) * @param image - image to upload * @param manifestBuffer - buffer of the manifest to upload * @param manifest - manifest to upload * @param index - index of the image to upload * @returns - The links for the manifest and the image in Arweave. */ function arweaveUpload(walletKeyPair, anchorProgram, env, image, manifestBuffer, manifest, index) { var _a, _b; return __awaiter(this, void 0, void 0, function* () { const imageExt = image.type; const estimatedManifestSize = estimateManifestSize([image.name, 'metadata.json']); const storageCost = yield fetchAssetCostToStore([image.size, manifestBuffer.length, estimatedManifestSize]); console.log(`lamport cost to store ${image.name}: ${storageCost}`); const instructions = [ anchor.web3.SystemProgram.transfer({ fromPubkey: walletKeyPair.publicKey, toPubkey: constants_1.ARWEAVE_PAYMENT_WALLET, lamports: Math.round(storageCost), }), ]; const tx = yield (0, transactions_1.sendTransactionWithRetryWithKeypair)(anchorProgram.provider.connection, walletKeyPair, instructions, 'confirmed'); console.log(`solana transaction (${env}) for arweave payment:`, tx); const data = new FormData(); const manifestBlob = new Blob([manifestBuffer], { type: constants_1.JSON_EXTENSION }); data.append('transaction', tx['txid']); data.append('env', env); data.append('file[]', image, image.name); data.append('file[]', manifestBlob, 'metadata.json'); const result = yield upload(data, manifest, index); console.log('result', result); const metadataFile = (_a = result.messages) === null || _a === void 0 ? void 0 : _a.find((m) => m.filename === 'manifest.json'); const imageFile = (_b = result.messages) === null || _b === void 0 ? void 0 : _b.find((m) => m.filename === image.name); if (metadataFile === null || metadataFile === void 0 ? void 0 : metadataFile.transactionId) { const link = `https://arweave.net/${metadataFile.transactionId}`; const imageLink = `https://arweave.net/${imageFile.transactionId}?ext=${imageExt.replace('.', '')}`; console.log(`File uploaded: ${link}`); console.log(`imageLink uploaded: ${imageLink}`); return [link, imageLink]; } else { // @todo improve throw new Error(`No transaction ID for upload: ${index}`); } }); } exports.arweaveUpload = arweaveUpload;