Reputation: 1
I want a solution to create a spl-token from frontend side as I saw many solutions with keypair exposing on frontend side or generating random keypair but when I switch to phantom wallet connection with or without anchor no result. Function is nothing just a show piece. And has no use.
I want a solution regarding connection with phantom wallet with anchor or without anchor working solution with no exposing of keypair on frontend side or random generation of keypair on frontend side .. optimal solution required
Upvotes: 0
Views: 37
Reputation: 101
You can create mint account in frontend with your phantom wallet.
try {
const { solana } = window;
if (!solana || !solana.isPhantom)
throw new Error("Phantom wallet not found");
if (!walletConnected) {
throw new Error("Wallet is not connected");
}
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
const phantomPublicKey = new PublicKey(publicKey!);
const mintAccount = Keypair.generate();
const tx = new Transaction();
const lamports = await getMinimumBalanceForRentExemptMint(connection);
tx.instructions = [
SystemProgram.createAccount({
fromPubkey: phantomPublicKey,
newAccountPubkey: mintAccount.publicKey,
lamports,
space: MINT_SIZE,
programId: TOKEN_PROGRAM_ID,
}),
createInitializeMint2Instruction(
mintAccount.publicKey,
6,
phantomPublicKey,
phantomPublicKey,
TOKEN_PROGRAM_ID
),
];
const blockhash = (await connection.getLatestBlockhash()).blockhash;
tx.recentBlockhash = blockhash;
tx.feePayer = phantomPublicKey;
const signedTx = await solana.signAndSendTransaction(tx);
// Check if signature exists
const { signature } = signedTx;
console.log("Signature:", signature);
const transactionConfirmation = await connection.confirmTransaction(
signature
);
console.log("Transaction confirmed", transactionConfirmation);
} catch (error) {
console.error("Transaction failed:", error.message || error);
}
};
Upvotes: 0