@leda-mint-io/candymachine-client-sdk
Version:
Metaplex Candy Machine Client SDK
233 lines (232 loc) • 11.8 kB
JavaScript
;
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.verifyAssets = exports.getCandyMachineV2Config = exports.loadCandyProgramV2 = void 0;
const anchor = __importStar(require("@j0nnyboi/anchor"));
const safe_token_1 = require("@safecoin/safe-token");
const web3_js_1 = require("@safecoin/web3.js");
const constants_1 = require("../constants");
const enums_1 = require("../enums");
const constants_2 = require("../constants");
const helpers_1 = require("../mint/helpers");
const helpers_2 = require("./helpers");
/**
* Load the candy program v2..
* @param provider The anchor provider.
* @param customRpcUrl (Optional) The custom rpc url of the candy machine.
* @returns The IDL.
*
*/
function loadCandyProgramV2(provider, customRpcUrl) {
return __awaiter(this, void 0, void 0, function* () {
if (customRpcUrl)
console.log('USING CUSTOM URL', customRpcUrl);
const idl = (yield anchor.Program.fetchIdl(constants_1.CANDY_MACHINE_PROGRAM_V2_ID, provider));
const program = new anchor.Program(idl, constants_1.CANDY_MACHINE_PROGRAM_V2_ID, provider);
console.log('program id from anchor', program.programId.toBase58());
return program;
});
}
exports.loadCandyProgramV2 = loadCandyProgramV2;
/**
* Get the candy machine settings.
* @param walletKeyPair The wallet key pair.
* @param configForm The config form.
* @param anchorProgram The anchor program.
* @returns The settings of the candy machine.
*/
function getCandyMachineV2Config(walletKeyPair, configForm, anchorProgram) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
if (configForm === undefined) {
throw new Error('The configForm is undefined');
}
const config = configForm;
const { storage, nftStorageKey, ipfsInfuraProjectId, number, ipfsInfuraSecret, pinataJwt, pinataGateway, awsS3Bucket, noRetainAuthority, noMutable, batchSize, price, splToken, splTokenAccount, solTreasuryAccount, gatekeeper, endSettings, hiddenSettings, whitelistMintSettings, goLiveDate, uuid, arweaveJwk, } = config;
let wallet;
let parsedPrice = price;
const splTokenAccountFigured = splTokenAccount
? splTokenAccount
: splToken
? (yield (0, helpers_1.getAtaForMint)(new web3_js_1.PublicKey(splToken), walletKeyPair))[0]
: null;
if (splToken) {
if (solTreasuryAccount) {
throw new Error('If spl-token-account or spl-token is set then sol-treasury-account cannot be set');
}
if (!splToken) {
throw new Error('If spl-token-account is set, spl-token must also be set');
}
const splTokenKey = new web3_js_1.PublicKey(splToken);
const splTokenAccountKey = new web3_js_1.PublicKey(splTokenAccountFigured);
if (!splTokenAccountFigured) {
throw new Error('If spl-token is set, spl-token-account must also be set');
}
console.log('anchor program loaded', anchorProgram);
const mintInfo = yield (0, safe_token_1.getMint)(anchorProgram.provider.connection, splTokenKey, undefined, safe_token_1.TOKEN_PROGRAM_ID);
if (!mintInfo.isInitialized) {
throw new Error(`The specified spl-token is not initialized`);
}
const tokenAccount = yield (0, safe_token_1.getAccount)(anchorProgram.provider.connection, splTokenAccountKey, undefined, safe_token_1.TOKEN_PROGRAM_ID);
if (!tokenAccount.isInitialized) {
throw new Error(`The specified spl-token-account is not initialized`);
}
if (!tokenAccount.mint.equals(splTokenKey)) {
throw new Error(`The spl-token-account's mint (${tokenAccount.mint.toString()}) does not match specified spl-token ${splTokenKey.toString()}`);
}
wallet = new web3_js_1.PublicKey(splTokenAccountKey);
parsedPrice = price * 10 ** mintInfo.decimals;
if ((whitelistMintSettings && (whitelistMintSettings === null || whitelistMintSettings === void 0 ? void 0 : whitelistMintSettings.discountPrice)) ||
(whitelistMintSettings && ((_a = whitelistMintSettings === null || whitelistMintSettings === void 0 ? void 0 : whitelistMintSettings.discountPrice) === null || _a === void 0 ? void 0 : _a.toNumber()) === 0)) {
;
whitelistMintSettings.discountPrice *= 10 ** mintInfo.decimals;
}
}
else {
parsedPrice = price * 10 ** 9;
if ((whitelistMintSettings === null || whitelistMintSettings === void 0 ? void 0 : whitelistMintSettings.discountPrice) || ((_b = whitelistMintSettings === null || whitelistMintSettings === void 0 ? void 0 : whitelistMintSettings.discountPrice) === null || _b === void 0 ? void 0 : _b.toNumber()) === 0) {
;
whitelistMintSettings.discountPrice *= 10 ** 9;
}
wallet = solTreasuryAccount ? new web3_js_1.PublicKey(solTreasuryAccount) : walletKeyPair;
}
if (whitelistMintSettings) {
whitelistMintSettings.mint = new web3_js_1.PublicKey(whitelistMintSettings.mint);
if ((whitelistMintSettings === null || whitelistMintSettings === void 0 ? void 0 : whitelistMintSettings.discountPrice) || ((_c = whitelistMintSettings === null || whitelistMintSettings === void 0 ? void 0 : whitelistMintSettings.discountPrice) === null || _c === void 0 ? void 0 : _c.toNumber()) === 0) {
whitelistMintSettings.discountPrice = new anchor.BN(whitelistMintSettings.discountPrice);
}
}
if (endSettings) {
if (endSettings.endSettingType.date) {
endSettings.number = new anchor.BN((0, helpers_2.parseDate)(endSettings.value));
}
else if (endSettings.endSettingType.amount) {
endSettings.number = new anchor.BN(endSettings.value);
}
delete endSettings.value;
}
if (hiddenSettings) {
const utf8Encode = new TextEncoder();
hiddenSettings.hash = utf8Encode.encode(hiddenSettings.hash.toString());
}
console.log('correct config');
return {
storage,
nftStorageKey,
ipfsInfuraProjectId,
number,
ipfsInfuraSecret,
pinataJwt,
pinataGateway: pinataGateway ? pinataGateway : null,
awsS3Bucket,
retainAuthority: !noRetainAuthority,
mutable: !noMutable,
batchSize,
price: new anchor.BN(parsedPrice),
treasuryWallet: wallet,
splToken: splToken ? new web3_js_1.PublicKey(splToken) : null,
gatekeeper: gatekeeper
? {
gatekeeperNetwork: new web3_js_1.PublicKey(gatekeeper.gatekeeperNetwork),
expireOnUse: gatekeeper.expireOnUse,
}
: null,
endSettings,
hiddenSettings,
whitelistMintSettings,
goLiveDate: goLiveDate ? new anchor.BN((0, helpers_2.parseDate)(goLiveDate)) : null,
uuid,
arweaveJwk,
};
});
}
exports.getCandyMachineV2Config = getCandyMachineV2Config;
/**
* @typedef {Object} VerifiedAssets
* @property {File[]} supportedFiles how the person is called
* @property {number} elemCount how many years the person lived
*/
/**
*
* @param files : list of files to analuze (json+image)
* @param storage : Storage to use
* @param number :number of assets
* @returns {VerifiedAssets} returns an array of verified assets and the number of assets
*/
function verifyAssets(files, storage, number) {
let imageFileCount = 0;
let animationFileCount = 0;
let jsonFileCount = 0;
/**
* From the files list, check that the files are valid images and animations or json files.
*/
const supportedFiles = files.filter((it) => {
if (constants_1.SUPPORTED_IMAGE_TYPES.some((e) => e === it.type)) {
imageFileCount++;
}
else if (constants_1.SUPPORTED_ANIMATION_TYPES.some((e) => e === it.type)) {
animationFileCount++;
}
else if (it.type == constants_2.JSON_EXTENSION) {
jsonFileCount++;
}
else {
console.warn(`WARNING: Skipping unsupported file type ${it}`);
return false;
}
return true;
});
if (animationFileCount !== 0 && storage === enums_1.StorageType.Arweave) {
throw new Error('The "arweave" storage option is incompatible with animation files. Please try again with another storage option using `--storage <option>`.');
}
if (animationFileCount !== 0 && animationFileCount !== jsonFileCount) {
throw new Error(`number of animation files (${animationFileCount}) is different than the number of json files (${jsonFileCount})`);
}
else if (imageFileCount !== jsonFileCount) {
throw new Error(`number of img files (${imageFileCount}) is different than the number of json files (${jsonFileCount})`);
}
const elemCount = number ? number : imageFileCount;
if (elemCount < imageFileCount) {
throw new Error(`max number (${elemCount}) cannot be smaller than the number of images in the source folder (${imageFileCount})`);
}
if (animationFileCount === 0) {
console.info(`Beginning the upload for ${elemCount} (img+json) pairs`);
}
else {
console.info(`Beginning the upload for ${elemCount} (img+animation+json) sets`);
}
console.log('supportedFiles', supportedFiles);
return { supportedFiles, elemCount };
}
exports.verifyAssets = verifyAssets;