Created
April 6, 2022 19:41
-
-
Save vitorsouzaalmeida/1011945dbce6670b6ca3ec8f66ad74b3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
Metadata, | |
MasterEdition, | |
CreateMetadataV2, | |
DataV2, | |
CreateMasterEditionV3, | |
} from "@metaplex-foundation/mpl-token-metadata"; | |
const BN = require("bn.js"); | |
const web3 = require("@solana/web3.js"); | |
const spl = require("@solana/spl-token"); | |
const TOKEN_ACCOUNT_SIZE = 82; | |
export async function MintMasterNFT(ctx, req, res) { | |
try { | |
const { authority, connection } = ctx; | |
const mintKeypair = web3.Keypair.generate(); | |
const mintPubkey = mintKeypair.publicKey; | |
const dataSize = TOKEN_ACCOUNT_SIZE; | |
const lamports = await connection.getMinimumBalanceForRentExemption( | |
dataSize | |
); | |
const mintAuthority = authority; | |
const createAccIx = web3.SystemProgram.createAccount({ | |
fromPubkey: mintAuthority.publicKey, | |
newAccountPubkey: mintPubkey, | |
space: dataSize, | |
lamports: lamports, | |
programId: spl.TOKEN_PROGRAM_ID, | |
}); | |
const initMintIx = spl.Token.createInitMintInstruction( | |
spl.TOKEN_PROGRAM_ID, // programId | |
mintPubkey, // mint | |
0, // decimals | |
mintAuthority.publicKey, // mintAuthority | |
null // freezeAuthority | |
); | |
const minterTokenAccount = await spl.Token.getAssociatedTokenAddress( | |
spl.ASSOCIATED_TOKEN_PROGRAM_ID, | |
spl.TOKEN_PROGRAM_ID, | |
mintPubkey, | |
mintAuthority.publicKey | |
); | |
const createTokenAccIx = spl.Token.createAssociatedTokenAccountInstruction( | |
spl.ASSOCIATED_TOKEN_PROGRAM_ID, | |
spl.TOKEN_PROGRAM_ID, | |
mintPubkey, | |
minterTokenAccount, | |
mintAuthority.publicKey, | |
mintAuthority.publicKey | |
); | |
const mintToIx = spl.Token.createMintToInstruction( | |
spl.TOKEN_PROGRAM_ID, // programId | |
mintPubkey, // mint | |
minterTokenAccount, // destination | |
mintAuthority.publicKey, // authority | |
[], // multi-signers | |
1 // amount | |
); | |
const finalTransaction = new web3.Transaction(); | |
finalTransaction.add(createAccIx); | |
finalTransaction.add(initMintIx); | |
finalTransaction.add(createTokenAccIx); | |
finalTransaction.add(mintToIx); | |
const metadataPDA = await Metadata.getPDA(mintPubkey); | |
const metadataData = new DataV2({ | |
name: req.body.name, | |
symbol: "Symbol", | |
uri: "JSON_URL_with_metadata_here", | |
sellerFeeBasisPoints: 0, | |
creators: null, | |
collection: null, | |
uses: null, | |
}); | |
const metadataTx = new CreateMetadataV2( | |
{ feePayer: mintAuthority.publicKey }, | |
{ | |
metadata: metadataPDA, | |
metadataData: metadataData, | |
updateAuthority: mintAuthority.publicKey, | |
mint: mintPubkey, | |
mintAuthority: mintAuthority.publicKey, | |
} | |
); | |
finalTransaction.add(metadataTx); | |
// Wait to avoid metada program attempting to get invalid data | |
const editionPDA = await MasterEdition.getPDA(mintPubkey); | |
const masterPDATx = new CreateMasterEditionV3( | |
{ feePayer: mintAuthority.publicKey }, | |
{ | |
edition: editionPDA, | |
metadata: metadataPDA, | |
updateAuthority: mintAuthority.publicKey, | |
mint: mintPubkey, | |
mintAuthority: mintAuthority.publicKey, | |
maxSupply: new BN(1), | |
} | |
); | |
finalTransaction.add(masterPDATx); | |
const masterTxId = await connection.sendTransaction(finalTransaction, [ | |
mintAuthority, | |
mintKeypair, | |
]); | |
console.log("master tx", masterTxId); | |
res.status(200).json({ | |
mint: mintPubkey.toString(), | |
metadata_pda: metadataPDA.toString(), | |
master_edition_pda: editionPDA.toString(), | |
}); | |
} catch (e) { | |
console.log(e); | |
res.status(500).json({ error: e.message }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment