solana-framework
Version:
solana-framework is solana uni-tools for typescript
178 lines • 7.95 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.NFTx = void 0;
const web3_js_1 = require("@solana/web3.js");
const buffer_1 = require("buffer");
const spl_token_1 = require("@solana/spl-token");
const generated_1 = require("./mpl-token-metadata/generated");
class NFTx {
constructor(creator, splTokenProgram = spl_token_1.TOKEN_2022_PROGRAM_ID) {
// MasterEditionV2
this.getMasterEditionAccountInfo = async (connection, mint) => {
return await generated_1.MasterEditionV2.fromAccountAddress(connection, this.getMasterEditionAccount(mint));
};
this.getMetadataAccountInfo = async (connection, mint) => {
return await generated_1.Metadata.fromAccountAddress(connection, NFTx.getMetadataAccount(mint));
};
this.createVerifyCreator = (mint) => {
return (0, generated_1.createVerifyInstruction)({
authority: this.creator.publicKey,
delegateRecord: undefined,
metadata: NFTx.getMetadataAccount(mint),
collectionMint: undefined,
collectionMetadata: undefined,
collectionMasterEdition: undefined,
systemProgram: web3_js_1.SystemProgram.programId,
sysvarInstructions: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
}, {
verificationArgs: generated_1.VerificationArgs.CreatorV1
});
};
this.createVerifyCollection = (mint) => {
return (0, generated_1.createVerifyInstruction)({
authority: this.creator.publicKey,
delegateRecord: undefined,
metadata: NFTx.getMetadataAccount(mint),
collectionMint: mint,
collectionMetadata: NFTx.getMetadataAccount(mint),
collectionMasterEdition: this.getMasterEditionAccount(mint),
systemProgram: web3_js_1.SystemProgram.programId,
sysvarInstructions: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
}, {
verificationArgs: generated_1.VerificationArgs.CollectionV1
});
};
this.creator = creator;
this.splTokenProgram = splTokenProgram;
}
createNFTIx(mint, name, symbol, uri, options) {
let printSupply;
if (typeof options?.printSupply === "number") {
printSupply = { __kind: "Limited", fields: [options.printSupply] };
}
else {
printSupply = { __kind: options?.printSupply ?? "Zero" };
}
return (0, generated_1.createCreateInstruction)({
metadata: NFTx.getMetadataAccount(mint),
mint: mint,
masterEdition: this.getMasterEditionAccount(mint),
authority: this.creator.publicKey,
payer: this.creator.publicKey,
updateAuthority: this.creator.publicKey,
sysvarInstructions: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
splTokenProgram: this.splTokenProgram,
}, {
createArgs: {
__kind: "V1",
assetData: {
name: name,
symbol: symbol,
uri: uri,
sellerFeeBasisPoints: 0,
creators: [{
address: this.creator.publicKey,
verified: true,
share: 100
}],
primarySaleHappened: false,
isMutable: true,
tokenStandard: options?.decimals && options.decimals > 0 ? generated_1.TokenStandard.Fungible : generated_1.TokenStandard.NonFungible,
collection: options?.collection ? { verified: false, key: options.collection } : null,
uses: null,
collectionDetails: null,
ruleSet: null,
},
decimals: options?.decimals ?? 0,
printSupply: printSupply
}
});
}
createMintIx(mint, to, amount = 1n) {
return (0, generated_1.createMintInstruction)({
token: this.getAssociatedTokenAddress(mint, to),
tokenOwner: this.creator.publicKey,
metadata: NFTx.getMetadataAccount(mint),
masterEdition: this.getMasterEditionAccount(mint),
tokenRecord: undefined,
mint: mint,
authority: this.creator.publicKey,
delegateRecord: undefined,
payer: this.creator.publicKey,
systemProgram: web3_js_1.SystemProgram.programId,
sysvarInstructions: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
splTokenProgram: this.splTokenProgram,
splAtaProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
authorizationRulesProgram: undefined,
authorizationRules: undefined,
}, {
mintArgs: {
__kind: "V1",
amount: amount,
authorizationData: null,
}
});
}
// The metadata of edition NFT is immutable
createPrintIx(mint, newMint, newEditionNumber, to) {
return (0, generated_1.createPrintInstruction)({
editionMetadata: NFTx.getMetadataAccount(newMint),
edition: this.getMasterEditionAccount(newMint),
editionMint: newMint,
editionTokenAccountOwner: to,
editionTokenAccount: this.getAssociatedTokenAddress(newMint, to),
editionMintAuthority: this.creator.publicKey,
editionTokenRecord: undefined,
masterEdition: this.getMasterEditionAccount(mint),
editionMarkerPda: this.getEditionMarkerPda(mint, newEditionNumber),
payer: this.creator.publicKey,
masterTokenAccountOwner: this.creator.publicKey,
masterTokenAccount: this.getAssociatedTokenAddress(mint, this.creator.publicKey),
masterMetadata: NFTx.getMetadataAccount(mint),
updateAuthority: this.creator.publicKey,
splTokenProgram: this.splTokenProgram,
splAtaProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
sysvarInstructions: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
systemProgram: web3_js_1.SystemProgram.programId,
}, {
printArgs: {
__kind: "V1",
edition: newEditionNumber
}
});
}
getAssociatedTokenAddress(mint, to) {
return (0, spl_token_1.getAssociatedTokenAddressSync)(mint, to, true, this.splTokenProgram, spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID);
}
// MetadataV1
static getMetadataAccount(mint) {
const [metadata] = web3_js_1.PublicKey.findProgramAddressSync([
buffer_1.Buffer.from('metadata', 'utf8'),
generated_1.PROGRAM_ID.toBuffer(),
mint.toBuffer(),
], generated_1.PROGRAM_ID);
return metadata;
}
getEditionMarkerPda(masterMint, masterEditionNumber) {
const [editionMarkerPda] = web3_js_1.PublicKey.findProgramAddressSync([
buffer_1.Buffer.from('metadata', 'utf8'),
generated_1.PROGRAM_ID.toBuffer(),
masterMint.toBuffer(),
buffer_1.Buffer.from('edition', 'utf8'),
buffer_1.Buffer.from((BigInt(masterEditionNumber) / 248n).toString()),
], generated_1.PROGRAM_ID);
return editionMarkerPda;
}
// MasterEditionV2
getMasterEditionAccount(mint) {
const [master_edition] = web3_js_1.PublicKey.findProgramAddressSync([
buffer_1.Buffer.from('metadata', 'utf8'),
generated_1.PROGRAM_ID.toBuffer(),
mint.toBuffer(),
buffer_1.Buffer.from('edition', 'utf8'),
], generated_1.PROGRAM_ID);
return master_edition;
}
}
exports.NFTx = NFTx;
//# sourceMappingURL=NFTx.js.map