Skip to content

Instantly share code, notes, and snippets.

@Restuta
Created April 8, 2024 04:43
Show Gist options
  • Save Restuta/4aa370540b9d9b7d752773d17f3203ef to your computer and use it in GitHub Desktop.
Save Restuta/4aa370540b9d9b7d752773d17f3203ef to your computer and use it in GitHub Desktop.
Mint Solana SPL token + Metadata
import {
percentAmount,
generateSigner,
signerIdentity,
createSignerFromKeypair,
} from '@metaplex-foundation/umi';
import { mplToolbox, setComputeUnitPrice } from '@metaplex-foundation/mpl-toolbox';
import {
TokenStandard,
createAndMint,
} from '@metaplex-foundation/mpl-token-metadata';
import { createUmi } from '@metaplex-foundation/umi-bundle-defaults';
import { mplCandyMachine } from '@metaplex-foundation/mpl-candy-machine';
import '@solana/web3.js';
import {
ComputeBudgetProgram,
PublicKey,
TransactionInstruction,
LAMPORTS_PER_SOL,
} from '@solana/web3.js';
function solToLamports(sol: number) {
return sol * LAMPORTS_PER_SOL;
}
// your minth authority and FEE payer (has to have some sol, 0.2 would do it)
const secret = [];
const umi = createUmi(
'<your RPC url>'
).use(mplToolbox());
const userWallet = umi.eddsa.createKeypairFromSecretKey(new Uint8Array(secret));
const userWalletSigner = createSignerFromKeypair(umi, userWallet);
const metadata = {
name: 'test token',
symbol: 'TEST',
uri: '<metadata-url>',
};
const mint = generateSigner(umi);
umi.use(signerIdentity(userWalletSigner));
umi.use(mplCandyMachine());
console.log('Minting 100 million tokens...');
const computeIx = setComputeUnitPrice(umi, { microLamports: solToLamports(0.01) });
const mintTxBuilder = createAndMint(umi, {
mint,
authority: umi.identity,
name: metadata.name,
symbol: metadata.symbol,
uri: metadata.uri,
sellerFeeBasisPoints: percentAmount(0),
decimals: 8,
amount: 100000000_00000000,
tokenOwner: userWallet.publicKey,
tokenStandard: TokenStandard.Fungible,
}).prepend(computeIx);
console.log('Minting 1 million tokens...');
mintTxBuilder
.sendAndConfirm(umi, {
// Send options.
send: { skipPreflight: true },
})
.then((x) => {
console.dir(x, { depth: 4, colors: true });
console.log('Successfully minted 1 million tokens (', mint.publicKey, ')');
})
.catch((e) => {
console.error(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment