@leda-mint-io/candymachine-client-sdk
Version:
Metaplex Candy Machine Client SDK
157 lines (156 loc) • 6.84 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.getFileExtension = exports.getFileName = exports.getUnixTs = exports.createCandyMachineV2Account = exports.createCandyMachineV2 = exports.uuidFromConfigPubkey = exports.sleep = exports.parseDate = void 0;
const anchor = __importStar(require("@j0nnyboi/anchor"));
const web3_js_1 = require("@safecoin/web3.js");
const constants_1 = require("../constants");
/**
* Parse a date.
* @param date The date to parse.
* @returns The parsed date.
*/
function parseDate(date) {
if (date === 'now') {
return Date.now() / 1000;
}
return Date.parse(date) / 1000;
}
exports.parseDate = parseDate;
function sleep(milliseconds) {
return new Promise((resolve) => setTimeout(resolve, milliseconds));
}
exports.sleep = sleep;
function uuidFromConfigPubkey(configAccount) {
return configAccount.toBase58().slice(0, 6);
}
exports.uuidFromConfigPubkey = uuidFromConfigPubkey;
/**
* Create a candy machine v2.
* @param anchorProgram The anchor program to use.
* @param payerWallet The payer wallet to use.
* @param treasuryWallet The treasury wallet to use.
* @param candyData The candy machine data to use.
* @returns The config account.
*/
const createCandyMachineV2 = function (anchorProgram, payerWallet, treasuryWallet,
// splToken: PublicKey,
candyData) {
return __awaiter(this, void 0, void 0, function* () {
const candyAccount = web3_js_1.Keypair.generate();
candyData.uuid = uuidFromConfigPubkey(candyAccount.publicKey);
if (!candyData.symbol) {
throw new Error(`Invalid config, there must be a symbol.`);
}
if (!candyData.creators || candyData.creators.length === 0) {
throw new Error(`Invalid config, there must be at least one creator.`);
}
const totalShare = (candyData.creators || []).reduce((acc, curr) => acc + curr.share, 0);
if (totalShare !== 100) {
throw new Error(`Invalid config, creators shares must add up to 100`);
}
let remainingAccounts = [];
// if (splToken) {
// remainingAccounts.push({
// pubkey: splToken,
// isSigner: false,
// isWritable: false,
// });
// }
const cmCreation = {
candyMachine: candyAccount.publicKey,
uuid: candyData.uuid,
txId: yield anchorProgram.methods
.initializeCandyMachine(candyData)
.accounts({
candyMachine: candyAccount.publicKey,
wallet: treasuryWallet,
authority: payerWallet.publicKey,
payer: payerWallet.publicKey,
systemProgram: web3_js_1.SystemProgram.programId,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
})
// MAYBE NEED TO ADD PAYER WALLET
.signers([candyAccount])
.remainingAccounts(remainingAccounts)
.preInstructions([
yield createCandyMachineV2Account(anchorProgram, candyData, payerWallet.publicKey, candyAccount.publicKey),
])
.rpc(),
};
console.log('cmCreation', cmCreation);
return cmCreation;
});
};
exports.createCandyMachineV2 = createCandyMachineV2;
/**
* Create a candy machine v2 account.
* @param anchorProgram The anchor program to use.
* @param candyData The candy machine data to use.
* @param payerWallet The payer wallet to use.
* @param candyAccount The candy machine account to use.
* @returns The instruction to create the candy machine account.
*/
function createCandyMachineV2Account(anchorProgram, candyData, payerWallet, candyAccount) {
return __awaiter(this, void 0, void 0, function* () {
console.log('creating v2 account');
const size = constants_1.CONFIG_ARRAY_START_V2 +
4 +
candyData.itemsAvailable.toNumber() * constants_1.CONFIG_LINE_SIZE_V2 +
8 +
2 * (Math.floor(candyData.itemsAvailable.toNumber() / 8) + 1);
const candyMachineAccount = anchor.web3.SystemProgram.createAccount({
fromPubkey: payerWallet,
newAccountPubkey: candyAccount,
space: size,
lamports: yield anchorProgram.provider.connection.getMinimumBalanceForRentExemption(size),
programId: constants_1.CANDY_MACHINE_PROGRAM_V2_ID,
});
console.log('account created', candyMachineAccount);
return candyMachineAccount;
});
}
exports.createCandyMachineV2Account = createCandyMachineV2Account;
const getUnixTs = () => {
return new Date().getTime() / 1000;
};
exports.getUnixTs = getUnixTs;
const getFileName = (fileName) => {
return fileName.split('.')[0];
};
exports.getFileName = getFileName;
const getFileExtension = (fileName) => {
return fileName.split('.')[1];
};
exports.getFileExtension = getFileExtension;