Skip to content

Commit

Permalink
Remove duplicated 'mintToken' method in CandyMachine
Browse files Browse the repository at this point in the history
  • Loading branch information
code-jorge committed Feb 4, 2022
1 parent 0ad8d58 commit b0dc1ac
Showing 1 changed file with 1 addition and 114 deletions.
115 changes: 1 addition & 114 deletions app/src/CandyMachine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,120 +64,7 @@ const CandyMachine = ({ walletAddress }) => {
)
)[0];
};

const mintToken = async () => {
try {
const mint = web3.Keypair.generate();
const token = await getTokenWallet(
walletAddress.publicKey,
mint.publicKey
);
const metadata = await getMetadata(mint.publicKey);
const masterEdition = await getMasterEdition(mint.publicKey);
const rpcHost = process.env.REACT_APP_SOLANA_RPC_HOST;
const connection = new Connection(rpcHost);
const rent = await connection.getMinimumBalanceForRentExemption(
MintLayout.span
);

const accounts = {
config,
candyMachine: process.env.REACT_APP_CANDY_MACHINE_ID,
payer: walletAddress.publicKey,
wallet: process.env.REACT_APP_TREASURY_ADDRESS,
mint: mint.publicKey,
metadata,
masterEdition,
mintAuthority: walletAddress.publicKey,
updateAuthority: walletAddress.publicKey,
tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
tokenProgram: TOKEN_PROGRAM_ID,
systemProgram: SystemProgram.programId,
rent: web3.SYSVAR_RENT_PUBKEY,
clock: web3.SYSVAR_CLOCK_PUBKEY,
};

const signers = [mint];
const instructions = [
web3.SystemProgram.createAccount({
fromPubkey: walletAddress.publicKey,
newAccountPubkey: mint.publicKey,
space: MintLayout.span,
lamports: rent,
programId: TOKEN_PROGRAM_ID,
}),
Token.createInitMintInstruction(
TOKEN_PROGRAM_ID,
mint.publicKey,
0,
walletAddress.publicKey,
walletAddress.publicKey
),
createAssociatedTokenAccountInstruction(
token,
walletAddress.publicKey,
walletAddress.publicKey,
mint.publicKey
),
Token.createMintToInstruction(
TOKEN_PROGRAM_ID,
mint.publicKey,
token,
walletAddress.publicKey,
[],
1
),
];

const provider = getProvider();
const idl = await Program.fetchIdl(candyMachineProgram, provider);
const program = new Program(idl, candyMachineProgram, provider);

const txn = await program.rpc.mintNft({
accounts,
signers,
instructions,
});

console.log('txn:', txn);

// Setup listener
connection.onSignatureWithOptions(
txn,
async (notification, context) => {
if (notification.type === 'status') {
console.log('Received status event');

const { result } = notification;
if (!result.err) {
console.log('NFT Minted!');
}
}
},
{ commitment: 'processed' }
);
} catch (error) {
let message = error.msg || 'Minting failed! Please try again!';

if (!error.msg) {
if (error.message.indexOf('0x138')) {
} else if (error.message.indexOf('0x137')) {
message = `SOLD OUT!`;
} else if (error.message.indexOf('0x135')) {
message = `Insufficient funds to mint. Please fund your wallet.`;
}
} else {
if (error.code === 311) {
message = `SOLD OUT!`;
} else if (error.code === 312) {
message = `Minting period hasn't started yet.`;
}
}

console.warn(message);
}
};


const createAssociatedTokenAccountInstruction = (
associatedTokenAddress,
payer,
Expand Down

0 comments on commit b0dc1ac

Please sign in to comment.